Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to handle 404 requests that fallback into index.html but the route doesn't exists? #139

Open
odahcam opened this issue Jun 30, 2024 · 5 comments

Comments

@odahcam
Copy link

odahcam commented Jun 30, 2024

I'm facing this issue right now any requests made to my server that are not captured by any mapped routes result in a html being returned to the client instead of a desired 404..

@szymmis
Copy link
Owner

szymmis commented Jun 30, 2024

Hi @odahcam, basically you have two ways of handling this. If you are using client-side routing then you can just catch it and render 404 page but I guess you would not ask if you were using that. Second solution is to have a middleware that checks request url and sends 404 response if the path should not be available.

@odahcam
Copy link
Author

odahcam commented Aug 5, 2024

I made both, thanks!

It would be cool if the library had something built-in to help us handle 404. Just some opinionated list of interface that users could use to setup known routes on the root level of the app so ViteExpress automatically catches them and also adds a bypass for the files in the outDir. If you find it ok I could create a PR on this.

@odahcam
Copy link
Author

odahcam commented Aug 7, 2024

So there is a problem with the middleware approach, it doesn't work well for development. Vite tries to load development assets that the dev may don't know about and that makes it not viable for development, since the developer would need to keep watching the network relentless to spot any files that are now needed or not needed anymore. Also it would force the developer to add an extra condition to check if it is in development so they only allow such files requests on development.

Screenshot 2024-08-06 at 17 45 30

If ViteExpress had a hook that runs after a check if the static files exist and before falling back to the catchall route, the dev could decide whether to fallback or early return a not found error based on the information available.

@szymmis
Copy link
Owner

szymmis commented Aug 29, 2024

I think it is a good idea to have that kind of hook somehow available. If you are still willing to propose some solution I'd be happy to help with it, if not I understand that.

I think having some config value (like a regex pattern) that you can specify to tell vite-express which front-end paths are valid + a hook that let's you validate them on the fly would be the perfect solution for you problem, if I understand that correctly.

Let me know what you think!

@graue
Copy link

graue commented Sep 6, 2024

I like that proposed solution.

In my case, I'm not using client-side routing at all, so I would prefer to turn off serving index.html for unknown paths entirely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants