-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
vite preview …
server does not send configured headers
#9864
Comments
For a workaround, I guess this plugin will work. const p = {
name: 'p',
configurePreviewServer(server) {
server.middlewares.use((req, res, next) => {
res.setHeader("Cross-Origin-Opener-Policy", "same-origin")
res.setHeader("Cross-Origin-Embedder-Policy", "require-corp")
next()
})
}
} |
That's a good thought, @sapphi-red , but, I used to have this plugin in my let crossOriginIsolation = {
name: 'cross-origin-isolation',
configureServer: server => {
server.middlewares.use((_, response, next) => {
response.setHeader("Cross-Origin-Opener-Policy", "same-origin");
response.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
next();
});
}
}; The fact that this plugin of mine was not working lead me down the path to the Now, I notice that I was using |
Ok. I tested it and the following plugin works for both the dev. server and for the preview server: function crossOriginIsolationMiddleware(_, response, next) {
response.setHeader("Cross-Origin-Opener-Policy", "same-origin");
response.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
next();
}
const crossOriginIsolation = {
name: 'cross-origin-isolation',
configureServer: server => { server.middlewares.use(crossOriginIsolationMiddleware); },
configurePreviewServer: server => { server.middlewares.use(crossOriginIsolationMiddleware); },
}; Thanks, @sapphi-red . The work-around is good to go. |
Description
Vite's dev. server has the ability to serve custom HTTP headers -- a feature that is often very necessary. For example, I use it to serve the following two headers to enforce cross-origin isolation and enable high-precision
performance.now()
:The same feature is currently not provided by Vite's preview server (
vite preview …
) but it should be! These headers are no less necessary when previewing production artefacts than when using the dev. server. (vite/3.0.9 linux-x64 node-v16.17.0
)Concretely, the following configuration would be ignored by
vite preview …
and the cross-origin isolation headers would not be served by that:The documentation for
vite preview …
options does not indicate that the above configuration should work, suggesting that I am requesting a new feature, here, but the TypeScript definitions for Vite's configuration structures suggest that this is a bug, not a feature omission:config.preview
is an optionalPreviewOptions
which extendsCommonServerOptions
preview.ts
line 23CommonServerOptions
includesheaders
: optionalHttpServerHeaders
http.ts
line 72CommonServerOptions
is the same type used forconfig.server
for the dev. serverserver/index.ts
line 76Suggested solution
The ability to send headers when previewing production artefacts is vital for proper previewing with Vite.
vite preview …
options should be updated.Alternative
No response
Additional context
No response
Validations
The text was updated successfully, but these errors were encountered: