http icon indicating copy to clipboard operation
http copied to clipboard

Add fallback file for SPA sites

Open Hona opened this issue 2 years ago • 4 comments

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 ⭐

Hona avatar Jan 24 '24 04:01 Hona

sorry bestie i have no clue what this means

nabijaczleweli avatar Jan 29 '24 17:01 nabijaczleweli

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

Hona avatar Jan 30 '24 22:01 Hona

So this is "return a redirect instead of a 404"?

nabijaczleweli avatar Feb 10 '24 15:02 nabijaczleweli

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.

thecoshman avatar May 04 '24 15:05 thecoshman

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.

moschroe avatar Jun 13 '25 22:06 moschroe

Oh that patch explains it much better.

nabijaczleweli avatar Jun 14 '25 00:06 nabijaczleweli

Can you try the current develop branch (at least 4b31b1f59e25f9e049881c367d7a3d704b3f2bf8)? http --404 /srv/404.jpeg &c. ought to work.

nabijaczleweli avatar Jun 14 '25 01:06 nabijaczleweli

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?

moschroe avatar Jun 14 '25 10:06 moschroe

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).

nabijaczleweli avatar Jun 14 '25 12:06 nabijaczleweli

Released in v2.3.0

nabijaczleweli avatar Jun 15 '25 16:06 nabijaczleweli