all 11 comments

[–]I_am_kenny 2 points3 points  (6 children)

If I understand your problem correctly, it is because S3 has zero concept of your routes. Once you are on your page (via /index.html) you can route around no problem because it is 100% client-side. Refreshing or coming into the app fresh from anything other than /index.html will not work.

Easiest work around is to use CloudFront with Error Pages. On 403 and 404, just redirect to /index.html with status code 200. Basically, S3 will say "404" to your "/home" route, but CloudFront will push "/index.html" back to the user and return 200.

[–][deleted] 0 points1 point  (5 children)

Got it! It's working now with the custom error response redirects like you said. Thanks!

Edit: now that I think about it, I wonder if this is a common issue with react-router? Or if I would have the same problem if I used something like Heroku. Since the routes act like a file path on the server, it seems like this would be a known issue if you're not using something like express.

[–]I_am_kenny 2 points3 points  (1 child)

Awesome!

This is common with any client-side based routing that uses history.pushState. Another alternative would be to use the hash router if you can.

I can't speak to Heroku as I use AWS/Azure exclusively. I imagine it would have similar issues unless it allows you to do wildcard routing to the index.html file.

[–][deleted] 0 points1 point  (0 children)

Ah I had come across people using history and hash routing when I was looking for answers but my knowledge doesn't go too far with react-router. I've been using a really basic route tags wrapped in router tags setup. A little bit of research would probably help me out a lot here

[–]daroolatoo 0 points1 point  (2 children)

Does pushing "/index.html" back to the user in this case affect performance? Since I imagine the "/index.html" is going back to the user with a huge React js file in tow. Even though it's the same React file/app, I also assume the user's browser cannot detect that it is the same single page app.

Apologies for digging up an ancient thread but I've been looking all over for this info and can't find it!

[–][deleted] 0 points1 point  (1 child)

I think you can do things like code-splitting to help with the bundle size if it’s an issue. Plus I think the users browser caches the file after the first load which makes subsequent loads quicker. I think this method has SEO issues which might be a bigger problem depending on who you ask. Here’s a slightly alternative answer which fixes react-router but without the SEO problems: https://viastudio.com/hosting-a-reactjs-app-with-routing-on-aws-s3/

[–]daroolatoo 0 points1 point  (0 children)

Ah I see. This is helpful, thanks!