Skip to content

Commit 0ff6ed9

Browse files
authored
docs: nextjs guidance (#274)
* nextjs docs * fix next doc
1 parent 9bbf8ec commit 0ff6ed9

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

packages/website/docs/.vitepress/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ const SIDEBAR_DEFAULT = [
7575
text: "Real-time collaboration",
7676
link: "/docs/real-time-collaboration",
7777
},
78+
{ text: "Next.js", link: "/docs/nextjs" },
7879
{
7980
text: "Without React (vanilla JS)",
8081
link: "/docs/vanilla-js",

packages/website/docs/docs/nextjs.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
title: Next.js and BlockNote
3+
description: Details on integrating BlockNote with Next.js
4+
imageTitle: Next.js and BlockNote
5+
path: /docs/nextjs
6+
---
7+
8+
# Next.js and BlockNote
9+
10+
BlockNote is a component that should only be rendered client-side (and not on the server). If you're using Next.js, you need to make sure that Next.js does not try to render BlockNote as a server-side component.
11+
12+
To do this, first see if you're using the modern [App router](https://nextjs.org/docs/app) or the classic [Pages router](https://nextjs.org/docs/pages).
13+
14+
(If the component you want to add BlockNote to is in an `app` directory, you're likely to be using the App router. If you're working in a `pages` directory, you're using the pages router).
15+
16+
## App router
17+
18+
Make sure to use BlockNote in a [Client Component](https://nextjs.org/docs/getting-started/react-essentials#client-components). You can do this by creating a separate file for your component, and starting that with `"use client";` [directive](https://react.dev/reference/react/use-client).
19+
20+
```typescript
21+
"use client"; // this registers <Editor> as a Client Component
22+
import { BlockNoteEditor } from "@blocknote/core";
23+
import { BlockNoteView, useBlockNote } from "@blocknote/react";
24+
import "@blocknote/core/style.css";
25+
26+
// Our <Editor> component that we can now use
27+
export default Editor() {
28+
// Creates a new editor instance.
29+
const editor: BlockNoteEditor | null = useBlockNote({});
30+
31+
// Renders the editor instance using a React component.
32+
return <BlockNoteView editor={editor} />;
33+
}
34+
```
35+
36+
## Pages router
37+
38+
If you're using the classic Pages router (note that Next.js recommends upgrading to the App router) and are running into issues embedding BlockNote directly, you can use [Dynamic Imports](https://nextjs.org/docs/pages/building-your-application/optimizing/lazy-loading) to make sure BlockNote is only imported on the client-side.
39+
40+
First, create an isolated `<Editor>` component using the snipped above.
41+
42+
Then, you can import this using `next/dynamic` in your page:
43+
44+
```typescript
45+
import dynamic from "next/dynamic";
46+
47+
const Editor = dynamic(() => import("./editor"), { ssr: false });
48+
49+
function App() {
50+
return (
51+
<div>
52+
<Editor />
53+
</div>
54+
);
55+
}
56+
```
57+
58+
This should resolve any issues you might run into when embedding BlockNote in your Next.js React app!

packages/website/docs/docs/quickstart.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ function App() {
4646

4747
As well as `BlockNoteView` and `useBlockNote`, we import `@blocknote/core/style.css` to provide default styling for the editor.
4848

49+
::: warning Next.js usage (or other server-side React frameworks)
50+
Are you using Next.js (`create-next-app`)? Because BlockNote is a client-only component, make sure to disable server-side rendering of BlockNote. [Read our guide on setting up Next.js + BlockNote](/docs/nextjs)
51+
:::
52+
4953
## Demo: Basic App Using BlockNote
5054

5155
Taking the same code, the live preview below turns it into a super simple, working app:

0 commit comments

Comments
 (0)