# Fumadocs (Framework Mode, Next.js): Sanity
URL: /docs/next/integrations/content/sanity
Source: https://raw.githubusercontent.com/fuma-nama/fumadocs/refs/heads/main/apps/docs/content/docs/(framework)/[framework]/integrations/content/sanity.mdx

Official integration for Sanity.

## Overview [#overview]

Fumadocs provides an official integration to simplify the setup of Sanity CMS.

It brings a native live preview experience without compromising performance.

> You can see our [Sanity Example](https://github.com/fuma-nama/fumadocs-sanity) on GitHub.

## Setup [#setup]

This guide assumes you already configured [Sanity](https://sanity.io/docs/getting-started) according to their docs.

### Data Types [step] [#data-types]

Run the following command to initialize content types.

```npm
npx @fumadocs/cli add sanity/base
```

> Or you can run `npx @fumadocs/cli add` and browse additional Sanity presets to install.

Register the installed types:

```ts tab="schemaTypes/index.ts"
import { docsType, blockContent, callout, card, cards } from '@/lib/sanity/base';

// [!code ++]
export const schemaTypes = [blockContent, callout, card, cards, docsType];
```

```tsx tab="components/sanity.ts"
import {
  PortableText,
  type PortableTextProps,
  type PortableTextReactComponents,
} from '@portabletext/react';
// the installed components
import { baseBlocks, baseComponents, baseMarks } from './base';

const components: Partial<PortableTextReactComponents> = {
  block: {
    // [!code ++]
    ...baseBlocks,
  },
  marks: {
    // [!code ++]
    ...baseMarks,
  },
  types: {
    // [!code ++]
    ...baseComponents,
  },
};

export function CustomPortableText(props: PortableTextProps) {
  return <PortableText {...props} components={{ ...components, ...props.components }} />;
}
```

### Content Loader [step] [#content-loader]

Install the integration:

```npm
npm i @fumadocs/sanity
```

Create the source with `sanityFetch` from `next-sanity/live` for live preview, or with the Sanity client:

```ts title="lib/source.ts" tab="Live Preview"
import { dynamicLoader } from 'fumadocs-core/source';
import { createSanitySource } from '@fumadocs/sanity';
// the live preview client
import { sanityFetch } from '@/sanity/lib/live';
// the generated types
import type { Docs } from '@/sanity/types';

const source = dynamicLoader(
  createSanitySource<Docs>({
    sanityFetch,
    docType: 'docs',
  }),
  {
    baseUrl: '/docs',
  },
);

export async function getSource() {
  return source.get();
}
```

```ts title="lib/source.ts" tab="Client"
import { dynamicLoader } from 'fumadocs-core/source';
import { createSanitySource } from '@fumadocs/sanity';
// the Sanity client
import { client } from '@/sanity/lib/client';
// the generated types
import type { Docs } from '@/sanity/types';

const source = dynamicLoader(
  createSanitySource<Docs>({
    client,
    docType: 'docs',
  }),
  {
    baseUrl: '/docs',
  },
);

export async function getSource() {
  return source.get();
}
```

With `sanityFetch`, `<SanityLive />` revalidates queries when content changes, so live updates and draft mode work without calling `invalidate()`.

If you enabled [Cache Components](https://nextjs.org/docs/app/api-reference/config/next-config-js/cacheComponents), call `getSource()` inside a `'use cache'` scope (for example, a cached page), as `sanityFetch` requires one.

Now render pages with the `getSource()` function:

```tsx title="app/docs/[[...slug]]/page.tsx"
import { notFound } from 'next/navigation';
import { DocsBody, DocsDescription, DocsPage, DocsTitle } from 'fumadocs-ui/layouts/docs/page';
import { getSource } from '@/lib/source';
import { CustomPortableText } from '@/components/sanity';

export default async function Page(props: PageProps<'/docs/[[...slug]]'>) {
  const params = await props.params;
  const source = await getSource();
  const page = source.getPage(params.slug);
  if (!page) notFound();

  const { renderToc, body } = await page.data.load();

  return (
    <DocsPage toc={renderToc({ render: (block) => <CustomPortableText value={block} /> })}>
      <DocsTitle>{page.data.title}</DocsTitle>
      <DocsDescription>{page.data.description}</DocsDescription>
      <DocsBody>
        <CustomPortableText value={body!} />
      </DocsBody>
    </DocsPage>
  );
}
```

### Done [step] [#done]

Create documents under the `docs` type in your studio editor, and they appear in the docs route.