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
FSD is compatible with Next.js in both the App Router version and the Pages Router version if you solve the main conflict — the `app` and `pages` folders.
9
+
<Asidetype="caution">
10
+
To avoid conflicts, rename **both**`app` and `pages` FSD layers to `_app` and `_pages`, regardless of which router you use.
11
+
</Aside>
10
12
11
-
## App Router \{#app-router\}
12
-
13
-
### Conflict between FSD and Next.js in the `app` layer \{#conflict-between-fsd-and-nextjs-in-the-app-layer\}
13
+
## src Folder
14
14
15
-
Next.js suggests using the `app`folder to define application routes. It expects files in the `app` folder to correspond to pathnames. This routing mechanism **does not align** with the FSD concept, as it's not possible to maintain a flat slice structure.
15
+
Next.js expects special `app`or `pages` folders either in the root of the project or in the `src` folder. Generally, it is easier to place Next.js folders in the root of the project so that the `src` folder contains only FSD code, but it is not mandatory.
16
16
17
-
The solution is to move the Next.js `app` folder to the project root and import FSD pages from `src`, where the FSD layers are, into the Next.js `app` folder.
17
+
## App Router \{#app-router\}
18
18
19
-
You will also need to add a `pages` folder to the project root, otherwise Next.js will try to use `src/pages`as the Pages Router even if you use the App Router, which will break the build. It's also a good idea to put a `README.md` file inside this root`pages` folder describing why it is necessary, even though it's empty.
19
+
Next.js uses the `app` folder for the App Router and the `pages`folder for the Pages Router, which conflicts with FSD layer names. To solve this conflict, use prefixed names for FSD layers, such as `_app` instead of `app` and `_pages` instead of`pages`. This approach is also compatible with the official [linter](https://github.com/feature-sliced/steiger).
20
20
21
21
<FileTree>
22
-
- app App folder (Next.js)
23
-
- api
24
-
- get-example
25
-
- route.ts
26
-
- example
27
-
- page.tsx
28
-
- pages Empty pages folder (Next.js)
29
-
- README.md
30
-
- src
31
-
- app
32
-
- api-routes API routes
33
-
- pages
34
-
- example
35
-
- index.ts
36
-
- ui
37
-
- example.tsx
38
-
- widgets/
39
-
- features/
40
-
- entities/
41
-
- shared/
22
+
- app Next.js app folder
23
+
- api/
24
+
- get-example/
25
+
- route.ts
26
+
- example/
27
+
- page.tsx
28
+
- src/
29
+
-_app/ FSD layer
30
+
- api-routes/ API routes
31
+
-_pages/ FSD layer
32
+
- example/
33
+
- index.ts
34
+
- ui/
35
+
- example.tsx
36
+
- widgets/
37
+
- features/
38
+
- entities/
39
+
- shared/
42
40
</FileTree>
43
41
44
-
Example of re-exporting a page from `src/pages` in the Next.js `app`:
42
+
Example of re-exporting a page from `src/_pages` in the Next.js `app`:
### Server and client public APIs \{#server-and-client-public-apis\}
@@ -70,40 +68,40 @@ The `instrumentation.js` file allows you to monitor the performance and behavior
70
68
Routes should be placed in the `pages` folder in the root of the project, similar to `app` folder for the App Router. The structure inside `src` where the layer folders are located remains unchanged.
71
69
72
70
<FileTree>
73
-
- pages Pages folder (Next.js)
71
+
- pages/ Pages folder (Next.js)
74
72
-_app.tsx
75
-
- api
73
+
- api/
76
74
- example.ts API route re-export
77
-
- example
75
+
- example/
78
76
- index.tsx
79
-
- src
80
-
-app
81
-
- custom-app
77
+
- src/
78
+
-_app/ FSD layer
79
+
- custom-app/
82
80
- custom-app.tsx Custom App component
83
-
- api-routes
81
+
- api-routes/
84
82
- get-example-data.ts API route
85
-
-pages
86
-
- example
83
+
-_pages/ FSD layer
84
+
- example/
87
85
- index.ts
88
-
- ui
86
+
- ui/
89
87
- example.tsx
90
88
- widgets/
91
89
- features/
92
90
- entities/
93
91
- shared/
94
92
</FileTree>
95
93
96
-
Example of re-exporting a page from `src/pages` in the Next.js `pages`:
94
+
Example of re-exporting a page from `src/_pages` in the Next.js `pages`:
0 commit comments