The 404 that was a project type
The contact function was deployed, correct, and tested, and still returned 404. This project is a Worker, and Workers do not compile functions/ the way Pages does.
What we were doing. Replacing the dead PHP mailer with functions/api/contact.js. The code was written, 17 tests passed under the real workerd runtime, the commit was pushed, the deploy succeeded.
What happened. POST /api/contact returned 404. So did GET, which should have hit the handler's own 405.
Ruling things out
Worth recording the sequence, because the wrong instinct here is to doubt the code:
- The file was in the pushed tree at the right path.
- It parsed as an ES module and exported
onRequest. - No
_worker.jsor_routes.jsonexisted to suppress it. - The deploy definitely ran: a file deleted in the same commit was gone from the live site.
- Run locally under
wrangler pages dev, everything worked perfectly.
So the code was correct and deployed, and still did nothing. The answer came from a detail in a dashboard screenshot: the project's domain ended in .workers.dev, not .pages.dev.
This is a Worker with static assets, not a Pages project. Pages auto-compiles a functions/ directory into routes. A Worker project does not. Without wrangler.jsonc naming an entry point, every file under functions/ was simply uploaded as a static asset.
Confirmed directly: requesting /functions/api/contact.js returned 200 with content-type: text/javascript. The endpoint was serving its own source code, exactly as the old PHP files had.
The fix
wrangler.jsonc declaring worker.js as main, with the site bound as assets. worker.js routes /api/* to handlers and passes everything else to the ASSETS binding. Routing is a hand-written lookup table rather than wrangler pages functions build, which would add a build step to a site that otherwise has none.
.assetsignore arrived at the same time, so the config and the function source would not themselves become downloadable, which would have repeated the .env mistake in a new costume.
What would catch it sooner
Ask what the endpoint returns for its own file path. A 200 with a source-code content type means the file is being served, not executed. That one request distinguishes "routing is broken" from "code is broken" instantly, and would have skipped most of the afternoon.
Also: local success proves the code, not the deployment. wrangler pages dev ran the function happily because it was told to treat the directory as Pages functions. The live project was never doing that.