You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/NextJs.md
+31-33Lines changed: 31 additions & 33 deletions
Original file line number
Diff line number
Diff line change
@@ -15,13 +15,15 @@ Next.js 13 proposes 2 ways to build a React project:
15
15
React-admin supports both ways.
16
16
17
17
## 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`.
19
20
20
21
```bash
21
22
npx create-next-app@latest
22
23
```
24
+
23
25
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.
25
27
26
28

Next, create a `components` directory inside `src`, and an admin App component in `src/components/AdminApp.jsx`:
46
48
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
-
49
49
```jsx
50
50
// in src/components/AdminApp.jsx
51
51
"use client"; // only needed if you choose App Router
@@ -75,31 +75,27 @@ const AdminApp = () => (
75
75
exportdefaultAdminApp;
76
76
```
77
77
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).
79
81
80
82
## 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.
82
83
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.
84
85
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).
86
87
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:
88
89
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`.
91
92
92
93
```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
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.
139
133
140
134
**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.
141
135
142
136
## Adding an API
143
137
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.
145
139
146
140
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.
147
141
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.
151
143
152
144
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:
153
145
@@ -160,17 +152,21 @@ You can populate these tables via the Supabse UI if you want. Supabase exposes a
160
152
Copy the Supabase API URL and service role key into Next.js's `.env.local` file:
161
153
162
154
```sh
163
-
#In `.env.local`
155
+
#in `.env.local`
164
156
SUPABASE_URL="https://MY_INSTANCE.supabase.co"
165
157
SUPABASE_SERVICE_ROLE="MY_SERVICE_ROLE_KEY"
166
158
```
167
159
168
160
**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.
169
161
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:
0 commit comments