> ## 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

# Connect Weaverse Studio

> Connect a Next.js storefront to Weaverse Studio: the bridge script, page binding, route identity, per-item revalidation, theme routes and troubleshooting.

Studio edits a Next.js storefront through two pieces that work together:

1. **A root-level script connector** that loads the Next Studio bridge when the storefront runs inside Studio.
2. **A page-level runtime binding** that connects the rendered page to Studio's editor.

On top of those, a few app-owned pieces make editing complete: the route identity each page reports, a revalidation route for resource pickers, and the theme's own `routes` so Studio opens the right URLs.

## Mount the Studio connector once

Mount `WeaverseNextStudioConnect` in the root layout. It loads the bridge script in design, preview and revision contexts and does nothing in published mode. It is a Client Component that you can render straight from the layout, a Server Component:

```tsx theme={null}
// app/layout.tsx
import { WeaverseNextStudioConnect } from '@weaverse/hydrogen/next'

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        <WeaverseNextStudioConnect />
        {children}
      </body>
    </html>
  )
}
```

Mount it in the root layout, not per page. The layout also renders around routes that never render a Weaverse page, such as `not-found.tsx` and `error.tsx`, and Studio needs the bridge before a page tree exists. It is SSR-safe: the component only touches `document` inside an effect.

## Page binding comes with the renderer

`WeaverseNextRenderer` creates and binds the page runtime; you don't wire it yourself. It maps Studio's internals onto the App Router:

| Studio action | Next.js behavior |
| - | - |
| Navigate | `router.push()` / `router.replace()` |
| Revalidate the page | `router.refresh()` (fallback) |
| Revalidate one item | A request to your [revalidation route](#per-item-revalidation) |

Both the connector and the renderer are required for full editing. See [Rendering Pages](https://weaverse.io/docs/nextjs/rendering-pages.md) for the client boundary that renders `WeaverseNextRenderer`.

## Report the route identity

Studio attaches to the route the page says it is. The server client's request context carries that identity, and it travels to the browser in `configs.requestInfo`:

| Field | Why Studio needs it |
| - | - |
| `pathname` | The page's path. `CUSTOM` pages are resolved by pathname, and without one the SDK falls back to `/`. |
| `pageType`, `handle` | Which page Studio is editing, and which item to revalidate. |
| `i18n` | The bridge reads `i18n.language`; a missing `i18n` breaks the bridge instead of degrading it. |
| `url` | The full request URL. Build it from the forwarded host and protocol; a bare pathname resolves against `http://localhost`, which never matches a deployed preview. |

```ts theme={null}
// app/weaverse/request-context.ts
import type { PageType, WeaverseNextRequestContext } from '@weaverse/hydrogen/next'

export function buildRequestContext({
  headers,
  pathname,
  searchParams,
  page,
}: {
  headers: Headers
  pathname: string
  searchParams: URLSearchParams
  page?: { type: PageType; handle?: string }
}): WeaverseNextRequestContext {
  let host = headers.get('x-forwarded-host') ?? headers.get('host')
  let proto = headers.get('x-forwarded-proto') ?? 'https'
  let search = searchParams.size > 0 ? `?${searchParams}` : ''

  return {
    headers,
    pathname,
    searchParams,
    url: `${proto}://${host}${pathname}${search}`,
    i18n: { country: 'US', language: 'EN', locale: 'en-US', pathPrefix: '' },
    pageType: page?.type,
    handle: page?.handle,
  }
}
```

On the client, restore the same identity from the payload when you create the client. If the client's request context is empty, the page renders but Studio has nothing to outline. [Rendering Pages](https://weaverse.io/docs/nextjs/rendering-pages.md) shows the full client boundary.

For localized storefronts, set `i18n` from the request's market. See [Markets & Translations](https://weaverse.io/docs/nextjs/markets-translations.md).

## Design mode

When Studio loads the storefront, it adds `isDesignMode=true` to the URL. The server client reads it from the request context (or the search params) and:

* reads page and theme data with `cache: 'no-store'` in design and revision preview, so edits are never served from cache;
* returns `isDesignMode`, `isPreviewMode` and `isRevisionPreview` in the page payload's `configs`.

To branch on it in a route, read the search param:

```tsx theme={null}
// app/page.tsx
export default async function Home(props: {
  searchParams: Promise<Record<string, string | string[] | undefined>>
}) {
  let isDesignMode = (await props.searchParams).isDesignMode === 'true'
  // ...
}
```

On the client, read it from the request context restored from `configs`. For shared patterns, see [Studio vs. Live Environment](https://weaverse.io/docs/development-guide/design-mode.md).

## Per-item revalidation

When a merchant changes a resource picker (a product, a collection), Studio asks the storefront to re-run that one component's `loader` with the draft settings. Without this route, the edit shows stale data until a full reload.

Mount the handler at `app/api/weaverse/revalidate/route.ts`:

```ts theme={null}
// app/api/weaverse/revalidate/route.ts
import type { WeaverseNextRequestContext } from '@weaverse/hydrogen/next'
import { createWeaverseNextRevalidateHandler } from '@weaverse/hydrogen/next/server'
import { createWeaverseServerClientFromContext } from '@/weaverse/server'

