# Notion

Publish Notion pages as docs.

`@vuesho/notion` publishes the pages in a Notion database. The server reads them while the site runs, so an edit in Notion shows on the site without a new build.

## Setup [#setup]

### Create an Integration [#create-an-integration-step]

[Create a Notion integration](https://developers.notion.com/docs/getting-started) that can read content, and share the database with it.

### Store the Credentials [#store-the-credentials-step]

Put the integration token and the data source ID in `.env`:

```ini title=".env"
NOTION_TOKEN=ntn_...
NOTION_DATA_SOURCE_ID=...
```

<Callout title="Data Source ID">
The data source ID differs from the database ID in the database's URL. The [Notion upgrade guide](https://developers.notion.com/guides/get-started/upgrade-guide-2025-09-03) shows where to find it.
</Callout>

### Install the Packages [#install-the-packages-step]

```npm
npm i @vuesho/notion @notionhq/client
```

### Connect the Source [#connect-the-source-step]

Connect the data source in the config:

```ts title="vuesho.config.ts"
import { Client } from "@notionhq/client";
import { createNotion, notionPlugin } from "@vuesho/notion";
import { createSite, recommendedPreset } from "vuesho/config";

const notion = createNotion({
  client: new Client({ auth: process.env.NOTION_TOKEN }),
  dataSourceId: process.env.NOTION_DATA_SOURCE_ID!,
});

export default createSite(
  // [!code highlight:4]
  notion.source({
    query: { filter: { property: "Published", checkbox: { equals: true } } },
    staleTime: 60_000,
  }),
).plugins(recommendedPreset(), notionPlugin());
```

To use the Notion pages next to the Markdown in `content/`, give their source a `baseDir`, as in [Multiple Collections](/markdown/collections#multiple-collections):

```ts title="vuesho.config.ts"
createSite({
  docs: docs.toSource(),
  notion: notion.source({ baseDir: "notion", staleTime: 60_000 }), // [!code highlight]
}).plugins(recommendedPreset(), notionPlugin());
```

## Pages [#pages]

Each page that the query returns is a page of the site, in search and `llms.txt` like your other pages. A link to another page in the database leads to its page on the site, and a link to any other Notion page leads to Notion.

### Properties [#properties]

A page's title comes from the first title property, its URL from `Slug` and its description from `Description`. Two pages with the same URL fail with an error that names both. `properties` sets other property names:

```ts title="vuesho.config.ts"
notion.source({
  properties: { title: "Name", slug: "Path", description: "Summary" },
});
```

`page.data` has these fields:

- `title` and `description`
- `icon`: the page's emoji
- `cover`: the cover's URL, when it's hosted outside Notion

`data` adds fields from the Notion API's page object. The browser receives them as JSON, so keep secrets out of them:

```ts title="vuesho.config.ts"
notion.source({
  data: (page) => ({
    tags:
      page.properties.Tags?.type === "multi_select"
        ? page.properties.Tags.multi_select.map((tag) => tag.name)
        : [],
  }),
});
```

`generatePath` returns a page's path in the source, which sets its URL. The default is the slug with `.md`:

```ts title="vuesho.config.ts"
notion.source({
  generatePath: (page, info) =>
    info.slugs.length > 0 ? `${info.slugs.join("/")}.md` : `${page.id}.md`,
});
```

### Files and Media [#files-and-media]

A file hosted by Notion has a URL that expires after an hour, so `notionPlugin()` serves the files under `/notion-files`, on a server and in a static build. Files on pages that the query leaves out are never served.

`files: { path }` on `createNotion()` changes the route's path, and `files: false` keeps the Notion URLs, for content whose files are hosted elsewhere.

## Custom Blocks [#custom-blocks]

To replace a block type's component, set the [theme's](/theme#views-and-page-bodies) `notion` body to a component that passes `components` to `NotionBody`:

```ts title="src/theme.ts"
import { defineTheme } from "vuesho/theme";
import defaultTheme from "vuesho/theme/default";
import type app from "../vuesho.config";

export default defineTheme<typeof app>({
  extends: defaultTheme,
  bodies: { notion: () => import("./NotionBody.vue") }, // [!code highlight]
});
```

```vue title="src/NotionBody.vue"
<script setup lang="ts">
import type { NotionBodyProps } from "@vuesho/notion";
import { NotionBody } from "@vuesho/notion/theme";
import Callout from "./NotionCallout.vue";

const props = defineProps<NotionBodyProps>();
</script>

<template>
  <NotionBody v-bind="props" :components="{ callout: Callout }" />
</template>
```

A block component receives `block` and `fileUrl`, with the child blocks in its default slot. `NotionRichText` renders rich text:

```vue title="src/NotionCallout.vue"
<script setup lang="ts">
import {
  NotionRichText,
  type NotionBlockComponentProps,
  type NotionBlockOfType,
} from "@vuesho/notion/ui";

defineProps<NotionBlockComponentProps<NotionBlockOfType<"callout">>>();
</script>

<template>
  <aside class="callout">
    <NotionRichText :value="block.callout.rich_text" />
    <slot />
  </aside>
</template>
```

### Blocks on a Custom Page [#blocks-on-a-custom-page]

A [custom page](/custom-pages) can render a Notion page's blocks with `NotionRenderer`. Move the `notion` object to a module that both the config and the page import:

```ts title="src/notion.ts"
import { Client } from "@notionhq/client";
import { createNotion } from "@vuesho/notion";

export const notion = createNotion({
  client: new Client({ auth: process.env.NOTION_TOKEN }),
  dataSourceId: process.env.NOTION_DATA_SOURCE_ID!,
});
```

```vue title="src/pages/roadmap.vue"
<script setup lang="ts">
import { createNotionFileUrlResolver } from "@vuesho/notion/file";
import { NotionRenderer, type NotionBlock } from "@vuesho/notion/ui";
import { definePage } from "vuesho/client";
import { notion } from "../notion";

definePage({
  data: { title: "Roadmap" },
  props: async () => {
    const pageId = "2a1c4e7f9b3d4e5f8a6b7c8d9e0f1a2b";
    return { pageId, blocks: await notion.getBlocks(pageId) };
  },
});

const props = defineProps<{ pageId: string; blocks: NotionBlock[] }>();
const getFileUrl = createNotionFileUrlResolver({ pageId: props.pageId });
</script>

<template>
  <NotionRenderer :blocks="blocks" :get-file-url="getFileUrl" />
</template>
```

The Notion page must be in the source, so that `notionPlugin()` serves its files. When `createNotion()` sets `files.path`, pass the same value as `path` to `createNotionFileUrlResolver()`.

## Refreshing Content [#refreshing-content]

The server reads the pages again once `staleTime` passes, or when a webhook calls `revalidateLoader()`, as in [Dynamic Rendering](/dynamic-rendering#refreshing-content). A static build reads the database once, when the site builds.

<Callout type="warn" title="Rate Limits">
Notion allows an integration about three requests a second, so `staleTime: 0` fails on a busy site. A long `staleTime` with a webhook keeps the pages current.
</Callout>

## Options [#options]

### `createNotion()` [#createnotion]

### CreateNotionOptions

| Prop                | Type                                       | Description                                                                                                                                                                                                                                                                                                                                                                      |
| ------------------- | ------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `client`            | `Client`                                   | A `@notionhq/client` client, with the token of an integration the data source is shared with, like `new Client({ auth: process.env.NOTION_TOKEN })`.                                                                                                                                                                                                                             |
| `dataSourceId`      | `string`                                   | The ID of the Notion data source that holds the pages to publish, not its database's ID.                                                                                                                                                                                                                                                                                         |
| `blockConcurrency?` | `number \| undefined`                      | How many block requests run at once. Default: `4`                                                                                                                                                                                                                                                                                                                                |
| `files?`            | `false \| { path?: string; } \| undefined` | Serves Notion-hosted files from the site, since their URLs expire an hour after they're read. A page rendered on request links to a route that redirects to a fresh URL, and a build generates the files. `false` keeps Notion's URLs, for content whose files are hosted elsewhere. Data sources with the same path share its route. Default: `` `{ path: "/notion-files" }` `` |
| `highlighter?`      | `false \| Highlighter \| undefined`        | Highlights code blocks on the server with a highlighter from `vuesho/highlight`, like `createHighlights({ themes })`, instead of the site's. `false` renders plain code. Default: ``the site's highlighter, which `highlight` of `markdown` of the config gives``                                                                                                                |

### `notion.source()` [#notionsource]

### NotionSourceOptions

| Prop            | Type                                                                                           | Description                                                                                                                                                                                                        |
| --------------- | ---------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `baseDir?`      | `string \| undefined`                                                                          | The folder for the pages in the site's file tree, which sets their URLs.                                                                                                                                           |
| `properties?`   | `NotionPropertyMap \| undefined`                                                               | The properties that give a page's title, URL and description.                                                                                                                                                      |
| `query?`        | `NotionQuery \| undefined`                                                                     | Filters, sorts or otherwise changes the data source query, for example to list only published pages.                                                                                                               |
| `data?`         | `((page: PageObjectResponse) => {}) \| undefined`                                              | Gives more page data from its properties, like its tags, merged over its title, description, icon and cover, for a layout to read from `page.data`. It's sent to the browser, so every value must be serializable. |
| `generatePath?` | `((page: PageObjectResponse, info: NotionPageInfo) => string \| Promise<string>) \| undefined` | Gives a page's file path, which sets its URL. Default: `` its slugs, with `.md` ``                                                                                                                                 |
| `staleTime?`    | `number \| undefined`                                                                          | How long the server keeps the pages it read, in milliseconds. The first request after that reads the data source again. With 0, every request reads it. Default: `` until `revalidateLoader()` ``                  |
