-
Notifications
You must be signed in to change notification settings - Fork 12
fix: ignore SPA redirect in dev mode to allow Vite to take over #514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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
| const isSPARedirect = | ||
| redirect.from === '/*' && | ||
| redirect.to === '/index.html' && | ||
| (redirect.status === 200 || redirect.status === undefined) |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
|
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.
8ef8483 to
67cf9e7
Compare
Fixes #325
Problem
When using the standard recommended SPA redirect configuration in
netlify.toml: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 toindex.htmlinstead of serving the actual JS files. This results in errors like: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.