Redirect non-existing routes to index.html for Generated Static Sites

Currently if a glitch project is a Generated Static Site it can not use a standard browser history based routeing scheme, it must use a hash based url scheme (e.g. example.com/#/about). If you want to just a standard scheme (like example.com/about) you’re out of luck. Once your project has gone to sleep and glitch serves the statically compiled site, if you try to load a route that doesn’t map to an actual file then it will just throw a 404. Which is frustrating since most modern static sites don’t work this way. Most statically generated sites with multiple routes have the web server setup such that any non-existing routes get redirected to index.html. Then it’s up to your app to decide if it’s a valid url that it should render a page for or return a 404.

I’m suggesting that for new projects glitch should start redirect non-valid urls to index.html for generated static sites and leave it up to the app to decide if it’s a route or not (or at least provide the option to make a glitch project behave this way).

To demonstrate this behavior I’ve remixed the react generated static site and simply removed the hash router leaving the standard routing behavior. This causes the about page to become immediately inaccessible as soon as the project switches to static mode:

https://gss-route-test.glitch.me/about (accessible if the project code has been accessed in the last 5 minutes, otherwise not)

The project code:

Thanks!

This is a fantastic suggestion - in fact in the early days of generated static sites this was the behaviour by default. IMO that was a lot better, plus the react starter used proper routing back then iirc.
You absolutely have my vote (if I can free one up :pensive:)

Edit: I think originally they did something slightly different, for instance the 11ty starter gave a 404 error similar to the default on the old express project, and the react one was empty but with css or smth - but it was very similar to this.

1 Like

Thanks for writing this up, I’m going to see if it’s possible for us to make it happen soon!

2 Likes

Great! Thanks for that :blush:

1 Like