nestjs / serve-static

Serve static websites (SPA's) using Nest framework (node.js) 🥦

Home Page:https://nestjs.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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 · comments

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

  1. /context-path - will redirect using 301 and serve idnex from /contex-path/
  2. /context-path/ - will serve index.html using express.static(clientPath, options.serveStaticOptions) so we can use setHeaders method to apply custom headers
  3. /context-path/some/path/that/falls/back/to/index - will serve index.html using app.get(renderPath, renderFn); and more preciselly res.sendFile(indexFilePath); to which the serveStaticOptions 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:

Published as 2.1.3! Thanks 🔥