The best way to set custom headers such as "Content-Security-Policy" on index.html in case it was served as a fallback for url that was not matched to static asset
tomastrajan opened this issue · 1 comments
tomastrajan commented
I'm submitting a...
[ ] Regression
[ ] Bug report
[ x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
Current behavior
The ServeStaticModule
serves index.html
in various situations
/context-path
- will redirect using 301 and serve idnex from/contex-path/
/context-path/
- will serve index.html usingexpress.static(clientPath, options.serveStaticOptions)
so we can usesetHeaders
method to apply custom headers/context-path/some/path/that/falls/back/to/index
- will serve index.html usingapp.get(renderPath, renderFn);
and more precisellyres.sendFile(indexFilePath);
to which theserveStaticOptions
are NOT applied
Expected behavior
It would be great to be able to apply headers such as "Content-Security-Policy" on index.html undrr ALL circumstances, what would be the best way to acheive it ?
Minimal reproduction of the problem with instructions
ServeStaticModule.forRoot({
rootPath: join(process.cwd(), 'public'),
serveRoot: '/context-path'
serveStaticOptions: {
maxAge: MAX_AGE_7_DAYS,
etag: false,
setHeaders: (res, path) => { // this covers 1. and 2. situation but NOT the 3. one
if (path.endsWith('index.html')) {
res.setHeader('Cache-Control', 'no-cache');
}
}
}
})
What is the motivation / use case for changing the behavior?
Being able to set same headers on index.html
under all circumstances.
Environment
Nest version: 6.14.2
For Tooling issues:
- Node version: v12.13.1
- Platform: Windows
Others:
kamilmysliwiec commented
Published as 2.1.3! Thanks 🔥