Google sees 404s from my SPA (served through not_found_handling)

I have a Worker with a single page app serving on routes not matched by the worker code.
"assets": {
"not_found_handling": "single-page-application"
},
"assets": {
"not_found_handling": "single-page-application"
},
The SPA loads just fine in my browser, but if I make a cURL request I get a 404. For example curl https://www.togethertime.games/games/chess gives a 404, but https://www.togethertime.games/games/chess works just fine. This is affecting the Google Bot, too. Any suggestions on how to resolve?
10 Replies
Chaika
Chaika7mo ago
It's from a decision the Workers Static Assets team made to make it sort of quickly work but is a bit bleh. Leo outlined the header you see, and browsers send that header automagically
dan—1106
dan—1106OP7mo ago
ah thanks, that at least helps me to understand the behavior!
Chaika
Chaika7mo ago
For now, you should have your Worker call the not found logic when it doesn't find anything either/run
dan—1106
dan—1106OP7mo ago
what would my not found logic in the Worker do?
Chaika
Chaika7mo ago
call return env.ASSETS.fetch(request); at the end, if your worker script didn't find anything to run like for example,
async fetch(request, env) {
const url = new URL(request.url);

if (url.pathname.startsWith("/api/")) {
return new Response(JSON.stringify({ name: "Cloudflare" }), {
headers: { "Content-Type": "application/json" },
});
}

return env.ASSETS.fetch(request);
},
async fetch(request, env) {
const url = new URL(request.url);

if (url.pathname.startsWith("/api/")) {
return new Response(JSON.stringify({ name: "Cloudflare" }), {
headers: { "Content-Type": "application/json" },
});
}

return env.ASSETS.fetch(request);
},
dan—1106
dan—1106OP7mo ago
ah gotcha, that's helpful thank you!
Chaika
Chaika7mo ago
that'll invoke the not found behavior even without the header if nothing in the worker runs yea, this is def a bit messy right now, hoping the team just launches proper _routes.json like Pages had where you can configure exactly where your worker runs, and gets away from this current behavior
dan—1106
dan—1106OP7mo ago
do I need to add a binding to my assets? They don't appear in my worker-configuration.d.ts automatically
Chaika
Chaika7mo ago
if you don't have it configured already yea, ex:
[assets]
directory = "./dist"
binding = "ASSETS"
[assets]
directory = "./dist"
binding = "ASSETS"
or for wrangler json
"assets": {
"directory": "./dist",
"binding": "ASSETS"
}
"assets": {
"directory": "./dist",
"binding": "ASSETS"
}
https://developers.cloudflare.com/workers/static-assets/ I assume you have the directory/etc already set, just need to add the binding name
dan—1106
dan—1106OP7mo ago
this is the asset in my wrangler.json:
"assets": {
"not_found_handling": "single-page-application"
},
"assets": {
"not_found_handling": "single-page-application"
},
but it gets processed by the cloudflare/vite-plugin and generates a new wrangler file for the actual deploy i think that's perfect, looks like it solved it! i appreciate the quick help 😄

Did you find this page helpful?