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
Hello, I’m Allie!
Provide a 404.html page, that will be served instead of the index
NullVoxPopuli
NullVoxPopuliOP•7mo ago
for an SPA, I still need the index.html but it should only be served for HTML requests 🤔
Hello, I’m Allie!
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 API
NullVoxPopuli
NullVoxPopuliOP•7mo ago
I have extensions on my requests, like /path/to/file.md or /path/to/file.gjs (or /path/to/file.json)
Hello, I’m Allie!
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
NullVoxPopuli
NullVoxPopuliOP•7mo ago
aren't functions only available at the /function root path?
Hello, I’m Allie!
No. If you have /functions/index.js, that function will handle requests to /
NullVoxPopuli
NullVoxPopuliOP•7mo ago
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 🤔
Hello, I’m Allie!
Workers doesn't have a file system, so there isn't anything to check
NullVoxPopuli
NullVoxPopuliOP•7mo ago
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
Hello, I’m Allie!
context.next() is the way to ask the asset server for an asset, if that is what you mean?
NullVoxPopuli
NullVoxPopuliOP•7mo ago
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
[wrangler:err] ReferenceError: ispect is not defined
[wrangler:err] ReferenceError: ispect is not defined
(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
NullVoxPopuli
NullVoxPopuliOP•7mo ago
hm, when actually deployed, my middleware isn't active solved here: https://discord.com/channels/595317990191398933/1238913546897719306/1239000045446107156
Want results from more Discord servers?
Add your server