Serve static assets from root path, then handle /api/ requests with my worker?
I was hoping the "Static assets" feature could help me simplify my setup (currently Pages + Workers). I'm having trouble with
wrangler dev
currently. I want to serve static assets (stored in public
directory on filesystem) for every request except for /api/
requests, which my worker should handle.10 Replies
I tried these things together:
- pass
--route "xyz.example.com/api/*"
to wrangler dev
- set directory = "public"
in the [assets]
section of my wrangler.toml
But my worker is still being invoked for non /api/
requests.
This part is a bit confusing:
In the event a request does not match a static asset, and there is no Worker script (or that Worker script callsIt says "…and there is no Worker script". But doesn't a worker script always exist when using the Static Assets feature? Aww, I was really hoping this would work: (infetch()
on the assets binding),not_found_handling
determines how Cloudflare will construct a response.
wrangler dev
)
Am I forced to deploy two workers for this use case?
Might as well use Pages at that point, I suppose..By default, anything that matches a static asset skips your worker, and everything else hits it
So you don't pay for static asset hits/they don't invoke your worker, but you can have your worker api as well
you can pass the request to the static assets binding in your worker if it doesn't match a static asset and invokes it, to invoke the default 404 behavior:
return await env.ASSETS.fetch(request);
Right, yet I'm using
single-page-application
setting, so HTML pages other than /
end up hitting the worker first, sadly.
Yeah I'm trying to avoid the worker invocation for static assets
I'm thinking of using Pages and Pages Functions instead of WorkersIt's a long way off but eventually Pages will be moved to Workers Assets (and with a solid migration plan)
Please see if my use case is being tracked internally. Thank you 😺
For now, I'll go with Pages. Thanks for the help!
I want to serve static assets (stored inIs this something that static assets might support? love static assets concept, but doesn't work when i need to handle an oauth callback (e.g.public
directory on filesystem) for every request except for/api/
requests, which my worker should handle.
/api/callback
) since the worker won't be hit (unless i turn on run_worker_first
- which i want to avoid).maybe this is the issue https://github.com/cloudflare/workers-sdk/issues/8430
GitHub
🐛 BUG: @cloudflare/vite-plugin ignore
run_worker_first
· Issu...Which Cloudflare product(s) does this pertain to? Vite Plugin What versions are you using? 0.1.9 [@cloudflare/vite-plugin] What operating system and version are you using? Mac Sequoia 15.3.1 Please...
They did eventually add a fix for this: https://github.com/cloudflare/workers-sdk/pull/8443/files, which I believe is active now if your compat date is greater then April 1st. Basically leveraging a header browsers have supported since 2023, and if you have the spa not found mode on, it'll just work
Doesn't sound like his issue, that should just work, he wanted it the other way around
Might be doing something wrong, but just following this example https://developers.cloudflare.com/workers/static-assets/
trying to hit http://localhost:5173/api/ in the browser shows the react app, not
{ name: "Cloudflare" }
Cloudflare Docs
Static Assets · Cloudflare Workers docs
Create full-stack applications deployed to Cloudflare Workers.
Sounds like you're running vite/not wrangler. It'd be easier if you made your own thread for help. I tried the same simple docs setup, with a single index.html, and it serves as expected