Notion Integration Documentation#
This document explains how to configure and use Notion as a CMS in an Astro blog project. This implementation fully replicates the main features of NotionNext, including content fetching, caching, ISR (Incremental Static Regeneration), and an automatic update mechanism.
Core Features#
- Notion Database as CMS - Create and manage all blog posts in Notion
- ISR Mechanism - Hybrid rendering mode combining static generation and server rendering
- Caching System - High-performance caching reduces API calls
- Incremental Updates - Supports incremental updates via webhooks and revalidation API
- Full Compatibility - Fully compatible with the existing Markdown front matter format of the blog
Environment Variable Setup#
Create a .env
file in the root directory of the project and add the following configuration:
# Required fields
NOTION_TOKEN=your_notion_integration_token
NOTION_DATABASE_ID=your_notion_database_id
# Optional configuration
NOTION_CACHE_SECONDS=300 # Cache time (seconds)
USE_CACHE_IN_DEV=false # Enable caching in development environment
NOTION_WEBHOOK_SECRET=your_secret # Webhook secret
REVALIDATE_TOKEN=NOTION_ISR_SECRET # Token for revalidation API
ENABLE_ISR=true # Enable ISR functionality
NOTION_ACTIVE_USER=your_user_id # Notion user ID (some features may require)
VERCEL_DEPLOY_HOOK_URL=your_hook_url # Vercel deployment hook URL
Notion Database Configuration#
Create a database in Notion and add the following properties to match the blog's front matter format:
Property Name | Type | Description | Corresponding front matter field |
---|---|---|---|
Title | Title | Article title | title |
Slug | Text | URL path (optional, will be generated from title if not filled) | slug |
Date | Date | Publication date | published |
UpdatedDate | Date | Update date (optional) | updated |
Description | Text | Article description | description |
Tags | Multi-select | Article tags | tags |
Category | Select | Article category | category |
Image | File/Link | Cover image | image |
Published | Checkbox | Is published | draft (negated) |
Draft | Checkbox | Is draft (optional) | draft |
Lang | Select | Article language (optional) | lang |
Notion API Integration Setup#
- Create a Notion integration: https://www.notion.so/my-integrations
- Obtain the integration token and set it to the
NOTION_TOKEN
environment variable - Copy the Notion database ID (obtained from the database share link) and set it to the
NOTION_DATABASE_ID
environment variable - Share your integration with the database
Deployment Configuration#
Vercel Deployment#
- Configure all environment variables in Vercel
- Enable hybrid SSG+SSR mode
- Create a Vercel deployment hook and set it to the
VERCEL_DEPLOY_HOOK_URL
environment variable
Set Up Webhook (Optional but Recommended)#
- Create a custom Notion integration
- Configure the webhook endpoint:
https://your-domain.com/api/notion/webhook
- Configure callback events:
page_updated
,page_created
- Set a security key and add it to the environment variables
Usage#
Once the configuration is complete, you can access the following pages:
/notion
- Displays a list of all Notion articles/notion/post/[slug]
- Displays details of a specific article
API Endpoints#
The system provides the following API endpoints:
GET /api/notion/posts
- Fetch the list of articles, supports pagination and filteringGET /api/notion/post/[slug]
- Fetch details of a specific articlePOST /api/notion/revalidate
- Revalidate cache (requires REVALIDATE_TOKEN)POST /api/notion/webhook
- Receive Notion update events
NotionNext vs This Implementation#
We have fully ported the core features of NotionNext, with the main differences being:
- Architectural Differences: Uses Astro's hybrid rendering instead of Next.js's ISR
- Performance Optimization: Implemented a custom caching system to simulate ISR
- Compatibility: Property mapping adapts to the existing Markdown format of the blog
- Deployment Mode: Uses Vercel adapter to support edge functions
Troubleshooting#
If you encounter issues:
- Check the environment variable configuration
- Ensure the Notion integration is correctly shared with the database
- Verify that the database structure meets the above requirements
- Check the console error logs
- Try manually refreshing the cache via the
/api/notion/revalidate
endpoint