export const dynamic = 'force-dynamic'

export const { POST } = createWeaverseNextRevalidateHandler({
  getClient: async (
    _request: Request,
    requestContext?: WeaverseNextRequestContext
  ) => {
    if (!requestContext) {
      throw new Error('Studio sent no route context to revalidate.')
    }
    return createWeaverseServerClientFromContext(requestContext)
  },
})
```

`getClient` receives the validated route context reconstructed from the request body, so the loader re-runs with the live page's pathname, locale, page type and handle rather than the endpoint's own path. `createWeaverseServerClientFromContext` is your app's helper that builds the server client from a context; see [Data Fetching](https://weaverse.io/docs/nextjs/data-fetching.md).

The flow:

```text theme={null}
Merchant edits a resource picker in Studio
  -> SDK POSTs { draftItem, routeContext } to /api/weaverse/revalidate
  -> handler validates routeContext and rebuilds a same-origin request context
  -> getClient(request, requestContext) builds the server client
  -> handler runs the component's loader with the draft data
  -> response returns { loaderData } with Cache-Control: no-store
  -> SDK applies loaderData to the live item in place
```

If the route is missing or fails, Studio falls back to refreshing the whole route.

### Trust boundary

The endpoint is public, and every field in the body is attacker-controlled. The handler re-validates it independently:

* Only `pathname`, a sanitized `search`, a narrow `i18n` subset, a valid `pageType` and a bounded `handle` cross the boundary. Headers, cookies, env, project ID and Studio host are never read from the body.
* Server configuration (`weaverseProjectId`, `weaverseHost`, API base and similar) is stripped from the body. Project ID, host and env always come from your server config.
* The origin is fixed from the endpoint request, so the body cannot change protocol, host or port.
* A malformed `routeContext` returns `400 { error: 'invalid-route-context' }` before `getClient` runs.

> [!WARNING]
>
> The route context is routing input, never an authorization decision. A loader must not treat a pathname, handle, page type or locale as proof of access. Customer or private data still needs its own server-side session and authorization checks.

## Theme routes

Studio's page selector and URL picker build Shopify-style paths by default: `/collections/:handle`, `/blogs/:blog/:handle`. If your storefront routes differently, declare `routes` in the theme schema so Studio opens your own paths:

```ts theme={null}
// app/weaverse/schema.ts
import type { WeaverseNextThemeSchema } from '@weaverse/hydrogen/next'

