Allow middleware or a plugin to run before public assets are served

Open
#5,161 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
1-2 days
Newbie friendliness
49/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
typescript
Domain
backend

Research direction

Start by reading the Nitro v3 request handling path that serves public assets before global middleware, along with the request hook and plugin behavior described in the issue. Compare the two suggested API options and how wrapping nitroApp.fetch currently works. Done means a supported approach can guard static asset responses and the issue’s example requests behave as expected.

Written by the indexing model from the issue text.

Description

Describe the feature

In Nitro v3 the static handler runs before all global middleware, so server/middleware/* never sees a request for a public file, and the request hook cannot stop a request (a thrown error is caught and the chain continues). There is no supported way to guard every response: rejecting an unexpected Host, redirecting to a canonical host, or answering a malformed path.

Repro (3.0.261103-rc, node-server): public/robots.txt plus server/middleware/guard.ts returning new Response("no", { status: 403 }). GET /robots.txt → 200, any route → 403. A request hook throwing HTTPError(403) blocks neither.

Could middleware opt in to running before static files, or could the request hook return a Response?

Additional information

Today we wrap nitroApp.fetch in a plugin.

Dominant language
TypeScript
Stars
11.5k
Forks
968
Avg merge
4d 9h
Merged PRs (30d)
64

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

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.

More from nitrojs/nitro

All issues in nitrojs/nitro

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.