cli: Netlify Dev incorrectly rewrites requests for files that *don't* exist
I think this is the right place to open this; https://github.com/netlify/netlify-dev-plugin/issues/155 and https://github.com/netlify/netlify-dev-plugin/issues/181 seem related but don’t quite capture the issue.
There seems to be a difference in the way netlify dev processes redirects compared to the Netlify platform. (Note: I’m not certain both/either use node-redirects-parser, just assuming.) In a CRA app using react-router, you need a public/_redirects file like this to use any routes aside from the base path:
/* /index.html 200
That works fine on Netlify itself; requests to custom routes are properly rewritten without interfering with requests for files that actually exist. However netlify dev seemingly applies this rule to all requests, similar to the way Netlify would if you appended an exclamation point to the rewrite (200!). This means requests for scripts like /static/js/bundle.js and other basic assets are rewritten to /index.html. The rewritten requests get a 200 response but obviously it serves the HTML, so JS processing will immediately throw errors like Uncaught SyntaxError: Unexpected token <.
This breaks Netlify Dev, which has otherwise been immensely useful; I’m searching for a workaround/fix.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 4
- Comments: 16 (9 by maintainers)
hey, i think the issue is in @netlify/rules-proxy. This package seems to be checking if there’s already a static file with the following function:
I don’t think this works (at least in my case) since webpack-dev-server is generating all the static assets in a virtual location, so they would never actually exist in the file system.
I’m not too experienced with how this works, so please correct me if i’m wrong 😃
@edaemon I think that’s because
webpack-dev-serveritself handles the redirect in that case. Netlify Dev is just a proxy for thewebpack-dev-server, so every request that it gets (and doesn’t rewrite itself) it just passes on to WDS, which then handles the rewriting. So that part will only ever work in development where the dev-server is running, but not in production where Netlify provides its own server.IMO the best way to use Netlify Dev, in order to make it as close as possible to the real Netlify environment, is to not use any dev servers at all. If possible, configure your tools to just watch the files and write the changes to disk, and let Netlify Dev handle the serving.