mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +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>
104 lines
4.2 KiB
Docker
104 lines
4.2 KiB
Docker
# To be run in the root of the turbo monorepo
|
|
# NOTE: It's highly recommended to use the new builder, Buildkit. https://docs.docker.com/build/buildkit/
|
|
## USAGE:
|
|
# Build (Next): docker build . -f apps/studio/Dockerfile --target production -t studio:latest
|
|
# Build (TanStack): docker build . -f apps/studio/Dockerfile --target production -t studio:latest --build-arg STUDIO_FRAMEWORK=tanstack
|
|
# Run: docker run -p 3000:3000 supabase/studio
|
|
# Deploy: docker push supabase/studio:latest
|
|
# Clean build:
|
|
# docker builder prune
|
|
# docker build . -f apps/studio/Dockerfile --target production -t studio:latest --no-cache
|
|
|
|
# Which framework's build ends up in the image. This is the same variable
|
|
# scripts/dispatch.js keys on for the dev/build/start scripts, so
|
|
# `--build-arg STUDIO_FRAMEWORK=tanstack` is the docker spelling of the
|
|
# switch used everywhere else. Framework selection happens at image build
|
|
# time (the two runtimes need different build outputs and dependency
|
|
# trees), not at container start.
|
|
ARG STUDIO_FRAMEWORK=next
|
|
|
|
FROM node:22-slim AS base
|
|
ENV PNPM_HOME="/pnpm"
|
|
ENV PATH="$PNPM_HOME:$PATH"
|
|
|
|
# Fixes issues with Sentry CLI and SSL certificates during build
|
|
# TODO: Git is added because it's needed to build libpg, remove it once they publish a binary on the S3 bucket
|
|
RUN apt-get update -qq && \
|
|
apt-get install -y --no-install-recommends \
|
|
git \
|
|
python3 \
|
|
ca-certificates \
|
|
build-essential && \
|
|
rm -rf /var/lib/apt/lists/* && \
|
|
update-ca-certificates
|
|
|
|
RUN npm install -g pnpm@11.13.1
|
|
|
|
WORKDIR /app
|
|
|
|
# Prune unneeded dependencies with turbo (from apps/ for example)
|
|
FROM base AS turbo
|
|
COPY . .
|
|
|
|
RUN pnpm dlx turbo@2.9.14 prune studio --docker
|
|
|
|
# Install dev dependencies (only if needed)
|
|
FROM base AS deps
|
|
COPY --from=turbo /app/out/json ./
|
|
COPY --from=turbo /app/out/pnpm-lock.yaml ./
|
|
COPY ./patches/ ./patches
|
|
|
|
# No need to clean cache because production uses standalone build
|
|
RUN pnpm install --frozen-lockfile
|
|
|
|
# dev contains dependencies and source code not compiled
|
|
FROM deps AS dev
|
|
COPY --from=turbo /app/out/full ./
|
|
ENTRYPOINT ["docker-entrypoint.sh"]
|
|
EXPOSE 8082
|
|
CMD ["pnpm", "dev:studio"]
|
|
|
|
# Compile Next.js
|
|
FROM dev AS build-next
|
|
RUN pnpm --filter studio exec next build
|
|
|
|
# Assemble the runtime tree at /srv in the layout the production stage
|
|
# serves from: Next's self-contained standalone output is the app root,
|
|
# with the static assets and public/ laid alongside it.
|
|
RUN mkdir -p /srv && \
|
|
cp -a apps/studio/.next/standalone/. /srv/ && \
|
|
mkdir -p /srv/apps/studio/.next && \
|
|
cp -a apps/studio/.next/static /srv/apps/studio/.next/static && \
|
|
cp -a apps/studio/public /srv/apps/studio/public
|
|
|
|
# Compile TanStack Start (Vite + Nitro)
|
|
FROM dev AS build-tanstack
|
|
# Images serve their own assets, and the pruned context has no upload script.
|
|
RUN SKIP_ASSET_UPLOAD=1 NODE_OPTIONS=--max-old-space-size=4096 pnpm --filter studio run build:tanstack
|
|
|
|
# Assemble the runtime tree at /srv. Nitro's node-server output is
|
|
# self-contained (`.output/server` bundles the app with its traced
|
|
# dependencies, `.output/public` holds the client assets), so no node_modules
|
|
# install is needed. The server.js shim loads `.env` (container env vars win)
|
|
# and gives the production stage one CMD for both frameworks; package.json is
|
|
# copied so the shim runs as ESM.
|
|
RUN mkdir -p /srv/apps/studio && \
|
|
cp -a apps/studio/.output /srv/apps/studio/.output && \
|
|
cp apps/studio/package.json apps/studio/.env /srv/apps/studio/ && \
|
|
printf "process.loadEnvFile(new URL('.env', import.meta.url))\nawait import('./.output/server/index.mjs')\n" > /srv/apps/studio/server.js
|
|
|
|
# Alias whichever framework build was selected so the production stage can
|
|
# COPY from a single stage name. BuildKit only builds the selected branch.
|
|
FROM build-${STUDIO_FRAMEWORK} AS build
|
|
|
|
# Copy only compiled code and dependencies
|
|
FROM base AS production
|
|
COPY --from=build /srv ./
|
|
# Both servers read PORT (the TanStack `start` script defaults it to 8082);
|
|
# pin it to the port the healthcheck and compose files expect.
|
|
ENV PORT=3000
|
|
EXPOSE 3000
|
|
ENTRYPOINT ["docker-entrypoint.sh"]
|
|
HEALTHCHECK --interval=5s --timeout=5s --retries=3 CMD node -e "fetch('http://localhost:3000/api/platform/profile').then((r) => {if (r.status !== 200) throw new Error(r.status)})"
|
|
CMD ["node", "apps/studio/server.js"]
|