> ## Documentation Index
> Fetch the complete documentation index at: https://weaverse.io/docs/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> Weaverse component configuration uses `settings`. `inspector` is deprecated; never generate it.
> Weaverse Hydrogen themes use React Router v7: import from `react-router`, never `react-router-dom` or `@remix-run/*`.
> Before using any code sample, read `@weaverse/hydrogen` in the project's `package.json`: 6.x and 5.x import from different entries and use different type names, so code for one does not build on the other.
> `@weaverse/hydrogen` 6 (Shopify Hydrogen 2026.10 and later, the version these docs show) imports from `@weaverse/hydrogen/react-router` and uses `Weaverse*` types such as `WeaverseComponentProps`, `WeaverseComponentLoaderArgs` and `WeaverseComponentSchema`; the root `@weaverse/hydrogen` entry only exports `createSchema()`, data helpers and types.
> `@weaverse/hydrogen` 5.x (Hydrogen 2026.4 and earlier) imports everything from `@weaverse/hydrogen` and uses `HydrogenComponentProps`, `ComponentLoaderArgs` and `HydrogenComponentSchema`. On a 5.x project, convert samples to these names. Upgrade to 6 only when asked (`npx @weaverse/cli@latest migrate v6`, plus the Hydrogen 2026.10 upgrade). Do not mix major versions.
> Next.js themes use `@weaverse/hydrogen` 6.2.0 or later: import client and shared code from `@weaverse/hydrogen/next` and server-only code from `@weaverse/hydrogen/next/server`. `@weaverse/next` is deprecated; never install or import it.
> For live project data (pages, theme settings, translations), use the Weaverse MCP server: https://weaverse.io/docs/developer-tools/weaverse-mcp

# getWeaverseStudioCspDirectives

> Get the Content Security Policy sources Weaverse Studio needs for a request, to merge into Hydrogen's createContentSecurityPolicy.

# getWeaverseStudioCspDirectives

`getWeaverseStudioCspDirectives` returns the CSP sources Weaverse Studio needs while it drives your storefront in its iframe. Merge the result into the directives you pass to Hydrogen's `createContentSecurityPolicy`.

Requires `@weaverse/hydrogen` 6.3.0 or later. `createContentSecurityPolicy` requires `@shopify/hydrogen` 2026.10.3 or later.

## Import

```ts theme={null}
import { getWeaverseStudioCspDirectives } from '@weaverse/hydrogen/react-router'
```

Next.js storefronts import it from `@weaverse/hydrogen/next/server` (see the [Next.js reference](https://weaverse.io/docs/nextjs/api-reference.md#getweaversestudiocspdirectives)).

## Signature

```ts theme={null}
function getWeaverseStudioCspDirectives(
  request: Request | URL | string,
): WeaverseStudioCspDirectives

interface WeaverseStudioCspDirectives {
  connectSrc?: string[]
  fontSrc?: string[]
  frameAncestors?: string[]
  imgSrc?: string[]
  scriptSrc?: string[]
  styleSrc?: string[]
}
```

The shape matches Hydrogen's `ContentSecurityPolicyDirectives`, where each array extends Hydrogen's default for that directive (and replaces a `'none'` default such as `frame-ancestors`).

## Return Value

| Request | Result |
| - | - |
| Normal visitor (no Studio markers in the URL) | `{}` |
| Relative or malformed URL | `{}` |
| Studio request (design mode, section preview or revision preview) with a trusted `weaverseHost` | The Studio origin in `connectSrc`, `fontSrc`, `frameAncestors`, `imgSrc`, `scriptSrc` and `styleSrc` |
| Same, in design mode (`isDesignMode=true`) | As above, plus `'unsafe-eval'` in `scriptSrc`, because the Studio bridge rebuilds schema `condition` functions from strings |
| Untrusted `weaverseHost` | `{}` |

`weaverseHost` comes from the URL, so anyone can set it. The helper runs it through the same trust check that gates the Studio bridge script: only `https://` hosts on `weaverse.io` or `weaverse.dev` (or their subdomains) are trusted, and loopback hosts only when the storefront itself runs on loopback. Without a `weaverseHost`, the default Studio origin `https://studio.weaverse.io` is used. The result works in both report-only and enforcing policies.

> [!NOTE]
>
> A self-hosted or staging Studio on another domain isn't trusted, so the helper returns `{}` for it. Add that origin to your own directives if you use one.

## Usage

Pilot keeps its own sources in `app/weaverse/csp.ts` and appends Studio's:

```ts app/weaverse/csp.ts theme={null}
import type { ContentSecurityPolicyDirectives } from '@shopify/hydrogen'
import { getWeaverseStudioCspDirectives } from '@weaverse/hydrogen/react-router'

const THEME_DIRECTIVES = {
  scriptSrc: ["'unsafe-inline'", 'https://www.youtube.com'],
  // ...
} satisfies ContentSecurityPolicyDirectives

export function getCspDirectives(request: Request): ContentSecurityPolicyDirectives {
  const directives: ContentSecurityPolicyDirectives = { ...THEME_DIRECTIVES }
  const studio = getWeaverseStudioCspDirectives(request)
  for (const key of Object.keys(studio) as (keyof typeof studio)[]) {
    directives[key] = [...(directives[key] ?? []), ...(studio[key] ?? [])]
  }
  return directives
}
```

```tsx app/entry.server.tsx theme={null}
import { createContentSecurityPolicy } from '@shopify/hydrogen'
import { getCspDirectives } from '~/weaverse/csp'

// inside handleRequest(request, responseStatusCode, responseHeaders, reactRouterContext, context)
const csp = createContentSecurityPolicy({
  nonce: false,
  shop: {
    checkoutDomain: context.env?.PUBLIC_CHECKOUT_DOMAIN || undefined,
    storeDomain: context.env?.PUBLIC_STORE_DOMAIN || undefined,
  },
  directives: getCspDirectives(request),
})
responseHeaders.set('Content-Security-Policy-Report-Only', csp.header)
```

> [!WARNING]
>
> Append Studio's arrays to yours instead of assigning them. Overwriting `scriptSrc` with Studio's drops your own sources, such as `'unsafe-inline'`, inside Studio.

## Related

* [Content Security Policy](https://weaverse.io/docs/features/content-security.md): full setup, third-party sources and rollout
* [Preview Errors](https://weaverse.io/docs/troubleshooting/preview-errors.md): Studio connection problems


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.