2

I have a react app, it uses react router dom. I built it and moved it to, say, http://domain.tld/ the site works fine. I have two problems :

  • if I visit the app and then click on a pdf linked (which is just a real file on my server) react-router-dom somehow hijacks it, and shows me the 404 page I set up in react router
  • same goes if I try to access, for example, http://domain.tld/api/whatevs, it doesn't work either

in both case if I empty the cache, I can see my pdf, or the json result of my api then a js is injected, and a refresh gives me my 404 page.

How can I prevent react router to hijack everything ? I already have a mandatory apache rewrite to redirect in case files dont exist.

EDIT: as requested in a comment, here is how my routes are defined :

<BrowserRouter>
  <div>
   <Menu />
   <Route path="/" exact component={Home}/>
   <Route path="/pages/:page_name" component={Page}/>
   <Footer />
  </div>
</BrowserRouter>

I removed some routes, but the structure is intact.

EDIT2: from what I gathered, in fact, the service worker 'hijacks' all my static server route, and that's suppose to be normal, so I'll just unregister it, but if I'm correct I can't do that unless I want my app to reload on each link, I'm still looking for a way to tell it : "pretty please, dont touch /api/, /assets/ etc"

EDIT3: and this might just be what I need : Setting service worker to exclude certain urls only

I'll try when I'm at work tomorrow

beauchette
  • 1,046
  • 2
  • 13
  • 31

0 Answers0