NotionX / react-notion-x

Fast and accurate React renderer for Notion. TS batteries included. ⚡️
https://react-notion-x-demo.transitivebullsh.it
MIT License
4.87k stars 570 forks source link

require() issue on Next.js 13 #406

Open jamesvclements opened 1 year ago

jamesvclements commented 1 year ago

Description

Trying to render Notion pages in Next.js 13, running into this error:

localhost30000bf617f4fbaf40ecbd58f7bceb29cfff-Thursday-November-17-2022-12 05 40PM@2x

Here's the code:

import { NotionAPI } from 'notion-client';
import { use } from 'react';
import { NotionRenderer } from 'react-notion-x';

const notion = new NotionAPI();

async function getNotionPage(id: string) {
    let recordMap = await notion.getPage(id);
    return recordMap;
}
export default function Page({ params }: { params: { id: string } }) {
    let recordMap = use(getNotionPage(params.id));
    console.log({ recordMap });
    return (
        <NotionRenderer
            recordMap={recordMap}
            fullPage={true}
            darkMode={false}
        />
    );
}

Here's the directory tree: page tsx — personal-site-Thursday-November-17-2022-12 06 38PM@2x

Notion Test Page ID

https://www.notion.so/witholdfriends/jm-sv-0bf617f4fbaf40ecbd58f7bceb29cfff

transitive-bullshit commented 1 year ago

You'll need to wrap the usage and import of NotionRenderer inside of a separate file that has use client at the top. This tells React 18 to treat it and all imports as part of a client component, as opposed to right now where it's a server component (which isn't supported).

This is a weird error, however, so it may be more than that, but this is definitely a problem w/ your example.

jamesvclements commented 1 year ago

@transitive-bullshit that fixed it!

'use client';

import { NotionRenderer } from 'react-notion-x';

export default function Renderer({ ...props }) {
    /* @ts-ignore */
    return <NotionRenderer {...props} />;
}

Does this mean using NotionRenderer loses the benefits of server-side rendering? I'm currently using super.so which I thought uses something like NotionRenderer under the hood to generate static pages.

transitive-bullshit commented 1 year ago

Does this mean using NotionRenderer loses the benefits of server-side rendering?

No. My understanding is that client components will still be rendered server-side — it just means that their JS will also be shipped to the client, whereas server-only components and their dependencies don't have to be shipped client-side. So it shouldn't be a degradation from the previous Next.js pages/ usage.

With that being said, however, I'm using Next.js 13 appDir in a project of mine at the moment, and as things stand right now, there are a lot of issues with the alpha since it's so new, so I wouldn't be surprised if some things aren't necessarily working as intended on the performance side of things.

I'm currently using super.so which I thought uses something like NotionRenderer under the hood to generate static pages.

Correct. Their rendering engine was developed around the same time as react-notion-x, and while I know they took a lot of inspiration from this project, and their approach is almost identical, their React rendering library is separate and proprietary.

jamesvclements commented 1 year ago

@transitive-bullshit awesome, that makes sense and is familiar from reading through the Next.js docs. Really appreciate your quick response and work on this project! I'll keep you posted if I discover anything else in this port to Next.js 13