@modern-js/main-doc 2.19.2-alpha.0 → 2.21.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/docs/en/apis/app/runtime/router/router.mdx +22 -0
  3. package/docs/en/components/init-rspack-app.mdx +1 -1
  4. package/docs/en/components/ua-polyfill.mdx +40 -0
  5. package/docs/en/configure/app/output/temp-dir.mdx +24 -0
  6. package/docs/en/configure/app/security/nonce.mdx +13 -0
  7. package/docs/en/guides/advanced-features/compatibility.mdx +22 -40
  8. package/docs/en/guides/advanced-features/rspack-start.mdx +38 -0
  9. package/docs/en/guides/basic-features/css.mdx +37 -27
  10. package/docs/en/guides/basic-features/data-fetch.mdx +56 -69
  11. package/docs/en/guides/basic-features/routes.mdx +189 -93
  12. package/docs/en/guides/get-started/quick-start.mdx +2 -2
  13. package/docs/en/guides/topic-detail/framework-plugin/extend.mdx +1 -1
  14. package/docs/en/guides/topic-detail/framework-plugin/hook-list.mdx +44 -4
  15. package/docs/en/guides/topic-detail/generator/codesmith/develop.mdx +1 -1
  16. package/docs/en/guides/topic-detail/generator/plugin/abstract.mdx +1 -1
  17. package/docs/en/guides/topic-detail/generator/plugin/develop.mdx +1 -1
  18. package/docs/en/guides/topic-detail/generator/plugin/use.mdx +1 -1
  19. package/docs/en/guides/topic-detail/generator/project.mdx +4 -4
  20. package/docs/en/guides/topic-detail/micro-frontend/c02-development.mdx +4 -4
  21. package/docs/en/tutorials/first-app/c01-start.mdx +1 -1
  22. package/docs/zh/apis/app/runtime/router/router.mdx +22 -0
  23. package/docs/zh/components/init-rspack-app.mdx +1 -1
  24. package/docs/zh/components/ua-polyfill.mdx +39 -0
  25. package/docs/zh/configure/app/output/temp-dir.mdx +22 -0
  26. package/docs/zh/configure/app/security/nonce.mdx +13 -0
  27. package/docs/zh/guides/advanced-features/compatibility.mdx +14 -51
  28. package/docs/zh/guides/advanced-features/rspack-start.mdx +37 -0
  29. package/docs/zh/guides/basic-features/data-fetch.mdx +14 -14
  30. package/docs/zh/guides/basic-features/routes.mdx +22 -22
  31. package/docs/zh/guides/get-started/quick-start.mdx +2 -2
  32. package/docs/zh/guides/topic-detail/changesets/changelog.mdx +1 -1
  33. package/docs/zh/guides/topic-detail/changesets/commit.mdx +1 -1
  34. package/docs/zh/guides/topic-detail/changesets/release-note.mdx +1 -1
  35. package/docs/zh/guides/topic-detail/framework-plugin/extend.mdx +1 -1
  36. package/docs/zh/guides/topic-detail/framework-plugin/hook-list.mdx +45 -5
  37. package/docs/zh/guides/topic-detail/generator/codesmith/develop.mdx +1 -1
  38. package/docs/zh/guides/topic-detail/generator/plugin/abstract.mdx +1 -1
  39. package/docs/zh/guides/topic-detail/generator/plugin/develop.mdx +1 -1
  40. package/docs/zh/guides/topic-detail/generator/plugin/use.mdx +1 -1
  41. package/docs/zh/guides/topic-detail/generator/project.mdx +4 -4
  42. package/docs/zh/guides/topic-detail/micro-frontend/c02-development.mdx +4 -4
  43. package/docs/zh/tutorials/first-app/c01-start.mdx +1 -1
  44. package/package.json +7 -7
  45. package/src/components/ShowcaseList/index.tsx +4 -4
