nitrojs / nitro

Next Generation Server Toolkit. Create web servers with everything you need and deploy them wherever you prefer.
https://nitro.build
MIT License
6.16k stars 507 forks source link

`routeRules` does not work in DEV mode for static files #2749

Open xak2000 opened 1 month ago

xak2000 commented 1 month ago

Environment

Node Version: v18.20.3 Nitro Version: 2.9.7

Reproduction

In this example /hello.txt route should return Content-type: application/json header, and it does work in the PROD mode, but doesn't work in the DEV mode, where Content-type: text/plain; charset=UTF-8 is returned.

Try to access /hello.txt route and check the headers of the response:

  1. In DEV mode.
  2. In PROD mode.

public/hello.txt

World

nitro.config.ts

// https://nitro.unjs.io/config
export default defineNitroConfig({
  srcDir: 'server',
  routeRules: {
    '/hello.txt': {
      headers: {
        'Content-Type': 'application/json',
      },
    },
  },
});

Describe the bug

When nitro.config.ts contains a routeRules section that sets headers for a static file (from public dir), then these headers are not set when the server is running in DEV mode (npm run dev). The headers are set, though, when the server is running in PROD mode (npm run build && npm run preview).

Additional context

The bug was originally created as nuxt/nuxt#26740.

Logs

No response