@modern-js/main-doc 0.0.0-next-1685369216913 → 0.0.0-next-1685418932858
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 +4 -4
- 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/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/basic-features/routes.mdx +11 -7
- 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/guides/basic-features/routes.mdx +7 -4
- 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
|
:::
|
|
@@ -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
|
|
|
@@ -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.
|
|
@@ -4,7 +4,7 @@ sidebar_position: 1
|
|
|
4
4
|
---
|
|
5
5
|
# **/*.[tj]s
|
|
6
6
|
|
|
7
|
-
在 BFF
|
|
7
|
+
在 [BFF 函数写法](/guides/advanced-features/bff/type.html#函数写法)下,声明 API 路由的文件。除了[某些约定文件](/apis/app/hooks/api/api#白名单)外,`api` 目录下的文件会被注册为接口的路由。
|
|
8
8
|
|
|
9
9
|
:::info
|
|
10
10
|
使用 `api` 目录需要开启 BFF 功能,需要在项目下执行 new 命令启用「BFF」功能。
|
|
@@ -31,7 +31,7 @@ sidebar_position: 1
|
|
|
31
31
|
|
|
32
32
|
### 动态路由
|
|
33
33
|
|
|
34
|
-
|
|
34
|
+
路由系统支持通过 `[]` 命名的文件目录生成动态路由。
|
|
35
35
|
|
|
36
36
|
- `api/user/[username]/info.ts` -> `$BASENAME/user/:username/info`
|
|
37
37
|
- `api/user/[username]/delete.ts` -> `$BASENAME/user/:username/delete`
|
|
@@ -64,7 +64,7 @@ export const get = async () => {
|
|
|
64
64
|
};
|
|
65
65
|
```
|
|
66
66
|
|
|
67
|
-
这样导出函数,则会得到一个 `
|
|
67
|
+
这样导出函数,则会得到一个 `GET` 接口。
|
|
68
68
|
|
|
69
69
|
应用工程中支持了 9 个 Method 定义,即:`GET`、`POST`、`PUT`、`DELETE`、`CONNECT`、`TRACE`、`PATCH`、`OPTION`、`HEAD`,即可以用这些 Method 作为函数导出的名字。
|
|
70
70
|
|
|
@@ -75,6 +75,6 @@ export const get = async () => {
|
|
|
75
75
|
可以在一个文件中定义多个不同 Method 的函数,但如果定义多个相同 Method 的函数,则只有第一个会生效。
|
|
76
76
|
|
|
77
77
|
:::info
|
|
78
|
-
|
|
78
|
+
需要注意的是,定义的函数都应该是异步的,这个与函数调用时类型有关。
|
|
79
79
|
|
|
80
80
|
:::
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
---
|
|
2
2
|
title: lambda/*.[tj]s
|
|
3
|
-
sidebar_position:
|
|
3
|
+
sidebar_position: 3
|
|
4
4
|
---
|
|
5
5
|
# lambda/*.[tj]s
|
|
6
6
|
|
|
7
|
-
在 BFF
|
|
7
|
+
在 [BFF 框架写法](/guides/advanced-features/bff/type.html#框架写法)下,声明 API 路由的文件。除了[某些约定文件](/apis/app/hooks/api/lambda#白名单)外,`lambda/` 目录下的文件会被注册为接口的路由。
|
|
8
8
|
|
|
9
9
|
:::info
|
|
10
10
|
使用 `api` 目录需要开启 BFF 功能,需要在项目下执行 new 命令启用「BFF」功能。
|
|
@@ -34,7 +34,7 @@ sidebar_position: 1
|
|
|
34
34
|
|
|
35
35
|
### 动态路由
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
路由系统支持通过 `[]` 命名的文件目录生成动态路由。
|
|
38
38
|
|
|
39
39
|
- `api/lambda/user/[username]/info.ts` -> `$BASENAME/user/:username/info`
|
|
40
40
|
- `api/lambda/user/[username]/delete.ts` -> `$BASENAME/user/:username/delete`
|
|
@@ -44,7 +44,7 @@ sidebar_position: 1
|
|
|
44
44
|
|
|
45
45
|
### 白名单
|
|
46
46
|
|
|
47
|
-
默认 `
|
|
47
|
+
默认 `lambda` 目录下所有文件都会当作 BFF 函数文件去解析,但同样我们也设置了白名单,这些文件不被被解析:
|
|
48
48
|
|
|
49
49
|
- 命名以 `_` 开头的文件。例如:`_utils.ts`。
|
|
50
50
|
- 命名以 `_` 开头的文件夹下所有文件。例如:`_utils/index.ts`、`_utils/cp.ts`。
|
|
@@ -54,4 +54,4 @@ sidebar_position: 1
|
|
|
54
54
|
|
|
55
55
|
## 函数定义
|
|
56
56
|
|
|
57
|
-
和函数写法下[函数定义](/apis/app/hooks/api/
|
|
57
|
+
和函数写法下[函数定义](/apis/app/hooks/api/api#函数定义)完全一致。
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
---
|
|
2
2
|
title: test.[tj]s
|
|
3
|
-
sidebar_position:
|
|
3
|
+
sidebar_position: 4
|
|
4
4
|
---
|
|
5
5
|
# test.[tj]s
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
The BFF test file of the application supports writing test cases in files with the suffix `.test.[tj]sx?` under the `api/` directory.
|
|
8
8
|
|
|
9
9
|
:::info
|
|
10
|
-
|
|
10
|
+
Enabling unit testing and integration testing requires running the new command to enable the "Unit Testing/Integration Testing" function under the project first.
|
|
11
11
|
|
|
12
12
|
:::
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
---
|
|
2
|
-
title: icon
|
|
2
|
+
title: icon/
|
|
3
3
|
sidebar_position: 2
|
|
4
4
|
---
|
|
5
5
|
|
|
6
|
-
#
|
|
6
|
+
# icon/
|
|
7
7
|
|
|
8
|
-
##
|
|
8
|
+
## Favicon
|
|
9
9
|
|
|
10
10
|
当项目根目录的 `config` 目录下存在 `favicon.*` 文件时,Modern.js 会自动将该文件设置到 [html.favicon](/configure/app/html/favicon) 配置项中,用于生成页面的 favicon 图标:
|
|
11
11
|
|
|
@@ -24,15 +24,15 @@ sidebar_position: 2
|
|
|
24
24
|
|
|
25
25
|
在设置 app icon 时,Modern.js 会按以下顺序寻找文件:
|
|
26
26
|
|
|
27
|
-
- favicon.png
|
|
28
|
-
- favicon.jpg
|
|
29
|
-
- favicon.jpeg
|
|
30
|
-
- favicon.svg
|
|
31
|
-
- favicon.ico
|
|
27
|
+
- `favicon.png`
|
|
28
|
+
- `favicon.jpg`
|
|
29
|
+
- `favicon.jpeg`
|
|
30
|
+
- `favicon.svg`
|
|
31
|
+
- `favicon.ico`
|
|
32
32
|
|
|
33
|
-
##
|
|
33
|
+
## Apple Touch Icon
|
|
34
34
|
|
|
35
|
-
当项目根目录的 `config` 目录下存在 `icon.*` 文件时,Modern.js 会自动将该文件设置到 [html.appIcon](/configure/app/html/app-icon) 配置项中,用于生成 iOS 系统下的
|
|
35
|
+
当项目根目录的 `config` 目录下存在 `icon.*` 文件时,Modern.js 会自动将该文件设置到 [html.appIcon](/configure/app/html/app-icon) 配置项中,用于生成 iOS 系统下的 Apple Touch Icon 图标。
|
|
36
36
|
|
|
37
37
|
```
|
|
38
38
|
./config
|
|
@@ -49,8 +49,8 @@ sidebar_position: 2
|
|
|
49
49
|
|
|
50
50
|
在设置 app icon 时,Modern.js 会按以下顺序寻找文件:
|
|
51
51
|
|
|
52
|
-
- icon.png
|
|
53
|
-
- icon.jpg
|
|
54
|
-
- icon.jpeg
|
|
55
|
-
- icon.svg
|
|
56
|
-
- icon.ico
|
|
52
|
+
- `icon.png`
|
|
53
|
+
- `icon.jpg`
|
|
54
|
+
- `icon.jpeg`
|
|
55
|
+
- `icon.svg`
|
|
56
|
+
- `icon.ico`
|