Hosting Spring and a SPA on AWS

So for the past few days I’ve been killing myself with hosting my first actual app.. Today, I finally got it fully working and I am so happy. But very soon, I noticed that there’s a big problem..

In my React app I am using a BrowserRouter to route around the app. I have my homepage, my about page, and an adminpage. When I tried to access my about page with a direct URL, I got a nice little 404. After googling for a few minutes, it was pretty clear what was wrong.. Since ReactJS is a SPA, routing around the page isn’t actually routing around the page, it just unmounts the old and mounts the new Component.. I tried fixing this with the following code in Spring:

@Controller
public class Controller {
@GetMapping(value = {“/”,“/about”})
public String allIndex(){
return “index.html”;
}
@GetMapping(“/adminpage”)
public String getIndex(){
return “index.html”;
}
}

It… works.. But for some reason /adminpage still doesn’t want to work, but at least the pages that I want the user to see work, so I’m fine with that for now..

Now I am wondering, would it be better to host the React app on S3 by itself, and host the Spring app on beanstalk? Currently they’re both ran from a single .jar and they’re run on my beanstalk instance. Would splitting them up like that fix this? How do you deal with this issue? I’m sure the same thing happens with Angular, and all the other SPAs, right? I know React has the HashRouter, but I really don’t want to deal with those ugly URLs with the hashtag.

But nonetheless, I probably won’t bother fixing this app right now, because I am very happy with it, even if my admin panel doesn’t work. So I am wondering how to prevent this from happening in the future. Thank you for any help.

edit: I got the adminpanel to work, I was just spelling the endpoint wrong cuz im stoopid

If you or someone you know is contemplating suicide, please reach out. You can find help at a National Suicide Prevention Lifeline

USA: 18002738255
US Crisis textline: 741741 text HOME

United Kingdom: 116 123

Trans Lifeline (877-565-8860)

Others: https://en.wikipedia.org/wiki/List_of_suicide_crisis_lines

https://suicidepreventionlifeline.org

Great, now I’m on suicide watch as well

I really want to know how your post triggered that bot. :stuck_out_tongue: in b4 java

If I understand what you have correctly, I think you’re actually routing to a different page in your SPA (making it an MPA), whereas you could use a # in your URLs.

Your “Killing Myself” got the attention of this bot :joy:

I’m sorry I don’t know what you mean. It’s still a single page, I am using react-router and the Link component to link to other places in my page. There are no page reloads, it’s all a single page.

Why do you need to return the contents of index.html from the backend when navigating to /about or /adminpage?

Because if I try to access /about with a direct link like localhost:8080/about it gives me a 404. You can read more about the problem here

That’s what I was asking. If I host the react app on S3 and Spring on Beanstalk, will I face the same issues that I am facing now? Will React in that case actually know how to route properly?

You dont need any lambda support for simple spa hosting. Just set 404 and 403 handling to point to index.html.

https://gist.github.com/bradwestfall/b5b0e450015dbc9b4e56e5f398df48ff#spa

If you prepend # to the path in your URLs it will always load /index.html. Alternatively you could set up a rule in the controller to map everything except your API endpoint to index.html (It’s “bean” a while since I’ve used spring so I don’t know how off the top of my head how).

If you moved the SPA front end to S3 you’ll have the exact same problem.

I am currently using exactly what you are referring to, check my OP.

I appreciate all the help, but I don’t see how this would be any different than what I am currently using with the @Controller where every url just redirects to index.html. The React router still doesn’t actually work like I want it to(and it never will since it was meant to work like I want it to), I’m just tricking it into making it work.

I have used a small Express server to redirect all requests to “/“ and then letting React Router render the correct component. Check out this StackOverflow for the code example: https://stackoverflow.com/a/38106623

Not a 1:1 with your tech stack, but could be a starting point.

I mean that’s kind of exactly what I am doing with my @Controller in the code provided.. Whenever a user makes any request to the site, Spring redirects it to index.html, and then React does the rest.

Ah! I missed the edit at the end and should have spent a second longer reading your code sample. Glad you got it working.