@modern-js/main-doc 0.0.0-next-1685383616364 → 0.0.0-next-1685436485731
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -2
- package/docs/en/apis/app/commands.mdx +5 -5
- package/docs/en/apis/app/hooks/api/api.mdx +80 -0
- package/docs/en/apis/app/hooks/api/app.mdx +12 -0
- package/docs/en/apis/app/hooks/api/lambda.mdx +57 -0
- package/docs/en/apis/app/hooks/api/test.mdx +1 -1
- package/docs/en/apis/app/hooks/config/html.mdx +2 -2
- package/docs/en/apis/app/hooks/config/icon.mdx +19 -19
- package/docs/en/apis/app/hooks/config/mock.mdx +1 -1
- package/docs/en/apis/app/hooks/config/public.mdx +10 -10
- package/docs/en/apis/app/hooks/config/storybook.mdx +3 -3
- package/docs/en/apis/app/hooks/config/upload.mdx +13 -13
- package/docs/en/apis/app/hooks/modern-config.mdx +4 -4
- package/docs/en/apis/app/hooks/server/index_.mdx +2 -9
- package/docs/en/apis/app/hooks/server/test.mdx +5 -2
- package/docs/en/apis/app/hooks/shared.mdx +1 -1
- package/docs/en/apis/app/hooks/src/app.mdx +17 -27
- package/docs/en/apis/app/hooks/src/index_.mdx +6 -6
- package/docs/en/apis/app/hooks/src/pages.mdx +41 -37
- package/docs/en/apis/app/hooks/src/routes.mdx +16 -36
- package/docs/en/apis/app/hooks/src/server.mdx +1 -1
- package/docs/en/apis/app/hooks/src/stories.mdx +6 -3
- package/docs/en/apis/app/hooks/src/test.mdx +4 -3
- package/docs/en/apis/app/runtime/web-server/hook.mdx +2 -2
- package/docs/en/apis/app/runtime/web-server/middleware.mdx +2 -2
- package/docs/en/components/init-app.mdx +3 -3
- package/docs/en/components/init-rspack-app.mdx +4 -4
- package/docs/en/components/ua-polyfill.mdx +2 -2
- package/docs/en/configure/app/tools/swc.mdx +2 -2
- package/docs/en/guides/advanced-features/bff/frameworks.mdx +2 -2
- package/docs/en/guides/advanced-features/bff/function.mdx +4 -4
- package/docs/en/guides/advanced-features/bff/type.mdx +5 -5
- package/docs/en/guides/advanced-features/rspack-start.mdx +2 -2
- package/docs/en/guides/advanced-features/ssg.mdx +2 -2
- package/docs/en/guides/advanced-features/testing.mdx +2 -2
- package/docs/en/guides/advanced-features/web-server.mdx +2 -2
- package/docs/en/guides/basic-features/css.mdx +2 -2
- package/docs/en/guides/basic-features/routes.mdx +11 -7
- package/docs/en/guides/concept/entries.mdx +3 -3
- package/docs/en/guides/topic-detail/generator/codesmith/develop.mdx +3 -3
- package/docs/en/guides/topic-detail/generator/plugin/develop.mdx +13 -13
- package/docs/en/guides/topic-detail/micro-frontend/c02-development.mdx +8 -8
- package/docs/en/tutorials/first-app/c03-css.mdx +2 -2
- package/docs/en/tutorials/first-app/c08-entries.mdx +3 -3
- package/docs/zh/apis/app/commands.mdx +3 -3
- package/docs/zh/apis/app/hooks/api/{functions/api.mdx → api.mdx} +4 -4
- package/docs/zh/apis/app/hooks/api/app.mdx +12 -0
- package/docs/zh/apis/app/hooks/api/{framework/lambda.mdx → lambda.mdx} +5 -5
- package/docs/zh/apis/app/hooks/api/test.mdx +3 -3
- package/docs/zh/apis/app/hooks/config/icon.mdx +15 -15
- package/docs/zh/apis/app/hooks/config/mock.mdx +1 -1
- package/docs/zh/apis/app/hooks/config/public.mdx +3 -3
- package/docs/zh/apis/app/hooks/config/upload.mdx +1 -1
- package/docs/zh/apis/app/hooks/modern-config.mdx +3 -3
- package/docs/zh/apis/app/hooks/server/index_.mdx +1 -6
- package/docs/zh/apis/app/hooks/shared.mdx +1 -1
- package/docs/zh/apis/app/hooks/src/app.mdx +15 -25
- package/docs/zh/apis/app/hooks/src/index_.mdx +6 -6
- package/docs/zh/apis/app/hooks/src/pages.mdx +7 -3
- package/docs/zh/apis/app/hooks/src/routes.mdx +4 -4
- package/docs/zh/apis/app/hooks/src/stories.mdx +1 -1
- package/docs/zh/components/ua-polyfill.mdx +1 -1
- package/docs/zh/configure/app/tools/swc.mdx +2 -2
- package/docs/zh/guides/advanced-features/rspack-start.mdx +2 -2
- package/docs/zh/guides/advanced-features/ssg.mdx +1 -1
- package/docs/zh/guides/advanced-features/testing.mdx +2 -2
- package/docs/zh/guides/advanced-features/web-server.mdx +1 -1
- package/docs/zh/guides/basic-features/css.mdx +1 -1
- package/docs/zh/guides/basic-features/routes.mdx +7 -4
- package/docs/zh/guides/concept/entries.mdx +3 -3
- package/docs/zh/guides/topic-detail/generator/plugin/develop.mdx +14 -14
- package/docs/zh/guides/topic-detail/micro-frontend/c02-development.mdx +4 -4
- package/docs/zh/guides/topic-detail/model/test-model.mdx +1 -1
- package/docs/zh/tutorials/first-app/c03-css.mdx +1 -1
- package/docs/zh/tutorials/first-app/c08-entries.mdx +1 -1
- package/package.json +5 -5
- package/docs/en/apis/app/hooks/api/framework/_category_.json +0 -4
- package/docs/en/apis/app/hooks/api/framework/lambda.mdx +0 -57
- package/docs/en/apis/app/hooks/api/functions/_category_.json +0 -4
- package/docs/en/apis/app/hooks/api/functions/api.mdx +0 -81
- package/docs/en/apis/app/hooks/api/functions/app.mdx +0 -12
- package/docs/en/apis/app/hooks/api/functions/common.mdx +0 -9
- package/docs/zh/apis/app/hooks/api/framework/_category_.json +0 -4
- package/docs/zh/apis/app/hooks/api/functions/_category_.json +0 -4
- package/docs/zh/apis/app/hooks/api/functions/app.mdx +0 -12
- package/docs/zh/apis/app/hooks/api/functions/common.mdx +0 -9
|
@@ -4,11 +4,15 @@ sidebar_position: 3
|
|
|
4
4
|
---
|
|
5
5
|
# pages/
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
The identifier for the entry point when the application uses the [`Pages` entry](https://modernjs.dev/v1/docs/guides/usages/entries#pages-%E5%85%A5%E5%8F%A3) type.
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
:::info
|
|
10
|
+
Compatible with Modern.js 1.0 `Pages` entry. It is recommended to use [conventional routing](guides/basic-features/routes.html#conventional-routing).
|
|
11
|
+
:::
|
|
12
|
+
|
|
13
|
+
When the project structure is of the `Pages entry` type, the client-side routing configuration will be obtained by analyzing the files in the `src/pages` directory.
|
|
10
14
|
|
|
11
|
-
For example, the following directory:
|
|
15
|
+
For example, the following directory structure:
|
|
12
16
|
|
|
13
17
|
```bash
|
|
14
18
|
.
|
|
@@ -20,7 +24,7 @@ For example, the following directory:
|
|
|
20
24
|
└── info.jsx
|
|
21
25
|
```
|
|
22
26
|
|
|
23
|
-
The generated
|
|
27
|
+
The corresponding generated routing configuration is:
|
|
24
28
|
|
|
25
29
|
```bash
|
|
26
30
|
[
|
|
@@ -30,22 +34,22 @@ The generated route is configured as:
|
|
|
30
34
|
]
|
|
31
35
|
```
|
|
32
36
|
|
|
33
|
-
Files
|
|
37
|
+
Files under the pages directory that meet the following conditions will not be treated as routing files:
|
|
34
38
|
|
|
35
|
-
- suffix is not `.(j|t)sx?`.
|
|
36
|
-
- `.d.ts` type definition
|
|
37
|
-
-
|
|
39
|
+
- Files whose suffix is not `.(j|t)sx?`.
|
|
40
|
+
- `.d.ts` type definition files.
|
|
41
|
+
- Test files ending in `.test.(j|t)sx?` or `.spec.(j|t)sx?` or `.e2e.(j|t)sx?`.
|
|
38
42
|
|
|
39
43
|
:::tip
|
|
40
|
-
|
|
44
|
+
It is recommended to only write entry code in the pages directory and write business logic in the independent features directory outside the pages directory. In this way, most of the files under the pages directory will be routing files, and there is no need for additional filtering rules.
|
|
41
45
|
|
|
42
46
|
:::
|
|
43
47
|
|
|
44
48
|
### Dynamic Routing
|
|
45
49
|
|
|
46
|
-
|
|
50
|
+
If the directory name of the route file is named with [], the generated route will be used as a dynamic route.
|
|
47
51
|
|
|
48
|
-
For example the following directory structure:
|
|
52
|
+
For example, the following directory structure:
|
|
49
53
|
|
|
50
54
|
```bash
|
|
51
55
|
.
|
|
@@ -60,7 +64,7 @@ For example the following directory structure:
|
|
|
60
64
|
└── info.jsx
|
|
61
65
|
```
|
|
62
66
|
|
|
63
|
-
The generated
|
|
67
|
+
The corresponding generated routing configuration is:
|
|
64
68
|
|
|
65
69
|
```js
|
|
66
70
|
[
|
|
@@ -72,13 +76,13 @@ The generated route is configured as:
|
|
|
72
76
|
]
|
|
73
77
|
```
|
|
74
78
|
|
|
75
|
-
|
|
79
|
+
On the basis of dynamic routing, special routing suffixes `(*, ?, +)` can be added.
|
|
76
80
|
|
|
77
|
-
For example: `src/pages/users/[id]*.tsx`
|
|
81
|
+
For example: `src/pages/users/[id]*.tsx` will result in the route `/users/:id*`.
|
|
78
82
|
|
|
79
83
|
### Global Layout
|
|
80
84
|
|
|
81
|
-
When the entire
|
|
85
|
+
When the entire application needs a global `layout`, it can be achieved through `pages/_app.tsx`. The specific writing method is as follows:
|
|
82
86
|
|
|
83
87
|
```js
|
|
84
88
|
import React from 'react';
|
|
@@ -93,9 +97,9 @@ export const App = ({Component, ...pageProps}:{ Component: React.ComponentType})
|
|
|
93
97
|
}
|
|
94
98
|
```
|
|
95
99
|
|
|
96
|
-
The above `Component` is the component
|
|
100
|
+
The above `Component` is the component matched when accessing a specific route.
|
|
97
101
|
|
|
98
|
-
For example the following directory structure:
|
|
102
|
+
For example, the following directory structure:
|
|
99
103
|
|
|
100
104
|
```bash
|
|
101
105
|
.
|
|
@@ -107,26 +111,26 @@ For example the following directory structure:
|
|
|
107
111
|
└── index.js
|
|
108
112
|
```
|
|
109
113
|
|
|
110
|
-
-
|
|
111
|
-
-
|
|
112
|
-
-
|
|
114
|
+
- Accessing `/` corresponds to the `Component` component in `pages/index.js`.
|
|
115
|
+
- Accessing `/a` corresponds to the `Component` component in `pages/a/index.js`.
|
|
116
|
+
- Accessing `/a/b` corresponds to the `Component` component in `pages/a/b/index.js`.
|
|
113
117
|
|
|
114
|
-
:::tip Advantages
|
|
118
|
+
:::tip Advantages of global layout
|
|
115
119
|
|
|
116
|
-
-
|
|
117
|
-
-
|
|
118
|
-
-
|
|
119
|
-
-
|
|
120
|
+
- Preserve the state of the global layout when the page changes.
|
|
121
|
+
- Add global styles.
|
|
122
|
+
- ComponentDidCatch error handling.
|
|
123
|
+
- Use [defineConfig](/apis/app/runtime/app/define-config) to dynamically configure the runtime configuration.
|
|
120
124
|
|
|
121
125
|
:::
|
|
122
126
|
|
|
123
127
|
### Partial Layout
|
|
124
128
|
|
|
125
|
-
When developing an
|
|
129
|
+
When developing an application, there are scenarios where sub-routes under the same route share a layout.
|
|
126
130
|
|
|
127
|
-
For this
|
|
131
|
+
For this scenario, Modern.js conventionally has a similar effect to global layout when there is `_layout.js` under the directory.
|
|
128
132
|
|
|
129
|
-
For example the following directory structure:
|
|
133
|
+
For example, the following directory structure:
|
|
130
134
|
|
|
131
135
|
```bash
|
|
132
136
|
└── pages
|
|
@@ -147,18 +151,18 @@ const ALayout = ({ Component, ...pageProps }) => {
|
|
|
147
151
|
export default ALayout;
|
|
148
152
|
```
|
|
149
153
|
|
|
150
|
-
The Component
|
|
154
|
+
The `Component` parameter is the component corresponding to a specific accessed route, for example:
|
|
151
155
|
|
|
152
|
-
-
|
|
153
|
-
-
|
|
156
|
+
- Accessing `/a` corresponds to the `Component` component in `pages/a/index.js`.
|
|
157
|
+
- Accessing `/a/b` corresponds to the `Component` component in `pages/a/b/index.js`.
|
|
154
158
|
|
|
155
|
-
In this way,
|
|
159
|
+
In this way, `pages/a/_layout.js` can be used to meet the layout needs of shared routes under the `a` directory.
|
|
156
160
|
|
|
157
|
-
### 404
|
|
161
|
+
### 404 Route
|
|
158
162
|
|
|
159
|
-
|
|
163
|
+
`pages/404.[tj]sx` is conventionally the default 404 route.
|
|
160
164
|
|
|
161
|
-
For example the following directory structure:
|
|
165
|
+
For example, the following directory structure:
|
|
162
166
|
|
|
163
167
|
```bash
|
|
164
168
|
.
|
|
@@ -169,7 +173,7 @@ For example the following directory structure:
|
|
|
169
173
|
├── 404.js
|
|
170
174
|
```
|
|
171
175
|
|
|
172
|
-
|
|
176
|
+
The generated routing configuration is as follows:
|
|
173
177
|
|
|
174
178
|
```bash
|
|
175
179
|
[
|
|
@@ -179,4 +183,4 @@ the generated route is configured is as:
|
|
|
179
183
|
]
|
|
180
184
|
```
|
|
181
185
|
|
|
182
|
-
All unmatched routes will
|
|
186
|
+
All unmatched routes will be matched to `pages/404.[tj]s`.
|
|
@@ -4,13 +4,13 @@ sidebar_position: 2
|
|
|
4
4
|
---
|
|
5
5
|
# routes/
|
|
6
6
|
|
|
7
|
-
The entry
|
|
7
|
+
The identifier for the entry point when the application uses [Conventional Routing](guides/basic-features/routes.html#conventional-routing).
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
Conventional routing uses `routes/` as the convention for the entry point and analyzes the files in the `src/routes` directory to obtain the client-side routing configuration.
|
|
10
10
|
|
|
11
|
-
Any `layout.[tj]sx` and `page.[tj]sx` under `src/routes` will be used as
|
|
11
|
+
Any `layout.[tj]sx` and `page.[tj]sx` under `src/routes` will be used as the application's routes:
|
|
12
12
|
|
|
13
|
-
```bash {3}
|
|
13
|
+
```bash {3,4}
|
|
14
14
|
.
|
|
15
15
|
└── routes
|
|
16
16
|
├── layout.tsx
|
|
@@ -20,9 +20,9 @@ Any `layout.[tj]sx` and `page.[tj]sx` under `src/routes` will be used as a route
|
|
|
20
20
|
└── page.tsx
|
|
21
21
|
```
|
|
22
22
|
|
|
23
|
-
##
|
|
23
|
+
## Basic Example
|
|
24
24
|
|
|
25
|
-
The directory
|
|
25
|
+
The directory name under `routes` will be used as the mapping of the route URL. `layout.tsx` is used as the layout component and `page.tsx` is used as the content component in the routing. They are the leaf nodes of the entire route. For example, the following directory structure:
|
|
26
26
|
|
|
27
27
|
```bash
|
|
28
28
|
.
|
|
@@ -32,14 +32,14 @@ The directory names in the `routes` directory will be used as a mapping of the r
|
|
|
32
32
|
└── page.tsx
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
will generate two routes:
|
|
36
36
|
|
|
37
37
|
- `/`
|
|
38
38
|
- `/user`
|
|
39
39
|
|
|
40
|
-
## Dynamic
|
|
40
|
+
## Dynamic Routing
|
|
41
41
|
|
|
42
|
-
If the directory name of the route file is named with `[]`, the generated route will be used as a dynamic route. For example, the following file
|
|
42
|
+
If the directory name of the route file is named with `[]`, the generated route will be used as a dynamic route. For example, the following file directory:
|
|
43
43
|
|
|
44
44
|
```
|
|
45
45
|
└── routes
|
|
@@ -50,35 +50,15 @@ If the directory name of the route file is named with `[]`, the generated route
|
|
|
50
50
|
└── page.tsx
|
|
51
51
|
```
|
|
52
52
|
|
|
53
|
-
The `routes/[id]/page.tsx` file will be converted to
|
|
53
|
+
The `routes/[id]/page.tsx` file will be converted to the `/:id` route. Except for the `/blog` route that can be matched exactly, all other `/xxx` routes will be matched to this route.
|
|
54
54
|
|
|
55
|
-
In the component, you can
|
|
55
|
+
In the component, you can use [useParams](/apis/app/runtime/router/router#useparams) to obtain the corresponding named parameter.
|
|
56
56
|
|
|
57
|
-
|
|
57
|
+
When using the [loader](/guides/basic-features/data-fetch#the-loader-function) function to obtain data, `params` will be passed as an input parameter to the `loader` function, and the corresponding parameter can be obtained through the attribute of `params`.
|
|
58
58
|
|
|
59
|
-
##
|
|
59
|
+
## Layout Component
|
|
60
60
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
```
|
|
64
|
-
└── routes
|
|
65
|
-
├── user
|
|
66
|
-
│ └── [id$]
|
|
67
|
-
│ └── page.tsx
|
|
68
|
-
├── blog
|
|
69
|
-
│ └── page.tsx
|
|
70
|
-
└── page.tsx
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
The `routes/user/[id$]/page.tsx` file will be converted to the `/user/:id?` route. All routes under `/user` will match this route, and the `id` parameter is optional. This route is commonly used to differentiate between **creating** and **editing**.
|
|
74
|
-
|
|
75
|
-
In the component, you can get the corresponding named parameters using [useParams](/apis/app/runtime/router/router#useparams).
|
|
76
|
-
|
|
77
|
-
In the loader, params will be passed as an argument to the [loader](/guides/basic-features/data-fetch#loader-函数), and you can get them through `params.xxx`.
|
|
78
|
-
|
|
79
|
-
## Layout component
|
|
80
|
-
|
|
81
|
-
As in the example below, you can add a common layout component for all routing components by adding `layout.tsx`
|
|
61
|
+
In the following example, a common layout component can be added to all route components by adding `layout.tsx`:
|
|
82
62
|
|
|
83
63
|
```bash
|
|
84
64
|
.
|
|
@@ -90,7 +70,7 @@ As in the example below, you can add a common layout component for all routing c
|
|
|
90
70
|
└── page.tsx
|
|
91
71
|
```
|
|
92
72
|
|
|
93
|
-
|
|
73
|
+
In the layout component, you can use `<Outlet>` to represent the child components:
|
|
94
74
|
|
|
95
75
|
```tsx title=routes/layout.tsx
|
|
96
76
|
import { Link, Outlet, useLoaderData } from '@modern-js/runtime/router';
|
|
@@ -105,6 +85,6 @@ export default () => {
|
|
|
105
85
|
```
|
|
106
86
|
|
|
107
87
|
:::note
|
|
108
|
-
`<Outlet>` is a new API in React Router 6, see [Outlet]
|
|
88
|
+
`<Outlet>` is a new API in React Router 6. For details, see [Outlet](https://reactrouter.com/en/main/components/outlet#outlet).
|
|
109
89
|
|
|
110
90
|
:::
|
|
@@ -4,4 +4,4 @@ sidebar_position: 8
|
|
|
4
4
|
---
|
|
5
5
|
# *.[server|node].[tj]sx
|
|
6
6
|
|
|
7
|
-
Used in application
|
|
7
|
+
Used in the application project to place server-side code. When `*.tsx` and `*.[server|node].tsx` coexist, SSR will prefer to use the `*.[server|node].tsx` file instead of the `*.tsx` file when rendering on the server.
|
|
@@ -4,11 +4,14 @@ sidebar_position: 7
|
|
|
4
4
|
---
|
|
5
5
|
# **/*.stories.[tj]sx
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Application project Storybook files.
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
You can create files in the `*.stories.[tj]sx` format in the project source code directory `src/` as Storybook files.
|
|
10
|
+
|
|
11
|
+
Run the `npm run dev story` command in the project to use these files to debug relevant content in Storybook.
|
|
10
12
|
|
|
11
13
|
:::info
|
|
12
|
-
|
|
14
|
+
To use Storybook, you need to enable the "Visual Testing (Storybook)" mode by running the `new` command in the project first.
|
|
13
15
|
|
|
14
16
|
:::
|
|
17
|
+
|
|
@@ -2,13 +2,14 @@
|
|
|
2
2
|
title: '**/*.test.[tj]sx?'
|
|
3
3
|
sidebar_position: 6
|
|
4
4
|
---
|
|
5
|
+
|
|
5
6
|
# **/*.test.[tj]sx?
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
Application project test files.
|
|
8
9
|
|
|
9
|
-
The
|
|
10
|
+
The application project supports creating files with the suffix `.test.[tj]sx?` in the project source code directory (`src`) to write test cases.
|
|
10
11
|
|
|
11
12
|
:::info
|
|
12
|
-
To use unit
|
|
13
|
+
To use unit testing and integration testing, you need to enable the "Unit Testing / Integration Testing" feature by running the `new` command in the project first.
|
|
13
14
|
|
|
14
15
|
:::
|
|
@@ -24,8 +24,8 @@ Before using this API, please execute `pnpm run new` to create a new "Custom Web
|
|
|
24
24
|
|
|
25
25
|
```bash
|
|
26
26
|
pnpm run new
|
|
27
|
-
?
|
|
28
|
-
? New "Custom Web Server" source code directory
|
|
27
|
+
? Please select the operation you want: Create Elements
|
|
28
|
+
? Please select the type of element to create: New "Custom Web Server" source code directory
|
|
29
29
|
```
|
|
30
30
|
|
|
31
31
|
:::
|
|
@@ -27,8 +27,8 @@ Before using this API, please execute `pnpm run new` to create a new "Custom Web
|
|
|
27
27
|
|
|
28
28
|
```bash
|
|
29
29
|
pnpm run new
|
|
30
|
-
?
|
|
31
|
-
? New "Custom Web Server" source code directory
|
|
30
|
+
? Please select the operation you want: Create Element
|
|
31
|
+
? Please select the type of element to create: New "Custom Web Server" source code directory
|
|
32
32
|
```
|
|
33
33
|
|
|
34
34
|
:::
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
Modern.js generator will provide an interactive Q & A interface, initialization items according to the result, according to the default selection:
|
|
2
2
|
|
|
3
3
|
```bash
|
|
4
|
-
? Please select the
|
|
5
|
-
?
|
|
6
|
-
?
|
|
4
|
+
? Please select the type of project you want to create: Web App
|
|
5
|
+
? Please select the programming language: TS
|
|
6
|
+
? Please select the package manager: pnpm
|
|
7
7
|
```
|
|
8
8
|
|
|
9
9
|
After create the project, Modern.js automatically installs dependency and creates a git repository.
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
```bash
|
|
2
2
|
$ npx @modern-js/create@latest myapp
|
|
3
|
-
? Please select the
|
|
4
|
-
?
|
|
5
|
-
?
|
|
6
|
-
?
|
|
3
|
+
? Please select the type of project you want to create: Web App
|
|
4
|
+
? Please select the programming language: TS
|
|
5
|
+
? Please select the package manager: pnpm
|
|
6
|
+
? Please select the bundler: Rspack
|
|
7
7
|
```
|
|
@@ -9,8 +9,8 @@ Modern.js also provides a runtime Polyfill solution based on browser [UA](https:
|
|
|
9
9
|
exec `pnpm run new` to enable this features:
|
|
10
10
|
|
|
11
11
|
```bash
|
|
12
|
-
?
|
|
13
|
-
?
|
|
12
|
+
? Please select the operation you want: Enable features
|
|
13
|
+
? Please select the feature name: Enable UA-based Polyfill Feature
|
|
14
14
|
```
|
|
15
15
|
|
|
16
16
|
After executing the command, register the Polyfill plugin in `modern.config.ts`:
|
|
@@ -22,8 +22,8 @@ When using Rspack as the bundler, SWC will be used for transpiling and compressi
|
|
|
22
22
|
First, you need to execute `pnpm run new` to enable the SWC compile:
|
|
23
23
|
|
|
24
24
|
```bash
|
|
25
|
-
?
|
|
26
|
-
?
|
|
25
|
+
? Please select the operation you want: Enable features
|
|
26
|
+
? Please select the feature name: Enable SWC Compile
|
|
27
27
|
```
|
|
28
28
|
|
|
29
29
|
After the installation, please register the SWC plugin in the `modern.config.ts` file, then the SWC compilation and compression will be enabled.
|
|
@@ -6,7 +6,7 @@ title: Frameworks
|
|
|
6
6
|
|
|
7
7
|
Modern.js's BFF supports different runtime frameworks, currently Modern.js's BFF supports two runtime frameworks[Express.js](https://expressjs.com/) 和 [Koa.js](https://koajs.com/).
|
|
8
8
|
|
|
9
|
-
## Function
|
|
9
|
+
## Function Mode
|
|
10
10
|
|
|
11
11
|
Under the function writing, only the middleware writing method of various runtime frameworks is different, and other implementations are basically the same. Take Express as an example to introduce how to write a middleware by hand in the `api/_ app.ts` and add permission verification:
|
|
12
12
|
|
|
@@ -96,7 +96,7 @@ Refresh the page and you can see that `/api/hello` was accessed successfully:
|
|
|
96
96
|
|
|
97
97
|
The above code simulates the way to add middleware to the `/api/_app.ts` to achieve an easy login function. Also, other functions can be implemented in this hook file to extend BFF Server.
|
|
98
98
|
|
|
99
|
-
## Framework
|
|
99
|
+
## Framework Mode
|
|
100
100
|
|
|
101
101
|
Under the framework writing, Modern.js does not collect middleware in the `api/_app.ts`, and the running process is controlled by the plugin itself.
|
|
102
102
|
|
|
@@ -51,10 +51,10 @@ Execute `pnpm run dev`, then open `http://localhost:8080/` to see that the page
|
|
|
51
51
|
|
|
52
52
|
In Modern.js, the BFF function routing system is implemented based on the file system, and it is also a conventional routing system.
|
|
53
53
|
|
|
54
|
-
In **Function
|
|
54
|
+
In **Function Mode**, All files under `api/` will map to an interface. In **Framework Mode**, All files under `api/lambda` will map to an interface
|
|
55
55
|
|
|
56
56
|
:::note
|
|
57
|
-
Function
|
|
57
|
+
Function Mode & Framework Mode will introduce soon.
|
|
58
58
|
|
|
59
59
|
:::
|
|
60
60
|
|
|
@@ -69,14 +69,14 @@ Files named `index.[jt]s` are mapped to the previous directory.
|
|
|
69
69
|
- `api/index.ts` -> `{prefix}/`
|
|
70
70
|
- `api/user/index.ts` -> `{prefix}/user`
|
|
71
71
|
|
|
72
|
-
### Multi-layer
|
|
72
|
+
### Multi-layer Routing
|
|
73
73
|
|
|
74
74
|
Supports parsing nested files, if you create a nested folder structure, the files will still automatically parse routes in the same way.
|
|
75
75
|
|
|
76
76
|
- `api/hello.ts` -> `{prefix}/hello`
|
|
77
77
|
- `api/user/list.ts` -> `{prefix}/user/list`
|
|
78
78
|
|
|
79
|
-
### Dynamic
|
|
79
|
+
### Dynamic Routing
|
|
80
80
|
|
|
81
81
|
Create folders or files named with `[xxx]` to support dynamic named routing parameters.
|
|
82
82
|
|
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
---
|
|
2
2
|
sidebar_position: 2
|
|
3
|
-
title:
|
|
3
|
+
title: BFF Type
|
|
4
4
|
---
|
|
5
|
-
#
|
|
5
|
+
# BFF Type
|
|
6
6
|
|
|
7
7
|
Runtime framework support is also an important part of **BFF**. Modern.js supports extending BFF's runtime framework through plugins, and provides a series of built-in plugins, developers can directly use the conventions and ecology of the framework.
|
|
8
8
|
|
|
9
|
-
The plugin is compatible with most of the specifications of these frameworks, and each framework needs to provide two types of ways to extend the writing of BFF functions, namely **Function
|
|
9
|
+
The plugin is compatible with most of the specifications of these frameworks, and each framework needs to provide two types of ways to extend the writing of BFF functions, namely **Function Mode** and **Framework Mode**.
|
|
10
10
|
|
|
11
11
|
:::note
|
|
12
12
|
Whether the current `api/` directory structure is written as a framework is determined by the corresponding plugin, Modern.js don't care.
|
|
13
13
|
|
|
14
14
|
:::
|
|
15
15
|
|
|
16
|
-
## Function
|
|
16
|
+
## Function Mode
|
|
17
17
|
|
|
18
18
|
When the plugin considers that it is currently written as a function, it must support writing middleware in the `api/_ app.ts` to extend the BFF function.
|
|
19
19
|
|
|
@@ -32,7 +32,7 @@ The writing of middleware for different plugins is not the same, see [Runtime Fr
|
|
|
32
32
|
|
|
33
33
|
:::
|
|
34
34
|
|
|
35
|
-
## Framework
|
|
35
|
+
## Framework Mode
|
|
36
36
|
|
|
37
37
|
Framework writing is a way of using frame structure to extend BFF functions. Compared with function writing, although frame writing can use more frame structure and make the entire BFF Server clearer in complex scenarios, it is also more complex and requires more attention to the content at the framework level.
|
|
38
38
|
|
|
@@ -36,8 +36,8 @@ You can enable Rspack build by running `pnpm run new`:
|
|
|
36
36
|
|
|
37
37
|
```bash
|
|
38
38
|
$ pnpm run new
|
|
39
|
-
?
|
|
40
|
-
?
|
|
39
|
+
? Please select the operation you want: Enable features
|
|
40
|
+
? Please select the feature name: Enable Rspack Build
|
|
41
41
|
```
|
|
42
42
|
|
|
43
43
|
After executing the command, enable the Rspack build in `modern.config.ts`:
|
|
@@ -9,8 +9,8 @@ Static Site Generation is a solution for rendering complete static web pages at
|
|
|
9
9
|
First need to execute `pnpm run new` to enable the SSG features:
|
|
10
10
|
|
|
11
11
|
```bash
|
|
12
|
-
?
|
|
13
|
-
?
|
|
12
|
+
? Please select the operation you want: Enable features
|
|
13
|
+
? Please select the feature name: Enable SSG
|
|
14
14
|
```
|
|
15
15
|
|
|
16
16
|
After execute script, register SSG plugin in `modern.config.ts`:
|
|
@@ -9,8 +9,8 @@ Modern.js integrates the testing capabilities of [Jest](https://jestjs.io/) by d
|
|
|
9
9
|
First need to execute `pnpm run new` enable [unit test/integration test] features:
|
|
10
10
|
|
|
11
11
|
```
|
|
12
|
-
?
|
|
13
|
-
?
|
|
12
|
+
? Please select the operation you want: Enable features
|
|
13
|
+
? Please select the feature name: Enable Unit Test / Integration Test
|
|
14
14
|
```
|
|
15
15
|
|
|
16
16
|
After executing the above command, the `"test": "modern test"` command will be automatically generated in package.json.
|
|
@@ -27,8 +27,8 @@ Because the full web server cannot be controlled this way, and the extension log
|
|
|
27
27
|
You can execute the'pnpm run new 'command in the project root directory to enable the "Custom Web Serve" function:
|
|
28
28
|
|
|
29
29
|
```bash
|
|
30
|
-
?
|
|
31
|
-
? New "Custom Web Server" source code directory
|
|
30
|
+
? Please select the operation you want: Create Element
|
|
31
|
+
? Please select the type of element to create: New "Custom Web Server" source code directory
|
|
32
32
|
```
|
|
33
33
|
|
|
34
34
|
After executing the command, register the `@modern-js/plugin-server` plugin in `modern.config.ts`:
|
|
@@ -66,8 +66,8 @@ If you need to use other CSS-in-JS libraries such as [styled-jsx](https://www.np
|
|
|
66
66
|
Follow the steps below to make a selection:
|
|
67
67
|
|
|
68
68
|
```bash
|
|
69
|
-
?
|
|
70
|
-
?
|
|
69
|
+
? Please select the operation you want: Enable features
|
|
70
|
+
? Please select the feature name: Enable Tailwind CSS
|
|
71
71
|
```
|
|
72
72
|
|
|
73
73
|
Register the Tailwind plugin in `modern.config.ts`:
|
|
@@ -182,7 +182,7 @@ Routes generated from file directories named with `[$]` will be treated as dynam
|
|
|
182
182
|
|
|
183
183
|
The `routes/user/[id$]/page.tsx` file will be converted to the `/user/:id?` route. All routes under `/user` will match this route, and the `id` parameter is optional. This route is usually used to distinguish between **creation** and **editing**.
|
|
184
184
|
|
|
185
|
-
In the component, you can use [useParams](/apis/app/runtime/router/router#useparams) to
|
|
185
|
+
In the component, you can use [useParams](/apis/app/runtime/router/router#useparams) to get the corresponding named parameter.
|
|
186
186
|
|
|
187
187
|
In the loader, params will be passed as the input parameter of the [loader function](/guides/basic-features/data-fetch#loader-function), and you can get the parameter value through `params.xxx`.
|
|
188
188
|
|
|
@@ -191,7 +191,7 @@ In the loader, params will be passed as the input parameter of the [loader funct
|
|
|
191
191
|
If you create a `$.tsx` file under the routes directory, it will be treated as the catch-all routing component. When there is no matching route, this component will be rendered.
|
|
192
192
|
|
|
193
193
|
:::note
|
|
194
|
-
`$.tsx` can be considered as a special `page` route component. When there is a `layout`
|
|
194
|
+
`$.tsx` can be considered as a special `page` route component. When there is a `layout.tsx` file in the current directory, `$.tsx` will be rendered as a child component of `layout`.
|
|
195
195
|
:::
|
|
196
196
|
|
|
197
197
|
For example, the following directory structure:
|
|
@@ -215,7 +215,7 @@ params['*']; // => 'aaa/bbb'
|
|
|
215
215
|
|
|
216
216
|
### No-path Layout
|
|
217
217
|
|
|
218
|
-
When the directory name starts with `__`, the
|
|
218
|
+
When the directory name starts with `__`, the directory name will not be converted to an actual route path. For example, the following file directory:
|
|
219
219
|
|
|
220
220
|
```bash
|
|
221
221
|
.
|
|
@@ -449,7 +449,7 @@ However, this also brings a problem: if the chunk corresponding to the route has
|
|
|
449
449
|
|
|
450
450
|
In this case, you can define a `Loading` component to display a custom `Loading` component before the static resources are loaded.
|
|
451
451
|
|
|
452
|
-
To further improve the user experience and reduce loading time, Modern.js supports defining the `prefetch` attribute on the Link component to preload static resources and data.
|
|
452
|
+
To further improve the user experience and reduce loading time, Modern.js supports defining the `prefetch` attribute on the Link component to preload static resources and data.
|
|
453
453
|
|
|
454
454
|
```tsx
|
|
455
455
|
<Link prefetch="intent" to="page">
|
|
@@ -461,6 +461,8 @@ To further improve the user experience and reduce loading time, Modern.js suppor
|
|
|
461
461
|
|
|
462
462
|
:::
|
|
463
463
|
|
|
464
|
+
The `prefetch` attribute has three optional values:
|
|
465
|
+
|
|
464
466
|
- `none`: default value, no prefetching, no additional behavior.
|
|
465
467
|
- `intent`: This is the value we recommend for most scenarios. When you hover over the Link, the corresponding chunk and data defined in the data loader will be loaded automatically. When you move the mouse away, the loading will be cancelled automatically. In our tests, even fast clicks can reduce loading time by about 200ms.
|
|
466
468
|
- `render`: The corresponding chunk and data defined in the Data Loader will be loaded when the Link component is rendered.
|
|
@@ -504,8 +506,6 @@ export default () => {
|
|
|
504
506
|
Modern.js provides a series of optimizations for resource loading and rendering for conventional routing by default, and provides out-of-the-box SSR capabilities. When using self-controlled routing, developers need to encapsulate these capabilities themselves. It is recommended to use conventional routing.
|
|
505
507
|
:::
|
|
506
508
|
|
|
507
|
-
When using self-controlled routing, if the developer turns off the [`runtime.router`](/configure/app/runtime/router) configuration and uses `react-router-dom` directly, they also need to wrap it according to the React Router documentation.
|
|
508
|
-
|
|
509
509
|
## Other
|
|
510
510
|
|
|
511
511
|
By default, Modern.js will enable the built-in routing scheme, which is React Router.
|
|
@@ -518,7 +518,9 @@ export default defineConfig({
|
|
|
518
518
|
});
|
|
519
519
|
```
|
|
520
520
|
|
|
521
|
-
Modern.js
|
|
521
|
+
As mentioned above, when the [`runtime.router`](/configure/app/runtime/router) configuration is enabled, Modern.js will export the API of React Router from the `@modern-js/runtime/router` namespace for developers to use, ensuring that developers and Modern.js are using the same code, and automatically wrapping the `Provider` component according to the router configuration. In addition, in this case, the code of React Router will be packed into the JS output.
|
|
522
|
+
|
|
523
|
+
If the project already has its own routing plan or does not need to use client-side routing, this feature can be disabled.
|
|
522
524
|
|
|
523
525
|
```js
|
|
524
526
|
export default defineConfig({
|
|
@@ -527,3 +529,5 @@ export default defineConfig({
|
|
|
527
529
|
},
|
|
528
530
|
});
|
|
529
531
|
```
|
|
532
|
+
|
|
533
|
+
As mentioned above, if the [`runtime.router`](/configure/app/runtime/router) configuration is disabled and `react-router-dom` is used directly for project routing management, the `Provider` needs to be wrapped according to the React Router documentation.
|
|
@@ -33,9 +33,9 @@ The project initialized by Modern.js is single-entry (SPA), and the project stru
|
|
|
33
33
|
Modern.js can easily switch from single entry to multiple entry. You can execute `pnpm run new` under the project to create entry through generator:
|
|
34
34
|
|
|
35
35
|
```bash
|
|
36
|
-
?
|
|
37
|
-
?
|
|
38
|
-
?
|
|
36
|
+
? Please select the operation you want: Create Element
|
|
37
|
+
? Please select the type of element to create: New "entry"
|
|
38
|
+
? Please fill in the entry name: new-entry
|
|
39
39
|
```
|
|
40
40
|
|
|
41
41
|
After execution, Modern.js will automatically generate a new entry directory, and you can see that the `src/` directory has the following structure:
|