Skip to content

refactor(vite): use NitroDevApp routes to route dev requests before Vite #4735

Closed

Description

@pi0x

In Vite dev, nitroDevMiddlewarePre (src/build/vite/dev.ts) decides which requests go to Nitro before Vite. For this it rebuilds the list of routes owned by the dev app: a separate rou3 router for devHandlers and devProxy, plus isExplicitPublicAsset for public asset dirs. NitroDevApp (src/dev/app.ts) already registers all of these, so the two lists can drift apart.

Proposal

  • NitroDevApp records its explicit routes while registering them (dev handler routes, dev proxies, public asset dirs with fallthrough: false). Root catch-alls are skipped after removing the Nitro baseURL, and public dirs use joinURL(baseURL, asset.baseURL), same as where they are mounted.
  • It exposes hasRoute(method, path).
  • The Vite dev middleware uses devApp.hasRoute() and drops its own router and isExplicitPublicAsset. A match there is final, so a 404 from the dev app is returned instead of falling through to the Nitro env.

Bug this fixes

isExplicitPublicAsset compares the baseURL-prefixed path with asset.baseURL, which does not include the Nitro baseURL. With a non-root baseURL, a public dir with fallthrough: false is never matched, so asset-like requests go to Vite and get a 404 even when the file exists.

// vite.config.ts
nitro({
  baseURL: "/app/",
  publicAssets: [{ dir: "public/pub", baseURL: "/pub", fallthrough: false }],
});

GET /app/pub/x.txt with sec-fetch-dest: image returns Vite's "The server is configured with a public base URL of ..." 404 instead of the file.

Tests

Run the dev handler, dev proxy and public asset cases in test/vite with both baseURL: "/" and a non-root baseURL.

Created with AI assistance.

Activity

  1. added
    dev
    on Oct 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    dev

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions