simplest static asset hosting
So, I read through https://github.com/cloudflare/workers-sdk/issues/1162 and it was closed due to the
wrangler deploy --assets
flag existing; do we have any actual documentation about how that works? Can it be used to host assets for a worker or is it just for using a worker to only host static assets?
I'm curious about that answer regardless of whether this is actually the right solution (this is phrased an intentional XY problem), but backing up to give the context as well: I'm hosting discord bots on Workers and am trying to figure out the simplest way (to make a minimal template) to include more than 1MB of assets that they can access (the 25MB/file KV limit is fine here; would attach R2 for anything larger, I get that part). That I can tell, the methods that let you just have a directory in your repo whose contents are autodeployed are:
- Use @cloudflare/kv-asset-handler
as per https://developers.cloudflare.com/workers/configuration/sites/start-from-worker/. Works, boilerplate-y.
- Actually deploy a pages site with one function instead of a worker; have the worker fetch
a relative path. Works, feels kind of silly?
- Deploy your folder of static assets as a separate static pages site and fetch that site from your worker, hardcoding the .pages.dev domain (I actually did this one because I was lazy and I knew it would work).
- Something else? Is that what the --assets
flag does? What's this I hear about an ASSETS binding; is that only for pages?13 Replies
I'm not quite sure I understand, why do you need a Worker at all?
If you can deploy it as a Function, then you shouldn't need the Worker
Well, the application is just a Discord bot. Deploying it as a Function would be my second bullet point option, yeah? Using the Pages stack despite not having a site to actually deploy?
(repo for context: https://github.com/marcustyphoon/discord-bot-cloudflare-slash)
I'm not quite sure I see where the static assets come in though?
I don't have the version of it with assets public yet; got to strip some secrets and stuff
But to have a bot command respond with an image, I'm currently just fetching
my-static-files.pages.dev/some-image.png
(and made a separate repo that's just a folder of images and uploaded that as a pages site)Oh ok. Then maybe just go with R2?
Is there a nice way to have a folder in the repo synced to R2 (e.g. from wrangler cli)?
I should note too that while you can do as you are doing it now, using Pages as a static file locker, rather than a website hosting platform, is technically against the ToS
rclone sync
Make source and dest identical, modifying destination only.
Yeah, that was why I wanted to figure out the semantic way to do it :D
@cloudflare/kv-asset-handler
definitely works, but it seemed like it was intended to be deprecated?Funnily enough, they are trying to wind it down in favor of Pages
Right, yeah, the linked issue seemed to be well-targeted toward finding a solution to just this sort of thing with good devx, and then it wasn't clear to be if the resolution it was closed with actually does that
Hm, the setup process for this doesn't seem very suited to "here's a template repo that you can clone, put assets in a folder, and run a github action to sync"
Will look through that. But I meant more, backing up, that the user would have to be instructed to make an R2 binding and add it to one or multiple config files as part of using the template to make a bot, yeah? While the bucket = "./public" method is zero config, as one would reasonably expect
Which is why I love this platform, of course; KV is great; KV being wrapped so it acts like a static asset folder is great! Using the Pages stack for something it's technically not intended for feels silly, but if there's a similar wrapper for KV that's just a part of the developer platform under
assets
I'd love to use that; I just don't see any documentation about it.