Skip to content

fix(nginx): serve /studio-modules/ with CORS, no SPA fallback - #8420

Merged
tlgimenes merged 1 commit into
mainfrom
fix/nginx-studio-modules-cors
Nov 10, 2026
Merged

tlgimenes merged 1 commit into
mainfrom
fix/nginx-studio-modules-cors

Conversation

@tlgimenes

@tlgimenes tlgimenes commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Not merged — waiting for review.

Summary

A Blocks v8 site page (for example osklen, deco-sites/osklenbr-faststore#1388) loads Studio's editor modules (/studio-modules/v1/wireframe.js, html-swap.js) with a cross-origin import() after Studio sends deco:load. #8379 added Access-Control-Allow-Origin in the Bun API (withStudioModulesCors), but in the deployed image nginx serves the static files, so the request never reaches Bun. /studio-modules/ falls into location / and gets no ACAO header, gets X-Frame-Options: DENY and the frame CSP, and gets the SPA's index.html (200) for a file that does not exist. Today, in production and staging, the browser blocks the import:

Access to script at 'https://studio.decocms.com/studio-modules/v1/wireframe.js?build=spike' from origin 'http://127.0.0.1:18090' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

This PR adds location ^~ /studio-modules/ to deploy/helm/studio/files/api-nginx.conf:

  • Access-Control-Allow-Origin: * (public static code, fetched without credentials)
  • Cache-Control: no-cache, with no validators: etag off, if_modified_since off, and Last-Modified cleared. The filenames are not content-hashed, and pinned build mtimes make ETag and Last-Modified the same across deploys, so a validator could 304 against a stale copy.
  • X-Content-Type-Options: nosniff. No frame headers, because they only apply to documents.
  • A missing module returns a 404 with no-store and ACAO, never the SPA fallback.

It also adds a note to deploy/nginx/README.md.

Decisions taken

  • Caching is no-cache (decided by the user), the same as the dev server. Revalidating on every import is cheap for two small files, and it means the editor always runs the module of the pod it is talking to.
  • ACAO is * rather than an origin allow-list. The files are public, uncredentialed scripts, and site origins are open-ended (custom domains, preview hosts).
  • Only nginx config changes. The Bun-side handler from feat(cms): v8 CMS on R2 with content-addressed releases #8379 stays as is for dev and non-nginx setups.

Before production can use this

  • Cloudflare overrides the cache header. Production and staging currently answer /studio-modules/v1/wireframe.js with cache-control: max-age=14400 (server: cloudflare, cf-cache-status: MISS). That is a Cloudflare edge or browser-TTL rule, not this nginx config. With it in place, browsers can keep a module for 4h after a deploy. Whoever owns the Cloudflare config for studio(-stg).decocms.com should know this. A cache rule for /studio-modules/* that respects origin headers is the likely fix. This PR does not touch Cloudflare.
  • Prod needs a Studio release and an image tag bump after merge. The config is copied into the nginx image (apps/web/Dockerfile), so it takes effect only after a release and a tag bump in deploy.

How to spike it

The local nginx container uses the image from apps/web/Dockerfile, nginxinc/nginx-unprivileged:1.27-alpine, with this branch's api-nginx.conf as the template and the built studio-modules/v1/*.js plus a stub SPA in /usr/share/nginx/html:

docker run --rm -d --name smcors -p 18086:8080 \
  -e STUDIO_NGINX_API_MAX_CONNS=64 \
  -v $PWD/deploy/helm/studio/files/api-nginx.conf:/etc/nginx/templates/default.conf.template:ro \
  -v $PWD/html:/usr/share/nginx/html:ro \
  nginxinc/nginx-unprivileged:1.27-alpine
curl -sI 'http://localhost:18086/studio-modules/v1/wireframe.js?build=x'
curl -sI 'http://localhost:18086/studio-modules/v1/nope.js'

Then run a Playwright page on another origin (http://127.0.0.1:18090) that calls import() on the module URLs.

Evidence

I spiked head d689dbd with the nginxinc/nginx-unprivileged:1.27-alpine image, the built wireframe.js and html-swap.js (pinned 1985 mtimes), and a stub SPA. The container started, so the config loads.

Request Result
/studio-modules/v1/wireframe.js?build=x 200, application/javascript, ACAO: *, Cache-Control: no-cache, nosniff. No ETag, no Last-Modified, no X-Frame-Options, no CSP.
/studio-modules/v1/html-swap.js Same headers as wireframe.js.
/studio-modules/v1/nope.js 404, ACAO: *, Cache-Control: no-store. Not the SPA's index.html.
/studio-modules/v1/wireframe.js with If-Modified-Since: <file mtime> 200, not 304.
/assets/index-abc123.js No change: immutable, ETag kept.
/some/route (SPA) No change: no-cache, X-Frame-Options: DENY, frame-ancestors 'none'.

I also ran a cross-origin import() with Playwright Chromium from a page on http://127.0.0.1:18090:

  • The local patched nginx imports wireframe.js and html-swap.js successfully (ok: true, 1 export each).
  • A missing module fails as expected (404).
  • For comparison, prod and stg still fail today, with blocked by CORS policy: No 'Access-Control-Allow-Origin' header.

Known limitations

  • The Cloudflare max-age=14400 override (above) still applies until someone changes the edge rule.
  • I spiked against a local container, not a deployed preview. I did not find a CI job on this PR that publishes a studio-nginx preview image.

🤖 Generated with Claude Code


Summary by cubic

Fixes cross-origin loading of Studio's editor modules from Blocks v8 site pages. Previously nginx served /studio-modules/ through the default location, which lacked an Access-Control-Allow-Origin header and fell back to the SPA's index.html for missing files, so browsers blocked the modules' import(). The new location ^~ /studio-modules/ answers with Access-Control-Allow-Origin: *, no-cache (with ETag, Last-Modified, and If-Modified-Since disabled, since filenames are not content-hashed and build mtimes are pinned), and nosniff, and returns a no-store 404 for missing modules instead of the SPA fallback.

Deployment notes

  • Cloudflare still overrides the cache header for /studio-modules/* (browsers can cache a module for 4h); a Cloudflare cache rule respecting origin headers is needed for correct post-deploy behavior.
  • Takes effect only after a Studio release and an image tag bump, since the config is baked into the nginx image.

Written for commit d689dbd. Summary will update on new commits.

View guided diff Turn on auto-fix

The v8 editor modules (wireframe.js, html-swap.js) are imported by the
site page from Studio cross-origin. #8379 added the CORS header in the Bun
API, but in production nginx serves the static files, so /studio-modules/
fell into "location /": no Access-Control-Allow-Origin, frame headers, and
index.html for a missing file. Give it its own location: ACAO *, no-cache
without validators (ETag off, Last-Modified cleared, If-Modified-Since
ignored -- filenames are not hashed and build mtimes are pinned), nosniff,
and a no-store 404 on a miss.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the claude PR authored by a coding agent label Nov 10, 2026
@tlgimenes
tlgimenes marked this pull request as ready for review November 10, 2026 14:40
@tlgimenes
tlgimenes merged commit 978cb42 into main Nov 10, 2026
25 checks passed
@tlgimenes
tlgimenes deleted the fix/nginx-studio-modules-cors branch November 10, 2026 14:40
decocms Bot pushed a commit that referenced this pull request Nov 10, 2026
PR: #8420 fix(nginx): serve /studio-modules/ with CORS, no SPA fallback
Bump type: patch

- decocms (apps/api/package.json): 4.560.4 -> 4.560.5

Deploy-Scope: both
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

claude PR authored by a coding agent

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant