Skip to content

Conversation

@serhalp
Copy link
Member

@serhalp serhalp commented Nov 4, 2025

Fixes #325

Problem

When using the standard recommended SPA redirect configuration in netlify.toml:

[[redirects]]
  from = "/*"
  to = "/index.html"
  status = 200

This works in production (it rewrites all requests to /index.html) but breaks local development with Vite. The redirect catches all requests, even JS module requests, causing them to be redirected to index.html instead of serving the actual JS files. This results in errors like:

Failed to load module script: Expected a JavaScript-or-Wasm module script but the server responded with a MIME type of "".

Solution

This PR ports the existing solution from Netlify CLI (yeah, it really isn't obvious what's going in there, sorry): detect and ignore the SPA redirect pattern in dev mode. The redirect is still used in production (via the CLI), but in dev mode it's ignored, allowing Vite to handle routing and serve JS modules correctly.

Future work

This is a stopgap solution. Ideally it seems if we've centralized all build and dev functionality in this Vite plugin, users should not need to configure this special redirect manually but rather our plugin could inject it only at build time, only in SPA mode.

Filter out the SPA redirect pattern (from '/*' to '/index.html' with status 200)
in createRewriter when ignoreSPARedirect is true. This prevents the redirect
from interfering with local dev servers like Vite, while still allowing it to
work in production.

RedirectsHandler now always passes ignoreSPARedirect: true to createRewriter,
ensuring the SPA redirect is ignored in dev mode.

Added e2e test to verify JS modules load correctly and execute when SPA redirect
is configured in netlify.toml.

Fixes #325
@serhalp serhalp changed the title fix: ignore SPA redirect in dev mode to allow Vite to serve JS modules fix: ignore SPA redirect in dev mode to allow Vite to take over Nov 4, 2025
@serhalp serhalp marked this pull request as ready for review November 4, 2025 20:58
@serhalp serhalp requested review from a team as code owners November 4, 2025 20:58
@serhalp serhalp removed the request for review from a team November 4, 2025 20:58
const isSPARedirect =
redirect.from === '/*' &&
redirect.to === '/index.html' &&
(redirect.status === 200 || redirect.status === undefined)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it correct to filter out when status is undefined? Wouldn't this be a redirect by default if there was no status there? And redirect doesn't seem like it would work generally for SPA mode as you really need rewrite for that (plus we don't check for explicit redirect status code here either)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I guess so! Good catch, thanks.

@serhalp serhalp requested a review from pieh November 5, 2025 13:21
pieh
pieh previously approved these changes Nov 5, 2025
@serhalp
Copy link
Member Author

serhalp commented Nov 5, 2025

ugh, Windows tests are not flaky, it's some sort of path issue with the new test... only on Vite 5 for some reason... 😭 looking.

After normalization, redirects use 'origin' instead of 'from', so we need
to check redirect.origin for the SPA pattern match.
Removed direct JS file check that was causing path resolution issues on
Windows with Vite 5. The test now verifies the fix by checking that JS
executes and updates the page, which is a better test of the actual
user-facing behavior.
@serhalp serhalp requested a review from pieh November 5, 2025 17:36
@serhalp serhalp enabled auto-merge (squash) November 6, 2025 13:30
@serhalp serhalp merged commit 510642b into main Nov 6, 2025
38 of 42 checks passed
@serhalp serhalp deleted the 2025-11-04-jumr-bclmo branch November 6, 2025 14:07
@token-generator-app token-generator-app bot mentioned this pull request Nov 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Vite plugin + Redirects

3 participants