Skip to content

Dev middleware intercepts JS module requests when Sec-Fetch-Dest header is missing (IP/Safari) #4177

Closed

Description

@babpulss

Description

Nitro's dev middleware (nitroDevMiddlewarePre) relies solely on the Sec-Fetch-Dest header to decide whether to intercept a request or pass it through to Vite's transform middleware. When this header is missing, Nitro intercepts JS/TS module requests and processes them as SSR routes, breaking module loading.

This conflicts with Vite's server.host: true option, which is specifically designed to enable network/IP access.

When does Sec-Fetch-Dest go missing?

Browser localhost IP address (e.g. 192.168.0.64)
Chrome script ✓ missing ✗
Safari missing ✗ missing ✗
Firefox script ✓ needs testing

Verified with a standalone HTTP server logging incoming headers — Chrome only sends Sec-Fetch-Dest: script for module imports when the origin is localhost. Safari never sends it.

Reproduction

  1. Create a TanStack Start project with Nitro as the Vite plugin
  2. Have a route file with $ in the filename (e.g. routes/manager/$id.edit.tsx)
  3. Set server.host: true in vite.config.ts
  4. Access the app via IP address (e.g. http://192.168.0.64:3000)

Expected

Modules load correctly, same as localhost.

Actual

Nitro's middleware intercepts the request for /app/routes/manager/$id.edit.tsx, interprets $id as a route parameter (resolves to undefined), and returns a 307 redirect to /app/routes/manager/undefined, which 404s.

GET http://192.168.0.64:3000/app/routes/_authed/manager/$id.edit.tsx → 307 → /app/routes/_authed/manager/undefined → 404

On localhost with Chrome, the same request returns 200 with vary: sec-fetch-dest.

Root cause

In nitro/dist/_chunks/plugin.mjs, the nitroDevMiddlewarePre middleware:

server.middlewares.use(function nitroDevMiddlewarePre(req, res, next) {
    const fetchDest = req.headers["sec-fetch-dest"];
    if (fetchDest) {
      res.setHeader("vary", "sec-fetch-dest");
    }
    if (!fetchDest || /^(document|iframe|frame|empty)$/.test(fetchDest)) {
      nitroDevMiddleware(req, res, next);  // Nitro handles
    } else {
      next();  // Vite handles
    }
});

When Sec-Fetch-Dest is missing (!fetchDest is true), all requests fall into the Nitro handler, including JS/TS module imports that should be handled by Vite's transform middleware.

Suggested fix

Also check the URL file extension before intercepting. For example:

const isModuleRequest = /\.[mc]?[jt]sx?(\?|$)/.test(req.url);

if (!isModuleRequest && (!fetchDest || /^(document|iframe|frame|empty)$/.test(fetchDest))) {
    nitroDevMiddleware(req, res, next);
} else {
    next();
}

This would let Vite handle JS/TS module requests regardless of the Sec-Fetch-Dest header.

Current workaround

A custom Vite plugin that injects the missing header before Nitro's middleware:

function patchSecFetchDest(): Plugin {
  return {
    name: 'patch-sec-fetch-dest',
    configureServer(server) {
      server.middlewares.use((req, _res, next) => {
        if (!req.headers['sec-fetch-dest'] && req.url && /\.[mc]?[jt]sx?(\?|$)/.test(req.url)) {
          req.headers['sec-fetch-dest'] = 'script'
        }
        next()
      })
    },
  }
}

Environment

  • Nitro: v3 (via nitro/vite)
  • Vite: v8.0.3
  • TanStack Start: v1.167+
  • macOS, tested with Chrome 130+, Safari 18+, Playwright Chromium headless + WebKit

Activity

  1. babpulss commented on Apr 3, 2026

    @babpulss
    Author

    Closing — this is more accurately a TanStack Start issue since the $id filename convention comes from TanStack Router. Will re-file in the TanStack Router repo.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions