-
Notifications
You must be signed in to change notification settings - Fork 16
fix(uploads): store images in R2 so the Worker can boot - #4
Open
pallaoro wants to merge 1 commit into
Open
Conversation
src/server/uploads.ts wrote to the local filesystem via node:fs and resolved its uploads directory with fileURLToPath(import.meta.url) at module scope. Neither works on Workers: import.meta.url is undefined there, so the call threw while the module was still being imported and the Worker never started. Every route was dead, not just uploads. Swap the module for the R2 implementation the other templates already use, bind the bucket once in middleware, and declare the binding in wrangler.toml. Verified against a local `wrangler dev`: the Worker boots, GET /api/templates returns the seeded rows, design create/get/list/delete round-trip, and an uploaded PNG comes back byte-identical from GET /api/uploads/:filename.
This was referenced Sep 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
src/server/uploads.tsstored uploads on the local filesystem withnode:fs, and resolved its uploads directory at module scope:On Workers
import.meta.urlis undefined, sofileURLToPaththrows while the module is still being imported. The failure happens before any handler is registered, so the Worker never starts and every route is dead, not just uploads.Reproduced on a clean checkout with
wrangler dev:The fix
Use the R2-backed
uploads.tsthat the other templates in this family already ship (byte-identical to the one in OpenCMS and OpenFrame), bind the bucket once in middleware ahead of the routes, and declare the binding inwrangler.toml.src/server/uploads.tsnow talks toR2Bucketinstead offssrc/server/index.tscallsinitUploads(c.env.UPLOADS)in anapp.use("*")registered before the routes, so it runs firstwrangler.tomlgains theUPLOADSR2 bindingVerification
Against a local
wrangler dev:Ready on http://localhost:8787GET /api/templatesPOST /api/uploadsthenGET /api/uploads/:filenameimage/png, bytes identical to the file sentvite buildsucceeds.tsc --noEmitis unchanged from before this branch (the repo has never carried Cloudflare worker types, soD1DatabaseandR2Bucketdo not resolve; adding@cloudflare/workers-typesglobally overrides the DOMfetchtypings and breaks the client, so that is left for a separate change).