Add fallback file for SPA sites
Imagine I am using Blazor WebAssembly.
It starts from an index.html file but I would like to open a route, e.g. /mypage which the SPA can handle.
Would be nice to add an option for a fallback file.
Otherwise, I've been loving this tool ⭐
sorry bestie i have no clue what this means
Lets say you have 3 files in the webserver
- a.html
- b.html
- c.html
if the user routes to localhost/b.html etc it routes as usual.
Assuming there is a 'fallback' route that is configured as 'a.html', if they route to localhost/d.html (the file isn't found) then the server returns 'localhost/d.html' -> 'localhost/a.html'
more reading:
https://laraveldaily.com/post/route-fallback-if-no-other-route-is-matched https://medium.com/@codingthang/fallback-route-on-rails-e19be25383dd
So this is "return a redirect instead of a 404"?
I would guess this could be added as yet another cli argumnet... you provide a file that you want to be auto-redirected to, and then a regex for what can 'detect' a 404 and instead server the redirect. This auto-redirect would obviously only kick in if the file that was directly requested wasn't found in the first place. Not sure how it would interact with serving index files.
I needed this and implemented a quick fix. I'd be happy to open a pull request, please advise (found no contribution guidelines).
@Hona in the meantime, feel free to use my branch, it works in our deployment.
So this is "return a redirect instead of a 404"?
No, the server simply serves the fallback file under the arbitrary URL, so something like an SPA can handle the routing itself.
One example where this is needed, is a statically served frontend only using APIs and needing to handle a post-login redirect from an external OAUTH provider.
Oh that patch explains it much better.
Can you try the current develop branch (at least 4b31b1f59e25f9e049881c367d7a3d704b3f2bf8)? http --404 /srv/404.jpeg &c. ought to work.
Your changes look much more thorough with regards to the fallback file feature. There are some unrelated things in the commit, though (whitespace/reformatted strings). My quick tests with it look fine.
I wonder whether the argument could be named --fallback and the file optional, defaulting to index.html, which it would most likely be in the majority of cases.
I think arguments named after status codes (404, 403, ...) could be added as a different feature where one could provide custom error pages?
I think --fallback is too domain-specific, hence --404. I don't really see a use-case for rebinding statuses in general (even --404 only works on GETs, the reply to a 403 is never actually shown to users, what else would you want to rebind?) (this is not to say that I'm against the use-cases themselves, if they ever pop up, just against implementing over the void).
Released in v2.3.0