Modern.js provides out-of-the-box data fetching capabilities,developers can fetch data in the project through these APIs.
It should be noted that these APIs do not help applications initiate requests, but rather help developers better manage data and improve project performance.
Modern.js recommends using conventional routing for routing management. Through Modern.js's conventional (nested) 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.
Modern.js v1 supports fetching data via useLoader, which is no longer the recommended usage. We do not recommend mixing the two except during the migration process.
loader file. In later versions, we recommend defining it in the data file, while we will maintain compatibility with the loader file.data file, the corresponding loader needs to be exported with a name。Routing components such as layout.ts or page.ts can define a same-named loader file. The function exported by the data 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:
Define the following code in the file:
Here, routing components and data files share a type, so the import type syntax should be used.
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).
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.
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.
When navigating on the client based on Modern.js's conventional routing, 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.
loader FunctionThe loader function has two input parameters:
paramsWhen the route file is accessed through [], it is used as dynamic routing, and the dynamic routing fragment is passed as a parameter to the loader function:
When accessing /user/123, the parameter of the loader function is { params: { id: '123' } }.
requestrequest is a Fetch Request instance.
A common usage scenario is to get query parameters through request:
The return value of the loader function can be any serializable content or a Fetch Response instance:
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 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:
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 component:
In an SSR project you can control the status code of a page and display the corresponding UI by throw response in the data loader, as in the following example, where there is one loader for the entire routing route
For example, if there is a loader in the entire routing route that throws a response, the status code of the page will be consistent with this response and the UI of the page will switch to ErrorBoundary.
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:
The useRouteLoaderData function accepts a parameter routeId. When using conventional routing, 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.
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:
If you want to get the data returned by the loader in entry1/routes/layout.tsx, the value of routeId is entry1_layout.
This feature is currently experimental and the API may change in the future.
Create user/layout.data.ts and add the following code:
Add the following code in user/layout.tsx:
In routing navigation, Modern.js will only load the data of the changed part of the route.
For example, the current route is a/b, and the Data Loader corresponding to the a path has been executed.
When jumping from /a/b to /a/c, the Data Loader corresponding to a path will not be re-executed,
and the Data Loader corresponding to c path will execute and fetch the data.
It is mean that Modern.js will only load the data for the routing change portion of the data load, and this default optimization strategy avoids invalid duplicate data requests.
You may ask, how to update the data of the a path corresponding to the Data Loader?
In Modern.js, Modern.js will reload the data of the corresponding routing path in the following cases:
shouldRevalidate function is defined in the routing component, which returns trueIf you define the shouldRevalidate function on the route, the function will be checked first to determine whether the data needs to be reloaded.
shouldRevalidateCurrently shouldRevalidate works under csr and streaming ssr.
In each routing component (layout.tsx, page.tsx, $.tsx), we can export a shouldRevalidate function, which is triggered each time a route changes in the project. This function controls which routes' data are reloaded, and when it returns true, the data for the corresponding route is reloaded. data of the corresponding route will be reloaded.
For more details about the shouldRevalidate function, please refer to react-router
In the SSR project, the code in Data Loader will only be executed on the server side when the client performs SPA navigation. The framework will send an HTTP request to the SSR service to trigger the execution of Data Loader. However, in some scenarios, we may expect that requests sent by clients do not go through the SSR service and directly request data sources.
Why the Data Loader is only executed server-side in SSR projects can be found in FAQ
For example, the following scenarios:
In these scenarios, we can use Client Loader. After adding the Client Loader, in the following scenarios, the code in the Client Loader will be called instead of sending requests to SSR services:
After SSR is downgraded to CSR, when fetch data on the client side, Client Loader will be executed instead of the framework sending requests to Data Loader (Server) to fetch data.
When performing SPA navigation in SSR projects and fetch data, Client Loader will be executed.
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).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.
loader function for you, so you should not call the loader function yourself:import type.loader function will be bundled into a unified bundle, so we do not recommend using __filename and __dirname in server-side code.loader and BFF functionsIn CSR projects, loader is executed on the client side, and the BFF function can be called directly in the loader to make interface requests.
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.
The Data Loader in our SSR project is designed to only run on the server side. The main reasons for sending requests from the client to the server during client-side rendering are as follows:
Simplified usage, when using a server loader, the data fetching code for both the SSR and CSR phases is in the server loader (the real calls are made by the framework layer), and the code in the server loader doesn't need to care whether it's in a browser environment or a server-side environment.
Reducing data for network requests, The server loader can be used as a BFF layer, which reduces the amount of data that needs to be fetched by the front-end at runtime.
Reduce client-side bundle size, moving the logic code and its dependencies from the client to the server.
Improve maintainability, moving the logic code to the server side reduces the direct impact of data logic on the front-end UI. In addition, the problem of mistakenly introducing server-side dependencies in the client-side bundle or client-side dependencies in the server-side bundle is also avoided.
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.
It is not necessary to use useLoader to fetch data in CSR projects, and useLoader is not supported when using Rspack as the bundler.
Here is the simplest example:
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.
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:
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.
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.
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.
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.
For detailed API information, see useLoader.