@@ -1,27 +1,27 @@
1
1
  ---
2
- title: Fetch Data
2
+ title: Data Fetching
3
3
  sidebar_position: 3
4
4
  ---
5
- # Fetch Data
5
+ # Data Fetching
6
6
 
7
- Modern.js provides out of the box fetching data capabilities, developers can use these APIs to develop in CSR and SSR environments isomorphic.
7
+ Modern.js provides out-of-the-box data fetching capabilities, allowing developers to develop in an isomorphic way in both client-side and server-side code.
8
8
 
9
- It should be noted that these APIs do not help applications to initiate requests, but help developers better manage the relationship between data and routing.
9
+ It should be noted that these APIs do not help applications initiate requests, but rather help developers better manage data and improve project performance.
10
10
 
11
- ## Data loader(recommend)
11
+ ## Data Loader (Recommended)
12
12
 
13
- Modern.js recommends the use of conventional routing for route management. With Modern.js' [conventional (nested) routing](/guides/basic-features/routes#conventional-routing), each routing component (`layout.ts` or `page.ts`) can have a `loader` file with the same name that can be executed before the component renders, providing data to the routing component.
13
+ Modern.js recommends using [conventional routing](/guides/basic-features/routes) for routing management. Through Modern.js's [conventional (nested) routing](/guides/basic-features/routes#conventional-routing), each routing component (`layout.ts` or `page.ts`) can have a same-named `loader` file. The `loader` file needs to export a function that will be executed before the component is rendered to provide data for the routing component.
14
14
 
15
15
  :::info
