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 andconnect-srcsources for Shopify and theshopdomains. - Each array you pass in
directivesextends the matching default. A default that is only'none'(such asframe-ancestors) is replaced instead. - By default it generates a nonce and adds it to
script-src.nonce: falsebuilds a nonce-free policy. headeris the serialized policy.applyResponseHeaders(headers)sets the enforcingContent-Security-Policyheader (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
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,fontSrcandimgSrc. Design mode also adds'unsafe-eval'toscriptSrc, because the Studio bridge rebuilds schemaconditionfunctions from strings.
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
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’sapp/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 sendsContent-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.dataLayerexists 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 Repeat the checklist on the enforced build.
entry.server.tsx, replace the report-only header with:Harmless Console Noise
Not every red line is a CSP problem in your policy:- CORS errors for
googleads.g.doubleclick.netcome from inside the YouTube iframe. That frame runs under YouTube’s own policy, not yours, and playback still works. - Blocked source-map (
.map) fetches underconnect-srconly 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 tocreateContentSecurityPolicy, soframe-ancestorsandscript-srclist the Studio origin. See Preview Errors. - Studio connects but conditional settings misbehave:
'unsafe-eval'is missing fromscript-srcin 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,scriptSrcneeds'unsafe-inline'. TypeError: Invalid CSP directiveorcannot combine 'none':createContentSecurityPolicyvalidates directive names (camelCase, such asscriptSrc) and sources. Don’t pass nonces yourself, and don’t add sources next to'none'.