nitrojs / nitrojs/nitro

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

Open
#2,749 0 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug dev v2
Dominant language
TypeScript
Stars
11.2k
Forks
899
Avg merge
2d 24m
Merged PRs (30d)
40

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with public/hello.txt and nitro.config.ts, first running npm run dev and then npm run build && npm run preview. Compare the response headers for /hello.txt, confirming that routeRules applies the configured Content-Type in both modes; then use the reproduction to locate the differing development and production paths.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.