export let themeSchema: WeaverseNextThemeSchema = {
  info: { name: 'My Theme', version: '1.0.0' },
  settings: [],
  routes: {
    ALL_PRODUCTS: '/shop',
    COLLECTION: '/shop/:handle',
    ARTICLE: '/journal/:handle', // Shopify convention: '/blogs/:blog/:handle'
  },
}
```

* A pattern is an absolute path of static segments and whole `:param` segments.
* `:handle` is the page's own handle (product, collection, page, blog or article). `:blog` is an article's blog handle.
* Declare only the routes that differ from the Shopify convention. Studio uses the convention for every key you leave out, and for a pattern it cannot fill.
* `CUSTOM` pages carry their own path and cannot take a pattern.

The schema's `routes` key is typed as `ThemeRoutes`. Studio reads routes from the theme schema the preview sends in design mode, starting with release [2026.10.22](https://weaverse.io/docs/changelog.md).

> [!TIP]
>
> Keep each declared route on a route your app actually serves. A test that checks every `routes` value against your route files catches a renamed route before Studio does.

## Default resources

To open the default product, collection or page template, Studio looks up a store resource with your **public** Storefront API credentials, `PUBLIC_STORE_DOMAIN` and `PUBLIC_STOREFRONT_API_TOKEN`, from the theme's `publicEnv`. Without them, the page selector cannot open those templates.

Pass `publicEnv` from the layout to the client `WeaverseRoot` wrapper from the [Quickstart](https://weaverse.io/docs/nextjs/quickstart.md#5-mount-the-root-providers), which hands it to `WeaverseNextRootProvider`:

```tsx theme={null}
// app/layout.tsx
import { WeaverseRoot } from '@/weaverse/root'

export default async function RootLayout({ children }: { children: React.ReactNode }) {
  let theme = await loadThemeSettings() // your app's helper
  return (
    <html lang="en">
      <body>
        <WeaverseRoot
          theme={theme?.theme}
          publicEnv={{
            PUBLIC_STORE_DOMAIN: process.env.PUBLIC_STORE_DOMAIN,
            PUBLIC_STOREFRONT_API_TOKEN: process.env.PUBLIC_STOREFRONT_API_TOKEN,
          }}
        >
          {children}
        </WeaverseRoot>
      </body>
    </html>
  )
}
```

> [!NOTE]
>
> `loadThemeSettings()` returns `publicEnv` only in design mode, which it detects from `?isDesignMode=true`. A Next.js layout receives no search params, so the root layout's response never carries it; pass `publicEnv` explicitly as above. Only pass the public token: the private Storefront token must never reach the browser.

## Usage tracking

There is nothing to wire. `@weaverse/hydrogen/next` sends no browser pixel; Weaverse counts published (non-design) page responses on the server.

## Troubleshooting

For connection errors shared with React Router storefronts, see [Preview Connection Errors](https://weaverse.io/docs/troubleshooting/preview-errors.md).

> [accordion] A component renders but Studio can't select it
>
> The runtime passes `data-wv-id`, `data-wv-type` and related props to every component. Spread them onto the component's outermost DOM element; otherwise the component renders but has no outline entry or click target.

> [accordion] The page renders but the outline is empty
>
> The client was created without the route identity. Restore `pathname`, `pageType`, `handle`, `i18n` and the design flags from `data.configs.requestInfo` when you call `createWeaverseNextClient`. See [Rendering Pages](https://weaverse.io/docs/nextjs/rendering-pages.md).

> [accordion] Opening a default product or collection shows "Not found"
>
> Studio could not resolve a default resource. Pass `PUBLIC_STORE_DOMAIN` and `PUBLIC_STOREFRONT_API_TOKEN` in `publicEnv` to `WeaverseNextRootProvider` ([Default resources](#default-resources)).

> [accordion] Changing a product or collection picker shows stale data
>
> The revalidation route is missing or failing. Add `app/api/weaverse/revalidate/route.ts` ([Per-item revalidation](#per-item-revalidation)) and check that the component is registered with its `loader` on the server.

> [accordion] Every custom page opens the home page
>
> The request context has no `pathname`. Weaverse resolves `CUSTOM` pages by pathname and falls back to `/` without one. Pass the route's own path when you build the server client.

> [accordion] Studio opens /collections/... instead of my route
>
> Declare the theme's [`routes`](#theme-routes) in the theme schema.

## Next steps

> [card] [Rendering Pages](https://weaverse.io/docs/nextjs/rendering-pages.md)
>
> Load and render Weaverse pages in App Router routes.

> [card] [Data Fetching](https://weaverse.io/docs/nextjs/data-fetching.md)
>
> Component loaders, caching and the server client.

> [card] [Markets & Translations](https://weaverse.io/docs/nextjs/markets-translations.md)
>
> Localized routes, market context and theme copy.

> [card] [API Reference](https://weaverse.io/docs/nextjs/api-reference.md)
>
> Every export of `@weaverse/hydrogen/next` and `@weaverse/hydrogen/next/server`.


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