@modern-js/main-doc 2.11.0 → 2.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/docs/en/apis/app/commands.mdx +1 -1
  3. package/docs/en/apis/app/hooks/api/framework/lambda.mdx +2 -2
  4. package/docs/en/apis/app/hooks/api/functions/api.mdx +3 -3
  5. package/docs/en/apis/app/hooks/api/functions/app.mdx +1 -1
  6. package/docs/en/apis/app/hooks/api/test.mdx +2 -1
  7. package/docs/en/apis/app/hooks/config/icon.mdx +46 -6
  8. package/docs/en/apis/app/hooks/config/mock.mdx +1 -1
  9. package/docs/en/apis/app/hooks/config/upload.mdx +1 -1
  10. package/docs/en/apis/app/hooks/server/test.mdx +1 -1
  11. package/docs/en/apis/app/hooks/src/app.mdx +1 -1
  12. package/docs/en/apis/app/hooks/src/pages.mdx +1 -1
  13. package/docs/en/apis/app/runtime/app/define-config.mdx +1 -1
  14. package/docs/en/apis/app/runtime/core/bootstrap.mdx +2 -2
  15. package/docs/en/apis/app/runtime/core/use-loader.mdx +6 -2
  16. package/docs/en/apis/app/runtime/model/auto-actions.mdx +6 -6
  17. package/docs/en/apis/app/runtime/model/connect.mdx +2 -1
  18. package/docs/en/apis/app/runtime/model/create-app.mdx +1 -1
  19. package/docs/en/apis/app/runtime/model/handle-effect.mdx +3 -3
  20. package/docs/en/apis/app/runtime/model/model_.mdx +2 -2
  21. package/docs/en/apis/app/runtime/model/use-model.mdx +2 -2
  22. package/docs/en/apis/app/runtime/model/use-static-model.mdx +2 -2
  23. package/docs/en/apis/app/runtime/model/use-store.mdx +1 -1
  24. package/docs/en/apis/app/runtime/router/router.mdx +1 -0
  25. package/docs/en/apis/app/runtime/ssr/pre-render.mdx +2 -2
  26. package/docs/en/apis/app/runtime/testing/render.mdx +1 -1
  27. package/docs/en/components/enable-bff.mdx +2 -2
  28. package/docs/en/configure/app/html/script-loading.mdx +13 -0
  29. package/docs/en/configure/app/output/ssg.mdx +1 -1
  30. package/docs/en/configure/app/plugins.mdx +3 -3
  31. package/docs/en/configure/app/source/disable-entry-dirs.mdx +1 -1
  32. package/docs/en/configure/app/tools/bundler-chain.mdx +13 -0
  33. package/docs/en/configure/app/tools/swc.mdx +4 -3
  34. package/docs/en/configure/app/usage.mdx +65 -23
  35. package/docs/en/guides/advanced-features/bff/function.mdx +3 -3
  36. package/docs/en/guides/advanced-features/code-split.mdx +2 -2
  37. package/docs/en/guides/advanced-features/eslint.mdx +1 -1
  38. package/docs/en/guides/advanced-features/ssg.mdx +2 -2
  39. package/docs/en/guides/advanced-features/ssr.mdx +45 -8
  40. package/docs/en/guides/basic-features/alias.mdx +1 -1
  41. package/docs/en/guides/basic-features/data-fetch.mdx +1 -1
  42. package/docs/en/guides/basic-features/env-vars.mdx +5 -5
  43. package/docs/en/guides/basic-features/html.mdx +0 -4
  44. package/docs/en/guides/basic-features/routes.mdx +48 -28
  45. package/docs/en/guides/concept/entries.mdx +3 -3
  46. package/docs/en/guides/get-started/introduction.mdx +21 -1
  47. package/docs/en/guides/get-started/quick-start.mdx +6 -23
  48. package/docs/en/guides/get-started/upgrade.mdx +7 -7
  49. package/docs/en/guides/topic-detail/framework-plugin/hook.mdx +1 -1
  50. package/docs/en/guides/topic-detail/generator/codesmith/api/json.mdx +3 -3
  51. package/docs/en/guides/topic-detail/generator/config/app.mdx +1 -1
  52. package/docs/en/guides/topic-detail/generator/config/monorepo.mdx +1 -1
  53. package/docs/en/guides/topic-detail/generator/plugin/api/file/addHelper.mdx +1 -1
  54. package/docs/en/guides/topic-detail/model/manage-effects.mdx +2 -1
  55. package/docs/en/guides/topic-detail/model/model-communicate.mdx +1 -1
  56. package/docs/en/guides/topic-detail/model/use-model.mdx +2 -1
  57. package/docs/en/guides/troubleshooting/builder.mdx +31 -0
  58. package/docs/en/tutorials/foundations/introduction.mdx +0 -2
  59. package/docs/zh/apis/app/hooks/config/icon.mdx +44 -6
  60. package/docs/zh/apis/app/hooks/src/pages.mdx +1 -1
  61. package/docs/zh/apis/app/runtime/core/use-loader.mdx +4 -0
  62. package/docs/zh/apis/app/runtime/model/handle-effect.mdx +2 -1
  63. package/docs/zh/apis/app/runtime/model/model_.mdx +4 -2
  64. package/docs/zh/blog/updates/2022-0708-updates.md +1 -2
  65. package/docs/zh/components/enable-bff.mdx +2 -2
  66. package/docs/zh/configure/app/html/script-loading.mdx +13 -0
  67. package/docs/zh/configure/app/runtime/router.mdx +1 -1
  68. package/docs/zh/configure/app/source/disable-entry-dirs.mdx +2 -2
  69. package/docs/zh/configure/app/tools/bundler-chain.mdx +13 -0
  70. package/docs/zh/configure/app/tools/swc.mdx +4 -3
  71. package/docs/zh/configure/app/usage.mdx +66 -24
  72. package/docs/zh/guides/advanced-features/ssr.mdx +74 -36
  73. package/docs/zh/guides/basic-features/data-fetch.mdx +1 -1
  74. package/docs/zh/guides/basic-features/html.mdx +0 -4
  75. package/docs/zh/guides/basic-features/routes.mdx +64 -24
  76. package/docs/zh/guides/concept/entries.mdx +4 -4
  77. package/docs/zh/guides/get-started/introduction.mdx +21 -1
  78. package/docs/zh/guides/get-started/upgrade.mdx +3 -3
  79. package/docs/zh/guides/topic-detail/changesets/release.mdx +2 -2
  80. package/docs/zh/guides/topic-detail/model/use-model.mdx +2 -1
  81. package/docs/zh/guides/troubleshooting/builder.mdx +31 -0
  82. package/modern.config.ts +3 -2
  83. package/package.json +5 -5
  84. package/src/components/SolutionCards/index.module.scss +62 -0
  85. package/src/components/SolutionCards/index.tsx +26 -0
  86. package/docs/en/configure/app/source/plugin-import.mdx +0 -13
  87. package/docs/zh/configure/app/source/plugin-import.mdx +0 -13
