Skip to content

Commit 3d30acc

Browse files
committed
Add middleware to include charset in HTTP header for early detection
1 parent 942ee06 commit 3d30acc

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

src/middleware.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { defineMiddleware } from 'astro:middleware'
2+
3+
export const onRequest = defineMiddleware(async (_context, next) => {
4+
const response = await next()
5+
const contentType = response.headers.get('content-type')
6+
7+
if (!contentType || !contentType.startsWith('text/html') || /charset=/i.test(contentType)) {
8+
return response
9+
}
10+
11+
const headers = new Headers(response.headers)
12+
headers.set('content-type', `${contentType}; charset=utf-8`)
13+
14+
return new Response(response.body, {
15+
status: response.status,
16+
statusText: response.statusText,
17+
headers,
18+
})
19+
})

0 commit comments

Comments
 (0)