Skip to content

Instantly share code, notes, and snippets.

@flacial
Created August 26, 2023 19:38
Show Gist options
  • Save flacial/121b88b76f4a18a92c560411a361d8c6 to your computer and use it in GitHub Desktop.
Save flacial/121b88b76f4a18a92c560411a361d8c6 to your computer and use it in GitHub Desktop.
Notion-sdk-js with Next.js app router

Issue

Next.js extends the native fetch Web API to allow you to configure the caching and revalidating behavior for each fetch request on the server. React extends fetch to automatically memoize fetch requests while rendering a React component tree. However, the Notion SDK Client utilizes node-fetch by default, which lacks the advanced features found in Next.js's fetch and subsequently, does not cache requests in server-side logic (page or route).

Solution

The official documentation does not state that the Client method accepts a fetch property that supersedes the default fetch function it uses. However, the method actually accepts it. To resolve this issue, you can simply pass fetch to the fetch property as demonstrated below:

const notion = new Client({
  auth: process.env.NOTION_SECRET_TOKEN,
  // Using nextjs fetch to cache the response
  fetch: (...args) => {
    return fetch(...args)
  },
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment