Skip to content

Fix: route API calls to the deployment's own worker (sub-path apps)#2

Merged
tim-webflow merged 3 commits into
tim-webflow:update-for-root-appsfrom
jallegretti-webflow:update-for-root-apps
May 26, 2026
Merged

Fix: route API calls to the deployment's own worker (sub-path apps)#2
tim-webflow merged 3 commits into
tim-webflow:update-for-root-appsfrom
jallegretti-webflow:update-for-root-apps

Conversation

@jallegretti-webflow
Copy link
Copy Markdown

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 / since astro.config.mjs's base isn't set per environment. So on a page served from the /app worker, the client-side fetch(new URL('api/...', origin + BASE_URL)) resolved to https://site.com/api/... and was routed by the edge proxy to the / worker (which has its own R2 binding).

Setting base in astro.config.mjs doesn'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 of BASE_URL.
  • index.astro — passes mountPath to <FileUploader> as a prop.
  • files.astro — exposes the value via data-mount-path so the inline script can read it on the client.
  • FileUploader.tsx — accepts mountPath, computes apiBase once (with a typeof window guard 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.
  • /app deployment: apiBase = https://site.com/app/ → hits the /app worker + its R2 bucket.

Test plan

  • Deploy at /, upload a file, confirm it appears in the / deployment's cloud-files bucket.
  • Deploy at /app, upload a file, confirm it appears in the /app deployment's bucket (different bucket ID).
  • Verify the two deployments do not see each other's files.
  • Page renders correctly (no blank body / SSR errors).

jallegretti-webflow and others added 3 commits May 26, 2026 12:09
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
Copy link
Copy Markdown
Owner

@tim-webflow tim-webflow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, lgtm

@tim-webflow tim-webflow merged commit 194c9c7 into tim-webflow:update-for-root-apps May 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants