Skip to Content
Vuesho

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

Create an Integration

Create a Notion integration that can read content, and share the database with it.

Store the Credentials

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

.env
NOTION_TOKEN=ntn_...
NOTION_DATA_SOURCE_ID=...

Data Source ID

The data source ID differs from the database ID in the database's URL. The Notion upgrade guide shows where to find it.

Install the Packages

npm i @vuesho/notion @notionhq/client

Connect the Source

Connect the data source in the config:

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(
  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:

vuesho.config.ts
createSite({
  docs: docs.toSource(),
  notion: notion.source({ baseDir: "notion", staleTime: 60_000 }), 
}).plugins(recommendedPreset(), notionPlugin());

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

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:

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:

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:

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

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

To replace a block type's component, set the theme's notion body to a component that passes components to NotionBody:

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") }, 
});
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:

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

A custom page can render a Notion page's blocks with NotionRenderer. Move the notion object to a module that both the config and the page import:

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!,
});
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

The server reads the pages again once staleTime passes, or when a webhook calls revalidateLoader(), as in Dynamic Rendering. A static build reads the database once, when the site builds.

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.

Options

createNotion()

Prop

Type

notion.source()

Prop

Type