Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot access page on Refresh due to Hasbangs in a React SPA

I'm using a ReactJS SPA created from https://github.com/facebookincubator/create-react-app

I'm using S3 and Cloudfront to serve my website. Everything works fine until I reload the page - it throws an error (Access denied in my case) as it is not able to handle without the Hashbang.

Note: It works fine if I enter the URL with a hashbang

So essentially, this works: https://example.com/#/dashboard (Redirects to https://example.com/dashboard)

But if I refresh the page, it gives an error like:

enter image description here

We're using browserHistory to maintain the routes. I'm showing the related code only:

<Router history={browserHistory}>
<Route path='/dashboard' component={Dashboardpage} />
</Router>
like image 972
Karan Shah Avatar asked Feb 14 '17 08:02

Karan Shah


People also ask

How do I respond to page refresh in React?

To detect page refresh by pressing F5 in a React component, we can use the performance. navigation. type property to check what kind of navigation is done. If the value is 1, then we refreshed the page manually with the F5 key.

How do I force a browser refresh React?

If set to true, the browser will do a complete page refresh from the server and not from the cached version of the page. import React from 'react'; function App() { function refreshPage() { window. location. reload(false); } return ( <div> <button onClick={refreshPage}>Click to reload!

How do you refresh the page in React hooks?

To refresh a page, we need to use the window. location. reload() method in React. By default this method reloads the page from a cache, if we pass true as an argument it reloads the entire page from a server instead of cache.

Does React redirect refresh 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.


2 Answers

When you request https://example.com/dashboard, the first request i.e https://example.com is made to the server and it should return the index.html that contains your react-router which is smart enough to understand the path i.e without the hashbangs and loads the required component.So some redirecting routes has to be set up on server side.

In your case when you hit the https://example.com/dashboard, S3 and cloudfront should handle the error codes(i.e 404 or any) and redirect the page to index.html, after that the react-router will handle which component to load.

enter image description here

enter image description here

Hope my answer is clear ;)

You can also refer to the answer given here React-router urls don't work when refreshing or writting manually

like image 191
Shailaja shah Avatar answered Sep 20 '22 12:09

Shailaja shah


Besides having errors pages setup explained by Shailaja make sure that your CloudFront distribution is pointing to a website endpoint instead of a api endpoint. API Endpoints will not work correctly with react-router.

API Endpoint:

bucket-name.s3-website-region.amazonaws.com

Website Endpoint:

bucket-name.s3-website.region.amazonaws.com

like image 21
Jingyi Wang Avatar answered Sep 20 '22 12:09

Jingyi Wang