Skip to main content
A Content Security Policy (CSP) tells the browser which scripts, styles, frames, and connections your storefront may load. In a Weaverse theme the policy has to allow your own code, Shopify, Weaverse Studio, and every third-party service your sections use.

Requirements

Pilot uses this setup. Two files are involved:

How createContentSecurityPolicy Works

createContentSecurityPolicy({ shop, directives, nonce }) from @shopify/hydrogen returns { header, nonce, applyResponseHeaders }:
  • It starts from Shopify’s defaults: default-src 'self', script-src 'self' https://cdn.shopify.com, style-src 'self' 'unsafe-inline' https://cdn.shopify.com, frame-ancestors 'none', object-src 'none', plus image, font, media, frame and connect-src sources for Shopify and the shop domains.
  • Each array you pass in directives extends the matching default. A default that is only 'none' (such as frame-ancestors) is replaced instead.
  • By default it generates a nonce and adds it to script-src. nonce: false builds a nonce-free policy.
  • header is the serialized policy. applyResponseHeaders(headers) sets the enforcing Content-Security-Policy header (and, when the policy has a nonce, private no-cache headers).

Why Pilot Uses nonce: false

A per-response nonce makes every HTML response unique, so it can’t be stored by Oxygen’s full-page cache. Pilot caches anonymous documents, so it builds the policy without a nonce. React Router’s streamed hydration scripts differ per response and can’t be hashed, so Pilot allows inline scripts with 'unsafe-inline' in scriptSrc instead.
Hydrogen 2026.10 has no NonceProvider or useNonce. A theme that wants a nonce-based policy leaves nonce unset, passes csp.nonce to renderToReadableStream and ServerRouter, provides it to its own components through its own React context, and gives up full-page caching for those responses.

Wire It Into entry.server.tsx

app/entry.server.tsx
Hydrogen 2026.10 has no HydrogenRouterContextProvider: the context is React Router’s RouterContextProvider, which server.ts fills with your app context. context.env is typed once env.d.ts declares interface RouterContextProvider extends AppContext {} (see Step 2 of the integration guide). entry.client.tsx needs nothing CSP-specific: <HydratedRouter /> hydrates as usual, and root.tsx renders <ScrollRestoration /> and <Scripts /> without a nonce prop.

Sources Weaverse Studio Requires

getWeaverseStudioCspDirectives(request) from @weaverse/hydrogen/react-router returns the sources Studio needs for the request being rendered:
  • Normal visitors: {}. Outside Studio the browser never contacts Weaverse, so your policy needs nothing extra.
  • Inside Studio (design mode, section preview or revision preview): the Studio origin in frameAncestors, scriptSrc, connectSrc, styleSrc, fontSrc and imgSrc. Design mode also adds 'unsafe-eval' to scriptSrc, because the Studio bridge rebuilds schema condition functions from strings.
The Studio origin comes from the weaverseHost query parameter, which anyone can put in a URL. The helper only accepts https:// hosts on weaverse.io or weaverse.dev (and loopback hosts when the storefront itself runs on localhost); any other value returns {}, so a crafted ?weaverseHost= can’t widen your policy. Pilot merges these directives into its own:
app/weaverse/csp.ts
Append Studio’s sources to your arrays; don’t assign them over your own. Replacing scriptSrc with Studio’s would drop 'unsafe-inline' and your third-party script origins inside Studio.
A self-hosted or staging Studio on another domain isn’t a trusted host, so the helper returns {} for it. Add that origin to your own directives if you use one.

Common Third-Party Sources

These are the sources in Pilot’s app/weaverse/csp.ts: cdn.shopify.com is already in Hydrogen’s defaults. Hydrogen also sets script-src, img-src, media-src, frame-src and font-src itself, so default-src doesn’t cover them: add each origin to the directive the browser names in the violation.

Rolling Out Enforcement

Pilot sends Content-Security-Policy-Report-Only: the browser logs violations but blocks nothing. Switch to the enforcing header only after a clean pass.
1

Run report-only

Keep Content-Security-Policy-Report-Only and deploy to a preview environment.
2

Test every surface

With the browser console open, check:
  • Home page
  • A product page, including one with a 3D model
  • A page with a YouTube section, and play the video
  • Cart drawer, variant change, and the mobile menu (these confirm hydration works)
  • GTM loads (window.dataLayer exists and the GTM request succeeds)
  • Studio design mode connects, and edits update the preview
  • Revision preview and section preview in Studio (both load preview.js)
3

Fix each violation

Look for messages like Refused to load the script '<url>' because it violates the following Content Security Policy directive: "script-src ..." (prefixed with [Report Only] in report-only mode). Add the blocked origin to the directive the message names, in app/weaverse/csp.ts, and test again.
4

Enforce

In entry.server.tsx, replace the report-only header with:
Repeat the checklist on the enforced build.

Harmless Console Noise

Not every red line is a CSP problem in your policy:
  • CORS errors for googleads.g.doubleclick.net come from inside the YouTube iframe. That frame runs under YouTube’s own policy, not yours, and playback still works.
  • Blocked source-map (.map) fetches under connect-src only happen while DevTools is open. Shoppers never trigger them, so you don’t need to allow them.

Troubleshooting

  • Studio shows a blank preview or never connects: check that getWeaverseStudioCspDirectives(request) is merged into the directives you pass to createContentSecurityPolicy, so frame-ancestors and script-src list the Studio origin. See Preview Errors.
  • Studio connects but conditional settings misbehave: 'unsafe-eval' is missing from script-src in design mode. It comes from the Studio helper; make sure your merge appends instead of overwriting.
  • The page renders but nothing is interactive: React Router’s inline hydration scripts are blocked. With nonce: false, scriptSrc needs 'unsafe-inline'.
  • TypeError: Invalid CSP directive or cannot combine 'none': createContentSecurityPolicy validates directive names (camelCase, such as scriptSrc) and sources. Don’t pass nonces yourself, and don’t add sources next to 'none'.

Further Reading

Last modified on November 26, 2026