Fix: route API calls to the deployment's own worker (sub-path apps)#2
Merged
tim-webflow merged 3 commits intoMay 26, 2026
Conversation
Reads the deployment's mount path from the runtime env (injected by the Cosmic builder) and threads it through to the client so fetch() calls resolve to the same worker that served the page. Previously `/app` pages were hitting `/`'s worker (and its R2 bucket) because `import.meta.env.BASE_URL` is baked in at build time and always defaulted to `/`. - Add `src/utils/mountPath.ts` helper that normalizes the value to start and end with `/`. - `Layout.astro`: use it for the favicon `<link>`. - `index.astro`: pass `mountPath` as a prop to `<FileUploader>`. - `files.astro`: expose via `data-mount-path` and read in the script. - `FileUploader.tsx`: accept `mountPath` prop, compute `apiBase` with a `typeof window` guard so SSR doesn't crash, and use it for all four fetch calls. Co-authored-by: Cursor <cursoragent@cursor.com>
…ew-env Route API calls to per-deployment worker via COSMIC_MOUNT_PATH
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.
Problem
When this template is deployed twice on the same site — once at
/and again at a sub-path like/app— both deployments end up reading and writing to the same R2 bucket (the/deployment's).The root cause is
import.meta.env.BASE_URL: it's baked in at build time and defaults to/sinceastro.config.mjs'sbaseisn't set per environment. So on a page served from the/appworker, the client-sidefetch(new URL('api/...', origin + BASE_URL))resolved tohttps://site.com/api/...and was routed by the edge proxy to the/worker (which has its own R2 binding).Setting
baseinastro.config.mjsdoesn't help because Webflow Cloud's build system overwrites that config.Fix
Read the mount path from
COSMIC_MOUNT_PATH(injected as a worker env var by the Cosmic builder) at request time and thread it from the Astro page into the React island, so fetches always resolve to the same worker that served the page.src/utils/mountPath.ts— small helper that normalizes the env value into a base path that starts and ends with/.Layout.astro— favicon<link>now uses the mount path instead ofBASE_URL.index.astro— passesmountPathto<FileUploader>as a prop.files.astro— exposes the value viadata-mount-pathso the inline script can read it on the client.FileUploader.tsx— acceptsmountPath, computesapiBaseonce (with atypeof windowguard so SSR doesn't crash), and uses it for all four API calls.Result
/deployment:apiBase = https://site.com/→ hits the/worker + its R2 bucket./appdeployment:apiBase = https://site.com/app/→ hits the/appworker + its R2 bucket.Test plan
/, upload a file, confirm it appears in the/deployment'scloud-filesbucket./app, upload a file, confirm it appears in the/appdeployment's bucket (different bucket ID).