Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactJS routing/browser-sync reload on /path produces Cannot Get /Path error

I'm building a basic React/Flux application and using react-router-component for the routing, browser-sync for live reload on build changes, and browserify for dependency injection.

The problem that I have is that when the live reload or any reload occurs on a path that isn't / (i.e. /profile, /gallery, etc...), I get an error message of Cannot GET /path (or any route for that matter).

I suspect that this has something to do with the fact that it's a single page application and all routing is done on the client.

Here is my browser-sync setup (it's very basic). I think that I might need to add some middleware, but I'm not sure what to actually put in the middleware.

gulp.task('browser-sync', function() {
    browserSync({
        server: {
            baseDir: './client'
        },
        notify: false
    });
});
like image 323
JoshSGman Avatar asked Sep 10 '14 16:09

JoshSGman


People also ask

Does react router reload the page?

react-router-dom allows us to navigate through different pages on our app with/without refreshing the entire component. By default, BrowserRouter in react-router-dom will not refresh the entire page.

What is BrowserRouter in react?

BrowserRouter: BrowserRouter is a router implementation that uses the HTML5 history API(pushState, replaceState and the popstate event) to keep your UI in sync with the URL. It is the parent component that is used to store all of the other components.

What is* in React Router DOM?

Using an asterisk ( * )To set up a default page in React Router, pass an asterisk ( * ) to the Route 's path prop: <Switch> <Route exact path="/" component={Home} /> <Route path="/messages" component={Messages} /> <Route path="/about" component={About} /> <Route path="*" component={Home} /> </Switch>


1 Answers

This is likely because whatever web server you're using to serve out your app is trying to actually find /profile or /gallery on the server side. You need to instruct your server that ALL requests to anything goes to root instead. Sometimes depending on your software this is called 'html 5 mode'.

I noticed that there's a post on the browser-sync git repo about this with possible solutions here: https://github.com/shakyShane/browser-sync/issues/204

But the basic idea is to make the server send everything that isn't *.js or *.css to ./index.html (assuming that is your app's entry-point file). That way the server doesn't look for those routes itself, and it just loads your app which is then free to parse them correctly on the client-side.

Hope this helps.

like image 174
Mike Driver Avatar answered Nov 03 '22 09:11

Mike Driver