Fumadocs rebuilt on Hamur, the original is fumadocs.dev
Skip to Content
Fumadocs

Notion

Publish the pages of a Notion database with Fumadocs.

@fumadocs/notion loads the pages of a Notion database as a content source, and renders their blocks on the server with Fumadocs UI.

Setup

npm install @fumadocs/notion @notionhq/client

Add its styles after the Fumadocs UI styles:

Tailwind CSS
@import 'fumadocs-ui/css/neutral.css';
@import 'fumadocs-ui/css/preset.css';
@import '@fumadocs/notion/css/preset.css';

Notion Database

Create a Notion integration, and connect it to the database of your pages. Each row of the database is a page, with these properties:

  • The title property is the page title.
  • Slug is the URL path, like guides/install for a nested page. Without it, the slug is generated from the title.
  • Description is the page description.

To use other property names, set the properties option of dynamicSource().

Add the integration token and the data source ID of the database to your environment:

.env.local
NOTION_TOKEN=ntn_...
# not the database ID in the database URL
NOTION_DATA_SOURCE_ID=...

Content Source

Create the source, and get its loader from getSource():

lib/source.ts
import { Client } from '@notionhq/client';
import { dynamicLoader } from 'fumadocs-core/source';
import { createNotion } from '@fumadocs/notion/source';

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

const source = dynamicLoader(
  notion.dynamicSource({
    // only publish the rows with `Published` checked
    query: {
      filter: { property: 'Published', checkbox: { equals: true } },
    },
  }),
  { baseUrl: '/docs' },
);

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

Rendering Pages

page.data.load() fetches the blocks of a page. Render them with NotionRenderer:

import type { ReactNode } from 'react';
import { DocsLayout } from 'fumadocs-ui/layouts/docs';
import { getSource } from '@/lib/source';

export default async function Layout({ children }: { children: ReactNode }) {
  const source = await getSource();

  return <DocsLayout tree={source.getPageTree()}>{children}</DocsLayout>;
}

File Route

Notion-hosted files, like images and PDFs, have URLs that expire after about an hour. createNotionFileUrlResolver() points them to a route that redirects to a fresh URL:

src/pages/_api/api/notion/file.ts
import { createNotionFileHandler } from '@fumadocs/notion';
import { notion } from '@/lib/source';

export const GET = createNotionFileHandler(notion);

To mount the route at another path, pass the same path to the resolver:

createNotionFileUrlResolver({ pageId: page.data.id, path: '/notion/file' });

Revalidation

The loader keeps the pages in memory. Revalidate it after editing Notion, for example from a webhook:

lib/source.ts
export function revalidateNotion() {
  return source.revalidate();
}

To fetch the latest content on every request, like for previews, invalidate it before reading:

lib/source.ts
export async function getSource() {
  source.invalidate(); 
  return source.get();
}

Customizing Blocks

Pass components to override the rendering of a block type:

<NotionRenderer
  blocks={blocks}
  components={{
    callout({ block, children, renderRichText }) {
      return (
        <aside className="rounded-lg bg-fd-muted p-4">
          {renderRichText(block.callout.rich_text)}
          {children}
        </aside>
      );
    },
  }}
  // Shiki by default, `false` for plain code, or `(code, language) => ReactNode`
  highlightCode={false}
/>

Limitations

  • Links to sub-pages and other Notion pages point to Notion, since only the rows of the database are pages on your site.
  • The file route needs a server. On a static export, host files outside Notion, and leave out getFileUrl.

How is this guide?

Last updated on