mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
Studio's TanStack build serves every client JS/CSS chunk from Vercel at
`/dashboard/_vercel/immutable/...`, and each of those requests also goes
through the www zone's routing (supabase.com/dashboard is proxied by
www). The Next build served the same files from the R2 asset CDN. This
moves the TanStack build's client assets back to the asset CDN on
production builds, using native Vite / TanStack Start hooks rather than
an `assetPrefix`-style rewrite.
**Added:**
- `apps/studio/asset-cdn.ts`: one gate shared with `next.config.ts`
(production or `FORCE_ASSET_CDN=1`, `FORCE_ASSET_CDN=-1` disables,
staging CDN when `NEXT_PUBLIC_ENVIRONMENT=staging`) plus URL helpers.
Only files under `_vercel/immutable/` are rewritten, and web workers
never are
- `upload-static-assets.sh tanstack`: syncs
`.vercel/output/static/_vercel/immutable` to
`<SITE_NAME>/tanstack/_vercel/immutable` on R2 with `max-age=31536000,
immutable`. Uses `--size-only` (names are content hashes, so only new
files upload) and excludes source maps. Local files are kept
- The upload script now fails with a clear message when the asset CDN is
enabled but `ASSET_CDN_S3_ENDPOINT` or the AWS keys are missing, instead
of the AWS CLI failing on an empty `--endpoint-url`
- Unit tests for the gate and URL helpers
**Changed:**
- `server.ts` uses `createStartHandler({ transformAssets })` so the
shell's scripts, modulepreloads and stylesheets point at the CDN with
`crossorigin="anonymous"` (module scripts need CORS; it also keeps full
error details in Sentry)
- `vite.config.ts` sets `experimental.renderBuiltUrl` (client only) so
URLs inside the bundle follow: the preload helper's dependency map, and
fonts/images referenced from CSS. The CDN base is computed once from the
process env (the same env the upload script sees) and inlined into both
server and client so they can't disagree
- `build:tanstack` runs the upload after `vite build` unless
`SKIP_ASSET_UPLOAD=1`, the same as `build:next`. The Docker image's
TanStack build sets `SKIP_ASSET_UPLOAD=1` (images serve their own
assets, and the pruned context has no upload script)
**Unchanged on purpose:**
- Vite `base` and the router basepath stay `/dashboard`; documents,
`/_serverFn` and `/api` stay on Vercel
- Web workers stay same-origin (`worker-src 'self' blob: data:`, and
workers must be same-origin anyway), so `immutableStaticFiles` stays on
- With the CDN off (previews, self-hosted), the build output is
identical to master apart from release hashes
The CDN folder is shared and content-addressed rather than per-commit,
so unchanged chunks dedupe across deploys and older tabs keep finding
their lazy chunks whichever deployment serves the next document.
**Rollout:** production has `FORCE_ASSET_CDN=-1` for now, so merging
this doesn't change prod. Staging picks it up on merge and gets tested
first.
## To test
### On the preview (tests the upload, not the page)
The staging preview for this branch builds with `FORCE_ASSET_CDN=1`, so
it exercises the real build-and-upload path. **The preview page itself
is expected not to load in a browser:** the asset CDN only allows the
staging and production origins in its CORS rules, so module scripts are
blocked on a `*.vercel.app` URL.
- In the build log, check `Uploading immutable static files to R2...` is
followed by `Upload completed successfully!`
- View the preview's page source: the `<script>`, `modulepreload` and
stylesheet tags point at the staging asset CDN
(`.../<SITE_NAME>/tanstack/_vercel/immutable/...`) with
`crossorigin="anonymous"`
- Open a few of those asset URLs directly in a new tab (a JS chunk, a
CSS file, and a `.woff2` font from the CSS): each returns 200 with the
right content type
- Worker URLs inside the chunks still point at
`/dashboard/_vercel/immutable/.../*.worker-*.js` on the page's own
origin
### After merge, on the full staging environment
- Load the staging dashboard with DevTools open: every script,
modulepreload and stylesheet comes from the staging asset CDN with a
200, and there are no CORS or CSP errors in the console
- Fonts render (Inter, and the code fonts in the SQL editor)
- Sign in and click around: Table Editor, SQL Editor, GraphiQL, Logs.
Lazy routes load, and nothing comes from `/dashboard/_vercel/immutable`
except Monaco's workers
- SQL Editor and GraphiQL: Monaco works (syntax highlighting,
autocomplete), and its workers load same-origin
- Trigger an error and check the Sentry event has a readable stack trace
- After the next staging deploy, check the build log's upload only added
new files
---------
Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>