@modern-js/main-doc 2.22.1 → 2.23.0
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +38 -0
- package/docs/en/apis/app/commands.mdx +5 -5
- package/docs/en/apis/app/runtime/core/bootstrap.mdx +5 -5
- package/docs/en/apis/app/runtime/core/use-loader.mdx +2 -2
- package/docs/en/apis/app/runtime/core/use-module-apps.mdx +2 -2
- package/docs/en/apis/app/runtime/core/use-runtime-context.mdx +1 -1
- package/docs/en/apis/app/runtime/testing/render.mdx +1 -1
- package/docs/en/components/bff-proxy-path-rewrite.mdx +16 -0
- package/docs/en/components/bff-proxy-principle.mdx +1 -0
- package/docs/en/components/enable-bff-caution.mdx +3 -0
- package/docs/en/components/global-proxy-config.mdx +33 -13
- 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 +19 -30
- package/docs/en/configure/app/builder-plugins.mdx +1 -1
- package/docs/en/configure/app/plugins.mdx +19 -19
- package/docs/en/configure/app/runtime/intro.mdx +28 -4
- package/docs/en/configure/app/runtime/master-app.mdx +1 -1
- package/docs/en/configure/app/runtime/router.mdx +2 -2
- 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/configure/app/usage.mdx +5 -5
- package/docs/en/guides/advanced-features/code-split.mdx +15 -14
- package/docs/en/guides/advanced-features/rspack-start.mdx +0 -1
- package/docs/en/guides/advanced-features/ssr.mdx +2 -2
- package/docs/en/guides/advanced-features/testing.mdx +8 -7
- package/docs/en/guides/advanced-features/web-server.mdx +16 -16
- package/docs/en/guides/basic-features/alias.mdx +11 -9
- package/docs/en/guides/basic-features/env-vars.mdx +14 -14
- package/docs/en/guides/basic-features/html.mdx +41 -43
- package/docs/en/guides/basic-features/mock.mdx +10 -10
- package/docs/en/guides/topic-detail/framework-plugin/implement.mdx +4 -4
- package/docs/en/guides/topic-detail/framework-plugin/introduction.mdx +1 -1
- package/docs/en/guides/troubleshooting/cli.mdx +13 -13
- package/docs/en/guides/troubleshooting/dependencies.mdx +25 -25
- package/docs/en/tutorials/first-app/c03-css.mdx +1 -1
- package/docs/zh/apis/app/commands.mdx +4 -4
- package/docs/zh/apis/app/runtime/testing/render.mdx +1 -1
- package/docs/zh/components/bff-proxy-path-rewrite.mdx +16 -0
- package/docs/zh/components/bff-proxy-principle.mdx +1 -0
- package/docs/zh/components/enable-bff-caution.mdx +4 -0
- package/docs/zh/components/global-proxy-config.mdx +35 -11
- package/docs/zh/configure/app/bff/enable-handle-web.mdx +3 -3
- package/docs/zh/configure/app/bff/prefix.mdx +2 -3
- package/docs/zh/configure/app/bff/proxy.mdx +11 -21
- package/docs/zh/configure/app/runtime/intro.mdx +1 -1
- 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/configure/app/usage.mdx +9 -9
- package/docs/zh/guides/advanced-features/code-split.mdx +13 -12
- package/docs/zh/guides/advanced-features/rspack-start.mdx +0 -1
- package/docs/zh/guides/advanced-features/ssr.mdx +2 -2
- package/docs/zh/guides/advanced-features/testing.mdx +4 -4
- package/docs/zh/guides/basic-features/alias.mdx +5 -3
- package/docs/zh/guides/basic-features/html.mdx +9 -9
- package/docs/zh/guides/basic-features/mock.mdx +2 -2
- package/docs/zh/guides/topic-detail/_category_.json +1 -1
- package/docs/zh/guides/troubleshooting/cli.mdx +1 -1
- package/docs/zh/guides/troubleshooting/dependencies.mdx +1 -1
- package/package.json +7 -6
@@ -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({
|
@@ -12,7 +12,7 @@ The compile configuration can be configured in two places:
|
|
12
12
|
- `package.json` file
|
13
13
|
|
14
14
|
:::info
|
15
|
-
Configurations in both package.json and modern.config.ts file are not supported for the same configuration. Configuration in modern.config.ts is recommended.
|
15
|
+
Configurations in both `package.json` and `modern.config.ts` file are not supported for the same configuration. Configuration in `modern.config.ts` is recommended.
|
16
16
|
:::
|
17
17
|
|
18
18
|
Server runtime configuration can be configured in the `modern.server-runtime.config.(ts|js|mjs)` file in the root path.
|
@@ -54,7 +54,7 @@ When using Rspack as the bundler, due to some differences in configuration types
|
|
54
54
|
|
55
55
|
### modern.config.js
|
56
56
|
|
57
|
-
If you are developing a non-TypeScript project, you can use the configuration file in
|
57
|
+
If you are developing a non-TypeScript project, you can use the configuration file in `.js` format:
|
58
58
|
|
59
59
|
```js title="modern.config.js"
|
60
60
|
export default {
|
@@ -66,7 +66,7 @@ export default {
|
|
66
66
|
};
|
67
67
|
```
|
68
68
|
|
69
|
-
You can also
|
69
|
+
You can also configure depending on your environment with `process.env.NODE_ENV`:
|
70
70
|
|
71
71
|
```js title="modern.config.js"
|
72
72
|
export default {
|
@@ -122,7 +122,7 @@ export default defineConfig(async ({ env, command }) => {
|
|
122
122
|
|
123
123
|
You can specify the name of the configuration file using the `--config` option.
|
124
124
|
|
125
|
-
For example, if you need to use the `modern.prod.config.js` file when running build
|
125
|
+
For example, if you need to use the `modern.prod.config.js` file when running `build`, you can add the following scripts to `package.json`:
|
126
126
|
|
127
127
|
```json title="package.json"
|
128
128
|
{
|
@@ -155,7 +155,7 @@ In addition to configuration files, configuration options can also be set the `m
|
|
155
155
|
}
|
156
156
|
```
|
157
157
|
|
158
|
-
Due to the limitation of the JSON file format, only simple types such as numbers, strings, boolean values, arrays, etc. can be defined in `package.json`. When we need to set the value of the function type, it is recommended to
|
158
|
+
Due to the limitation of the JSON file format, only simple types such as numbers, strings, boolean values, arrays, etc. can be defined in `package.json`. When we need to set the value of the function type, it is recommended to do so in the Modern.js configuration file.
|
159
159
|
|
160
160
|
### Note
|
161
161
|
|
@@ -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 way 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://react.dev/reference/react/lazy).
|
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).
|
@@ -75,7 +75,7 @@ Warning: Expected server HTML to contain a matching <div> in <div>.
|
|
75
75
|
This is caused by the inconsistency between the rendering result and the SSR rendering result when React executes the hydrate logic on the client side. Although the page performs normally, in complex applications, it is likely to cause problems such as DOM hierarchy confusion and style confusion.
|
76
76
|
|
77
77
|
:::info
|
78
|
-
For hydrate logic, please refer to [here](https://
|
78
|
+
For hydrate logic, please refer to [here](https://react.dev/reference/react-dom/hydrate).
|
79
79
|
|
80
80
|
:::
|
81
81
|
|
@@ -455,7 +455,7 @@ export default Page;
|
|
455
455
|
```
|
456
456
|
|
457
457
|
`Await` needs to be wrapped inside the `Suspense` component. The `resolve` of `Await` passes in the data acquired asynchronously by the Data Loader. When the data acquisition is completed,
|
458
|
-
the obtained data is rendered through the [Render Props](https://
|
458
|
+
the obtained data is rendered through the [Render Props](https://react.dev/reference/react/cloneElement#passing-data-with-a-render-prop) mode. When the data acquisition is in pending status, the
|
459
459
|
content set by the `fallback` property of the `Suspense` component will display.
|
460
460
|
|
461
461
|
|
@@ -6,16 +6,16 @@ title: Testing
|
|
6
6
|
|
7
7
|
Modern.js integrates the testing capabilities of [Jest](https://jestjs.io/) by default.
|
8
8
|
|
9
|
-
First need to execute `pnpm run new` enable [unit test/integration test] features:
|
9
|
+
First need to execute `pnpm run new` to enable [unit test/integration test] features:
|
10
10
|
|
11
|
-
```
|
11
|
+
```bash
|
12
12
|
? Please select the operation you want: Enable features
|
13
13
|
? Please select the feature name: Enable Unit Test / Integration Test
|
14
14
|
```
|
15
15
|
|
16
|
-
After executing the above command, the `"test": "modern test"` command will be
|
16
|
+
After executing the above command, the `"test": "modern test"` command will be added in `package.json` automatically.
|
17
17
|
|
18
|
-
|
18
|
+
After registering the `@modern-js/plugin-testing` plugin in `modern.config.ts`, you can use the testing features:
|
19
19
|
|
20
20
|
```ts title="modern.config.ts"
|
21
21
|
import testPlugin from '@modern-js/plugin-testing';
|
@@ -35,12 +35,13 @@ If you need to customize the test directory, you can configure it with [tools.je
|
|
35
35
|
|
36
36
|
Modern.js test support [testing-library](https://testing-library.com/docs/). API can be imported from `@modern-js/runtime/testing`.
|
37
37
|
|
38
|
-
```
|
38
|
+
```ts
|
39
39
|
import { render, screen } from '@modern-js/runtime/testing';
|
40
40
|
```
|
41
41
|
|
42
|
-
Other
|
42
|
+
Other testing APIs supported by Modern.js can be referred to [here](/apis/app/runtime/testing/cleanup).
|
43
43
|
|
44
44
|
## transform
|
45
45
|
|
46
|
-
Modern.js
|
46
|
+
By default, Modern.js testing uses [babel-jest](https://www.npmjs.com/package/babel-jest) for source code compilation. If you need to use [ts-jest](https://github.com/kulshekhar/ts-jest), you can configure it through [testing.transform](/configure/app/testing/transformer).
|
47
|
+
|
@@ -4,15 +4,15 @@ sidebar_position: 3
|
|
4
4
|
---
|
5
5
|
# Custom Web Server
|
6
6
|
|
7
|
-
As a client
|
7
|
+
As a client-centric development framework, Modern.js has limited customization capabilities on the server side. However, in some development scenarios, special server-level logic needs to be customized, such as user authentication, request preprocessing, and adding page rendering skeletons.
|
8
8
|
|
9
|
-
Some developers may be wondering, Modern.js already provides [BFF](/guides/advanced-features/bff/function.html), why
|
9
|
+
Some developers may be wondering, Modern.js already provides [BFF](/guides/advanced-features/bff/function.html), why you need **Custom Web Server**.
|
10
10
|
|
11
|
-
|
11
|
+
The reason is that by default, page routing does not go through BFF, it has no way to provide server-side custom logic for page access. The reason for this design is that we do not want the service that controls the page to be bound to the BFF service, this is to avoid the BFF framework restricting how the page is deployed.
|
12
12
|
|
13
|
-
For example, hosting pages separately from BFF, deploying page services to non-Node environments,
|
13
|
+
For example, hosting pages separately from BFF, deploying page services to non-Node environments, customizing for deployment platforms, etc.
|
14
14
|
|
15
|
-
For the above reasons, Modern.js provides three ways that projects can customize server
|
15
|
+
For the above reasons, Modern.js provides three ways that projects can customize server-level capabilities progressively according to their needs.
|
16
16
|
|
17
17
|
:::warning
|
18
18
|
The three extension methods cannot work at the same time, and developers need to choose the appropriate method according to the scenario.
|
@@ -20,11 +20,11 @@ The three extension methods cannot work at the same time, and developers need to
|
|
20
20
|
|
21
21
|
## Extending Web Server with API
|
22
22
|
|
23
|
-
The first way is to customize the server
|
23
|
+
The first way is to customize the server-side at a specific lifecycle through the server-side runtime API provided by Modern.js. The purpose of providing this way is that in some cases, developers do not need to control the full Web Server, but only need to add server-level logic.
|
24
24
|
|
25
|
-
Because the full web server cannot be controlled this way, and the extension logic **only takes effect when the page is requested**. Therefore, it is relatively simple to apply
|
25
|
+
Because the full web server cannot be controlled this way, and the extension logic **only takes effect when the page is requested**. Therefore, it is relatively simple to apply server-level logic, and you do not want to create additional BFFs or BFFs and pages without common server-level logic scenarios.
|
26
26
|
|
27
|
-
You can
|
27
|
+
You can run the'pnpm run new 'command in the project root directory to enable the "Custom Web Serve" function:
|
28
28
|
|
29
29
|
```bash
|
30
30
|
? Please select the operation you want: Create Element
|
@@ -47,7 +47,7 @@ After the function is turned on, the `server/index.ts` file will be automaticall
|
|
47
47
|
|
48
48
|
The Hook provided by Modern.js is used to control the built-in logic in the Web Server, and all page requests go through the Hook.
|
49
49
|
|
50
|
-
|
50
|
+
Currently, two Hooks are provided: `AfterMatch` and `AfterRender`, which can be used to modify the rendering results. It can be written in `server/index.ts` as follows:
|
51
51
|
|
52
52
|
```ts
|
53
53
|
import type { AfterMatchHook, AfterRenderHook } from '@modern-js/runtime/server';
|
@@ -61,11 +61,11 @@ export const afterRender: AfterRenderHook = (ctx, next) => {
|
|
61
61
|
}
|
62
62
|
```
|
63
63
|
|
64
|
-
Projects should
|
64
|
+
Projects should follow these best practices when using Hook:
|
65
65
|
|
66
|
-
1.
|
66
|
+
1. Authentication in afterMatch.
|
67
67
|
2. Do Rewrite and Redirect in afterMatch.
|
68
|
-
3.
|
68
|
+
3. Inject HTML content in afterRender.
|
69
69
|
|
70
70
|
:::note
|
71
71
|
For more detail, see [Hook](/apis/app/runtime/web-server/hook).
|
@@ -91,10 +91,10 @@ export const middleware: Middlewre = (context, next) => {
|
|
91
91
|
For more detail, see [Middleware] (/apis/app/runtime/web-server/middleware).
|
92
92
|
:::
|
93
93
|
|
94
|
-
Projects should
|
94
|
+
Projects should follow these best practices when using Middleware:
|
95
95
|
|
96
96
|
1. In Middleware, you can directly operate origin request and response objects, do event tracking, and inject Node services (databases, Redis, etc.) that may be used for SSR rendering.
|
97
|
-
2.
|
97
|
+
2. Operations such as marking and crawler optimization can be done in Middleware.
|
98
98
|
3. In Middleware, you can ignore the default rendering and customize the rendering process.
|
99
99
|
|
100
100
|
**In general, in CSR projects, using Hook can basically meet all the needs of simple scenarios. In SSR projects, Middleware can be used for more complex Node extensions.**
|
@@ -103,9 +103,9 @@ Projects should have the following best practices when using Middleware:
|
|
103
103
|
|
104
104
|
The second way is to use BFF to Managed page rendering. In this way, all requests will first hit the BFF service.
|
105
105
|
|
106
|
-
This method can uniformly control the server
|
106
|
+
This method can uniformly control the server-level logic of all requests through BFF. Therefore, it is suitable for scenarios where the server-level logic is complex, and BFF and pages need common server-level logic. But it still relies on the Web Server of Modern.js as a whole, and cannot run the logic on existing services.
|
107
107
|
|
108
|
-
To use this method, we need to
|
108
|
+
To use this method, we first need to enable the "BFF" function through `pnpm new`. Then add [`bff.enableHandleWeb`](/configure/app/bff/enable-handle-web.html) configuration in the configuration file:
|
109
109
|
|
110
110
|
```ts
|
111
111
|
export default defineConfig({
|
@@ -4,7 +4,7 @@ sidebar_position: 8
|
|
4
4
|
---
|
5
5
|
# Alias
|
6
6
|
|
7
|
-
Modern.js
|
7
|
+
Modern.js allows you to use alias to import modules from custom directories in JS and CSS, and comes with the following built-in alias:
|
8
8
|
|
9
9
|
```js
|
10
10
|
{
|
@@ -14,11 +14,11 @@ Modern.js allow aliases in JS and CSS, and the following aliases are built in:
|
|
14
14
|
```
|
15
15
|
|
16
16
|
:::info
|
17
|
-
When
|
17
|
+
When enabling optional features, the `new` command will also dynamically add built-in alias specific to the features. For example, when enabling BFF, the `@api` alias is added by default.
|
18
18
|
|
19
19
|
:::
|
20
20
|
|
21
|
-
For example,
|
21
|
+
For example, importing modules from the `src/common` directory in the `src/App.tsx` file:
|
22
22
|
|
23
23
|
```bash
|
24
24
|
.
|
@@ -27,17 +27,19 @@ For example, import the modules from the `src/common/` directory in the `src/App
|
|
27
27
|
│ │ └── base.css
|
28
28
|
│ └── utils
|
29
29
|
│ └── index.ts
|
30
|
-
|
30
|
+
└── App.tsx
|
31
31
|
```
|
32
32
|
|
33
|
-
|
33
|
+
The code in `src/App.tsx` is as follows:
|
34
34
|
|
35
35
|
```ts
|
36
36
|
import utils from '@/src/common/utils';
|
37
37
|
import '@/src/common/styles/base.css';
|
38
38
|
```
|
39
39
|
|
40
|
-
Modern.js also provides a way to
|
40
|
+
Modern.js also provides a way to customize alias. For example, adding the `@common` alias is as follows:
|
41
|
+
|
42
|
+
For TypeScript projects, just set `compilerOptions.paths` in the project's `tsconfig.json`:
|
41
43
|
|
42
44
|
```json
|
43
45
|
{
|
@@ -50,9 +52,9 @@ Modern.js also provides a way to config aliases. Adding the `@common` alias as a
|
|
50
52
|
}
|
51
53
|
```
|
52
54
|
|
53
|
-
JavaScript
|
55
|
+
For JavaScript projects, set `source.alias` in `modern.config.js`:
|
54
56
|
|
55
|
-
```ts title="modern.config.
|
57
|
+
```ts title="modern.config.js"
|
56
58
|
export default defineConfig({
|
57
59
|
source: {
|
58
60
|
alias: {
|
@@ -62,4 +64,4 @@ export default defineConfig({
|
|
62
64
|
});
|
63
65
|
```
|
64
66
|
|
65
|
-
For
|
67
|
+
For the specific usage of alias configuration, please refer to the [source.alias documentation](/configure/app/source/alias).
|
@@ -10,15 +10,15 @@ Modern.js provides support for environment variables, including built-in environ
|
|
10
10
|
|
11
11
|
### ASSET_PREFIX
|
12
12
|
|
13
|
-
The
|
13
|
+
The current path prefix of the resource file, which is a read-only environment variable.
|
14
14
|
|
15
15
|
### NODE_ENV
|
16
16
|
|
17
17
|
The current execution environment and is a **read-only** environment variable whose have different values under different execution commands:
|
18
18
|
|
19
|
-
- `production`:
|
20
|
-
- `test`:
|
21
|
-
- `development`:
|
19
|
+
- `production`: Default value when running `modern build` or `modern serve`.
|
20
|
+
- `test`: Default value when running `modern test`.
|
21
|
+
- `development`: Default value when running `modern dev`, also the default value in other cases.
|
22
22
|
|
23
23
|
### MODERN_ENV
|
24
24
|
|
@@ -33,7 +33,7 @@ MODERN_ENV priority is higher than NODE_ENV.
|
|
33
33
|
|
34
34
|
When using `@modern-js/runtime`, Modern.js will automatically inject `MODERN_TARGET` to distinguish between SSR and CSR environments.
|
35
35
|
|
36
|
-
You can use `process.env.MODERN_TARGET` to
|
36
|
+
You can use `process.env.MODERN_TARGET` to determine the environment and execute appropriate code.
|
37
37
|
|
38
38
|
```ts title="App.tsx"
|
39
39
|
function App() {
|
@@ -65,7 +65,7 @@ function App() {
|
|
65
65
|
This can provide different outputs for different environments to ensure that the bundle size is minimized. It can also be convenient to deal with some side effects for different environments.
|
66
66
|
|
67
67
|
:::note Dead Code Elimination
|
68
|
-
In
|
68
|
+
In production environment, minimizers such as Terser and SWC will analyze the code and remove dead code to reduce the bundle size. This process is called "Dead Code Elimination" (DCE).
|
69
69
|
|
70
70
|
For example, the code inside the `if (false)` statement will be removed, while the code inside the `if (true)` will be preserved.
|
71
71
|
|
@@ -75,9 +75,9 @@ If you do not use `process.env.MODERN_TARGET` as described above, the code minim
|
|
75
75
|
|
76
76
|
## Custom Environment Variables
|
77
77
|
|
78
|
-
|
78
|
+
You can specify custom environment variables in both `shell` and `.env` files.
|
79
79
|
|
80
|
-
###
|
80
|
+
### Via `shell`
|
81
81
|
|
82
82
|
Add custom environment variables before the command:
|
83
83
|
|
@@ -85,7 +85,7 @@ Add custom environment variables before the command:
|
|
85
85
|
REACT_APP_FOO=123 BAR=456 pnpm run dev
|
86
86
|
```
|
87
87
|
|
88
|
-
###
|
88
|
+
### Via `.env` file
|
89
89
|
|
90
90
|
Create a `.env` file in the project root and add custom environment variables, which are added to the Node.js process by default, for example:
|
91
91
|
|
@@ -97,7 +97,7 @@ BAR=456
|
|
97
97
|
The `.env` file follows the following loading rules:
|
98
98
|
|
99
99
|
- `.env`: default.
|
100
|
-
- `.env.{ MODERN_ENV | NODE_ENV }`:
|
100
|
+
- `.env.{ MODERN_ENV | NODE_ENV }`: Overrides `.env` for a specific environment.
|
101
101
|
|
102
102
|
When you need to use different config according to the environment, you can define environment variables in the `.env` file corresponding to the environment name, and manually set the execution environment when starting the project.
|
103
103
|
|
@@ -137,7 +137,7 @@ In custom HTML templates, you can also use such environment variables directly.
|
|
137
137
|
|
138
138
|
### Any Other Names
|
139
139
|
|
140
|
-
If you need to use environment variables with any other names in your code, you can
|
140
|
+
If you need to use environment variables with any other names in your code, you can configure them in [`source.globalVars`](/configure/app/source/global-vars). For example:
|
141
141
|
|
142
142
|
```ts title="modern.config.ts"
|
143
143
|
export default defineConfig({
|
@@ -149,7 +149,7 @@ export default defineConfig({
|
|
149
149
|
});
|
150
150
|
```
|
151
151
|
|
152
|
-
At this point, the `process.env.VERSION` in the code will be replaced
|
152
|
+
At this point, the `process.env.VERSION` in the code will be replaced by the value of `VERSION` in the environment variables.
|
153
153
|
|
154
154
|
:::note
|
155
155
|
`source.globalVars` also supports replacing other expressions or strings with specified values, not limited to environment variables.
|
@@ -158,7 +158,7 @@ At this point, the `process.env.VERSION` in the code will be replaced with the v
|
|
158
158
|
|
159
159
|
## Use Global Replacement
|
160
160
|
|
161
|
-
In addition to environment variables, Modern.js also supports replacing variables in code with other values or expressions, which can be used
|
161
|
+
In addition to environment variables, Modern.js also supports replacing variables in code with other values or expressions, which can be used to distinguish between development environment and production environment in code.
|
162
162
|
|
163
163
|
For example, converts the expression `TWO` to `1 + 1`:
|
164
164
|
|
@@ -179,4 +179,4 @@ const foo = TWO;
|
|
179
179
|
const foo = 1 + 1;
|
180
180
|
```
|
181
181
|
|
182
|
-
In most cases, `source.globalVars` is already sufficient to replace variables. But the values passed in by `source.globalVars` will be serialized by JSON by default. So it cannot be replaced like `1 + 1` in the example above, at this time, we need use [`source.define`](/configure/app/source/define).
|
182
|
+
In most cases, `source.globalVars` is already sufficient to replace variables. But the values passed in by `source.globalVars` will be serialized by JSON by default. So it cannot be replaced like `1 + 1` in the example above, at this time, we need to use [`source.define`](/configure/app/source/define).
|