I'm incorrectly *not* getting a 404 on some fetch requests. How do I get 404s?
I have code that fetches files with extensions -- and that works great, but when a file is missing, I get the index.html page. I believe this to be wrong behavior. Is there a way to fix, or is this a bug with cloudflare? 😅
14 Replies
Provide a 404.html page, that will be served instead of the index
for an SPA, I still need the index.html
but it should only be served for HTML requests
🤔
How would it know what is an HTML request, and what isn't? For example,
/api
might return JSON, or an HTML page explaining how to use the APII have extensions on my requests, like
/path/to/file.md
or /path/to/file.gjs
(or /path/to/file.json
)Sorry, I meant that while you may follow that convention, Pages doesn't know that. You can return a 404 manually with a Function tho
aren't functions only available at the
/function
root path?No. If you have /functions/index.js, that function will handle requests to /
Ah ok. I'll give this a go. Thanks!
oh wait, this won't work: https://community.cloudflare.com/t/cant-resolve-fs-in-cloudflare-workers/112762
unless there is another way I can handle checking file existence 🤔
Workers doesn't have a file system, so there isn't anything to check
how do I know if a file exists 😅
my plan was to 404 if a .mjs, .md, or .json URL didn't exist
and fallback to context.next() if the URL didn't have a file extension
context.next()
is the way to ask the asset server for an asset, if that is what you mean?oh, yeah, maybe I just store that to a variable and inspect it before deciding to instead seind an error response
I'll try that
hm, I can't console.log, due to
(im' using the local wrangler tool, which is very cool!)
lol, nope, my bad, accidentally copy pasted something from my clipboard in there (ispect)
oof
need more coffee
hm, when actually deployed, my middleware isn't active
solved here: https://discord.com/channels/595317990191398933/1238913546897719306/1239000045446107156