Server-side files being redirected to using client routing due to registerServiceWorker
See original GitHub issueThere’s this odd bug I’m encountering where using process.env.PUBLIC_URL to link to pdf’s in the public
folder doesn’t work consistently with react-router. It will work fine on localhost, but the moment I deploy it live, the link to the pdf works only once or twice before failing and going to a 404 page.
Also not sure if this bug lies here or with react-router, I couldn’t find any reference on their repo. Sorry if it’s the wrong spot.
Has anyone else encountered this? For now I’m serving the pdf from within src
, but really dislike the complicated path and hash produced by file-loader, so might have to end up ejecting…
I’d also like to note this worked in prior versions of create-react-app
as seen here: https://github.com/facebookincubator/create-react-app/issues/775
Is this a bug report?
Yes
Environment
node -v
: 8.4.0npm -v
: 5.6.0react-scripts
: 1.0.11react-router
: 4.2.0react-router-dom
: 4.2.2
Steps to Reproduce
- Add file to public
- Reference it in code using
{process.env.PUBLIC_URL + "path to file"}
- Create a build and deploy
- Click link several times, notice that it stops working
Expected Behavior
Consistently open the file
Actual Behavior
Routed to 404 after using link once.
Issue Analytics
- State:
- Created 6 years ago
- Comments:11 (7 by maintainers)
Top GitHub Comments
Within a week I’d say.
Ok, here’s the supposed solution for server routes: delete registerServiceWorker, it makes server routes and react-router not play nicely. However, this didn’t work for me with my file.
I’m going to reopen this then, as this part of create-react-app should not alter react-router’s default behavior for server-side routes. I’m not quite sure what in registerServiceWorker is the cause.
What I found through testing, is that when using a <Link to="server-route" target="_blank"> or an tag, the link would work fine in Firefox Private or Safari Private, only once in other browsers (Firefox, Chrome, Chrome Incognito). Perhaps something in handling the history causes them to fail and render the server route as a client route?
EDIT: It looks like the service worker isn’t caching the public files in setOfCachedUrls, could this be the problem?
SECOND EDIT: After re-reading the Getting Started, I realize that server routes aren’t supported, but would just like to state it would be nice if they could be, especially in the case of files in
/public
someone might want to render (in my case a pdf)I will leave this for a maintainer to close just so they can note the feedback.
Related issue: https://github.com/ReactTraining/react-router/issues/5520