_routes.json not being leveraged

I have an TanStack Router SPA that is leveraging a _routes.json. The page loads fine, but we're finding that when an asset is missing from under /assets/* it returns the SPA 404 page which then breaks all manner of things due to it return html when the mime types requested are css/js. Am I configuring this incorrectly?
{
"version": 1,
"include": ["/*"],
"exclude": ["/assets/*", "/favicon.ico"]
}
{
"version": 1,
"include": ["/*"],
"exclude": ["/assets/*", "/favicon.ico"]
}
8 Replies
Eric Chernuka
Eric ChernukaOP6d ago
We're also uploading using pages deploy dist
Eric Chernuka
Eric ChernukaOP6d ago
No description
Chaika
Chaika6d ago
The _routes.json does this
include: Defines routes that will be invoked by Functions. Accepts wildcard behavior. exclude: Defines routes that will not be invoked by Functions. Accepts wildcard behavior. exclude always take priority over include.
So you're telling it not to run on /assets/ files. So it's not running. SPA Behavior though is a platform thing, not part of your function code. If it can't find an exact match, and there's no top level 404.html, it'll serve your index.html. What do you want it to do? If you want it to serve a custom actual 404 and 404.html for assets files, I believe you could just upload a 404.html in the assets directory for that to happen.
Eric Chernuka
Eric ChernukaOP6d ago
Ya, I think I need a 404 page for the fall through of content under the exclusion list. Essentially what I'm looking for is for CF to indicate a 404. Right now its returning a 200 and the SPA index.html which then causes the app to explode. So when an /assets/index-hash.js is found, it returns the source. When not found, I need it to return a 404 header so that vite's module import can throw an event indicating the asset is no longer present.
Chaika
Chaika6d ago
I believe you can just create /assets/404.html with some simple html, and the status code would be 404, and that should work even in SPA mode, but been a while since I messed with that
Eric Chernuka
Eric ChernukaOP6d ago
I think the docs indicate I need the 404.html at the root based on what I've read and not under the assets folder. I'll give it a go
Chaika
Chaika6d ago
Having a 404.html at the root would disable SPA mode, which I believe you want to keep? https://developers.cloudflare.com/pages/configuration/serving-pages/#not-found-behavior The not found behavior is what you'd want to leverage instead
This means that you can define custom 404 paths for situations like /blog/404.html and /404.html, and Pages will automatically render the correct one depending on the situation.
Looking at the Pages Asset Handler Code https://github.com/cloudflare/workers-sdk/blob/a6a267aa923b7f4200baca41b60aa35cf3bd71fe/packages/pages-shared/asset-server/handler.ts, the spa behavior is only invoked if traveling up doesn't result in a custom 404 page, so should just work if you want to continue to have SPA behavior for all other directories other then assets
Eric Chernuka
Eric ChernukaOP6d ago
Got it, so we do essentially want a 404 in the assets directory

Did you find this page helpful?