You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/document/main-doc/docs/en/guides/basic-features/data-fetch.mdx
+56-69Lines changed: 56 additions & 69 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -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 thebox 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:
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 loaderas 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:
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:
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:
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() {
145
145
const res =awaitfetch('https://api/user/profile');
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
exportdefaultErrorBoundary;
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
exportdefaultloader;
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() {
0 commit comments