@modern-js/main-doc 2.66.0 → 2.66.1-alpha.0
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/docs/en/apis/app/hooks/src/app.mdx +20 -34
- package/docs/en/apis/app/hooks/src/modern.runtime.mdx +9 -0
- package/docs/en/apis/app/runtime/app/define-config.mdx +6 -0
- package/docs/en/components/enable-micro-frontend.mdx +20 -3
- package/docs/en/components/micro-runtime-config.mdx +12 -13
- package/docs/en/components/reduck-notify.mdx +27 -0
- package/docs/en/configure/app/runtime/0-intro.mdx +63 -91
- package/docs/en/configure/app/usage.mdx +93 -69
- package/docs/en/guides/basic-features/render/_meta.json +1 -1
- package/docs/en/guides/basic-features/render/before-render.mdx +115 -0
- package/docs/en/guides/basic-features/routes.mdx +0 -95
- package/docs/en/guides/topic-detail/micro-frontend/c02-development.mdx +3 -5
- package/docs/en/guides/topic-detail/micro-frontend/c03-main-app.mdx +4 -2
- package/docs/en/guides/topic-detail/model/auto-actions.mdx +0 -4
- package/docs/en/guides/topic-detail/model/computed-state.mdx +0 -5
- package/docs/en/guides/topic-detail/model/define-model.mdx +0 -4
- package/docs/en/guides/topic-detail/model/faq.mdx +0 -5
- package/docs/en/guides/topic-detail/model/manage-effects.mdx +0 -5
- package/docs/en/guides/topic-detail/model/model-communicate.mdx +0 -5
- package/docs/en/guides/topic-detail/model/performance.mdx +0 -4
- package/docs/en/guides/topic-detail/model/quick-start.mdx +5 -7
- package/docs/en/guides/topic-detail/model/redux-integration.mdx +0 -4
- package/docs/en/guides/topic-detail/model/typescript-best-practice.mdx +0 -4
- package/docs/en/guides/topic-detail/model/use-model.mdx +0 -5
- package/docs/en/guides/topic-detail/model/use-out-of-modernjs.mdx +0 -4
- package/docs/zh/apis/app/hooks/src/app.mdx +18 -26
- package/docs/zh/apis/app/hooks/src/modern.runtime.mdx +9 -0
- package/docs/zh/apis/app/runtime/app/define-config.mdx +5 -0
- package/docs/zh/components/enable-micro-frontend.mdx +19 -12
- package/docs/zh/components/micro-runtime-config.mdx +3 -3
- package/docs/zh/components/reduck-notify.mdx +27 -0
- package/docs/zh/configure/app/runtime/0-intro.mdx +67 -86
- package/docs/zh/configure/app/usage.mdx +44 -21
- package/docs/zh/guides/basic-features/render/_meta.json +1 -1
- package/docs/zh/guides/basic-features/render/before-render.mdx +115 -0
- package/docs/zh/guides/basic-features/routes.mdx +0 -95
- package/docs/zh/guides/topic-detail/micro-frontend/c02-development.mdx +3 -5
- package/docs/zh/guides/topic-detail/micro-frontend/c03-main-app.mdx +4 -2
- package/docs/zh/guides/topic-detail/model/auto-actions.mdx +0 -4
- package/docs/zh/guides/topic-detail/model/computed-state.mdx +0 -4
- package/docs/zh/guides/topic-detail/model/define-model.mdx +1 -4
- package/docs/zh/guides/topic-detail/model/faq.mdx +0 -5
- package/docs/zh/guides/topic-detail/model/manage-effects.mdx +0 -4
- package/docs/zh/guides/topic-detail/model/model-communicate.mdx +1 -4
- package/docs/zh/guides/topic-detail/model/performance.mdx +0 -4
- package/docs/zh/guides/topic-detail/model/quick-start.mdx +7 -8
- package/docs/zh/guides/topic-detail/model/redux-integration.mdx +0 -4
- package/docs/zh/guides/topic-detail/model/typescript-best-practice.mdx +0 -4
- package/docs/zh/guides/topic-detail/model/use-model.mdx +0 -5
- package/docs/zh/guides/topic-detail/model/use-out-of-modernjs.mdx +0 -4
- package/package.json +1 -1
@@ -1,35 +1,37 @@
|
|
1
|
-
|
2
|
-
sidebar_position: 0
|
3
|
-
---
|
1
|
+
# Configuration
|
4
2
|
|
5
|
-
|
3
|
+
There are three types of configurations in Modern.js: Compile configuration, Runtime configuration, and Server Runtime configuration.
|
6
4
|
|
7
|
-
|
5
|
+
**Compile configuration** can be configured in two locations:
|
8
6
|
|
9
|
-
The
|
10
|
-
|
11
|
-
- `modern.config.(ts|js|mjs)` file in the root path
|
12
|
-
- `package.json` file
|
7
|
+
- The `modern.config.(ts|js|mjs)` file at the root path
|
8
|
+
- The `package.json` file
|
13
9
|
|
14
10
|
:::info
|
15
|
-
|
11
|
+
Modern.js does not support configuring the same configuration item in both `package.json` and `modern.config.ts` simultaneously. It is recommended to configure it in `modern.config.ts`. If Modern.js detects conflicts due to duplicate configurations, it will throw a warning.
|
16
12
|
:::
|
17
13
|
|
18
|
-
|
14
|
+
**Runtime configuration** can be configured in the `src/modern.runtime.(ts|js|mjs)` file.
|
15
|
+
|
16
|
+
{/* TODO server 配置文件更新 */}
|
17
|
+
**Server Runtime configuration** can be configured in the `modern.server-runtime.config.(ts|js|mjs)` file in the root path.
|
18
|
+
|
19
|
+
|
20
|
+
## Compile Configuration
|
19
21
|
|
20
|
-
|
22
|
+
### Configuring in Configuration Files
|
21
23
|
|
22
|
-
|
24
|
+
The configuration files for Modern.js are defined in the root directory of the project and support `.ts`, `.js`, and `.mjs` formats:
|
23
25
|
|
24
26
|
- `modern.config.ts`
|
25
27
|
- `modern.config.js`
|
26
28
|
- `modern.config.mjs`
|
27
29
|
|
28
|
-
|
30
|
+
#### modern.config.ts (Recommended)
|
29
31
|
|
30
|
-
We recommend using
|
32
|
+
We recommend using the `.ts` format for configuration files as it provides friendly TypeScript type hints, helping you avoid errors in the configuration.
|
31
33
|
|
32
|
-
Import the `defineConfig`
|
34
|
+
Import the `defineConfig` utility function from `@modern-js/app-tools`, which will assist you with type inference and type completion for the configuration:
|
33
35
|
|
34
36
|
```ts title="modern.config.ts"
|
35
37
|
import { defineConfig } from '@modern-js/app-tools';
|
@@ -43,18 +45,18 @@ export default defineConfig({
|
|
43
45
|
});
|
44
46
|
```
|
45
47
|
|
46
|
-
When using Rspack as the bundler, due to some differences in configuration types between webpack and Rspack, you need to specify `<'rspack'>` generic
|
48
|
+
When using Rspack as the bundler, due to some differences in configuration types between webpack and Rspack, you need to specify the `<'rspack'>` generic for `defineConfig`:
|
47
49
|
|
48
50
|
```diff title=modern.config.ts
|
49
51
|
- export default defineConfig({
|
50
52
|
+ export default defineConfig<'rspack'>({
|
51
|
-
|
53
|
+
// ...
|
52
54
|
});
|
53
55
|
```
|
54
56
|
|
55
|
-
|
57
|
+
#### modern.config.js
|
56
58
|
|
57
|
-
If you are developing a non-TypeScript project, you can use the
|
59
|
+
If you are developing a non-TypeScript project, you can use the `.js` format for the configuration file:
|
58
60
|
|
59
61
|
```js title="modern.config.js"
|
60
62
|
export default {
|
@@ -66,7 +68,7 @@ export default {
|
|
66
68
|
};
|
67
69
|
```
|
68
70
|
|
69
|
-
You can also configure
|
71
|
+
You can also configure different settings based on the environment using `process.env.NODE_ENV`:
|
70
72
|
|
71
73
|
```js title="modern.config.js"
|
72
74
|
export default {
|
@@ -76,9 +78,9 @@ export default {
|
|
76
78
|
};
|
77
79
|
```
|
78
80
|
|
79
|
-
|
81
|
+
#### Exporting Configuration Functions
|
80
82
|
|
81
|
-
Modern.js supports exporting a function in the configuration file,
|
83
|
+
Modern.js supports exporting a function in the configuration file, where you can dynamically compute the configuration and return it to Modern.js.
|
82
84
|
|
83
85
|
```js title="modern.config.js"
|
84
86
|
import { defineConfig } from '@modern-js/app-tools';
|
@@ -92,16 +94,16 @@ export default defineConfig(({ env, command }) => ({
|
|
92
94
|
}));
|
93
95
|
```
|
94
96
|
|
95
|
-
This function
|
97
|
+
This function accepts the following parameters:
|
96
98
|
|
97
|
-
- `env`:
|
99
|
+
- `env`: Corresponds to the value of `process.env.NODE_ENV`.
|
98
100
|
- When running `modern dev` or `modern start`, the value of `env` is `development`.
|
99
101
|
- When running `modern build` or `modern serve`, the value of `env` is `production`.
|
100
|
-
- `command`:
|
102
|
+
- `command`: Corresponds to the current command being run, such as `dev`, `start`, `build`, or `serve`.
|
101
103
|
|
102
|
-
|
104
|
+
#### Exporting Asynchronous Functions
|
103
105
|
|
104
|
-
Modern.js also supports exporting an asynchronous function in the configuration file, you
|
106
|
+
Modern.js also supports exporting an asynchronous function in the configuration file, allowing you to perform some asynchronous operations:
|
105
107
|
|
106
108
|
```js title="modern.config.js"
|
107
109
|
import { defineConfig } from '@modern-js/app-tools';
|
@@ -117,11 +119,11 @@ export default defineConfig(async ({ env, command }) => {
|
|
117
119
|
});
|
118
120
|
```
|
119
121
|
|
120
|
-
|
122
|
+
#### Specifying Configuration Files
|
121
123
|
|
122
|
-
|
124
|
+
The Modern.js command line supports specifying the name of the configuration file using the `--config` option.
|
123
125
|
|
124
|
-
For example, if you need to use the `modern.prod.config.js` file when
|
126
|
+
For example, if you need to use the `modern.prod.config.js` file when executing the `build` command, you can add the following configuration in `package.json`:
|
125
127
|
|
126
128
|
```json title="package.json"
|
127
129
|
{
|
@@ -132,15 +134,15 @@ For example, if you need to use the `modern.prod.config.js` file when running `b
|
|
132
134
|
}
|
133
135
|
```
|
134
136
|
|
135
|
-
You can also abbreviate the `--config` option
|
137
|
+
You can also abbreviate the `--config` option as `-c`:
|
136
138
|
|
137
139
|
```bash
|
138
140
|
$ modern build -c modern.prod.config.js
|
139
141
|
```
|
140
142
|
|
141
|
-
|
143
|
+
### Configuring in package.json (Not Recommended)
|
142
144
|
|
143
|
-
In addition to configuration files,
|
145
|
+
In addition to configuration files, you can also set configuration items under the `modernConfig` field in `package.json`, such as:
|
144
146
|
|
145
147
|
```json title="package.json"
|
146
148
|
{
|
@@ -154,20 +156,20 @@ In addition to configuration files, configuration options can also be set the `m
|
|
154
156
|
}
|
155
157
|
```
|
156
158
|
|
157
|
-
Due to the
|
159
|
+
Due to the limitations of the JSON file format, only simple types such as numbers, strings, booleans, and arrays can be defined in `package.json`. When we need to set function-type values, it is recommended to do so in the Modern.js configuration file.
|
158
160
|
|
159
|
-
|
161
|
+
#### Notes
|
160
162
|
|
161
|
-
- It is not recommended to use both `package.json` and `modern.config.
|
162
|
-
- `@modern-js/runtime` exports
|
163
|
+
- It is not recommended to use both `package.json` and `modern.config.js` for configuration simultaneously. If both are used and conflicts arise, Modern.js will prompt an error in the command line.
|
164
|
+
- The `@modern-js/runtime` exports a similarly named [defineConfig](/apis/app/runtime/app/define-config) API, so please be careful to distinguish between them.
|
163
165
|
|
164
|
-
|
166
|
+
### Local Debugging Configuration
|
165
167
|
|
166
|
-
To facilitate local debugging
|
168
|
+
To facilitate local debugging of configurations, Modern.js supports creating a `modern.config.local.(ts|js|mjs)` file in the root directory to override the configuration options in `modern.config.(ts|js|mjs)`.
|
167
169
|
|
168
|
-
|
170
|
+
#### Example
|
169
171
|
|
170
|
-
For example, the
|
172
|
+
For example, if the `modern.config.ts` file in your project is configured with a port number of `3000`:
|
171
173
|
|
172
174
|
```ts title="modern.config.ts"
|
173
175
|
import { defineConfig } from '@modern-js/app-tools';
|
@@ -179,7 +181,7 @@ export default defineConfig({
|
|
179
181
|
});
|
180
182
|
```
|
181
183
|
|
182
|
-
If you need to change the port number to `3001`
|
184
|
+
If you need to change the port number to `3001` for local debugging but do not want to modify the current project's `modern.config.ts` file, you can create a `modern.config.local.ts` file and add the following configuration:
|
183
185
|
|
184
186
|
```ts title="modern.config.local.ts"
|
185
187
|
import { defineConfig } from '@modern-js/app-tools';
|
@@ -191,52 +193,52 @@ export default defineConfig({
|
|
191
193
|
});
|
192
194
|
```
|
193
195
|
|
194
|
-
The configuration in the `modern.config.local.ts` file will be
|
196
|
+
The configuration in the `modern.config.local.ts` file will be deeply merged with the configuration in `modern.config.ts`, overriding the options in `modern.config.ts`, so `server.port` will be changed to `3001`.
|
195
197
|
|
196
|
-
|
198
|
+
#### Notes
|
197
199
|
|
198
200
|
When using `modern.config.local.ts`, please note the following:
|
199
201
|
|
200
|
-
- The `modern.config.local.ts` file
|
201
|
-
- The `modern.config.local.ts` file
|
202
|
-
-
|
202
|
+
- The `modern.config.local.ts` file will only be loaded when executing the `modern dev` or `modern start` commands and will not be loaded during `modern build`.
|
203
|
+
- The priority of the `modern.config.local.ts` file is higher than both `modern.config.ts` and the `modernConfig` field in `package.json`.
|
204
|
+
- Since `modern.config.local.ts` is only used for local debugging, it is not recommended to commit it to the code repository. Ensure that the project's `.gitignore` file includes `modern.config.local.ts` and similar files.
|
203
205
|
|
204
|
-
```bash title=".
|
206
|
+
```bash title=".gitignore"
|
205
207
|
modern.config.local.*
|
206
208
|
```
|
207
209
|
|
208
|
-
|
210
|
+
### Merging Multiple Configurations
|
209
211
|
|
210
|
-
In some cases, you may need to merge multiple configurations into one
|
212
|
+
In some cases, you may need to merge multiple configurations into one. You can use the `mergeConfig` utility function to merge multiple configurations.
|
211
213
|
|
212
|
-
The `mergeConfig` function accepts an array as a parameter,
|
214
|
+
The `mergeConfig` function accepts an array as a parameter, where each item in the array is a configuration object. `mergeConfig` will deeply merge each configuration object in the array, automatically merging multiple function items into an array, and finally returning a merged configuration object.
|
213
215
|
|
214
|
-
|
216
|
+
#### Example
|
215
217
|
|
216
218
|
```ts title="modern.config.ts"
|
217
219
|
import { mergeConfig } from '@modern-js/app-tools';
|
218
220
|
|
219
221
|
const config1 = {
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
222
|
+
dev: {
|
223
|
+
port: 3000,
|
224
|
+
},
|
225
|
+
tools: {
|
226
|
+
postcss: () => console.log('config1');
|
227
|
+
},
|
226
228
|
};
|
227
229
|
const config2 = {
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
230
|
+
dev: {
|
231
|
+
port: 3001,
|
232
|
+
},
|
233
|
+
tools: {
|
234
|
+
postcss: () => console.log('config2');
|
235
|
+
},
|
234
236
|
};
|
235
237
|
|
236
238
|
const mergedConfig = mergeConfig([config1, config2]);
|
237
239
|
```
|
238
240
|
|
239
|
-
In the above example, the merged configuration object
|
241
|
+
In the above example, the merged configuration object will be:
|
240
242
|
|
241
243
|
```ts
|
242
244
|
const mergedConfig = {
|
@@ -249,9 +251,9 @@ const mergedConfig = {
|
|
249
251
|
};
|
250
252
|
```
|
251
253
|
|
252
|
-
|
254
|
+
### Configuration Type Definitions
|
253
255
|
|
254
|
-
Modern.js exports `AppUserConfig` type, which corresponds to the type of Modern.js configuration object:
|
256
|
+
Modern.js exports the `AppUserConfig` type, which corresponds to the type of the Modern.js configuration object:
|
255
257
|
|
256
258
|
```ts title="modern.config.ts"
|
257
259
|
import type { AppUserConfig } from '@modern-js/app-tools';
|
@@ -263,7 +265,7 @@ const config: AppUserConfig = {
|
|
263
265
|
};
|
264
266
|
```
|
265
267
|
|
266
|
-
When using Rspack as the bundler, due to some differences in configuration types between webpack and Rspack, you need to specify `<'rspack'>` generic
|
268
|
+
When using Rspack as the bundler, due to some differences in configuration types between webpack and Rspack, you need to specify the `<'rspack'>` generic for `AppUserConfig`:
|
267
269
|
|
268
270
|
```ts title="modern.config.ts"
|
269
271
|
import type { AppUserConfig } from '@modern-js/app-tools';
|
@@ -274,3 +276,25 @@ const config: AppUserConfig<'rspack'> = {
|
|
274
276
|
},
|
275
277
|
};
|
276
278
|
```
|
279
|
+
|
280
|
+
## Runtime Configuration
|
281
|
+
|
282
|
+
For detailed information on runtime configuration, please refer to the [Introduction to Runtime Configuration](/configure/app/runtime/0-intro.html).
|
283
|
+
|
284
|
+
:::tip
|
285
|
+
If the current Runtime configuration needs to be used both at compile time and runtime, please add the relevant configuration parameters at the plugin registration location.
|
286
|
+
|
287
|
+
```ts title="modern.config.ts"
|
288
|
+
import { defineConfig } from '@modern-js/app-tools';
|
289
|
+
import { statePlugin } from '@modern-js/plugin-state';
|
290
|
+
|
291
|
+
export default defineConfig({
|
292
|
+
plugins: [
|
293
|
+
statePlugin({
|
294
|
+
/** Add parameters here */
|
295
|
+
}),
|
296
|
+
],
|
297
|
+
});
|
298
|
+
```
|
299
|
+
|
300
|
+
:::
|
@@ -1 +1 @@
|
|
1
|
-
["ssr", "streaming-ssr", "ssr-cache", "ssg"]
|
1
|
+
["ssr", "streaming-ssr", "ssr-cache", "ssg", "before-render"]
|
@@ -0,0 +1,115 @@
|
|
1
|
+
# Render Preprocessing
|
2
|
+
|
3
|
+
In certain scenarios, applications need to perform preprocessing operations before rendering. Modern.js recommends using **[Runtime Plugins](/plugin/introduction.html#runtime-plugins)** to implement this type of logic.
|
4
|
+
|
5
|
+
## Defining a Runtime Plugin
|
6
|
+
|
7
|
+
```ts
|
8
|
+
import type { RuntimePluginFuture } from '@modern-js/runtime';
|
9
|
+
|
10
|
+
const myRuntimePlugin = (): RuntimePluginFuture => ({
|
11
|
+
name: 'my-runtime-plugin',
|
12
|
+
setup: (api) => {
|
13
|
+
api.onBeforeRender((context) => {
|
14
|
+
// Logic to execute before rendering
|
15
|
+
console.log('Before rendering:', context);
|
16
|
+
});
|
17
|
+
},
|
18
|
+
});
|
19
|
+
|
20
|
+
export default myRuntimePlugin;
|
21
|
+
```
|
22
|
+
|
23
|
+
## Registering the Plugin
|
24
|
+
|
25
|
+
Register the plugin in your project's `src/modern.runtime.ts` file:
|
26
|
+
|
27
|
+
```ts
|
28
|
+
import { defineRuntimeConfig } from '@modern-js/runtime';
|
29
|
+
import myRuntimePlugin from './plugins/myRuntimePlugin';
|
30
|
+
|
31
|
+
export default defineRuntimeConfig({
|
32
|
+
plugins: [myRuntimePlugin()],
|
33
|
+
});
|
34
|
+
```
|
35
|
+
|
36
|
+
## Use Case -- Global Data Injection
|
37
|
+
|
38
|
+
Through the `context` parameter of the `onBeforeRender` hook, you can inject global data into your application. Application components can access this data using the `useRuntimeContext` Hook.
|
39
|
+
|
40
|
+
:::info
|
41
|
+
|
42
|
+
This feature is particularly useful in the following scenarios:
|
43
|
+
* Applications requiring page-level preliminary data
|
44
|
+
* Custom data injection workflows
|
45
|
+
* Framework migration scenarios (e.g., migrating from Next.js)
|
46
|
+
|
47
|
+
:::
|
48
|
+
|
49
|
+
**Defining a Data Injection Plugin**
|
50
|
+
|
51
|
+
```ts
|
52
|
+
import type { RuntimePluginFuture } from '@modern-js/runtime';
|
53
|
+
|
54
|
+
const dataInjectionPlugin = (): RuntimePluginFuture => ({
|
55
|
+
name: 'data-injection-plugin',
|
56
|
+
setup: api => {
|
57
|
+
api.onBeforeRender(context => {
|
58
|
+
// Inject data into the context
|
59
|
+
context.message = 'Hello World';
|
60
|
+
});
|
61
|
+
},
|
62
|
+
});
|
63
|
+
|
64
|
+
export default dataInjectionPlugin;
|
65
|
+
```
|
66
|
+
|
67
|
+
**Using Injected Data in Components**
|
68
|
+
|
69
|
+
```tsx
|
70
|
+
import { useRuntimeContext } from '@modern-js/runtime';
|
71
|
+
|
72
|
+
export default function MyComponent() {
|
73
|
+
const context = useRuntimeContext();
|
74
|
+
const { message } = context;
|
75
|
+
|
76
|
+
return <div>{message}</div>;
|
77
|
+
}
|
78
|
+
```
|
79
|
+
|
80
|
+
**Using with SSR**
|
81
|
+
|
82
|
+
In SSR scenarios, the browser can access data injected via `onBeforeRender` during server-side rendering. Developers can decide whether to re-fetch data on the browser side to override server data based on their requirements.
|
83
|
+
|
84
|
+
```ts
|
85
|
+
import type { RuntimePluginFuture } from '@modern-js/runtime';
|
86
|
+
|
87
|
+
const dataInjectionPlugin = (): RuntimePluginFuture => ({
|
88
|
+
name: 'data-injection-plugin',
|
89
|
+
setup: api => {
|
90
|
+
api.onBeforeRender(context => {
|
91
|
+
if (process.env.MODERN_TARGET === 'node') {
|
92
|
+
// Set data during server-side rendering
|
93
|
+
context.message = 'Hello World By Server';
|
94
|
+
} else {
|
95
|
+
// Check data during client-side rendering
|
96
|
+
if (!context.message) {
|
97
|
+
// If server data is not available, set client data
|
98
|
+
context.message = 'Hello World By Client';
|
99
|
+
}
|
100
|
+
}
|
101
|
+
});
|
102
|
+
},
|
103
|
+
});
|
104
|
+
|
105
|
+
export default dataInjectionPlugin;
|
106
|
+
```
|
107
|
+
|
108
|
+
## Compatibility Notes
|
109
|
+
|
110
|
+
In earlier versions of Modern.js, it was possible to add render preprocessing logic through the `init` hook in `routes/layout.tsx` and the `App.init` method. These approaches are still **supported**, but we **strongly recommend** implementing with Runtime plugins instead.
|
111
|
+
|
112
|
+
:::warning
|
113
|
+
|
114
|
+
In future versions, the `init` hook in `routes/layout.tsx` and the `App.init` method will be gradually **deprecated**. We recommend migrating to the Runtime plugin approach as soon as possible.
|
115
|
+
:::
|
@@ -480,101 +480,6 @@ The `prefetch` attribute has three optional values:
|
|
480
480
|
|
481
481
|
:::
|
482
482
|
|
483
|
-
|
484
|
-
## Runtime Configuration
|
485
|
-
|
486
|
-
{/* Todo Move to runtime configuration chapter */}
|
487
|
-
|
488
|
-
In the root `<Layout>` component (`routes/layout.ts`), you can dynamically define runtime configuration:
|
489
|
-
|
490
|
-
```tsx title="src/routes/layout.tsx"
|
491
|
-
// Define runtime configuration
|
492
|
-
import type { AppConfig } from '@modern-js/runtime';
|
493
|
-
|
494
|
-
export const config = (): AppConfig => {
|
495
|
-
return {
|
496
|
-
router: {
|
497
|
-
createRoutes() {
|
498
|
-
return [
|
499
|
-
{
|
500
|
-
path: 'modern',
|
501
|
-
element: <div>modern</div>,
|
502
|
-
},
|
503
|
-
];
|
504
|
-
},
|
505
|
-
},
|
506
|
-
};
|
507
|
-
};
|
508
|
-
```
|
509
|
-
|
510
|
-
## Pre-render Hooks
|
511
|
-
|
512
|
-
{/* Todo Move to runtime configuration chapter */}
|
513
|
-
|
514
|
-
In some scenarios, you need to perform certain actions before the application renders. You can define the `init` hook in `routes/layout.tsx`, which will be executed both on the client and server. A basic example is shown below:
|
515
|
-
|
516
|
-
```ts title="src/routes/layout.tsx"
|
517
|
-
import type { RuntimeContext } from '@modern-js/runtime';
|
518
|
-
|
519
|
-
export const init = (context: RuntimeContext) => {
|
520
|
-
// do something
|
521
|
-
};
|
522
|
-
```
|
523
|
-
|
524
|
-
With the `init` hook, you can mount some global data that can be accessed elsewhere in the application via the `runtimeContext` variable:
|
525
|
-
|
526
|
-
:::note
|
527
|
-
This feature is very useful when applications require pre-rendered data, custom data injection, or framework migration (e.g., Next.js).
|
528
|
-
:::
|
529
|
-
|
530
|
-
```ts title="src/routes/layout.tsx"
|
531
|
-
import { RuntimeContext } from '@modern-js/runtime';
|
532
|
-
|
533
|
-
type InitialData = {
|
534
|
-
message: string;
|
535
|
-
}
|
536
|
-
|
537
|
-
export const init = (context: RuntimeContext): InitialData => {
|
538
|
-
return {
|
539
|
-
message: 'Hello World',
|
540
|
-
};
|
541
|
-
};
|
542
|
-
```
|
543
|
-
|
544
|
-
```tsx title="src/routes/page.tsx"
|
545
|
-
import { useRuntimeContext } from '@modern-js/runtime;
|
546
|
-
|
547
|
-
export default () => {
|
548
|
-
const context = useRuntimeContext();
|
549
|
-
const { message } = context.initialData as InitialData;
|
550
|
-
|
551
|
-
return <div>{message}</div>;
|
552
|
-
};
|
553
|
-
```
|
554
|
-
|
555
|
-
With SSR, the browser can obtain the data returned by `init` during SSR. Developers can decide whether to re-fetch the data on the browser side to override the SSR data. For example:
|
556
|
-
|
557
|
-
```tsx title="src/routes/layout.tsx"
|
558
|
-
import type { RuntimeContext } from '@modern-js/runtime;
|
559
|
-
|
560
|
-
export const init = (runtimeContext: RuntimeContext) => {
|
561
|
-
if (process.env.MODERN_TARGET === 'node') {
|
562
|
-
return {
|
563
|
-
message: 'Hello World By Server',
|
564
|
-
};
|
565
|
-
} else {
|
566
|
-
const { context } = runtimeContext;
|
567
|
-
const data = context.getInitData();
|
568
|
-
// If the expected data is not obtained
|
569
|
-
if (!data.message) {
|
570
|
-
return {
|
571
|
-
message: 'Hello World By Client',
|
572
|
-
};
|
573
|
-
}
|
574
|
-
}
|
575
|
-
};
|
576
|
-
```
|
577
|
-
|
578
483
|
<Motivation />
|
579
484
|
|
580
485
|
## FAQ
|
@@ -14,9 +14,9 @@ Through this chapter you can learn:
|
|
14
14
|
|
15
15
|
The routing modes of the current project are divided into the following three types:
|
16
16
|
|
17
|
-
-
|
18
|
-
- Self-
|
19
|
-
- Other (
|
17
|
+
- [Conventional Routing](/guides/concept/entries.html#conventional-routing)
|
18
|
+
- [Self-controlled Routing](/guides/concept/entries.html#self-controlled-routing)
|
19
|
+
- Other (independent installation of `react-router-dom` in the project)
|
20
20
|
|
21
21
|
First, clarify the routing mode of the main application [create a file-based routing main App](#file-based-routing-main-app) or [create a self-controlled main App](#self-controlled-main-app)
|
22
22
|
|
@@ -198,7 +198,6 @@ export default defineConfig({
|
|
198
198
|
},
|
199
199
|
runtime: {
|
200
200
|
router: true,
|
201
|
-
state: true,
|
202
201
|
},
|
203
202
|
deploy: {
|
204
203
|
microFrontend: true,
|
@@ -245,7 +244,6 @@ export default defineConfig({
|
|
245
244
|
},
|
246
245
|
runtime: {
|
247
246
|
router: true,
|
248
|
-
state: true,
|
249
247
|
},
|
250
248
|
deploy: {
|
251
249
|
microFrontend: true,
|
@@ -35,8 +35,10 @@ import MicroRuntimeConfig from '@site-docs-en/components/micro-runtime-config';
|
|
35
35
|
|
36
36
|
This function allows you to pull a list of remote child applications and register them with the runtime framework:
|
37
37
|
|
38
|
-
```ts title="
|
39
|
-
|
38
|
+
```ts title="src/modern.runtime.ts"
|
39
|
+
import { defineRuntimeConfig } from '@modern-js/runtime';
|
40
|
+
|
41
|
+
export default defineRuntimeConfig({
|
40
42
|
masterApp: {
|
41
43
|
manifest: {
|
42
44
|
getAppList: async () => {
|
@@ -1,7 +1,3 @@
|
|
1
|
-
---
|
2
|
-
sidebar_position: 6
|
3
|
-
title: Automatically Actions
|
4
|
-
---
|
5
1
|
# Automatically Generated Actions
|
6
2
|
|
7
3
|
In the [Quick Start](/guides/topic-detail/model/quick-start), we implemented the simplest counter model, which still required 10 lines of code. In fact, Modern.js supports automatically generating commonly used actions based on the declared `state` type, which reduces the amount of boilerplate code. The currently supported types are:
|
@@ -1,8 +1,3 @@
|
|
1
|
-
---
|
2
|
-
sidebar_position: 4
|
3
|
-
title: Derived State
|
4
|
-
---
|
5
|
-
|
6
1
|
# Derived State
|
7
2
|
|
8
3
|
In some scenarios, components need to perform further calculations on the State in Model before they can be used in the components. This logic can be directly written in the component or implemented through derived states in Model. Derived states are defined under the `computed` field in the Model. Depending on the dependencies of the Model and the return type, there are three ways to define derived states.
|
@@ -1,8 +1,3 @@
|
|
1
|
-
---
|
2
|
-
sidebar_position: 5
|
3
|
-
title: Management Effect
|
4
|
-
---
|
5
|
-
|
6
1
|
# Management Effect
|
7
2
|
|
8
3
|
The actions in the model must be pure functions and cannot have any side effects during execution. However, in real-world scenarios, we often encounter many side effects, such as: requesting data from an HTTP API to obtain state data, or modifying `localStorage` or sending events while updating the state. In Reduck, side effects are managed through the model's `effects` function.
|
@@ -1,7 +1,3 @@
|
|
1
|
-
---
|
2
|
-
sidebar_position: 8
|
3
|
-
title: Performance Optimization
|
4
|
-
---
|
5
1
|
# Performance Optimization
|
6
2
|
|
7
3
|
Reduck has already done a lot of performance optimization work internally, so performance issues generally do not need to be considered. However, when performance is more sensitive, or when encountering performance issues, you can consider more targeted performance optimization from the following three aspects.
|