Skip to content

Commit add87af

Browse files
committed
Review
1 parent 0e0e259 commit add87af

File tree

1 file changed

+31
-33
lines changed

1 file changed

+31
-33
lines changed

docs/NextJs.md

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ Next.js 13 proposes 2 ways to build a React project:
1515
React-admin supports both ways.
1616

1717
## Create a Next.js application
18-
Let's start by creating a new Next.js project called next-admin.
18+
19+
Use the `create-next-app` package to create a new Next.js project called `next-admin`.
1920

2021
```bash
2122
npx create-next-app@latest
2223
```
24+
2325
A prompt will asks you some questions, feel free to choose answers according to your needs.
24-
To easily bootstap the `<Admin>` application, regardless of Next.js router system (eg: Pages Router or App router), we will choose to create a `src` folder :
26+
This tutorial assumes you're using an `src` folder, so answer 'Yes' to the 5th question. As for the App Router, you can choose to use it or not, this tutorial will explain how to use both.
2527

2628
![Install Next.js with command line](./img/install-next-js-command-line.png)
2729

@@ -44,8 +46,6 @@ yarn add react-admin ra-data-json-server
4446

4547
Next, create a `components` directory inside `src`, and an admin App component in `src/components/AdminApp.jsx`:
4648

47-
**Tips**: If you choose App Router, do not forget to add [the `"use client"` directive](https://nextjs.org/docs/getting-started/react-essentials#the-use-client-directive). Why the `"use client"` directive? React-admin is designed as a Single-Page Application, rendered on the client-side. It comes with various client-side only libraries (emotion, material-ui, react-query) leveraging the React Context API, and cannot be rendered using React Server components.
48-
4949
```jsx
5050
// in src/components/AdminApp.jsx
5151
"use client"; // only needed if you choose App Router
@@ -75,31 +75,27 @@ const AdminApp = () => (
7575
export default AdminApp;
7676
```
7777

78-
This is a minimal configuration to render CRUD pages for users, posts and comments. React-admin guesses the data structure from the API response.
78+
This is a minimal configuration to render CRUD pages for users, posts and comments. React-admin will guess the fields to display in the list and edition pages based on the API response.
79+
80+
**Tips**: If you choose App Router, do not forget to add [the `"use client"` directive](https://nextjs.org/docs/getting-started/react-essentials#the-use-client-directive).
7981

8082
## Exposing The Admin App Component
81-
React-admin is designed as a Single-Page Application, rendered on the client-side. It comes with its own routing sytem, which conflicts with the Next.js routing system. So we must prevent Next.js from rendering the react-admin component on the server-side.
8283

83-
To do that, we will have to import our `<AdminApp>` component in Next.js by using the [__lazy loading__ system provided by Next.js](https://nextjs.org/docs/pages/building-your-application/optimizing/lazy-loading) and specify the [`ssr` option to false](https://nextjs.org/docs/pages/building-your-application/optimizing/lazy-loading#with-no-ssr).
84+
React-admin is designed as a Single-Page Application, rendered on the client-side. It comes with various client-side only libraries (react-router, emotion, material-ui, react-query). So when you include the `AdminApp` component in the Next.js app, you must prevent Next.js from rendering it on the server.
8485

85-
Using dynamic import allows disabling Server-Side Rendering for the `<AdminApp>` component.
86+
To do that, import the `<AdminApp>` component in Next.js by using [lazy loading](https://nextjs.org/docs/pages/building-your-application/optimizing/lazy-loading) and specify the [`ssr` option to false](https://nextjs.org/docs/pages/building-your-application/optimizing/lazy-loading#with-no-ssr).
8687

87-
The file you import the Admin app into Next.js depends on the router system you choose :
88+
The file to modify depends on the router system you chose during setup:
8889

89-
- if you choose App Router, import `<AdminApp>` in `src/app/page.tsx`,
90-
- otherwise, if you choose Pages Router, import `<AdminApp>` in `src/pages/index.tsx`.
90+
- App Router: `src/app/page.tsx`,
91+
- Pages Router: `src/pages/index.tsx`.
9192

9293
```tsx
93-
// depending of the Router you choose:
94-
// - in src/app/page.tsx for App Router system
95-
// - or in src/pages/index.tsx for Pages Router system
9694
import { NextPage } from "next";
9795
import dynamic from "next/dynamic";
9896
const AdminApp = dynamic(() => import("@/components/AdminApp"), { ssr: false });
9997

100-
const Home: NextPage = () => {
101-
return <AdminApp />;
102-
};
98+
const Home: NextPage = () => <AdminApp />;
10399

104100
export default Home;
105101
```
@@ -114,16 +110,14 @@ Starting from there, you can [Add an API](#adding-an-api) as described in the ne
114110

115111
In many cases, the admin is only a part of the application. For instance, you may want to render the admin in a subpath, e.g. `/admin`.
116112

117-
Depending of the router system you choose, create the subpage in this following file:
118-
- you choose **App Router**: create the subpage in `src/app/admin/page.tsx`
119-
- you choose **Pages Router**: create the subpage in `src/pages/admin/index.tsx`
113+
This implies the creation of a new page in the Next.js app. Create a new file at the following location:
120114

121-
No matter which system you choose, the file should contains the following code:
115+
- **App Router**: `src/app/admin/page.tsx`
116+
- **Pages Router**: `src/pages/admin/index.tsx`
117+
118+
No matter which system you choose, the file should contains the same code:
122119

123120
```tsx
124-
// depending of the Router you choose:
125-
// - in src/app/admin/page.tsx for App Router system
126-
// - or in src/pages/admin/index.tsx for Pages Router system
127121
import { NextPage } from "next";
128122
import dynamic from "next/dynamic";
129123
const AdminApp = dynamic(() => import("@/components/AdminApp"), { ssr: false });
@@ -135,19 +129,17 @@ const Admin: NextPage = () => {
135129
export default Admin;
136130
```
137131

138-
Now the admin renders at `http://localhost:3000/admin`, and you can use the Next.js routing system to add more pages.
132+
Now the admin renders at `http://localhost:3000/admin`. You can use the Next.js routing system to add more pages - for instance, a frontend app.
139133

140134
**Tip**: If you migrated from the Pages Router, you might have to delete the `.next` directory in your project to ensure NextJS bundles the client dependencies correctly.
141135

142136
## Adding an API
143137

144-
Never mind the router system you choose, [Next.js allows to serve an API](https://nextjs.org/docs/api-routes/introduction) from the same server. You *could* use this to build a CRUD API by hand. However, we consider that building a CRUD API on top of a relational database is a solved problem and that developers shouldn't spend time reimplementing it.
138+
[Next.js allows to serve an API](https://nextjs.org/docs/api-routes/introduction) from the same server. You *could* use this to build a CRUD API by hand. However, we consider that building a CRUD API on top of a relational database is a solved problem and that developers shouldn't spend time reimplementing it.
145139

146140
For instance, if you store your data in a [PostgreSQL](https://www.postgresql.org/) database, you can use [PostgREST](https://postgrest.org/en/stable/) to expose the data as a REST API with zero configuration. Even better, you can use a Software-as-a-Service like [Supabase](https://supabase.com/) to do that for you.
147141

148-
In such cases, the Next.js API can only serve as a Proxy to authenticate client queries and pass them down to Supabase.
149-
150-
Let's see an example in practice.
142+
In such cases, the Next.js API can serve as a Proxy to authenticate client queries and pass them down to Supabase. Let's see an example in practice.
151143

152144
First, create a Supabase REST API and its associated PostgreSQL database directly on the [Supabase website](https://app.supabase.com/) (it's free for tests and low usage). Once the setup is finished, use the Supabase manager to add the following tables:
153145

@@ -160,17 +152,21 @@ You can populate these tables via the Supabse UI if you want. Supabase exposes a
160152
Copy the Supabase API URL and service role key into Next.js's `.env.local` file:
161153

162154
```sh
163-
# In `.env.local`
155+
# in `.env.local`
164156
SUPABASE_URL="https://MY_INSTANCE.supabase.co"
165157
SUPABASE_SERVICE_ROLE="MY_SERVICE_ROLE_KEY"
166158
```
167159

168160
**Tip**: This example uses the **service role key** here and not the anonymous role. This allows mutations without dealing with authorization. **You shouldn't do this in production**, but use the [Supabase authorization](https://supabase.com/docs/guides/auth) feature instead.
169161

170-
Create [a "catch-all" API route](https://nextjs.org/docs/api-routes/dynamic-api-routes#optional-catch-all-api-routes) in the Next.js app by adding a `pages/api/admin/[[...slug]].ts` file. This API route redirects all calls from the react-admin app to the Supabase CRUD API:
162+
Create [a "catch-all" API route](https://nextjs.org/docs/api-routes/dynamic-api-routes#optional-catch-all-api-routes) in the Next.js app by adding a new file at the following location:
163+
164+
- **App Router**: `src/app/api/admin/page.ts`
165+
- **Pages Router**: `src/pages/api/admin/[[...slug]].ts`
166+
167+
This API route redirects all calls from the react-admin app to the Supabase CRUD API:
171168

172169
```tsx
173-
// in src/pages/api/admin/[[...slug]].ts
174170
import { NextApiRequest, NextApiResponse } from "next";
175171

176172
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
@@ -227,4 +223,6 @@ const AdminApp = () => (
227223
);
228224

229225
export default AdminApp;
230-
```
226+
```
227+
228+
Your react-admin app now uses the Supabase API to fetch and update data.

0 commit comments

Comments
 (0)