Can't set headers in hooks.server handle function in vercel #12691
Answered
by
IslamZaoui
IslamZaoui
asked this question in
Q&A
-
Hello, I was trying to add security headers to my sveltekit web app: const handleSecurityHeaders: Handle = async ({ event, resolve }) => {
const response = await resolve(event)
response.headers.set('X-Frame-Options', 'SAMEORIGIN')
response.headers.set('X-Content-Type-Options', 'nosniff')
response.headers.set('Referrer-Policy', 'no-referrer-when-downgrade')
response.headers.set('Permissions-Policy', 'camera=(), microphone=(), geolocation=()')
response.headers.set('Access-Control-Allow-Origin', config.site_url || '*')
return response
} the headers appear when I'm in dev mode but when I deploy it to Vercel I can't see them until I add them to {
"$schema": "https://openapi.vercel.sh/vercel.json",
"headers": [
{
"source": "/(.*)",
"headers": [
{ "key": "Access-Control-Allow-Origin", "value": "https://islamzaoui.top" },
{ "key": "X-Frame-Options", "value": "SAMEORIGIN" },
{ "key": "X-Content-Type-Options", "value": "nosniff" },
{ "key": "Referrer-Policy", "value": "strict-origin-when-cross-origin" },
{ "key": "Permissions-Policy", "value": "geolocation=(), camera=(), microphone=()" }
]
}
]
} any suggestions? |
Beta Was this translation helpful? Give feedback.
Answered by
IslamZaoui
Sep 25, 2024
Replies: 1 comment 3 replies
-
The request const handleSecurityHeaders: Handle = async ({ event, resolve }) => {
event.setHeaders({
'X-Frame-Options': 'SAMEORIGIN',
'X-Content-Type-Options': 'nosniff',
'Referrer-Policy': 'no-referrer-when-downgrade',
'Permissions-Policy': 'camera=(), microphone=(), geolocation=()',
'Access-Control-Allow-Origin': config.site_url || '*',
});
return await resolve(event);
}; (The function also exists for |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
After troubleshooting in a fresh repo I found out that
export const prerender = true;
in +layout.ts caused this problemwhen prerendering is on the headers disappear in the production (both in vercel and local preview)