Accessing files in a worker
Let's say I have files like this in a worker, the
data.json
file may not always be a JSON file (nothing insane, just some js files or small scripts). I couldn't find any information on how could I access it from the main entrypoint. How could I do this without making a wasteful API request like this: https://developers.cloudflare.com/api/operations/worker-script-download-workerCloudflare API Documentation
Interact with Cloudflare's products and services via the Cloudflare API
7 Replies
You can import it as a module e.g
import json from './data.json'
This will be uploaded with the workerThis works when wrangler is doing the bundling, but not in quick edit which is what is seemingly used there:
I know you can import files in quickedit since one of the templates does it with a html file and a js file, but not 100% sure why it hates json specifically
Oh that's fun
You can though, do what the playground template does and just have
data.js
with But let's say it's not even a JS file, but a small database template for example, so there's no way to access it and I basically have to cram it in a JS file?
Only if you use QuickEdit. wrangler can be massaged into accepting any file format you throw at it
Thanks