Serving SPA in subdirectory
I know you can serve a SPA (single-page application) in the root of the Pages deployment, if you don't have a
404.html
in the root (https://developers.cloudflare.com/pages/configuration/serving-pages/#single-page-application-spa-rendering).
However, I want to have normal page serving behaviour for the root and most of the site, but have SPA style page serving in a specific subdirectory (e.g. example.com/spa/
) - so that any request URL that begins with /spa/*
responds with /spa/index.html
without redirecting. How could I achieve this?4 Replies
The current SPA logic in pages only fires if it walks up all the directories looking for a 404.html and doesn't find one.
The only way you would be able to achieve that is with a function running in advanced mode handling all of the requests.
SPAs are sometimes not happy about being under a subdirectory anyway, why not just use a different project and a subdomain? It may make more logical sense with deploying the project/git anyway.
Thanks for the reply! A separate repo/deployment would def make my life easier, but unfortunately it's not feasible for this project :facesad:
I did suspect I'd need a Function/Worker for this. For clarity, are you saying that I could have a Function that just handles the traffic to the SPA (which would be fine), or that I'd need a Function that needs to insert itself between all traffic throughout the entire deployment (which sounds less fine)?
Or.... could I not just call the root of the SPA
404.html
and then CFP would automatically respond with that? Only downside would be the HTTP status code - could that be overridden with Workers?it'd just have to run on all traffic to the spa, check for 404 and respond with index.html under the spa path
Cool - I'll have a go on that, then. Thanks!