question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React-router intercepting server routes

See original GitHub issue

Version

4.2.2

Steps to reproduce

Have a server that has an endpoint to be directly navigated to (e.g. /api/logout):

app.get('/api/logout', (req, res) => {
  req.logout();
  res.redirect('/');
});

if (process.env.NODE_ENV === 'production') {
  app.use(express.static('client/build'));

  const path = require('path');
  app.get('*', (req, res) => {
    res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
  })
}

Add navigation in the react app like <a href=“/api/logout”> or window.location = ‘/api/logout’

Expected Behavior

A page refresh to the same domain through <a> tag navigation or setting window.location should go through server routing before loading the react app.

Actual Behavior

The correct route request isn’t made to the server and the route is treated as a react-route.

This behavior is only experienced in a production environment. However, there is a strange case where everything works as intended. In firefox private browsing, routing works correctly. In regular firefox, regular chrome, and incognito chrome, routing is not working, as described above.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
dev-grantcommented, Sep 14, 2017

@pshrmn @timdorr I fixed things by removing the registerServiceWorker() from the create-react-app boilerplate.

0reactions
jrrcdevcommented, May 22, 2018

Removed registerServiceWorker and unregister fix this issue, what this registerServiceWorker do to fuck everything?

Read more comments on GitHub >

github_iconTop Results From Across the Web

React-router intercepting server routes · Issue #5520 - GitHub
The correct route request isn't made to the server and the route is treated as a react-route. This behavior is only experienced in...
Read more >
React Router doesn't intercept an HTTP/S request
React -router will "intercept", so to speak, any calls and subsequently perform the client side routing. There wouldn't be a server call for ......
Read more >
Example: React Router: Preventing Transitions.
React Router - Preventing Transitions. A simple example deployed using react-codesandboxer. 293. 0. 0. Edit Sandbox. Files. example.js. index.html. index.js.
Read more >
Hookrouter: A Modern Approach to React Routing
Routing in React applications using the Hookrouter​​ Routing is essential for Single Page Applications (SPAs) to navigate through pages and to ...
Read more >
React Router Version 6 Tutorial – How to Set Up Router and ...
In React, the page contents are created from our components. So what React Router does is intercept the request being sent to the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found