3 ms·
Ahhh...I see what you're saying. So like, if you're on my-spa.com, and click a link to a post, the JS loads the post and updates the URL, but doesn't perform a
by Sohcahtoa82 3y ago
Ahhh...I see what you're saying.
So like, if you're on my-spa.com, and click a link to a post, the JS loads the post and updates the URL, but doesn't perform a full page load. So while you might be on https://my-spa.com/user/39820002/post/39811155 https://my-spa.com/user/39820002/post/39811155, your browser likely never actually sent a GET request to /user/39820002/post/39811155. But this creates the issue that if you manually navigate to that URL, your browser WILL send a request for it.
In that case, yeah, I think the correct thing to do is to return the 200 OK with your usual index.html. When the JS makes the API requests, those requests should return a 404, and the JS will then render a page that says "That post doesn't exist" or something.
But in the case of a request that couldn't possibly exist, like a GET request for /etc/passwd, they should still be 404s. Any router should be able to do that.
EDIT: I think a better way to do this hold thing is to make the resource an anchor tag in the URL. Instead of https://my-spa.com/user/39820002/post/39811155 https://my-spa.com/user/39820002/post/39811155, make it https://my-spa.com/#user/39820002/post/39811155 https://my-spa.com/#user/39820002/post/39811155
- __jonas 3y ago> So like, if you're on my-spa.com, and click a link to a post, the JS loads the post and updates the URL, but doesn't perform a full page load. So while you might be on https://my-spa.com/user/39820002/post/39811155 https://my-spa.com/user/39820002/post/39811155, your browser likely never actually sent a GET request to /user/39820002/post/39811155. But this creates the issue that if you manually navigate to that URL, your browser WILL send a request for it. Exactly, you got it. This is why SPA configurations for webservers exist, like the SPA_MODE on the one we are commenting on. > EDIT: I think a better way to do this hold thing is to make the resource an anchor tag in the URL. Instead of https://my-spa.com/user/39820002/post/39811155 https://my-spa.com/user/39820002/post/39811155, make it https://my-spa.com/#user/39820002/post/39811155 https://my-spa.com/#user/39820002/post/39811155 As someone else said: https://news.ycombinator.com/item?id=39819120 https://news.ycombinator.com/item?id=39819120 Hash routes have their own problems, I don't think they are a great solution, but indeed SPA routers often support them, example from react router: https://reactrouter.com/en/main/router-components/hash-router https://reactrouter.com/en/main/router-components/hash-route... Note that they strongly suggest to not use hash routing unless you have no other choice. I agree that returning 200 to every route isn't great for some scenarios, but realistically, SPAs exist, they usually work like this and they are deployed like this, can't really change that now. I would just not build an SPA with client side routing at this point in time since there are solutions that I personally prefer, other people may think differently though.