16
- Modern.js v1 supports getting data by [useLoader](#useloaderold), which is no longer the recommended usage and it is not recommended to mix both except for migration process.
16
+ Modern.js v1 supports fetching data via [useLoader](/guides/basic-features/data-fetch.html#useloader-(old-version)), which is no longer the recommended usage. We do not recommend mixing the two except during the migration process.
17
17
 
18
18
  :::
19
19
 
20
- ### Basic example
20
+ ### Basic Example
21
21
 
22
- A routing component such as `layout.ts` or `page.ts` can define a `loader` file with the same name. The `loader` file exports a function that provides the data required by the component, which is then get data by the `useLoaderData` function in the routing component, as in the following example:
22
+ Routing components such as `layout.ts` or `page.ts` can define a same-named `loader` file. The function exported by the `loader` file provides the data required by the component, and then the data is obtained in the routing component through the `useLoaderData` function, as shown in the following example:
23
23
 
24
- ```
24
+ ```bash
25
25
  .
26
26
  └── routes
27
27
  ├── layout.tsx
@@ -56,28 +56,28 @@ export default async (): Promise<ProfileData> => {
56
56
  ```
57
57
 
58
58
  :::caution
59
- Here the routing component and the `loader` file share a type, should use the `import type` syntax.
59
+ Here, routing components and `loader` files share a type, so the `import type` syntax should be used.
60
60
 
61
61
  :::
62
62
 
63
- In a CSR environment, the `loader` function is executed on the client side, and the browser API can be used within the `loader` function (but it is usually not needed and not recommended).
63
+ In the CSR environment, the `loader` function is executed on the client and can use browser APIs (although it is not necessary and not recommended).
64
64
 
65
- In an SSR environment, the `loader` function will only be executed on the server side, regardless of the first screen or the navigation on the client side, where any Node.js API can be called, and any dependencies and code used here will not be included in the client bundle.
65
+ In the SSR environment, whether it is the first screen or client navigation, the `loader` function will only be executed on the server, and any Node.js API can be called here. Also, any dependencies and code used here will not be included in the client's bundle.
66
66
 
67
67
  :::info
68
- In later versions, Modern.js may support `loader` functions running on the server side as well in CSR environments to improve performance and security, so here it is recommended to keep the loader as pure as possible and only do data fetching scenarios.
68
+ In future versions, Modern.js may support running the `loader` function on the server in the CSR environment to improve performance and security. Therefore, it is recommended to ensure that the `loader` function is as pure as possible and only used for data fetching scenarios.
69
69
 
70
70
  :::
71
71
 
72
- When navigating on the client side, all loader functions under `/user` and `/user/profile` are executed (requested) in parallel based on Modern.js's [conventional routing](/guides/basic-features/routes), i.e. when accessing `/user/profile`, the loader functions under `/user` and `/user/profile` are executed (requested) in parallel to improve client-side performance.
72
+ When navigating on the client based on Modern.js's [conventional routing](/guides/basic-features/routes), all `loader` functions will be executed in parallel (requested). That is, when accessing `/user/profile`, the loader functions under `/user` and `/user/profile` will be executed in parallel (requested) to improve the performance of the client.
73
73
 
74
- ### `loader` function
74
+ ### The `loader` Function
75
75
 
76
76
  The `loader` function has two input parameters:
77
77
 
78
- ##### `Params`
78
+ #### `params`
79
79
 
80
- When a routing file is passed through `[]`, it is passed as a [dynamic route](/guides/basic-features/routes#dynamic-route) and the dynamic route fragment is passed as an argument to the loader function:
80
+ When the route file is accessed through `[]`, it is used as [dynamic routing](/guides/basic-features/routes#dynamic-routing), and the dynamic routing fragment is passed as a parameter to the `loader` function:
81
81
 
82
82
  ```tsx
83
83
  // routes/user/[id]/page.loader.tsx
@@ -90,13 +90,13 @@ export default async ({ params }: LoaderFunctionArgs) => {
90
90
  };
91
91
  ```
92
92
 
93
- When accessing `/user/123`, the parameters of the `loader` function are `{ params: { id: '123' } }`.
93
+ When accessing `/user/123`, the parameter of the `loader` function is `{ params: { id: '123' } }`.
94
94
 
95
95
  #### `request`
96
96
 
97
97
  `request` is a [Fetch Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) instance.
98
98
 
99
- A common usage scenario is to obtain query parameters via `request`:
99
+ A common usage scenario is to get query parameters through `request`:
100
100
 
101
101
  ```tsx
102
102
  // routes/user/[id]/page.loader.ts
@@ -109,9 +109,9 @@ export default async ({ request }: LoaderFunctionArgs) => {
109
109
  };
110
110
  ```
111
111
 
112
- #### Return value
112
+ #### Return Value
113
113
 
114
- The return value of the `loader` function can be anything serializable, or it can be a [Fetch Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) instance:
114
+ The return value of the `loader` function can be any serializable content or a [Fetch Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) instance:
115
115
 
116
116
  ```tsx
117
117
  const loader = async (): Promise<ProfileData> => {
@@ -122,7 +122,7 @@ const loader = async (): Promise<ProfileData> => {
122
122
  export default loader;
123
123
  ```
124
124
 
125
- By default, the response `Content-type` returned by `loader` is `application/json` and `status` is 200, which you can set by customizing `Response`:
125
+ By default, the `Content-type` of the response returned by the `loader` is `application/json`, and the `status` is 200. You can customize the `Response` to set it:
126
126
 
127
127
  ```tsx
128
128
  const loader = async (): Promise<ProfileData> => {
@@ -138,7 +138,7 @@ const loader = async (): Promise<ProfileData> => {
138
138
 
139
139
  ### Request API
140
140
 
141
- Modern.js does a polyfill of the `fetch` API to initiate requests, which is consistent with the browser's `fetch` API, but can also be used on the server side to initiate requests, meaning that both CSRs and SSRs can use the unified `fetch` API for data fetching:
141
+ Modern.js provides a polyfill for the `fetch` API to make requests. This API is consistent with the browser's `fetch` API, but can also be used to make requests on the server. This means that whether it is CSR or SSR, a unified `fetch` API can be used to get data:
142
142
 
143
143
  ```tsx
144
144
  function loader() {
@@ -146,9 +146,9 @@ function loader() {
146
146
  }
147
147
  ```
148
148
 
149
- ### Error handling
149
+ ### Error Handling
150
150
 
151
- In the `loader` function, errors can be handled by `throw error` or `throw response`. When an error is thrown in the `loader` function, Modern.js will stop executing the code in the current loader and switch the front-end UI to the defined [`ErrorBoundary`](/guides/basic-features/routes#errorboundary) component.
151
+ In the `loader` function, errors can be handled by throwing an `error` or a `response`. When an error is thrown in the `loader` function, Modern.js will stop executing the code in the current `loader` and switch the front-end UI to the defined [`ErrorBoundary`](/guides/basic-features/routes#error-handling) component:
152
152
 
153
153
  ```tsx
154
154
  // routes/user/profile/page.loader.tsx
@@ -175,9 +175,9 @@ const ErrorBoundary = () => {
175
175
  export default ErrorBoundary;
176
176
  ```
177
177
 
178
- ### Get data from upper level components
178
+ ### Get data from parent component
179
179
 
180
- In many cases, the child component needs to access the data in the ancestor's loader, and you can easily access the ancestor's data with `useRouteLoaderData`: `useRouteLoaderData`:
180
+ In many cases, child components need to access data in the parent component `loader`. You can easily get the data from the parent component using `useRouteLoaderData`:
181
181
 
182
182
  ```tsx
183
183
  // routes/user/profile/page.tsx
@@ -195,9 +195,9 @@ export default function UserLayout() {
195
195
  }
196
196
  ```
197
197
 
198
- `userRouteLoaderData` takes one parameter `routeId`,When using conventional routing, Modern.js will automatically generate `routeId` for you. The value of `routeId` is the path of the corresponding component relative to `src/routes`, as in the example above, the child component wants to get the data returned by the loader in `routes/user/layout.tsx`, the value of `routeId` is `user/layout`.
198
+ The `useRouteLoaderData` function accepts a parameter `routeId`. When using [conventional routing](/guides/basic-features/routes), Modern.js will automatically generate the `routeId` for you. The value of `routeId` is the path of the corresponding component relative to `src/routes`. For example, in the above example, if the child component wants to get the data returned by the loader in `routes/user/layout.tsx`, the value of `routeId` is `user/layout`.
199
199
 
200
- In a multi-entry (MPA) scenario, the value of `routeId` needs to be added to the name of the corresponding entry, and the entry name is usually the entry directory name if not specified, such as the following directory structure:
200
+ In a multi-entry (MPA) scenario, the value of `routeId` needs to include the name of the corresponding entry. Unless specified, the entry name is generally the name of the entry directory. For example, in the following directory structure:
201
201
 
202
202
  ```bash
203
203
  .
@@ -210,17 +210,16 @@ In a multi-entry (MPA) scenario, the value of `routeId` needs to be added to the
210
210
  └── layout.tsx
211
211
  ```
212
212
 
213
- If you want to get the data returned by the loader in `entry1/routes/layout.tsx`, the value of `routeId` is `entry1_layout`.
213
+ If you want to get the data returned by the `loader` in `entry1/routes/layout.tsx`, the value of `routeId` is `entry1_layout`.
214
214
 
215
215
  ### (WIP)Loading UI
216
216
 
217
217
  :::info
218
- This feature is currently experimental and the API may be adjusted in the future.
219
- Currently, only CSR is supported, so stay tuned for Streaming SSR.
220
-
218
+ This feature is currently experimental and the API may change in the future.
219
+ Currently only supports CSR, please look forward to Streaming SSR.
221
220
  :::
222
221
 
223
- Add the following code to `user/layout.loader.ts`:
222
+ Create `user/layout.loader.ts` and add the following code:
224
223
 
225
224
  ```ts title="routes/user/layout.loader.ts"
226
225
  import { defer } from "@modern-js/runtime/router"
@@ -240,7 +239,7 @@ defer({
240
239
  export default loader;
241
240
  ```
242
241
 
243
- Add the following code to `user/layout.tsx`:
242
+ Add the following code in `user/layout.tsx`:
244
243
 
245
244
  ```tsx title="routes/user/layout.tsx"
246
245
  import {
@@ -272,19 +271,17 @@ export default function UserLayout() {
272
271
  ```
273
272
 
274
273
  :::info
275
- For specific usage of the Await component, see [Await](/guides/basic-features/routes#await)
276
-
277
- For specific usage of the defer function, see[defer](https://reactrouter.com/en/main/guides/deferred)
274
+ For details on how to use the Await component, please refer to [Await](https://reactrouter.com/en/main/components/await).
278
275
 
276
+ For details on how to use defer, please refer to [defer](https://reactrouter.com/en/main/guides/deferred).
279
277
  :::
280
278
 
281
- ### Wrong usage
279
+ ### Incorrect usage
282
280
 
283
- 1. Only serializable data can be returned in `loader`. In SSR environments, the return value of the `loader` function is serialized to a JSON string, which is then deserialized to an object on the client side. Therefore, no non-serializable data (such as functions) can be returned in the `loader` function.
281
+ 1. The `loader` can only return serializable data. In the SSR environment, the return value of the `loader` function will be serialized as a JSON string and then deserialized into an object on the client side. Therefore, the `loader` function cannot return non-serializable data (such as functions).
284
282
 
285
283
  :::warning
286
- This restriction is not currently in place under CSR, but we strongly recommend that you follow it, and we may add it under CSR in the future.
287
-
284
+ Currently, there is no such restriction under CSR, but we strongly recommend that you follow this restriction, and we may also add this restriction under CSR in the future.
288
285
  :::
289
286
 
290
287
  ```ts
@@ -297,7 +294,7 @@ export default () => {
297
294
  };
298
295
  ```
299
296
 
300
- 2. Modern.js will call the `loader` function for you, you shouldn't call it yourself in the component.
297
+ 2. Modern.js will call the `loader` function for you, so you should not call the `loader` function yourself:
301
298
 
302
299
  ```tsx
303
300
  // This won't work!
@@ -312,7 +309,7 @@ export default function RouteComp() {
312
309
  }
313
310
  ```
314
311
 
315
- 3. You cannot import a `loader` file from a routing component, nor can you import variables in a routing component from a `loader` file:
312
+ 3. You should not import the loader file from the route component, and you should also avoid importing variables from the route component into the loader file. If you need to share types, you should use `import type`.
316
313
 
317
314
  ```ts
318
315
  // Not allowed
@@ -339,23 +336,22 @@ export default async (): Promise<ProfileData> => {
339
336
  };
340
337
  ```
341
338
 
342
- 4. When run on the server side, the `loader` functions are packaged into a single bundle, so we do not recommend using `__filename` and `__dirname` for server-side code.
339
+ 4. When running on the server, the `loader` function will be packaged into a unified bundle, so we do not recommend using `__filename` and `__dirname` in server-side code.
343
340
 
344
341
  ### FAQ
345
342
 
346
- 1. Relationship between loader and BFF functions
343
+ 1. Relationship between `loader` and BFF functions
347
344
 
348
- In a CSR project, the loader is executed on the client side and the bff function can be called directly in the loader to make a request.
345
+ In CSR projects, `loader` is executed on the client side, and the BFF function can be called directly in the `loader` to make interface requests.
349
346
 
350
- In an SSR project, each loader is also a server-side API, and we recommend using loader instead of the BFF function which http method is `get` to avoid one more layer of forwarding and execution.
347
+ In SSR projects, each `loader` is also a server-side interface. We recommend using the `loader` instead of the BFF function with an http method of `get` as the interface layer to avoid an extra layer of forwarding and execution.
351
348
 
352
- ## useLoader(Old)
349
+ ## useLoader (old version)
353
350
 
354
- **`useLoader`** is an API in Modern.js old version. The API is a React Hook specially provided for SSR applications, allowing developers to fetch data in components.
351
+ **`useLoader`** is a legacy API in Modern.js v1. This API is a React Hook designed specifically for SSR applications, allowing developers to fetch data in components in isomorphic development.
355
352
 
356
353
  :::tip
357
- CSR don't need to use `useLoader` to fetch data.
358
-
354
+ It is not necessary to use `useLoader` to fetch data in CSR projects.
359
355
  :::
360
356
 
361
357
  Here is the simplest example:
@@ -378,9 +374,9 @@ export default () => {
378
374
  };
379
375
  ```
380
376
 
381
- After the above code starts, visit the page. You can see that the log is printed at terminal, but not at console in browser.
377
+ After running the above code, when you access the page, you can see that logs are output to the terminal, but not printed in the browser console.
382
378
 
383
- This is because Modern.js server-side rendering, the data returned by the `useLoader` is collected and injected into the HTML of the response. If SSR rendering succeeds, the following code snippet can be seen in the HTML:
379
+ This is because Modern.js collects the data returned by `useLoader` during server-side rendering and injects it into the corresponding HTML. If the SSR rendering is successful, you can see the following code snippet in the HTML:
384
380
 
385
381
  ```html
386
382
  <script>
@@ -388,25 +384,16 @@ This is because Modern.js server-side rendering, the data returned by the `useLo
388
384
  </script>
389
385
  ```
390
386
 
391
- In this global variable, every piece of data is recorded, and this data will be used first in the process of rendering on the browser side. If the data does not exist, the `useLoader` function will be re-executed.
387
+ This global variable is used to record data, and during the browser-side rendering process, this data is used first. If the data does not exist, the `useLoader` function will be executed again.
392
388
 
393
389
  :::note
394
- During the build phase, Modern.js will automatically generate a Loader ID for each `useLoader` and inject it into the JS bundle of SSR and CSR, which is used to associate Loader and data.
395
-
390
+ During the build phase, Modern.js will automatically generate a Loader ID for each `useLoader` and inject it into the SSR and CSR JS Bundles to associate the Loader with the data.
396
391
  :::
397
392
 
398
- Compared with `getServerSideProps` in the Next.js, get data in advance before rendering. Using `useLoader`, you can get the data required by the local UI in the component without passing the data layer by layer. Similarly, it will not add redundant logic to the outermost data acquisition function because different routes require different data requests. Of course, `useLoader` also has some problems, such as the difficulty of Treeshaking server-level code, and the need for one more pre-render at the server level.
393
+ Compared to `getServerSideProps` in Next.js, which fetches data before rendering, using `useLoader` allows you to get data required for local UI in the component without passing data through multiple layers. Similarly, you don't have to add redundant logic to the outermost data acquisition function because different routes require different data requests. Of course, `useLoader` also has some issues, such as difficulties in server-side code tree shaking and the need for an additional pre-rendering step on the server.
399
394
 
400
- Modern.js in the new version, a new Loader solution is designed. The new solution solves these problems and can cooperate with **nested routing** to optimize page performance.
395
+ In the new version of Modern.js, a new Loader solution has been designed. The new solution solves these problems and can be optimized for page performance in conjunction with [conventional routing](/guides/basic-features/routes).
401
396
 
402
397
  :::note
403
- Detailed APIs can be found at [useLoader](/apis/app/runtime/core/use-loader).
404
-
405
- :::
406
-
407
- ## Route Loader
408
-
409
- :::note
410
- Stay tuned.
411
-
398
+ For detailed API information, see [useLoader](/apis/app/runtime/core/use-loader).
412
399
  :::