@modern-js/main-doc 2.42.2 → 2.43.0
Sign up to get free protection for your applications and to get access to all the features.
- package/docs/en/apis/app/runtime/web-server/middleware.mdx +3 -3
- package/docs/en/configure/app/server/ssr.mdx +2 -0
- package/docs/en/guides/advanced-features/optimize-bundle.mdx +1 -1
- package/docs/en/guides/advanced-features/ssr/_category_.json +8 -0
- package/docs/en/guides/advanced-features/ssr/cache.mdx +186 -0
- package/docs/en/guides/advanced-features/ssr/index.mdx +22 -0
- package/docs/en/guides/advanced-features/ssr/stream.mdx +236 -0
- package/docs/en/guides/advanced-features/ssr/usage.mdx +341 -0
- package/docs/en/guides/basic-features/css.mdx +2 -13
- package/docs/en/guides/get-started/tech-stack.mdx +1 -1
- package/docs/en/guides/topic-detail/framework-plugin/introduction.mdx +63 -16
- package/docs/zh/apis/app/runtime/web-server/middleware.mdx +4 -4
- package/docs/zh/configure/app/server/ssr.mdx +2 -0
- package/docs/zh/guides/advanced-features/optimize-bundle.mdx +1 -1
- package/docs/zh/guides/advanced-features/ssr/_category_.json +8 -0
- package/docs/zh/guides/advanced-features/ssr/cache.mdx +189 -0
- package/docs/zh/guides/advanced-features/ssr/index.mdx +22 -0
- package/docs/zh/guides/advanced-features/ssr/stream.mdx +240 -0
- package/docs/zh/guides/advanced-features/{ssr.mdx → ssr/usage.mdx} +7 -225
- package/docs/zh/guides/basic-features/css.mdx +2 -13
- package/docs/zh/guides/basic-features/data/data-write.mdx +1 -1
- package/docs/zh/guides/get-started/tech-stack.mdx +1 -1
- package/docs/zh/guides/topic-detail/framework-plugin/introduction.mdx +61 -16
- package/package.json +7 -7
@@ -0,0 +1,341 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 1
|
3
|
+
title: Usage
|
4
|
+
---
|
5
|
+
|
6
|
+
# Usage
|
7
|
+
|
8
|
+
Enabling SSR is simple. Just set the value of [`server.ssr`](/configure/app/server/ssr) to `true`.
|
9
|
+
|
10
|
+
```ts title="modern.config.ts"
|
11
|
+
import { defineConfig } from '@modern-js/app-tools';
|
12
|
+
|
13
|
+
export default defineConfig({
|
14
|
+
server: {
|
15
|
+
ssr: true,
|
16
|
+
},
|
17
|
+
});
|
18
|
+
```
|
19
|
+
|
20
|
+
## SSR Data Fetch
|
21
|
+
|
22
|
+
Modern.js provides a Data Loader that simplifies data fetching for developers working with SSR and CSR. Each routing module, such as `layout.tsx` and `page.tsx`, can define its own Data Loader:
|
23
|
+
|
24
|
+
```ts title="src/routes/page.data.ts"
|
25
|
+
export const loader = () => {
|
26
|
+
return {
|
27
|
+
message: 'Hello World',
|
28
|
+
};
|
29
|
+
};
|
30
|
+
```
|
31
|
+
|
32
|
+
Within the component, data returned by the `loader` function can be accessed through the Hooks API:
|
33
|
+
|
34
|
+
```tsx
|
35
|
+
export default () => {
|
36
|
+
const data = useLoaderData();
|
37
|
+
return <div>{data.message}</div>;
|
38
|
+
};
|
39
|
+
```
|
40
|
+
|
41
|
+
Modern.js breaks the traditional model of server-side rendering (SSR) development and offers users a more user-friendly SSR development experience.
|
42
|
+
|
43
|
+
This feature offers elegant degradation processing. If the SSR request fails, it will automatically downgrade and restart the request on the browser side.
|
44
|
+
|
45
|
+
Developers should still be mindful of data fallback, including `null` values or unexpected data returns. This will help prevent React rendering errors and messy results during SSR.
|
46
|
+
|
47
|
+
:::info
|
48
|
+
|
49
|
+
1. When requesting a page through client-side routing, Modern.js sends an HTTP request. The server receives the request and executes the corresponding Data Loader function for the page, then returns the execution result as a response to the browser.
|
50
|
+
|
51
|
+
2. When using Data Loader, data is fetched before rendering. Modern.js also supports obtaining data during component rendering. For more related content, please refer to [Data Fetch](/guides/basic-features/data/data-fetch).
|
52
|
+
|
53
|
+
:::
|
54
|
+
|
55
|
+
## Keep Rendering Consistent
|
56
|
+
|
57
|
+
In some businesses, it is usually necessary to make different UI displays based on the current operating container environment characteristics, such as [UA](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent) information.
|
58
|
+
|
59
|
+
If not handled carefully, unexpected rendering results are likely to occur.
|
60
|
+
|
61
|
+
Here is an example to show the problem when SSR and CSR rendering are inconsistent, add the following code to the component:
|
62
|
+
|
63
|
+
```tsx
|
64
|
+
{
|
65
|
+
typeof window !== 'undefined' ? <div>browser content</div> : null;
|
66
|
+
}
|
67
|
+
```
|
68
|
+
|
69
|
+
After launching the application and accessing the page, you will find that the browser console throws a warning message.
|
70
|
+
|
71
|
+
```sh
|
72
|
+
Warning: Expected server HTML to contain a matching <div> in <div>.
|
73
|
+
```
|
74
|
+
|
75
|
+
This is caused by React's hydration logic on the client side detecting inconsistencies between the rendered result and the SSR rendering result. Although the page appears normal, in complex applications, it is likely to cause problems such as DOM hierarchy confusion and style disorder.
|
76
|
+
|
77
|
+
:::info
|
78
|
+
More information about [`React hydrate`](https://reactjs.org/docs/react-dom.html#hydrate).
|
79
|
+
|
80
|
+
:::
|
81
|
+
|
82
|
+
The application needs to maintain consistency between SSR and CSR rendering results. If there is inconsistency, it means that this part of the content does not need to be rendered in SSR.
|
83
|
+
|
84
|
+
Modern.js provides a [`<NoSSR>`](/apis/app/runtime/core/use-runtime-context) component for such content that does not need to be rendered in SSR.
|
85
|
+
|
86
|
+
```ts
|
87
|
+
import { NoSSR } from '@modern-js/runtime/ssr';
|
88
|
+
```
|
89
|
+
|
90
|
+
Wrap the element that does not require SSR with the `NoSSR` component:
|
91
|
+
|
92
|
+
```tsx
|
93
|
+
<NoSSR>
|
94
|
+
<div>client content</div>
|
95
|
+
</NoSSR>
|
96
|
+
```
|
97
|
+
|
98
|
+
After modifying the code, refreshing the page shows that the previous warning has disappeared. Opening the Network tab of the browser devtools and checking the returned HTML document does not contain content wrapped by `NoSSR` components.
|
99
|
+
|
100
|
+
:::info
|
101
|
+
['useRuntimeContext'](/apis/app/runtime/core/use-runtime-context) can get complete request information, which can be used to ensure consistent rendering results between SSR and CSR.
|
102
|
+
|
103
|
+
:::
|
104
|
+
|
105
|
+
## Pay Attention to Memory Leaks
|
106
|
+
|
107
|
+
:::warning
|
108
|
+
Developers need to pay special attention to memory leaks in the SSR mode. Even tiny memory leaks can have an impact on the service after a large number of accesses.
|
109
|
+
|
110
|
+
:::
|
111
|
+
|
112
|
+
When using SSR, each request from the browser will trigger the server to re-execute the component rendering logic. Therefore, it is necessary to avoid defining any data structures that may continue to grow globally, subscribing to events globally, or creating streams that will not be destroyed globally.
|
113
|
+
|
114
|
+
For example, when using [redux-observable](https://redux-observable.js.org/), developers who are used to CSR development usually code in the component like this:
|
115
|
+
|
116
|
+
```tsx
|
117
|
+
/* This code is for demonstration purposes only */
|
118
|
+
import { createEpicMiddleware, combineEpics } from 'redux-observable';
|
119
|
+
|
120
|
+
const epicMiddleware = createEpicMiddleware();
|
121
|
+
const rootEpic = combineEpics();
|
122
|
+
|
123
|
+
export default function Test() {
|
124
|
+
epicMiddleware.run(rootEpic);
|
125
|
+
return <div>Hello Modern.js</div>;
|
126
|
+
}
|
127
|
+
```
|
128
|
+
|
129
|
+
Create a Middleware instance `epicMiddleware` outside the component and call epicMiddleware.run inside the component.
|
130
|
+
|
131
|
+
When running on the client-side, this code will not cause any issues. However, during SSR, the Middleware instance cannot be destroyed.
|
132
|
+
|
133
|
+
Every time a component is rendered and `epicMiddleware.run(rootEpic)` is called, new event bindings are added internally which causes the entire object to grow continuously and ultimately affects application performance.
|
134
|
+
|
135
|
+
CSR issues are not easy to detect, so when switching from CSR to SSR, if you are unsure whether the application has such problems, you can perform stress testing on applications.
|
136
|
+
|
137
|
+
## Converging Server Data.
|
138
|
+
|
139
|
+
In order to maintain the data requested during the SSR phase, it can be directly used on the browser side. Modern.js will inject the data and state collected during rendering into HTML.
|
140
|
+
|
141
|
+
However, in CSR applications, there are often situations where interface data is large and component states are not converged. If SSR is used directly in this case, the rendered HTML may have a problem of being too large.
|
142
|
+
|
143
|
+
At this time, SSR may not only fail to improve user experience for applications but may also have an opposite effect.
|
144
|
+
|
145
|
+
Therefore, when using SSR, **developers need to properly slim down the application**.
|
146
|
+
|
147
|
+
1. Focus on the first screen, SSR can request only the data needed for the first screen and render the remaining parts on the browser side.
|
148
|
+
2. Remove data unrelated to rendering from the returned data of the interface.
|
149
|
+
|
150
|
+
## Serverless Pre-render
|
151
|
+
|
152
|
+
:::warning
|
153
|
+
x.43.0+ has been deprecated, Please instance of [SSR Cache](guides/advanced-features/ssr/cache).
|
154
|
+
:::
|
155
|
+
|
156
|
+
Modern.js provides the Serverless Pre-rendering (SPR) feature to improve SSR performance.
|
157
|
+
|
158
|
+
SPR uses pre-rendering and caching technology to provide responsive performance for SSR pages. It enables SSR applications to have the response speed and stability of static web pages while also maintaining dynamic data updates.
|
159
|
+
|
160
|
+
Using SPR in Modern.js is very simple. Just add the PreRender component to your component, and the page where it is located will automatically enable SPR.
|
161
|
+
|
162
|
+
Here is a simulated component that uses the useLoaderData API. The request in the Data Loader takes 2 seconds to consume.
|
163
|
+
|
164
|
+
```tsx title="page.data.ts"
|
165
|
+
import { useLoaderData } from '@modern-js/runtime/router';
|
166
|
+
|
167
|
+
export const loader = async () => {
|
168
|
+
await new Promise((resolve, reject) => {
|
169
|
+
setTimeout(() => {
|
170
|
+
resolve(null);
|
171
|
+
}, 2000);
|
172
|
+
});
|
173
|
+
|
174
|
+
return {
|
175
|
+
message: 'Hello Modern.js',
|
176
|
+
};
|
177
|
+
};
|
178
|
+
```
|
179
|
+
|
180
|
+
```tsx title="page.tsx"
|
181
|
+
import { useLoaderData } from '@modern-js/runtime/router';
|
182
|
+
|
183
|
+
export default () => {
|
184
|
+
const data = useLoaderData();
|
185
|
+
return <div>{data?.message}</div>;
|
186
|
+
};
|
187
|
+
```
|
188
|
+
|
189
|
+
After executing the `dev` command and opening the page, it is obvious that the page needs to wait 2s before returning.
|
190
|
+
|
191
|
+
Use the `<PreRender>` component for optimization next, which can be directly exported from `@modern-js/runtime/ssr`.
|
192
|
+
|
193
|
+
```ts
|
194
|
+
import { PreRender } from '@modern-js/runtime/ssr';
|
195
|
+
```
|
196
|
+
|
197
|
+
Use the `<PreRender>` component within the routing component and set the parameter `interval` to indicate that the expiration time of this rendering result is 5 seconds:
|
198
|
+
|
199
|
+
```tsx
|
200
|
+
<PreRender interval={5} />
|
201
|
+
```
|
202
|
+
|
203
|
+
After modification, execute `pnpm run build && pnpm run serve` to start the application and open the page.
|
204
|
+
|
205
|
+
The first time it is opened, there is no difference in rendering compared to before, and there is still a 2-second delay.
|
206
|
+
|
207
|
+
Clicking refresh opens the page instantly, but at this point, the page data has not changed due to the refresh because the cache has not yet expired.
|
208
|
+
|
209
|
+
After waiting for 5 seconds and refreshing the page, the data on the page remained unchanged. After refreshing again, the data changed, but the response was nearly immediate.
|
210
|
+
|
211
|
+
This is because during the previous request, SPR had already asynchronously obtained a new rendering result in the background, and this time's requested page is a version that has been cached on the server.
|
212
|
+
|
213
|
+
One can imagine that when the `interval` is set to 1, users can have a responsive experience of static pages while perceiving real-time data.
|
214
|
+
|
215
|
+
:::info
|
216
|
+
For more detail, see [`<PreRender>`](/apis/app/runtime/ssr/pre-render).
|
217
|
+
|
218
|
+
:::
|
219
|
+
|
220
|
+
## Treeshaking
|
221
|
+
|
222
|
+
When SSR is enabled, Modern.js uses the same entry point to build both SSR Bundle and CSR Bundle. Therefore, errors may occur if there are Web APIs in the SSR Bundle or Node APIs in the CSR Bundle.
|
223
|
+
|
224
|
+
Introducing Web APIs in components usually do some global listening or obtaining browser-related data, such as:
|
225
|
+
|
226
|
+
```tsx
|
227
|
+
document.addEventListener('load', () => {
|
228
|
+
console.log('document load');
|
229
|
+
});
|
230
|
+
const App = () => {
|
231
|
+
return <div>Hello World</div>;
|
232
|
+
};
|
233
|
+
export default App;
|
234
|
+
```
|
235
|
+
|
236
|
+
Importing Node API in component files is usually done when using `useLoader`, for example:
|
237
|
+
|
238
|
+
```ts
|
239
|
+
import fse from 'fs-extra';
|
240
|
+
export default () => {
|
241
|
+
const file = fse.readFileSync('./myfile');
|
242
|
+
return {
|
243
|
+
...
|
244
|
+
};
|
245
|
+
};
|
246
|
+
```
|
247
|
+
|
248
|
+
### Use Environment Variables
|
249
|
+
|
250
|
+
For the first case, we can directly use the built-in environment variable `MODERN_TARGET` in Modern.js to determine and remove unused code during build time:
|
251
|
+
|
252
|
+
```ts
|
253
|
+
if (process.env.MODERN_TARGET === 'browser') {
|
254
|
+
document.addEventListener('load', () => {
|
255
|
+
console.log('document load');
|
256
|
+
});
|
257
|
+
}
|
258
|
+
```
|
259
|
+
|
260
|
+
After the development environment is bundled, the SSR and CSR artifacts will be compiled into the following content. Therefore, there will be no more Web API errors in the SSR environment.
|
261
|
+
|
262
|
+
```ts
|
263
|
+
// SSR production
|
264
|
+
if (false) {
|
265
|
+
}
|
266
|
+
|
267
|
+
// CSR production
|
268
|
+
if (true) {
|
269
|
+
document.addEventListener('load', () => {
|
270
|
+
console.log('document load');
|
271
|
+
});
|
272
|
+
}
|
273
|
+
```
|
274
|
+
|
275
|
+
:::note
|
276
|
+
For more information, see [environment variables](/guides/basic-features/env-vars).
|
277
|
+
|
278
|
+
:::
|
279
|
+
|
280
|
+
### Use File Suffix
|
281
|
+
|
282
|
+
But for example, in the second case, `fs-extra` is imported in the code, which has side effects using Node API internally. If it is directly referenced in the component, it will cause an error when CSR loading.
|
283
|
+
|
284
|
+
Environment variables does not work in this case. Modern.js also supports using files with the `.node.` suffix to distinguish between the packaging files of SSR Bundle and CSR Bundle products.
|
285
|
+
|
286
|
+
You can create a proxy layer by creating files with the same name but different extensions, such as `.ts` and `.node.ts`:
|
287
|
+
|
288
|
+
```ts title="compat.ts"
|
289
|
+
export const readFileSync: any = () => {};
|
290
|
+
```
|
291
|
+
|
292
|
+
```ts title="compat.node.ts"
|
293
|
+
export { readFileSync } from 'fs-extra';
|
294
|
+
```
|
295
|
+
|
296
|
+
Import `./compat` directly in the file. In SSR environment, files with `.node.ts` suffix will be used first, while in CSR environment, files with `.ts` suffix will be used.
|
297
|
+
|
298
|
+
```ts title="App.tsx"
|
299
|
+
import { readFileSync } from './compat'
|
300
|
+
|
301
|
+
export const loader = () => {
|
302
|
+
const file = readFileSync('./myfile');
|
303
|
+
return {
|
304
|
+
...
|
305
|
+
};
|
306
|
+
};
|
307
|
+
```
|
308
|
+
|
309
|
+
### Independent File
|
310
|
+
|
311
|
+
The two methods mentioned above will both bring some mental burden to developers. In real business scenarios, we found that most of the mixed Node/Web code appears in data requests.
|
312
|
+
|
313
|
+
Therefore, Modern.js has designed a [Data Fetch](/guides/basic-features/data/data-fetch) to separate CSR and SSR code based on [Nested Routing](/guides/basic-features/routes).
|
314
|
+
|
315
|
+
We can separate **data requests from component code** by using independent files. Write the component logic in `routes/page.tsx` and write the data request logic in `routes/page.data.ts`.
|
316
|
+
|
317
|
+
```ts title="routes/page.tsx"
|
318
|
+
export default Page = () => {
|
319
|
+
return <div>Hello World<div>
|
320
|
+
}
|
321
|
+
```
|
322
|
+
|
323
|
+
```ts title="routes/page.data.tsx"
|
324
|
+
import fse from 'fs-extra';
|
325
|
+
export const loader = () => {
|
326
|
+
const file = fse.readFileSync('./myfile');
|
327
|
+
return {
|
328
|
+
...
|
329
|
+
};
|
330
|
+
}
|
331
|
+
```
|
332
|
+
|
333
|
+
## Remote Request
|
334
|
+
|
335
|
+
When initiating interface requests in SSR, developers sometimes encapsulate isomorphic request tools themselves. For some interfaces that require passing user cookies, developers can use the ['useRuntimeContext'](/guides/basic-features/data/data-fetch#route-loader) API to get the request header for implementation.
|
336
|
+
|
337
|
+
It should be noted that the obtained request header is for HTML requests, which may not be suitable for API requests. Therefore, ** don't passed through all request headers **.
|
338
|
+
|
339
|
+
In addition, some backend interfaces or common gateways will verify based on the information in the request header. Full pass-through can easily lead to various difficult-to-troubleshoot issues, so it is recommended to **pass through as needed**.
|
340
|
+
|
341
|
+
If it is really necessary to pass through all request headers, please be sure to filter the `host` field.
|
@@ -22,7 +22,7 @@ Please refer to [Modern.js Builder - Using PostCSS](https://modernjs.dev/builder
|
|
22
22
|
|
23
23
|
## Using CSS Modules
|
24
24
|
|
25
|
-
Please read the [Using CSS Modules](
|
25
|
+
Please read the [Using CSS Modules](/guides/basic-features/css-modules) section to learn about the complete usage of CSS Modules.
|
26
26
|
|
27
27
|
## Using CSS-in-JS
|
28
28
|
|
@@ -72,18 +72,7 @@ To use [Tailwind CSS](https://tailwindcss.com/) in Modern.js, you can follow the
|
|
72
72
|
? Please select the feature name: Enable Tailwind CSS
|
73
73
|
```
|
74
74
|
|
75
|
-
After successful initialization, you will see
|
76
|
-
|
77
|
-
```json title="./package.json"
|
78
|
-
{
|
79
|
-
"dependencies": {
|
80
|
-
"tailwindcss": "^3.0.0"
|
81
|
-
},
|
82
|
-
"devDependencies": {
|
83
|
-
"@modern-js/plugin-tailwindcss": "^2.0.0"
|
84
|
-
}
|
85
|
-
}
|
86
|
-
```
|
75
|
+
After successful initialization, you will see that the `package.json` has added dependencies for `tailwindcss` and `@modern-js/plugin-tailwindcss`.
|
87
76
|
|
88
77
|
2. Register the Tailwind plugin in `modern.config.ts`:
|
89
78
|
|
@@ -97,7 +97,7 @@ Modern.js supports three CSS preprocessors: [Sass](https://sass-lang.com/), [Les
|
|
97
97
|
|
98
98
|
Modern.js provides out-of-the-box support for [CSS Modules](https://github.com/css-modules/css-modules), which is implemented internally based on [css-loader](https://www.npmjs.com/package/css-loader).
|
99
99
|
|
100
|
-
Please refer to ["Use CSS Modules"](
|
100
|
+
Please refer to ["Use CSS Modules"](/guides/basic-features/css-modules) for usage instructions.
|
101
101
|
|
102
102
|
---
|
103
103
|
|
@@ -6,22 +6,69 @@ sidebar_position: 1
|
|
6
6
|
|
7
7
|
## Modern.js Plugin System
|
8
8
|
|
9
|
-
Modern.js
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
9
|
+
Modern.js offers a comprehensive plugin system with a complete lifecycle. Plugins can be used to extend different stages of project operation, request handling, rendering, and more.
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
Plugins must be explicitly registered in the configuration file to be effective. When you need to add plugins to Modern.js, you can configure them in the `[plugin](/configure/app/plugins.html)` field:
|
14
|
+
|
15
|
+
```ts title="edenx.config.ts"
|
16
|
+
// an example for bff
|
17
|
+
import { appTools, defineConfig } from '@modern-js/app-tools';
|
18
|
+
import { bffPlugin } from '@modern-js/plugin-bff';
|
19
|
+
|
20
|
+
export default defineConfig({
|
21
|
+
plugins: [appTools(), bffPlugin()],
|
22
|
+
});
|
23
|
+
```
|
24
|
+
|
25
|
+
:::note
|
26
|
+
Note that this configuration only supports adding Modern.js plugins and does not support adding Webpack plugins.
|
27
|
+
:::
|
28
|
+
|
29
|
+
## Official Plugins
|
30
|
+
|
31
|
+
Modern.js offers a range of official plugins, which are integrated with the Modern.js generator. All the functionalities of the official plugins can be enabled by executing the `new` command. For instance, to enable the BFF (Backend For Frontend) feature:
|
32
|
+
|
33
|
+
```bash
|
34
|
+
$ npx modern new
|
35
|
+
? Please select the operation you want: Enable Features
|
36
|
+
? Please select the feature name: (Use arrow keys)
|
37
|
+
Enable Tailwind CSS
|
38
|
+
❯ Enable BFF
|
39
|
+
Enable SSG
|
40
|
+
Enable Micro Frontend
|
41
|
+
Enable Unit Test / Integration Test
|
42
|
+
Enable Visual Testing (Storybook)
|
43
|
+
```
|
44
|
+
|
45
|
+
After the selection is completed, the Modern.js generator will automatically install the corresponding plugins and third-party dependencies. Upon completion of the installation, you will see:
|
46
|
+
|
47
|
+
```bash
|
48
|
+
[INFO] Dependency automatic installation succeeded
|
49
|
+
|
50
|
+
[INFO] install plugin dependencies success!add follow code to modern.config.ts :
|
51
|
+
|
52
|
+
import { bffPlugin } from '@modern-js/plugin-bff';
|
53
|
+
import { expressPlugin } from '@modern-js/plugin-express';
|
54
|
+
|
55
|
+
export default defineConfig({
|
56
|
+
...,
|
57
|
+
plugins: [..., bffPlugin(), expressPlugin()],
|
58
|
+
});
|
59
|
+
```
|
60
|
+
|
61
|
+
At this point, you can add the plugin to the configuration file according to the output in the console.
|
62
|
+
|
63
|
+
## Composition
|
64
|
+
|
65
|
+
The Modern.js plugin system is mainly divided into three parts: Hook model, Manager, and Context Sharing Mechanism.
|
66
|
+
|
67
|
+
- The Hook model is used to determine the execution logic of the current Hook.
|
68
|
+
- The Manager controls the execution and scheduling of Hooks.
|
69
|
+
- The Context Sharing Mechanism is used to pass information between different Hooks.
|
70
|
+
|
71
|
+
Currently, Modern.js offers several different Hook models: **Pipeline, Waterfall, Workflow**.
|
25
72
|
|
26
73
|
:::note
|
27
74
|
Subsequent chapters will introduce the execution methods of each model in detail.
|
@@ -91,7 +91,7 @@ type MiddlewareContext = {
|
|
91
91
|
```ts
|
92
92
|
export const Middleware = () => async (ctx, next) => {
|
93
93
|
const start = Date.now();
|
94
|
-
ctx.res.once('finish', () => {
|
94
|
+
ctx.source.res.once('finish', () => {
|
95
95
|
console.log(Date.now() - start);
|
96
96
|
});
|
97
97
|
};
|
@@ -99,12 +99,12 @@ export const Middleware = () => async (ctx, next) => {
|
|
99
99
|
|
100
100
|
### 注入服务端工具 & 数据
|
101
101
|
|
102
|
-
Modern.js 提供了 `
|
102
|
+
Modern.js 提供了 `response.locals` 属性用来存放当前请求的局部变量。
|
103
103
|
|
104
104
|
```ts
|
105
105
|
export const Middleware = () => async (ctx, next) => {
|
106
|
-
ctx.
|
107
|
-
ctx.
|
106
|
+
ctx.response.locals.id = 'Modern.js';
|
107
|
+
ctx.response.locals.rpc = createRpcInstance();
|
108
108
|
};
|
109
109
|
```
|
110
110
|
|
@@ -30,6 +30,7 @@ export default defineConfig({
|
|
30
30
|
- `inlineScript`:`boolean = true`,默认情况下,SSR 的数据会以内联脚本的方式注入到 HTML 中,并且直接赋值给全局变量。配置为 `false` 后,会下发 JSON,而不是赋值给全局变量。
|
31
31
|
- `disablePrerender`: `boolean = fasle`, 为了兼容旧数据请求方式 - `useLoader`, 默认情况下 Modern.js 会对组件进行一次预渲染即有两次渲染。
|
32
32
|
开发者在保证项目中没有使用 useLoader Api 情况下, 可通过配置 `disablePrerender=true`来减少一次渲染。
|
33
|
+
- `unsafeHeaders`: `string[] = []`, 为了安全考虑,Modern.js 不会往 SSR_DATA 添加过多的内容。开发者可以通过该配置,对需要注入的 headers 进行配置。
|
33
34
|
|
34
35
|
```ts title="modern.config.ts"
|
35
36
|
export default defineConfig({
|
@@ -39,6 +40,7 @@ export default defineConfig({
|
|
39
40
|
mode: 'stream',
|
40
41
|
inlineScript: false,
|
41
42
|
disablePrerender: true,
|
43
|
+
unsafeHeaders: ['User-Agent'],
|
42
44
|
},
|
43
45
|
},
|
44
46
|
});
|