@modern-js/main-doc 0.0.0-next-1686559924703 → 0.0.0-next-1686578839599
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 +6 -4
- package/docs/en/configure/app/bff/enable-handle-web.mdx +5 -5
- package/docs/en/configure/app/bff/prefix.mdx +6 -6
- package/docs/en/configure/app/bff/proxy.mdx +30 -19
- package/docs/en/configure/app/server/base-url.mdx +4 -4
- package/docs/en/configure/app/server/enable-framework-ext.mdx +4 -4
- package/docs/en/configure/app/server/port.mdx +6 -6
- package/docs/en/configure/app/server/public-routes.mdx +5 -5
- package/docs/en/configure/app/server/routes.mdx +13 -13
- package/docs/en/configure/app/server/ssr-by-entries.mdx +4 -4
- package/docs/en/configure/app/server/ssr.mdx +5 -5
- package/docs/en/guides/advanced-features/code-split.mdx +15 -14
- package/docs/zh/configure/app/bff/enable-handle-web.mdx +3 -3
- package/docs/zh/configure/app/bff/prefix.mdx +3 -2
- package/docs/zh/configure/app/bff/proxy.mdx +21 -11
- package/docs/zh/configure/app/server/base-url.mdx +1 -1
- package/docs/zh/configure/app/server/public-routes.mdx +2 -2
- package/docs/zh/configure/app/server/routes.mdx +2 -2
- package/docs/zh/configure/app/server/ssr-by-entries.mdx +3 -3
- package/docs/zh/configure/app/server/ssr.mdx +1 -1
- package/docs/zh/guides/advanced-features/code-split.mdx +13 -12
- package/package.json +5 -5
- package/docs/en/components/bff-proxy-path-rewrite.mdx +0 -16
- package/docs/en/components/bff-proxy-principle.mdx +0 -1
- package/docs/en/components/enable-bff-caution.mdx +0 -3
- package/docs/zh/components/bff-proxy-path-rewrite.mdx +0 -16
- package/docs/zh/components/bff-proxy-principle.mdx +0 -1
- package/docs/zh/components/enable-bff-caution.mdx +0 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @modern-js/main-doc
|
|
2
2
|
|
|
3
|
-
## 0.0.0-next-
|
|
3
|
+
## 0.0.0-next-1686578839599
|
|
4
4
|
|
|
5
5
|
### Patch Changes
|
|
6
6
|
|
|
@@ -24,12 +24,14 @@
|
|
|
24
24
|
|
|
25
25
|
docs(main): 更新 HTML 模板文档
|
|
26
26
|
|
|
27
|
-
-
|
|
27
|
+
- ef865f285: docs(main): update config server doc
|
|
28
28
|
|
|
29
|
-
docs(main): 更新
|
|
29
|
+
docs(main): 更新 server 配置文档
|
|
30
30
|
|
|
31
|
+
- 8bb0e146f: chore: review code split docs
|
|
32
|
+
chore: 审查代码分割文档
|
|
31
33
|
- Updated dependencies [7e6fb5fc1]
|
|
32
|
-
- @modern-js/builder-doc@0.0.0-next-
|
|
34
|
+
- @modern-js/builder-doc@0.0.0-next-1686578839599
|
|
33
35
|
|
|
34
36
|
## 2.22.1
|
|
35
37
|
|
|
@@ -7,13 +7,13 @@ title: enableHandleWeb
|
|
|
7
7
|
- **Type:** `boolean`
|
|
8
8
|
- **Default:** `false`
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
:::caution
|
|
11
|
+
First you need to enable the "BFF" function using [new](/apis/app/commands#modern-new) command.
|
|
12
|
+
:::
|
|
11
13
|
|
|
12
|
-
|
|
14
|
+
By default, the BFF service can only handle requests from the BFF API.
|
|
13
15
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
When this value is set to `true`, page request will also pass through BFF, and the default logic for page rendering built into Modern.js will run as the last middleware of the BFF service.
|
|
16
|
+
When this value is set to `true`, page request traffic also goes through the BFF, and the logic built into Modern.js for page rendering defaults to running as the last middleware for the BFF service.
|
|
17
17
|
|
|
18
18
|
```ts title="modern.config.ts"
|
|
19
19
|
export default defineConfig({
|
|
@@ -7,18 +7,18 @@ sidebar_label: prefix
|
|
|
7
7
|
- **Type:** `string`
|
|
8
8
|
- **Default:** `/api`
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
:::caution
|
|
11
|
+
First you need to enable the "BFF" function using [new](/apis/app/commands#modern-new) command.
|
|
12
|
+
:::
|
|
11
13
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
By default, the prefix for accessing routes in the BFF API directory is `/api`, as shown in the following directory structure:
|
|
14
|
+
By default, the route access BFF prefix's directory is `/api`, with the following directory structure:
|
|
15
15
|
|
|
16
16
|
```bash
|
|
17
17
|
api
|
|
18
18
|
└── hello.ts
|
|
19
19
|
```
|
|
20
20
|
|
|
21
|
-
The route
|
|
21
|
+
The corresponding route for `api/hello.ts` access is `localhost:8080/api/hello`.
|
|
22
22
|
|
|
23
23
|
This configuration option can modify the default route prefix:
|
|
24
24
|
|
|
@@ -30,4 +30,4 @@ export default defineConfig({
|
|
|
30
30
|
});
|
|
31
31
|
```
|
|
32
32
|
|
|
33
|
-
The corresponding
|
|
33
|
+
The corresponding `api/hello.ts` access route is `localhost:8080/api-demo/hello`.
|
|
@@ -7,13 +7,16 @@ sidebar_label: proxy
|
|
|
7
7
|
- **Type:** `Record<string, string>`
|
|
8
8
|
- **Default:** `{}`
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
:::caution Caution
|
|
11
|
+
First you need to enable the "BFF" function using [new](/apis/app/commands#modern-new) command.
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
:::
|
|
14
|
+
|
|
15
|
+
With simple configuration, no code is required, Modern.js automatically forwards requests. Requests sent to Modern.js BFF server are proxied to the specified service.
|
|
13
16
|
|
|
14
|
-
|
|
17
|
+
BFF Proxy uses the powerful [http-proxy-middleware](https://github.com/chimurai/http-proxy-middleware), and if you need more advanced usage, you can check its [documentation](https://github.com/chimurai/http-proxy-middleware#options).
|
|
15
18
|
|
|
16
|
-
Add the following configuration to `modern.server-runtime.config.
|
|
19
|
+
Add the following configuration to `modern.server-runtime.config.ts`, you can turn on the proxy:
|
|
17
20
|
|
|
18
21
|
```ts title="modern.server-runtime.config.ts"
|
|
19
22
|
import { defineConfig } from '@modern-js/app-tools/server';
|
|
@@ -26,31 +29,41 @@ export default defineConfig({
|
|
|
26
29
|
});
|
|
27
30
|
```
|
|
28
31
|
|
|
29
|
-
Assuming the
|
|
32
|
+
Assuming that the starting Modern.js BFF server's service address is `localhost:8080`, all requests whose path starts with `api` will be intercepted, such as requests sent to `localhost:8080/api/v1/topics` will be proxied to `https://cnodejs.org/api/v1/topics`.
|
|
30
33
|
|
|
31
|
-
|
|
34
|
+
You can do path rewriting, such as proxying requests sent to `localhost:8080/api/topics` to `https://cnodejs.org/api/v1/topics`.
|
|
32
35
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
+
```js title="modern.server-runtime.config.js"
|
|
37
|
+
import { defineConfig } from '@modern-js/app-tools/server';
|
|
38
|
+
export default defineConfig({
|
|
39
|
+
bff: {
|
|
40
|
+
proxy: {
|
|
41
|
+
'/api': {
|
|
42
|
+
target: 'https://cnodejs.org',
|
|
43
|
+
pathRewrite: { '/api/topics': '/api/v1/topics' },
|
|
44
|
+
changeOrigin: true,
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
```
|
|
36
50
|
|
|
37
|
-
|
|
51
|
+
Unlike [dev.proxy](/configure/app/dev/proxy), the proxy described in this section only works on requests entering the BFF/API service; at the same time, this configuration can be used not only in the development environment, but also in the production environment. The corresponding request will also be proxied in the production environment.
|
|
38
52
|
|
|
39
|
-
|
|
53
|
+
## Common usage
|
|
40
54
|
|
|
41
|
-
|
|
55
|
+
### Solve interface cross-domain problems
|
|
42
56
|
|
|
43
|
-
|
|
57
|
+
In the process of project development, because web pages and interface services are not deployed under the same domain name, cross-domain problems are often encountered.
|
|
44
58
|
|
|
45
|
-
|
|
46
|
-
There are many ways to solve cross-domain issues, and here we can easily solve them using `bff.proxy`.
|
|
59
|
+
There are many ways to solve cross-domain problems, and here we use `bff.proxy` to easily solve cross-domain problems.
|
|
47
60
|
|
|
48
61
|
:::info
|
|
49
|
-
|
|
62
|
+
In BFF proxy mode, if you do not need to write the BFF interface, the API directory can be deleted; at this time, BFF proxy will still be enabled.
|
|
50
63
|
|
|
51
64
|
:::
|
|
52
65
|
|
|
53
|
-
As shown below, the
|
|
66
|
+
As shown below, in the `modern.server-runtime.config.js`, write the following configuration; we send all web pages to the same domain that request proxies starting with `/api` to another domain's service.
|
|
54
67
|
|
|
55
68
|
```ts title="modern.server-runtime.config.ts"
|
|
56
69
|
export default defineServerConfig({
|
|
@@ -61,5 +74,3 @@ export default defineServerConfig({
|
|
|
61
74
|
},
|
|
62
75
|
};
|
|
63
76
|
```
|
|
64
|
-
|
|
65
|
-
|
|
@@ -7,13 +7,13 @@ sidebar_label: baseUrl
|
|
|
7
7
|
- **Type:** `string | string[]`
|
|
8
8
|
- **Default:** `undefined`
|
|
9
9
|
|
|
10
|
-
Uniformly set the server-
|
|
10
|
+
Uniformly set the prefix of server-side routes (commonly used in situations where multiple applications share the same domain name to distinguish traffic).
|
|
11
11
|
|
|
12
12
|
```ts title="modern.config.ts"
|
|
13
13
|
export default defineConfig({
|
|
14
14
|
server: {
|
|
15
|
-
// All generated routes
|
|
16
|
-
//
|
|
15
|
+
// All generated routes will automatically have the prefix `/base`
|
|
16
|
+
// Generated server-side route file path: dist/route.json
|
|
17
17
|
baseUrl: '/base'
|
|
18
18
|
|
|
19
19
|
// Multiple baseUrl
|
|
@@ -22,7 +22,7 @@ export default defineConfig({
|
|
|
22
22
|
})
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
-
After `dev`, you
|
|
25
|
+
After running `dev`, you will see that the route access will have the corresponding prefix added:
|
|
26
26
|
|
|
27
27
|
```bash
|
|
28
28
|
App running at:
|
|
@@ -7,9 +7,9 @@ sidebar_label: enableFrameworkExt
|
|
|
7
7
|
- **Type:** `boolean`
|
|
8
8
|
- **Default:** `false`
|
|
9
9
|
|
|
10
|
-
By default,
|
|
10
|
+
By default, when the [custom Web Server feature](/guides/advanced-features/web-server) is enabled, the Middleware will use the Modern.js's syntax.
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
Enabling `server.enableFrameworkExt` allows the use of syntax extensions from other frameworks.
|
|
13
13
|
|
|
14
14
|
```ts title="modern.config.ts"
|
|
15
15
|
export default defineConfig({
|
|
@@ -32,7 +32,7 @@ export const middleware: Middleware = (ctx, next) => {
|
|
|
32
32
|
};
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
After enabling it, the Middleware type will be exported from other namespaces, and syntax extensions from frameworks can be used:
|
|
36
36
|
|
|
37
37
|
```ts title="server/index.ts"
|
|
38
38
|
import { SomeType } from '@modern-js/runtime/{namespace}';
|
|
@@ -44,6 +44,6 @@ export const middleware: SomeType = (...args) => {
|
|
|
44
44
|
```
|
|
45
45
|
|
|
46
46
|
:::note
|
|
47
|
-
The above code is
|
|
47
|
+
The above code is pseudocode, and the specific usage needs to refer to the corresponding framework extension.
|
|
48
48
|
|
|
49
49
|
:::
|
|
@@ -7,7 +7,7 @@ sidebar_label: port
|
|
|
7
7
|
- **Type:** `number`
|
|
8
8
|
- **Default:** `8080`
|
|
9
9
|
|
|
10
|
-
When
|
|
10
|
+
When running the `dev`, `start`, and `serve` commands, Modern.js will start with `8080` as the default port and automatically increase the port number when the port is occupied. You can use this configuration to modify the port number that the Server starts with:
|
|
11
11
|
|
|
12
12
|
```js title="modern.config.ts"
|
|
13
13
|
export default defineConfig({
|
|
@@ -17,14 +17,14 @@ export default defineConfig({
|
|
|
17
17
|
});
|
|
18
18
|
```
|
|
19
19
|
|
|
20
|
-
### Difference
|
|
20
|
+
### Difference between `server.port` and `dev.port`
|
|
21
21
|
|
|
22
|
-
In most cases, we recommend using `server.port` instead of `dev.port` to set the port number, the differences between them are as follows:
|
|
22
|
+
In most cases, we recommend using `server.port` instead of `dev.port` to set the port number, and the differences between them are as follows:
|
|
23
23
|
|
|
24
|
-
- `dev.port` only
|
|
25
|
-
- In the development, `dev.port`
|
|
24
|
+
- `dev.port` only works in the development environment, while `server.port` works in both the development and production environments.
|
|
25
|
+
- In the development environment, `dev.port` has a higher priority than `server.port`.
|
|
26
26
|
|
|
27
|
-
When you set `dev.port` and `server.port
|
|
27
|
+
When you set both `dev.port` and `server.port`, `dev.port` will take effect in the development environment, and `server.port` will take effect in the production environment. For example, in the following example, the port number listened to in the development environment is `3001`, and the port number listened to in the production environment is `3002`.
|
|
28
28
|
|
|
29
29
|
```ts title="modern.config.ts"
|
|
30
30
|
export default defineConfig({
|
|
@@ -5,20 +5,20 @@ sidebar_label: publicRoutes
|
|
|
5
5
|
# server.publicRoutes
|
|
6
6
|
|
|
7
7
|
- **Type:** `Object`
|
|
8
|
-
- **Default:**
|
|
8
|
+
- **Default:** Server-side routing rules generated based on file conventions, with one route rule generated for each file.
|
|
9
9
|
|
|
10
|
-
This configuration option only applies to server-
|
|
10
|
+
This configuration option only applies to server-side routing and can customize the access route of resources under `config/public/`.
|
|
11
11
|
|
|
12
|
-
The `key` of the object is the relative file path of the
|
|
12
|
+
The `key` of the object is the relative file path of the `config/public/` (without using `./`), and the value can be a `string`.
|
|
13
13
|
|
|
14
14
|
```ts title="modern.config.ts"
|
|
15
15
|
export default defineConfig({
|
|
16
16
|
server: {
|
|
17
17
|
publicRoutes: {
|
|
18
|
-
// Set
|
|
18
|
+
// Set a long route
|
|
19
19
|
'index.json': '/user-config/card-info/extra/help.json',
|
|
20
20
|
|
|
21
|
-
// Set
|
|
21
|
+
// Set a route without a suffix
|
|
22
22
|
'robot.txt': '/app/authentication',
|
|
23
23
|
},
|
|
24
24
|
},
|
|
@@ -5,30 +5,30 @@ sidebar_label: routes
|
|
|
5
5
|
# server.routes
|
|
6
6
|
|
|
7
7
|
- **Type:** `Object`
|
|
8
|
-
- **Default:**
|
|
8
|
+
- **Default:** Server-side routing rules generated based on file conventions, with one route rule generated for each entry, and the entry name is equal to the route path.
|
|
9
9
|
|
|
10
|
-
This configuration option only applies to server-
|
|
10
|
+
This configuration option only applies to server-side routing and can customize the access route of application entries.
|
|
11
11
|
|
|
12
|
-
## Custom access
|
|
12
|
+
## Custom access routes
|
|
13
13
|
|
|
14
|
-
The `key` of the object is the
|
|
14
|
+
The `key` of the object is the name of the current application entry, and the value can be a `string | Array<string>`.
|
|
15
15
|
|
|
16
|
-
When the value type is `string`, the current value represents the
|
|
16
|
+
When the value type is `string`, the current value represents the route path for accessing the entry.
|
|
17
17
|
|
|
18
18
|
```ts title="modern.config.ts"
|
|
19
19
|
export default defineConfig({
|
|
20
20
|
server: {
|
|
21
21
|
routes: {
|
|
22
|
-
//
|
|
22
|
+
// Default route is /entryName1, customized to /p/test1
|
|
23
23
|
entryName1: '/p/test1'
|
|
24
|
-
//
|
|
24
|
+
// Supports dynamic server-side routing configuration
|
|
25
25
|
entryName2: '/detail/:id'
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
});
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
-
Multiple access routes can also be set for
|
|
31
|
+
Multiple access routes can also be set for the entry through `Array<string>`:
|
|
32
32
|
|
|
33
33
|
```ts title="modern.config.ts"
|
|
34
34
|
export default defineConfig({
|
|
@@ -40,9 +40,9 @@ export default defineConfig({
|
|
|
40
40
|
});
|
|
41
41
|
```
|
|
42
42
|
|
|
43
|
-
At this
|
|
43
|
+
At this time, the `page-a` entry can be accessed through both the `/a` and `/b` routes.
|
|
44
44
|
|
|
45
|
-
After executing the `dev` command, you can
|
|
45
|
+
After executing the `dev` command, you can view two route records for the `page-a` entry in `dist/route.json`:
|
|
46
46
|
|
|
47
47
|
```json
|
|
48
48
|
{
|
|
@@ -65,9 +65,9 @@ After executing the `dev` command, you can see in `dist/route.json` that there a
|
|
|
65
65
|
}
|
|
66
66
|
```
|
|
67
67
|
|
|
68
|
-
## Custom response
|
|
68
|
+
## Custom response headers
|
|
69
69
|
|
|
70
|
-
|
|
70
|
+
Response headers can be set by configuring the `resHeaders` of the entry:
|
|
71
71
|
|
|
72
72
|
```ts title="modern.config.ts"
|
|
73
73
|
export default defineConfig({
|
|
@@ -85,6 +85,6 @@ export default defineConfig({
|
|
|
85
85
|
```
|
|
86
86
|
|
|
87
87
|
:::note
|
|
88
|
-
This configuration takes effect in both the production
|
|
88
|
+
This configuration takes effect in both the production and development environments, and different response headers can be set according to the NODE_ENV to distinguish between environments. However, if you only need to set response headers in the development environment, it is recommended to use `tools.devServer.headers`.
|
|
89
89
|
|
|
90
90
|
:::
|
|
@@ -7,10 +7,10 @@ sidebar_label: ssrByEntries
|
|
|
7
7
|
- **Type:** `Object`
|
|
8
8
|
- **Default:** `undefined`
|
|
9
9
|
|
|
10
|
-
Set
|
|
10
|
+
Set SSR options by entry, and the properties inside the option are the same as [ssr](./ssr.mdx). The specified value will be replaced and merged with the content of the SSR attribute, for example:
|
|
11
11
|
|
|
12
12
|
:::info
|
|
13
|
-
The "entry name" defaults to the directory name. In a few cases, when
|
|
13
|
+
The "entry name" defaults to the directory name. In a few cases, when defining an entry through `source.entries`, the entry name is the `key` of the `source.entries` object.
|
|
14
14
|
|
|
15
15
|
:::
|
|
16
16
|
|
|
@@ -19,11 +19,11 @@ export default defineConfig({
|
|
|
19
19
|
server: {
|
|
20
20
|
ssr: true,
|
|
21
21
|
ssrByEntries: {
|
|
22
|
-
// page-a does not enable
|
|
22
|
+
// page-a does not enable SSR
|
|
23
23
|
'page-a': false,
|
|
24
24
|
},
|
|
25
25
|
},
|
|
26
26
|
});
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
In the above configuration, the project enables SSR as a whole, but the SSR rendering ability is disabled for the `page-a` entry.
|
|
@@ -11,7 +11,7 @@ Enalbe SSR configuration.
|
|
|
11
11
|
|
|
12
12
|
### Boolean Type
|
|
13
13
|
|
|
14
|
-
When the value type is `boolean`, it indicates whether to enable SSR deployment mode
|
|
14
|
+
When the value type is `boolean`, it indicates whether to enable SSR deployment mode. The default is `false` to disable it.
|
|
15
15
|
|
|
16
16
|
```ts title="modern.config.ts"
|
|
17
17
|
export default defineConfig({
|
|
@@ -23,11 +23,11 @@ export default defineConfig({
|
|
|
23
23
|
|
|
24
24
|
### Object Type
|
|
25
25
|
|
|
26
|
-
When the value type is `Object`,
|
|
26
|
+
When the value type is `Object`, the following properties can be configured:
|
|
27
27
|
|
|
28
|
-
- `mode`: `string = 'string'`,
|
|
29
|
-
- `forceCSR`: `boolean = false`,
|
|
30
|
-
- `inlineScript
|
|
28
|
+
- `mode`: `string = 'string'`, which defaults to using `renderToString` for rendering. Configure `stream` to enable streaming rendering.
|
|
29
|
+
- `forceCSR`: `boolean = false`, which is off by default for forcing CSR rendering. Configure `true` to force CSR by adding `?csr=true` when accessing the page.
|
|
30
|
+
- `inlineScript`: `boolean = true`, by default, SSR data is injected into HTML as inline scripts and assigned directly to global variables. Configure `false` to distribute JSON instead of assigning to global variables.
|
|
31
31
|
|
|
32
32
|
```ts title="modern.config.ts"
|
|
33
33
|
export default defineConfig({
|
|
@@ -2,13 +2,12 @@
|
|
|
2
2
|
title: Code Split
|
|
3
3
|
sidebar_position: 6
|
|
4
4
|
---
|
|
5
|
-
# Code
|
|
5
|
+
# Code Splitting
|
|
6
6
|
|
|
7
|
-
Code
|
|
7
|
+
Code splitting is a common method to optimize frontend resource loading. This article will introduce the three types of code splitting supported by Modern.js:
|
|
8
8
|
|
|
9
9
|
:::info
|
|
10
|
-
When
|
|
11
|
-
|
|
10
|
+
When using Modern.js [Conventional routing](/guides/basic-features/routes#conventional-routing). By default, code splitting is done according to the routing component, so you don't need to do it yourself.
|
|
12
11
|
:::
|
|
13
12
|
|
|
14
13
|
- `import`
|
|
@@ -17,7 +16,7 @@ When you use Modern.js [Conventional routing](/guides/basic-features/routes#conv
|
|
|
17
16
|
|
|
18
17
|
## import
|
|
19
18
|
|
|
20
|
-
use dynamic `import()`,
|
|
19
|
+
use dynamic `import()`, the JS modules pass to this API will be packaged as a separate JS file, for example:
|
|
21
20
|
|
|
22
21
|
```ts
|
|
23
22
|
import('./math').then(math => {
|
|
@@ -25,15 +24,14 @@ import('./math').then(math => {
|
|
|
25
24
|
});
|
|
26
25
|
```
|
|
27
26
|
|
|
28
|
-
The JS modules corresponding to the './math' path will be packaged in a separate JS file.
|
|
29
|
-
|
|
30
27
|
## React.lazy
|
|
31
28
|
|
|
32
29
|
The officially way provides by React to split component code.
|
|
33
30
|
|
|
34
31
|
:::caution
|
|
35
|
-
|
|
32
|
+
`React.lazy` is typically used in together with `<Suspense>`, hence it is only available in CSR or React 18 Streaming SSR.
|
|
36
33
|
|
|
34
|
+
For projects that use Traditional SSR(renderToString), `React.lazy` is not supported. Please use the Loadable API instead.
|
|
37
35
|
:::
|
|
38
36
|
|
|
39
37
|
```ts
|
|
@@ -56,11 +54,11 @@ function MyComponent() {
|
|
|
56
54
|
}
|
|
57
55
|
```
|
|
58
56
|
|
|
59
|
-
For
|
|
57
|
+
For details, see [React lazy](https://reactjs.org/docs/code-splitting.html#reactlazy).
|
|
60
58
|
|
|
61
|
-
##
|
|
59
|
+
## Loadable
|
|
62
60
|
|
|
63
|
-
use
|
|
61
|
+
In Modern.js, you can use the Loadable API, which is exported from `@modern-js/runtime/loadable`. Here's an example:
|
|
64
62
|
|
|
65
63
|
```ts
|
|
66
64
|
import loadable from '@modern-js/runtime/loadable';
|
|
@@ -72,9 +70,12 @@ function MyComponent() {
|
|
|
72
70
|
}
|
|
73
71
|
```
|
|
74
72
|
|
|
75
|
-
|
|
73
|
+
With the out-of-the-box support of `loadable` in SSR by Modern.js, you no longer need to add Babel plugins or inject scripts into HTML during SSR.
|
|
76
74
|
|
|
77
|
-
|
|
78
|
-
SSR is supported out of the box by `loadable`.
|
|
75
|
+
However, it's important to note that any Loadable API in SSR does not support the use of `<Suspense>`.
|
|
79
76
|
|
|
77
|
+
:::info
|
|
78
|
+
If you want to use `<Suspense>` in CSR projects with React 17 and below, you can substitute `React.lazy` with `loadable.lazy`.
|
|
80
79
|
:::
|
|
80
|
+
|
|
81
|
+
For details, see [Loadable API](/apis/app/runtime/utility/loadable).
|
|
@@ -7,9 +7,9 @@ title: enableHandleWeb
|
|
|
7
7
|
- **类型:** `boolean`
|
|
8
8
|
- **默认值:** `false`
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
:::caution 注意
|
|
11
|
+
请先在当前应用项目根目录使用【[new](/apis/app/commands#modern-new)】 启用 BFF 功能。
|
|
12
|
+
:::
|
|
13
13
|
|
|
14
14
|
默认情况下,BFF 服务只能处理 BFF API 的请求。
|
|
15
15
|
|
|
@@ -7,9 +7,10 @@ sidebar_label: prefix
|
|
|
7
7
|
- **类型:** `string`
|
|
8
8
|
- **默认值:** `/api`
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
:::caution 注意
|
|
11
|
+
请先在当前应用项目根目录使用【[new](/apis/app/commands#modern-new)】 启用 BFF 功能。
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
:::
|
|
13
14
|
|
|
14
15
|
默认情况下,BFF API 目录下的路由访问前缀是 `/api`, 如下目录结构:
|
|
15
16
|
|
|
@@ -7,11 +7,13 @@ sidebar_label: proxy
|
|
|
7
7
|
- **类型:** `Record<string, string>`
|
|
8
8
|
- **默认值:** `{}`
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
:::info 补充信息
|
|
11
|
+
请先在当前应用项目根目录使用【[new](/apis/app/commands#modern-new)】 启用 BFF 功能。
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
:::
|
|
13
14
|
|
|
14
|
-
|
|
15
|
+
通过简单配置,无需编写代码,Modern.js 会自动转发请求。发送给 Modern.js BFF server 的请求,会代理到指定的服务上。
|
|
16
|
+
BFF Proxy 使用了强大的 [http-proxy-middleware](https://github.com/chimurai/http-proxy-middleware),如果需要更多高级的用法, 可以查看它的[文档](https://github.com/chimurai/http-proxy-middleware#options)。
|
|
15
17
|
|
|
16
18
|
在 `modern.server-runtime.config.js` 中加入以下配置;即可开启代理:
|
|
17
19
|
|
|
@@ -26,18 +28,27 @@ export default defineConfig({
|
|
|
26
28
|
});
|
|
27
29
|
```
|
|
28
30
|
|
|
29
|
-
|
|
31
|
+
假设启动的 Modern.js BFF server 的服务地址是 `localhost:8080`,所有 path 以 `api` 开头的请求都会被拦截,如发送到 `localhost:8080/api/v1/topics` 的请求会被代理到 `https://cnodejs.org/api/v1/topics`。
|
|
30
32
|
|
|
31
|
-
|
|
33
|
+
你可以做路径的重写,如将发送到 `localhost:8080/api/topics` 的请求代理到 `https://cnodejs.org/api/v1/topics`。
|
|
32
34
|
|
|
33
|
-
|
|
35
|
+
```ts title="modern.server-runtime.config.ts"
|
|
36
|
+
import { defineConfig } from '@modern-js/app-tools/server';
|
|
37
|
+
export default defineConfig({
|
|
38
|
+
bff: {
|
|
39
|
+
proxy: {
|
|
40
|
+
'/api': {
|
|
41
|
+
target: 'https://cnodejs.org',
|
|
42
|
+
pathRewrite: { '/api/topics': '/api/v1/topics' },
|
|
43
|
+
changeOrigin: true,
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
```
|
|
34
49
|
|
|
35
50
|
与 [dev.proxy](/configure/app/dev/proxy) 不同,本节所介绍的代理只作用于进入 BFF/API 服务的请求;同时,这一配置不但可以在开发环境中使用,在生产环境中也会代理相应的请求。
|
|
36
51
|
|
|
37
|
-
import BFFProxyPrinciple from "@site-docs/components/bff-proxy-principle";
|
|
38
|
-
|
|
39
|
-
<BFFProxyPrinciple />
|
|
40
|
-
|
|
41
52
|
## 常见用法
|
|
42
53
|
|
|
43
54
|
### 解决接口跨域问题
|
|
@@ -61,4 +72,3 @@ export default defineServerConfig({
|
|
|
61
72
|
},
|
|
62
73
|
};
|
|
63
74
|
```
|
|
64
|
-
|
|
@@ -5,11 +5,11 @@ sidebar_label: publicRoutes
|
|
|
5
5
|
# server.publicRoutes
|
|
6
6
|
|
|
7
7
|
- **类型:** `Object`
|
|
8
|
-
- **默认值:**
|
|
8
|
+
- **默认值:** 根据文件约定自动生成的服务端路由规则,每个文件生成一条路由规则。
|
|
9
9
|
|
|
10
10
|
该配置选项只作用于服务端路由,可以自定义 `config/public/` 下资源的访问路由。
|
|
11
11
|
|
|
12
|
-
对象的 `key`
|
|
12
|
+
对象的 `key` 为 `config/public/` 的相对文件路径(不使用 `./`), 值可以是 `string`。
|
|
13
13
|
|
|
14
14
|
```ts title="modern.config.ts"
|
|
15
15
|
export default defineConfig({
|
|
@@ -5,9 +5,9 @@ sidebar_label: routes
|
|
|
5
5
|
# server.routes
|
|
6
6
|
|
|
7
7
|
- **类型:** `Object`
|
|
8
|
-
- **默认值:**
|
|
8
|
+
- **默认值:** 根据文件约定自动生成的服务端路由规则,每个入口生成一条路由规则,入口名称等于路由路径。
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
该配置选项只作用于服务端路由,可以自定义应用入口的访问路由。
|
|
11
11
|
|
|
12
12
|
## 自定义访问路由
|
|
13
13
|
|
|
@@ -7,7 +7,7 @@ sidebar_label: ssrByEntries
|
|
|
7
7
|
- **类型:** `Object`
|
|
8
8
|
- **默认值:** `undefined`
|
|
9
9
|
|
|
10
|
-
按入口设置
|
|
10
|
+
按入口设置 SSR 选项,选项内的属性同 [ssr](./ssr.mdx),指定值会和 SSR 属性内容做替换合并操作,例如:
|
|
11
11
|
|
|
12
12
|
:::info
|
|
13
13
|
「 入口名 」默认为目录名,少数情况下通过 `source.entries` 自定义入口时,入口名为 `source.entries` 对象的 `key`。
|
|
@@ -19,11 +19,11 @@ export default defineConfig({
|
|
|
19
19
|
server: {
|
|
20
20
|
ssr: true,
|
|
21
21
|
ssrByEntries: {
|
|
22
|
-
// page-a 不启用
|
|
22
|
+
// page-a 不启用 SSR
|
|
23
23
|
'page-a': false,
|
|
24
24
|
},
|
|
25
25
|
},
|
|
26
26
|
});
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
-
如上配置,项目整体启用了
|
|
29
|
+
如上配置,项目整体启用了 SSR, 但是针对入口 `page-a` 关闭了 SSR 渲染能力。
|
|
@@ -25,7 +25,7 @@ export default defineConfig({
|
|
|
25
25
|
|
|
26
26
|
当值类型为 `Object` 时,可以配置如下属性:
|
|
27
27
|
|
|
28
|
-
- `mode`:`string = 'string'`,默认为使用 `renderToString` 渲染。配置为
|
|
28
|
+
- `mode`:`string = 'string'`,默认为使用 `renderToString` 渲染。配置为 `stream` 开启流式渲染。
|
|
29
29
|
- `forceCSR`:`boolean = false`,默认关闭强制 CSR 渲染。配置为 `true` 后,在页面访问时添加 `?csr=true` 即可强制 CSR。
|
|
30
30
|
- `inlineScript`:`boolean = true`,默认情况下,SSR 的数据会以内联脚本的方式注入到 HTML 中,并且直接赋值给全局变量。配置为 `false` 后,会下发 JSON,而不是赋值给全局变量。
|
|
31
31
|
|
|
@@ -7,8 +7,7 @@ sidebar_position: 6
|
|
|
7
7
|
代码分割是优化前端资源加载的一种常用手段,本文将介绍 Modern.js 支持的三种代码分割方式:
|
|
8
8
|
|
|
9
9
|
:::info
|
|
10
|
-
使用 Modern.js [约定式路由](/guides/basic-features/routes#约定式路由)
|
|
11
|
-
|
|
10
|
+
使用 Modern.js [约定式路由](/guides/basic-features/routes#约定式路由)时,默认会根据路由组件做代码分割,无需自行进行代码分割。
|
|
12
11
|
:::
|
|
13
12
|
|
|
14
13
|
- `import`
|
|
@@ -17,7 +16,7 @@ sidebar_position: 6
|
|
|
17
16
|
|
|
18
17
|
## import
|
|
19
18
|
|
|
20
|
-
使用动态 `import()`
|
|
19
|
+
使用动态 `import()` 语法,传入的 JS 模块将会被被打包到单独的 JS 文件中。例如:
|
|
21
20
|
|
|
22
21
|
```ts
|
|
23
22
|
import('./math').then(math => {
|
|
@@ -25,15 +24,14 @@ import('./math').then(math => {
|
|
|
25
24
|
});
|
|
26
25
|
```
|
|
27
26
|
|
|
28
|
-
`./math` 路径对应的 JS 模块会被打包到单独的 JS 文件中。
|
|
29
|
-
|
|
30
27
|
## React.lazy
|
|
31
28
|
|
|
32
29
|
React 官方提供的组件代码分割的方式。
|
|
33
30
|
|
|
34
31
|
:::caution
|
|
35
|
-
React
|
|
32
|
+
`React.lazy` 通常配合 `<Suspense>` 使用,因此仅在 CSR 或 React 18 Streaming SSR 中可用。
|
|
36
33
|
|
|
34
|
+
对于使用传统 SSR(字符串渲染)的项目,不支持 `React.lazy`,请使用 Loadable API。
|
|
37
35
|
:::
|
|
38
36
|
|
|
39
37
|
```ts
|
|
@@ -56,11 +54,11 @@ function MyComponent() {
|
|
|
56
54
|
}
|
|
57
55
|
```
|
|
58
56
|
|
|
59
|
-
`React.lazy` 更多用法请见 [React
|
|
57
|
+
`React.lazy` 更多用法请见 [React 官网文档](https://zh-hans.reactjs.org/docs/code-splitting.html#reactlazy)。
|
|
60
58
|
|
|
61
|
-
##
|
|
59
|
+
## Loadable
|
|
62
60
|
|
|
63
|
-
|
|
61
|
+
在 Modern.js 中,可以从 `@modern-js/runtime/loadable` 中导出使用 Loadable API,示例如下:
|
|
64
62
|
|
|
65
63
|
```ts
|
|
66
64
|
import loadable from '@modern-js/runtime/loadable';
|
|
@@ -72,9 +70,12 @@ function MyComponent() {
|
|
|
72
70
|
}
|
|
73
71
|
```
|
|
74
72
|
|
|
75
|
-
`loadable
|
|
73
|
+
Modern.js 开箱即用的支持在 SSR 中使用 `loadable`,无需再添加 Babel 插件,或是在 SSR 时向 HTML 中注入脚本。
|
|
76
74
|
|
|
77
|
-
|
|
78
|
-
`loadable` 开箱即用的支持 SSR,但不支持和 Suspense 一起使用,如果是 CSR 项目可以使用 [loadable.lazy](https://loadable-components.com/docs/suspense/)
|
|
75
|
+
但需要注意的是,任何 Loadable API 在 SSR 中都不支持配合 `<Suspense>` 使用。
|
|
79
76
|
|
|
77
|
+
:::info
|
|
78
|
+
如果希望在 React 17 及以下的 CSR 项目中使用 `<Suspense>`,可以用 [loadable.lazy](https://loadable-components.com/docs/suspense/) 代替 `React.lazy`。
|
|
80
79
|
:::
|
|
80
|
+
|
|
81
|
+
更多用法请见 [Loadable API](/apis/app/runtime/utility/loadable)。
|
package/package.json
CHANGED
|
@@ -15,14 +15,14 @@
|
|
|
15
15
|
"modern",
|
|
16
16
|
"modern.js"
|
|
17
17
|
],
|
|
18
|
-
"version": "0.0.0-next-
|
|
18
|
+
"version": "0.0.0-next-1686578839599",
|
|
19
19
|
"publishConfig": {
|
|
20
20
|
"registry": "https://registry.npmjs.org/",
|
|
21
21
|
"access": "public",
|
|
22
22
|
"provenance": true
|
|
23
23
|
},
|
|
24
24
|
"peerDependencies": {
|
|
25
|
-
"@modern-js/builder-doc": "0.0.0-next-
|
|
25
|
+
"@modern-js/builder-doc": "0.0.0-next-1686578839599"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"classnames": "^2",
|
|
@@ -34,9 +34,9 @@
|
|
|
34
34
|
"fs-extra": "^10",
|
|
35
35
|
"@types/node": "^16",
|
|
36
36
|
"@types/fs-extra": "^9",
|
|
37
|
-
"@modern-js/builder-doc": "0.0.0-next-
|
|
38
|
-
"@modern-js/doc-tools": "0.0.0-next-
|
|
39
|
-
"@modern-js/doc-plugin-auto-sidebar": "0.0.0-next-
|
|
37
|
+
"@modern-js/builder-doc": "0.0.0-next-1686578839599",
|
|
38
|
+
"@modern-js/doc-tools": "0.0.0-next-1686578839599",
|
|
39
|
+
"@modern-js/doc-plugin-auto-sidebar": "0.0.0-next-1686578839599"
|
|
40
40
|
},
|
|
41
41
|
"scripts": {
|
|
42
42
|
"dev": "modern dev",
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
Path rewriting can also be performed here, such as proxying the request sent to `localhost:8080/api/topics` to `https://cnodejs.org/api/v1/topics`.
|
|
2
|
-
|
|
3
|
-
```js title="modern.server-runtime.config.js"
|
|
4
|
-
import { defineConfig } from '@modern-js/app-tools/server';
|
|
5
|
-
export default defineConfig({
|
|
6
|
-
bff: {
|
|
7
|
-
proxy: {
|
|
8
|
-
'/api': {
|
|
9
|
-
target: 'https://cnodejs.org',
|
|
10
|
-
pathRewrite: { '/api/topics': '/api/v1/topics' },
|
|
11
|
-
changeOrigin: true,
|
|
12
|
-
},
|
|
13
|
-
},
|
|
14
|
-
},
|
|
15
|
-
});
|
|
16
|
-
```
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
BFF Proxy uses the powerful [http-proxy-middleware](https://github.com/chimurai/http-proxy-middleware). For more advanced usage, please refer to its [documentation](https://github.com/chimurai/http-proxy-middleware#options).
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
这里还可以进行路径重写,如将发送到 `localhost:8080/api/topics` 的请求代理到 `https://cnodejs.org/api/v1/topics`。
|
|
2
|
-
|
|
3
|
-
```ts title="modern.server-runtime.config.ts"
|
|
4
|
-
import { defineConfig } from '@modern-js/app-tools/server';
|
|
5
|
-
export default defineConfig({
|
|
6
|
-
bff: {
|
|
7
|
-
proxy: {
|
|
8
|
-
'/api': {
|
|
9
|
-
target: 'https://cnodejs.org',
|
|
10
|
-
pathRewrite: { '/api/topics': '/api/v1/topics' },
|
|
11
|
-
changeOrigin: true,
|
|
12
|
-
},
|
|
13
|
-
},
|
|
14
|
-
},
|
|
15
|
-
});
|
|
16
|
-
```
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
BFF Proxy 使用了强大的 [http-proxy-middleware](https://github.com/chimurai/http-proxy-middleware),如果需要更多高级的用法, 可以查看它的[文档](https://github.com/chimurai/http-proxy-middleware#options)。
|