@@ -19,17 +19,33 @@ Server runtime configuration can be configured in the `modern.server-runtime.con
19
19
 
20
20
  ## Configure in the configuration file
21
21
 
22
- Modern.js configuration files are defined in the root path of the project, and supports `.js`, `.ts` and `.mjs` formats:
22
+ Modern.js configuration files are defined in the root path of the project, and supports `.ts`, `.js` and `.mjs` formats:
23
23
 
24
- - `modern.config.js`
25
24
  - `modern.config.ts`
25
+ - `modern.config.js`
26
26
  - `modern.config.mjs`
27
27
 
28
- ### modern.config.js
28
+ ### modern.config.ts (recommended)
29
29
 
30
- You can use JavaScript syntax in the `modern.config.js` file and it is more flexible than in the `package.json` file.
30
+ We recommend using configuration files in `.ts` format, which provides friendly TypeScript type hints to help you avoid configuration errors.
31
31
 
32
- For example, you can define configuration options for function types in `modern.config.js`:
32
+ Import the `defineConfig` tool function from `@modern-js/app-tools`, which will help you with configuration type derivation and type completion:
33
+
34
+ ```ts title="modern.config.ts"
35
+ import { defineConfig } from '@modern-js/app-tools';
36
+
37
+ export default defineConfig({
38
+ source: {
39
+ alias: {
40
+ '@common': './src/common',
41
+ },
42
+ },
43
+ });
44
+ ```
45
+
46
+ ### modern.config.js
47
+
48
+ If you are developing a non-TypeScript project, you can use the configuration file in .js format:
33
49
 
34
50
  ```js title="modern.config.js"
35
51
  export default {
@@ -51,24 +67,6 @@ export default {
51
67
  };
52
68
  ```
53
69
 
54
- ### modern.config.ts (recommended)
55
-
56
- We recommend using configuration files in `.ts` format, which provides friendly TypeScript type hints to help you avoid configuration errors.
57
-
58
- Import the `defineConfig` tool function from `@modern-js/app-tools`, which will help you with configuration type derivation and type completion:
59
-
60
- ```ts title="modern.config.ts"
61
- import { defineConfig } from '@modern-js/app-tools';
62
-
63
- export default defineConfig({
64
- source: {
65
- alias: {
66
- '@common': './src/common',
67
- },
68
- },
69
- });
70
- ```
71
-
72
70
  ### Export Configuration Function
73
71
 
74
72
  Modern.js supports exporting a function in the configuration file, and you can dynamically compute the configuration in the function and return it to Modern.js.
@@ -200,3 +198,47 @@ modern.config.local.ts
200
198
  modern.config.local.js
201
199
  modern.config.local.mjs
202
200
  ```
201
+
202
+ ## Merge Multiple Configurations
203
+
204
+ In some cases, you may need to merge multiple configurations into one configuration. You can use the `mergeConfig` util to merge multiple configurations.
205
+
206
+ The `mergeConfig` function accepts an array as a parameter, and each item in the array is a configuration object. `mergeConfig` will deeply merge each configuration object in the array, automatically merge multiple functions into an array, and returns a merged configuration object.
207
+
208
+ ### Example
209
+
210
+ ```ts title="modern.config.ts"
211
+ import { mergeConfig } from '@modern-js/app-tools';
212
+
213
+ const config1 = {
214
+ dev: {
215
+ port: 3000,
216
+ },
217
+ tools: {
218
+ postcss: () => console. log('config1');
219
+ },
220
+ };
221
+ const config2 = {
222
+ dev: {
223
+ port: 3001,
224
+ },
225
+ tools: {
226
+ postcss: () => console. log('config2');
227
+ },
228
+ };
229
+
230
+ const mergedConfig = mergeConfig([config1, config2]);
231
+ ```
232
+
233
+ In the above example, the merged configuration object is:
234
+
235
+ ```ts
236
+ const mergedConfig = {
237
+ dev: {
238
+ port: 3001,
239
+ },
240
+ tools: {
241
+ postcss: [() => console.log('config1'), () => console.log('config2')],
242
+ },
243
+ };
244
+ ```
@@ -114,7 +114,7 @@ For example, following the example, a `GET` interface can be exported.
114
114
  export const get = async () => {
115
115
  return {
116
116
  name: 'Modern.js',
117
- desc: '现代 web 工程方案',
117
+ desc: 'Web engineering system',
118
118
  };
119
119
  };
120
120
  ```
@@ -125,14 +125,14 @@ Following the example below, a `POST` interface can be exported.
125
125
  export const post = async () => {
126
126
  return {
127
127
  name: 'Modern.js',
128
- desc: '现代 web 工程方案',
128
+ desc: 'Web engineering system',
129
129
  };
130
130
  };
131
131
  ```
132
132
 
133
133
  - Modern.js supports 9 definitions for HTTP Method: `GET`、`POST`、`PUT`、`DELETE`、`CONNECT`、`TRACE`、`PATCH`、`OPTION`、`HEAD`, can be exported using these methods as functions.
134
134
 
135
- - The name is size insensitiveif `GET`,can write `get`、`Get`、`GEt`、`GET`,can be accurately identified. But default export as `export default xxx` will be map to `Get`.
135
+ - The name is size insensitive, if `GET`, can write `get`、`Get`、`GEt`、`GET`, can be accurately identified. But default export as `export default xxx` will be map to `Get`.
136
136
 
137
137
  - Multiple functions of different Methods can be defined in one file, but if multiple functions of the same Method are defined, only the first will take effect.
138
138
 
@@ -17,7 +17,7 @@ When you use Modern.js [Conventional routing](/guides/basic-features/routes#conv
17
17
 
18
18
  ## import
19
19
 
20
- use dynamic `import()`,`import` The JS modules pass to this API will be packaged into a separate JS file as a new packaging entry, for example:
20
+ use dynamic `import()`, `import` The JS modules pass to this API will be packaged into a separate JS file as a new packaging entry, for example:
21
21
 
22
22
  ```ts
23
23
  import('./math').then(math => {
@@ -60,7 +60,7 @@ For detail, see [React lazy](https://reactjs.org/docs/code-splitting.html#reactl
60
60
 
61
61
  ## loadable
62
62
 
63
- use `loadable` APIfor example:
63
+ use `loadable` API, for example:
64
64
 
65
65
  ```ts
66
66
  import loadable from '@modern-js/runtime/loadable';
@@ -4,7 +4,7 @@ sidebar_position: 8
4
4
  ---
5
5
  # ESLint
6
6
 
7
- **Modern.js ESLint Rules** is the full set of **ESLint** rulesincludes `@modern-js` (Lint rules for Node.js projects) and `@modern-js-app` (Lint rules for web projects).
7
+ **Modern.js ESLint Rules** is the full set of **ESLint** rules, includes `@modern-js` (Lint rules for Node.js projects) and `@modern-js-app` (Lint rules for web projects).
8
8
 
9
9
  More ESLint usage is described below with specific questions.
10
10
 
@@ -13,7 +13,7 @@ First need to execute `pnpm run new` to enable the SSG features:
13
13
  ? Enable features Enable SSG
14
14
  ```
15
15
 
16
- After execute scriptregister SSG plugin in `modern.config.ts`:
16
+ After execute script, register SSG plugin in `modern.config.ts`:
17
17
 
18
18
  ```ts title="modern.config.ts"
19
19
  import ssgPlugin from '@modern-js/plugin-ssg';
@@ -106,7 +106,7 @@ export default defineConfig({
106
106
  });
107
107
  ```
108
108
 
109
- run `pnpm run build` and `pnpm run serve`,access `http://localhost:8080/about`. In the Preview view, you can see that the page has been rendered.
109
+ run `pnpm run build` and `pnpm run serve`, access `http://localhost:8080/about`. In the Preview view, you can see that the page has been rendered.
110
110
 
111
111
  Looking at the bundle file, a new `main/about/index.html` file has been added in the `dist/` directory.
112
112
 
@@ -246,6 +246,21 @@ if (process.env.MODERN_TARGET === 'browser') {
246
246
  }
247
247
  ```
248
248
 
249
+ After packaging in the development environment, SSR and CSR artifacts will be compiled into the following content. Therefore, there will be no more errors due to Web API in the SSR environment:
250
+
251
+ ```ts
252
+ // SSR production
253
+ if (false) {
254
+ }
255
+
256
+ // CSR production
257
+ if (true) {
258
+ document.addEventListener('load', () => {
259
+ console.log('document load');
260
+ });
261
+ }
262
+ ```
263
+
249
264
  :::note
250
265
  For more information, see [environment variables](/guides/basic-features/env-vars).
251
266
 
@@ -253,7 +268,9 @@ For more information, see [environment variables](/guides/basic-features/env-var
253
268
 
254
269
  ### Use File Suffix
255
270
 
256
- In the second case, the Treeshaking method does not guarantee that the code is completely separated. Modern.js also supports the packaging file of SSR Bundle and CSR Bundle products through the file suffixed with `.node.`.
271
+ However, in the second case, for example, when `fs-extra` is imported into the code, it internally uses the Node API with side effects. If it is directly referenced in the component, it will cause CSR loading errors.
272
+
273
+ Env vars is not effective in this situation. Modern.js also supports distinguishing SSR Bundle and CSR Bundle packaging files through files with the `.node.` suffix.
257
274
 
258
275
  For example, the import of `fs-extra` in the code, when it is directly referenced to the component, will cause the CSR to load an error. You can create `.ts` and `.node.ts` files of the same name as a layer of proxy:
259
276
 
@@ -280,7 +297,27 @@ export const loader = () => {
280
297
 
281
298
  ### Independent File
282
299
 
283
- Both of the above methods will bring some burden to the developer. Modern.js based on [Nested Routing](/guides/basic-features/routes) developed and designed [Data Fetch](/guides/basic-features/data-fetch) to separate CSR and SSR code.
300
+ Both of the above methods can bring some mental burden to developers. In real business scenarios, we found that most of the mixed Node/Web code occurs in data requests.
301
+
302
+ Therefore, Modern.js developed a [Data Fetch](/guides/basic-features/data-fetch) to separate CSR and SSR code based on [Nested Routing](/guides/basic-features/routes).
303
+
304
+ We can separate **data request** and **component code** by using independent files. Write component logic in `routes/page.tsx` and data request logic in `routes/page.loader.ts`.
305
+
306
+ ```ts title="routes/page.tsx"
307
+ export default Page = () => {
308
+ return <div>Hello World<div>
309
+ }
310
+ ```
311
+
312
+ ```ts title="routes/page.loader.tsx"
313
+ import fse from 'fs-extra';
314
+ export default () => {
315
+ const file = fse.readFileSync('./myfile');
316
+ return {
317
+ ...
318
+ };
319
+ }
320
+ ```
284
321
 
285
322
  ## Remote Request
286
323
 
@@ -317,7 +354,7 @@ The streaming SSR of Modern.js is implemented based on React Router, and the mai
317
354
 
318
355
  ### Return async data
319
356
 
320
- ```ts title='page.loader.ts'
357
+ ```ts title="page.loader.ts"
321
358
  import { defer, type LoaderFunctionArgs } from '@modern-js/runtime/router';
322
359
 
323
360
  interface User {
@@ -351,7 +388,7 @@ therefore, the parameter passed to `defer` is `{data: user}`.
351
388
 
352
389
  `defer` can also receive asynchronous data and synchronous data at the same time. For example:
353
390
 
354
- ```ts title='page.loader.ts'
391
+ ```ts title="page.loader.ts"
355
392
 
356
393
  // skip some codes
357
394
 
@@ -388,7 +425,7 @@ export default ({ params }: LoaderFunctionArgs) => {
388
425
 
389
426
  Use the `Await` component to render the data returned asynchronously from the Data Loader. For example:
390
427
 
391
- ```tsx title='page.tsx'
428
+ ```tsx title="page.tsx"
392
429
  import { Await, useLoaderData } from '@modern-js/runtime/router';
393
430
  import { Suspense } from 'react';
394
431
  import type { Data } from './page.loader';
@@ -430,7 +467,7 @@ So, here we import like this: `import type { Data } from './page.loader'`;
430
467
 
431
468
  You can also get the asynchronous data returned by Data Loader through `useAsyncValue`. For example:
432
469
 
433
- ```tsx title='page.tsx'
470
+ ```tsx title="page.tsx"
434
471
  import { useAsyncValue } from '@modern-js/runtime/router';
435
472
 
436
473
  // skip some codes
@@ -468,7 +505,7 @@ export default Page;
468
505
  The `errorElement` property of the `Await` component can be used to handle errors thrown when the Data Loader executes or when a child component renders.
469
506
  For example, we intentionally throw an error in the Data Loader function:
470
507
 
471
- ```ts title='page.loader.ts'
508
+ ```ts title="page.loader.ts"
472
509
  import { defer } from '@modern-js/runtime/router';
473
510
 
474
511
  export default () => {
@@ -484,7 +521,7 @@ export default () => {
484
521
 
485
522
  Then use `useAsyncError` to get the error, and assign the component used to render the error to the `errorElement` property of the `Await` component:
486
523
 
487
- ```tsx title='page.ts'
524
+ ```tsx title="page.ts"
488
525
  import { Await, useAsyncError, useLoaderData } from '@modern-js/runtime/router';
489
526
  import { Suspense } from 'react';
490
527
 
@@ -4,7 +4,7 @@ sidebar_position: 8
4
4
  ---
5
5
  # Alias
6
6
 
7
- Modern.js allow aliases in JS and CSSand the following aliases are built in:
7
+ Modern.js allow aliases in JS and CSS, and the following aliases are built in:
8
8
 
9
9
  ```js
10
10
  {
@@ -223,7 +223,7 @@ Currently, only CSR is supported, so stay tuned for Streaming SSR.
223
223
  Add the following code to `user/layout.loader.ts`:
224
224
 
225
225
  ```ts title="routes/user/layout.loader.ts"
226
- import { defer } from "@edenx/runtime/router"
226
+ import { defer } from "@modern-js/runtime/router"
227
227
 
228
228
  const loader = () =>
229
229
  defer({
@@ -6,7 +6,7 @@ sidebar_position: 7
6
6
 
7
7
  Modern.js provides support for environment variables, including built-in environment variables and custom environment variables.
8
8
 
9
- ## Built-in Environment
9
+ ## Built-in Environment Variables
10
10
 
11
11
  ### ASSET_PREFIX
12
12
 
@@ -31,7 +31,7 @@ MODERN_ENV priority is higher than NODE_ENV.
31
31
 
32
32
  ### MODERN_TARGET
33
33
 
34
- Auto inject when use `@modern-js/runtime`,Used to distinguish between SSR and CSR environments. Developers can judge by themselves in the code, and dead code will be removed by default when building.
34
+ Auto inject when use `@modern-js/runtime`, Used to distinguish between SSR and CSR environments. Developers can judge by themselves in the code, and dead code will be removed by default when building.
35
35
 
36
36
  ```ts title="App.tsx"
37
37
  function App() {
@@ -93,7 +93,7 @@ The `.env` file follows the following loading rules:
93
93
 
94
94
  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.
95
95
 
96
- For example, when starting a project with the following commandthe `.env` and `.env.staging` will load:
96
+ For example, when starting a project with the following command, the `.env` and `.env.staging` will load:
97
97
 
98
98
  ```shell
99
99
  MODERN_ENV=staging pnpm run dev
@@ -129,7 +129,7 @@ In custom HTML templates, you can also use such environment variables directly.
129
129
 
130
130
  ### Any Other Names
131
131
 
132
- If you need to use environment variables with any other names in your codeyou can config [`source.globalVars`](/configure/app/source/global-vars), for example:
132
+ If you need to use environment variables with any other names in your code, you can config [`source.globalVars`](/configure/app/source/global-vars), for example:
133
133
 
134
134
  ```ts title="modern.config.ts"
135
135
  export default defineConfig({
@@ -171,4 +171,4 @@ const foo = TWO;
171
171
  const foo = 1 + 1;
172
172
  ```
173
173
 
174
- 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 aboveat this time, we need use [`source.define`](/configure/app/source/define).
174
+ 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).
@@ -173,10 +173,6 @@ In the application root directory, create the `config/html/` directory, which su
173
173
  <%= meta %>
174
174
  <title><%= title %></title>
175
175
  <%= topTemplate %>
176
-
177
- <script>
178
- window.__assetPrefix__ = '<%= assetPrefix %>';
179
- </script>
180
176
  <%= headTemplate %>
181
177
  {/* webpack inject css */}
182
178
  </head>
@@ -2,6 +2,7 @@
2
2
  title: Routes
3
3
  sidebar_position: 1
4
4
  ---
5
+
5
6
  # Routes
6
7
 
7
8
  Modern.js build-in provides partial support for [React Router 6](https://reactrouter.com/en/main) and provides various types of routing modes. According to different [entry](/guides/concept/entries) types, routing is divided into three modes, namely **Conventional routing**, **Self-controlled routing** and **Other**.
@@ -57,8 +58,8 @@ Similarly, `routes/user/layout.tsx` will be used as a layout component for all c
57
58
  ```tsx
58
59
  <Layout>
59
60
  <UserLayout>
60
- <UserPage>
61
- <UserLayout>
61
+ <UserPage />
62
+ </UserLayout>
62
63
  </Layout>
63
64
  ```
64
65
 
@@ -106,8 +107,8 @@ In order to facilitate the introduction of the relationship between `<Layout>` a
106
107
  ```tsx
107
108
  <Layout>
108
109
  <UserLayout>
109
- <UserPage>
110
- <UserLayout>
110
+ <UserPage />
111
+ </UserLayout>
111
112
  </Layout>
112
113
  ```
113
114
 
@@ -158,10 +159,9 @@ The `routes/$.tsx` component is rendered when accessing any path that does not m
158
159
  import { useParams } from '@modern-js/runtime/router';
159
160
  // When the path is `/aaa/bbb`
160
161
  const params = useParams();
161
- params['*'] // => 'aaa/bbb'
162
+ params['*']; // => 'aaa/bbb'
162
163
  ```
163
164
 
164
-
165
165
  ### Layout with No Path
166
166
 
167
167
  When a directory name begins with `__`, the corresponding directory name is not converted to the actual routing path, such as the following file directory:
@@ -230,15 +230,15 @@ When a route jumps from `/` to `/blog`, if the JS Chunk of the `<Blog>` componen
230
230
  You can redirect routes by creating a [`data loader`](/guides/basic-features/data-fetch) file, Suppose you have the file `routes/user/page.tsx` and you want to redirect the route corresponding to this file, you can create the file `routes/user/page.loader.ts`:
231
231
 
232
232
  ```ts title="routes/user/page.loader.ts"
233
- import { redirect } from "@edenx/runtime/router"
233
+ import { redirect } from '@modern-js/runtime/router';
234
234
 
235
235
  export default () => {
236
236
  const user = await getUser();
237
- if(!user){
237
+ if (!user) {
238
238
  return redirect('/login');
239
239
  }
240
240
  return null;
241
- }
241
+ };
242
242
  ```
243
243
 
244
244
  ### ErrorBoundary
@@ -255,15 +255,15 @@ Within the `<ErrorBoundary>` component, you can use [useRouteError](/apis/app/ru
255
255
 
256
256
  ```tsx
257
257
  import { useRouteError } from '@modern-js/runtime/router';
258
- export default const ErrorBoundary = () => {
258
+ export const ErrorBoundary = () => {
259
259
  const error = useRouteError();
260
260
  return (
261
261
  <div>
262
- <h1>{error.status}</h1>
263
- <h2>{error.message}</h2>
262
+ <h1>{error.status}</h1>
263
+ <h2>{error.message}</h2>
264
264
  </div>
265
- )
266
- }
265
+ );
266
+ };
267
267
  ```
268
268
 
269
269
  ### Hooks before rendering
@@ -286,15 +286,13 @@ This feature is useful when the application requires pre-page data, custom data
286
286
  :::
287
287
 
288
288
  ```ts title="src/routes/layout.tsx"
289
- import {
290
- RuntimeContext,
291
- } from '@modern-js/runtime';
289
+ import { RuntimeContext } from '@modern-js/runtime';
292
290
 
293
291
  export const init = (context: RuntimeContext) => {
294
292
  return {
295
293
  message: 'Hello World',
296
- }
297
- }
294
+ };
295
+ };
298
296
  ```
299
297
 
300
298
  ```tsx title="src/routes/page.tsx"
@@ -305,7 +303,7 @@ export default () => {
305
303
  const { message } = context.getInitData();
306
304
 
307
305
  return <div>{message}</div>;
308
- }
306
+ };
309
307
  ```
310
308
 
311
309
  When working with SSR, the browser side can get the data returned by `init` during SSR, and the developer can decide whether to retrieve the data on the browser side to overwrite the SSR data, for example:
@@ -317,18 +315,18 @@ export const init = (context: RuntimeContext) => {
317
315
  if (process.env.MODERN_TARGET === 'node') {
318
316
  return {
319
317
  message: 'Hello World By Server',
320
- }
318
+ };
321
319
  } else {
322
320
  const { context } = runtimeContext;
323
321
  const data = context.getInitData();
324
322
  // If do not get the expected data
325
323
  if (!data.message) {
326
324
  return {
327
- message: 'Hello World By Client'
328
- }
325
+ message: 'Hello World By Client',
326
+ };
329
327
  }
330
328
  }
331
- }
329
+ };
332
330
  ```
333
331
 
334
332
  ### Runtime Configuration
@@ -341,12 +339,34 @@ import type { AppConfig } from '@modern-js/runtime';
341
339
  export const config = (): AppConfig => {
342
340
  return {
343
341
  router: {
344
- supportHtml5History: false
345
- }
346
- }
342
+ supportHtml5History: false,
343
+ },
344
+ };
347
345
  };
348
346
  ```
349
347
 
348
+ ### Prefetch
349
+
350
+ When using convention-based routing, Modern.js will automatically split chunks according to the route, and when the user accesses a specific route, the corresponding resources will be loaded automatically, which can effectively reduce the first screen loading time. However, this also brings a problem, when the user accesses a route, if the asset corresponding to that route is not yet loaded, a white screen will appear.
351
+
352
+ In this case you can show a custom `loading` component by defining a `loading` component before the static resource is loaded.
353
+
354
+ To further improve the user experience and reduce time of loading, Modern.js supports defining the `prefetch` property on the Link component to load static resources and data in advance, with three optional values for the `prefetch` property:
355
+
356
+ ```
357
+ <Link prefetch="intent" to="page">
358
+ ```
359
+
360
+ :::info
361
+ - This feature is currently only supported in Webpack projects, not in Rspack projects.
362
+ - Prefetching of data will only prefetch the data returned from the data loader of the SSR project.
363
+
364
+ :::
365
+
366
+ - `none`, the default value, will not do prefetch, no additional behavior.
367
+ - `intent`, the value we recommend for most scenarios, will automatically start loading the corresponding resources and the data defined in the data loader when you mouse over the Link, and will automatically unload it when the mouse is moved away. In our tests, even a direct click can reduce the loading time by about 200ms.
368
+ - `render`, when the Link component renders, it will load the corresponding resources and the data defined in the data loader.
369
+
350
370
  ## Self-controlled routing
351
371
 
352
372
  With `src/App.tsx` as the agreed entry, Modern.js will not do additional operations with multiple routes, developers can use the React Router 6 API for development by themselves, for example:
@@ -367,7 +387,7 @@ export default () => {
367
387
  ```
368
388
 
369
389
  :::note
370
- Modern.js has a series of resource loading and rendering optimizations to the default convention-based routing, and provides out-of-the-box SSR capabilities, when using self-directed routing, need to be packaged by the developer, and it is recommended that developers use convention-based routing.
390
+ Modern.js has a series of resource loading and rendering optimizations to the default convention-based routing, and provides out-of-the-box SSR capabilities, when using self-directed routing, need to be packaged by the developer, and it is recommended that developers use convention-based routing.
371
391
 
372
392
  :::
373
393
 
@@ -132,9 +132,9 @@ The content of the file generated by Modern.js is as follows:
132
132
  ```js
133
133
  import React from 'react';
134
134
  import ReactDOM from 'react-dom/client';
135
- import customBootstrap from '@_edenx_src/index.tsx';
136
- import App from '@_edenx_src/App';
137
- import { router, state } from '@edenx/runtime/plugins';
135
+ import customBootstrap from '@_modern_js_src/index.tsx';
136
+ import App from '@_modern_js_src/App';
137
+ import { router, state } from '@modern-js/runtime/plugins';
138
138
 
139
139
  const IS_BROWSER = typeof window !== 'undefined' && window.name !== 'nodejs';
140
140
  const MOUNT_ID = 'root';
@@ -9,7 +9,27 @@ sidebar_position: 1
9
9
 
10
10
  Currently, Modern.js includes three solutions, each targeting at different development scenarios: web application development, npm package development, and document site development:
11
11
 
12
- ![](https://lf3-static.bytednsdoc.com/obj/eden-cn/zq-uylkvT/ljhwZthlaukjlkulzlp/Solutions-en-0945.png)
12
+ import { SolutionCards } from '@site/src/components/SolutionCards';
13
+
14
+ <SolutionCards
15
+ cards={[
16
+ {
17
+ title: 'Modern.js Framework',
18
+ description: 'For web application development',
19
+ link: 'http://modernjs.dev/en/',
20
+ },
21
+ {
22
+ title: 'Modern.js Module',
23
+ description: 'For npm package development',
24
+ link: 'http://modernjs.dev/module-tools/en/',
25
+ },
26
+ {
27
+ title: 'Modern.js Doc',
28
+ description: 'For document site development',
29
+ link: 'http://modernjs.dev/doc-tools/',
30
+ },
31
+ ]}
32
+ />
13
33
 
14
34
  As a part of the Modern.js engineering system, each of these solutions can be used separately and has its own independent documentation site. Developers can choose one or more solutions as needed.
15
35
 
@@ -7,39 +7,22 @@ sidebar_position: 2
7
7
 
8
8
  ## Environment
9
9
 
10
- ### Node.js
10
+ import Prerequisites from "@site-docs-en/components/prerequisites"
11
11
 
12
- Requires [Node.js LTS](https://github.com/nodejs/Release) and ensures that the Node version is greater than or equal to 14.19.0, 16.x version is recommended.
13
-
14
- Modern.js recommend installing [nvm](https://github.com/nvm-sh/nvm#install--update-script) in the development environment and integrating [script to automatically switch node versions](https://github.com/nvm-sh/nvm#deeper-shell-integration) in the shell.
15
-
16
- Then there is a `.nvmrc` file with the content of `lts/fermium` or `lts/gallium` in the root directory of the repository, it will automatically install or switch to the correct Node.js version when entering the repository.
17
-
18
- ### pnpm
19
-
20
- [pnpm](https://pnpm.io/installation) is recommended for package management.
21
-
22
- ```bash
23
- npm install -g pnpm@7
24
- ```
25
-
26
- :::note
27
- Modern.js also supports other package managers, such as `yarn` or `npm`.
28
-
29
- :::
12
+ <Prerequisites />
30
13
 
31
14
  ## Installation
32
15
 
33
- Modern.js provides the `@modern-js/create` tool to create projects. Don't install globally, use `npx` to run on demand.
16
+ Modern.js provides the `@modern-js/create` tool for creating new projects. You can use `npx` to run it.
34
17
 
35
- Projects can be created using an existing empty directory:
18
+ You can create the project in an existing empty directory:
36
19
 
37
20
  ```bash
38
21
  mkdir myapp && cd myapp
39
22
  npx @modern-js/create
40
23
  ```
41
24
 
42
- Projects can also be created directly from the new directory:
25
+ You can also directly create the project as a new folder:
43
26
 
44
27
  ```bash
45
28
  npx @modern-js/create myapp
@@ -161,4 +144,4 @@ Open http://localhost:8000/ in the browser and the content should be the same as
161
144
 
162
145
  ## Deploy
163
146
 
164
- After the local verification is completed, the products under `dist/` can be organized into the structure required by the server for deployment.
147
+ Once the local verification is complete, the outputs under `dist` folder can be organized into the structure required by the server for deployment.