@modern-js/main-doc 2.66.1-alpha.0 → 2.66.1-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/en/apis/app/runtime/web-server/hook.mdx +5 -0
- package/docs/en/apis/app/runtime/web-server/middleware.mdx +2 -23
- package/docs/en/components/runtime-cli-config.mdx +0 -0
- package/docs/en/configure/app/runtime/0-intro.mdx +4 -0
- package/docs/en/configure/app/runtime/router.mdx +1 -1
- package/docs/en/configure/app/runtime/state.mdx +1 -1
- package/docs/en/guides/advanced-features/web-server.mdx +38 -102
- package/docs/zh/apis/app/runtime/web-server/hook.mdx +4 -0
- package/docs/zh/apis/app/runtime/web-server/middleware.mdx +2 -23
- package/docs/zh/components/runtime-cli-config.mdx +0 -0
- package/docs/zh/configure/app/runtime/0-intro.mdx +4 -0
- package/docs/zh/configure/app/runtime/router.mdx +1 -1
- package/docs/zh/configure/app/runtime/state.mdx +1 -1
- package/docs/zh/guides/advanced-features/web-server.mdx +35 -97
- package/package.json +1 -1
- package/docs/en/configure/app/server/enable-framework-ext.mdx +0 -49
- package/docs/zh/configure/app/server/enable-framework-ext.mdx +0 -49
@@ -6,6 +6,11 @@ title: Hook
|
|
6
6
|
|
7
7
|
Used to extend Modern.js built-in Web Server, all page requests are handled by these hooks.
|
8
8
|
|
9
|
+
:::warning
|
10
|
+
It is recommended to use [UnstableMiddleware](/apis/app/runtime/web-server/unstable_middleware) to handle page requests.
|
11
|
+
|
12
|
+
:::
|
13
|
+
|
9
14
|
:::note
|
10
15
|
For more detail, see [Extend Web Server](/guides/advanced-features/web-server).
|
11
16
|
|
@@ -6,9 +6,9 @@ title: Middleware
|
|
6
6
|
|
7
7
|
Used to extend the built-in Web Server of Modern.js, unlike [Hook](/apis/app/runtime/web-server/hook), Middleware can directly operate Node's origin request and response, and can be extended using the framework plugin.
|
8
8
|
|
9
|
-
:::
|
9
|
+
:::warning
|
10
10
|
|
11
|
-
|
11
|
+
Middleware will be removed in the next major version.
|
12
12
|
|
13
13
|
It is recommended to use [UnstableMiddleware](/apis/app/runtime/web-server/unstable_middleware) to handle page requests.
|
14
14
|
|
@@ -118,24 +118,3 @@ export const Middleware = () => async (ctx, next) => {
|
|
118
118
|
ctx.response.locals.rpc = createRpcInstance();
|
119
119
|
};
|
120
120
|
```
|
121
|
-
|
122
|
-
### Framework Extension
|
123
|
-
|
124
|
-
Middleware can also use runtime framework extensions like BFF.
|
125
|
-
|
126
|
-
When using framework runtime extensions, type information is exported from `@modern-js/runtime/{namespace}`. Middleware can use framework syntax, such as framework middleware writing, the following is pseudo-code:
|
127
|
-
|
128
|
-
```ts
|
129
|
-
import { SomeType } from '@modern-js/runtime/{namespace}';
|
130
|
-
|
131
|
-
export const middleware: SomeType = (ctx, next) => {
|
132
|
-
console.log(ctx.url);
|
133
|
-
next();
|
134
|
-
};
|
135
|
-
```
|
136
|
-
|
137
|
-
By default, the framework extension capability of Web Server is turned off after installing the framework extension plugin. If you want to use the framework extension, you can turn it on through ['server.enableFrameworkExt'](/configure/app/server/enable-framework-ext.html).
|
138
|
-
|
139
|
-
:::info
|
140
|
-
The type name exported by the framework extension may not 'Middleware', but is named by the framework extension plugin.
|
141
|
-
:::
|
File without changes
|
@@ -61,6 +61,10 @@ Using the `src/modern.runtime.ts` configuration approach does not support export
|
|
61
61
|
Using the `src/modern.runtime.ts` configuration approach requires Modern.js version **MAJOR_VERSION.66.0** or higher.
|
62
62
|
:::
|
63
63
|
|
64
|
+
import RuntimeCliConfig from '@site-docs/components/runtime-cli-config';
|
65
|
+
|
66
|
+
<RuntimeCliConfig />
|
67
|
+
|
64
68
|
## Configuration Evolution
|
65
69
|
|
66
70
|
Before MAJOR_VERSION.66.0, runtime configurations were scattered across multiple locations:
|
@@ -4,31 +4,21 @@ sidebar_position: 16
|
|
4
4
|
|
5
5
|
# Custom Web Server
|
6
6
|
|
7
|
-
|
7
|
+
Modern.js encapsulates most server-side capabilities required by projects, typically eliminating the need for server-side development. However, in certain scenarios such as user authentication, request preprocessing, or adding page skeletons, custom server-side logic may still be necessary.
|
8
8
|
|
9
|
-
|
9
|
+
Modern.js provides two types of APIs to extend the Web Server: **Middleware** and **Lifecycle Hooks**.
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
For example, hosting pages separately from BFF, deploying page services to non-Node environments, customizing for deployment platforms, etc.
|
14
|
-
|
15
|
-
For the above reasons, Modern.js provides three ways that projects can customize server-level capabilities progressively according to their needs.
|
16
|
-
|
17
|
-
:::warning
|
18
|
-
The three extension methods cannot work at the same time, and developers need to choose the appropriate method according to the scenario.
|
11
|
+
:::note
|
12
|
+
Middleware and Hooks only take effect when users request page routes, and BFF routes won't pass through these APIs.
|
19
13
|
:::
|
20
14
|
|
21
|
-
##
|
15
|
+
## Enabling Custom Web Server
|
22
16
|
|
23
|
-
|
24
|
-
|
25
|
-
Because the full web server cannot be controlled this way, and the extension logic **only takes effect when the page is requested**. Therefore, it is relatively simple to apply server-level logic, and you do not want to create additional BFFs or BFFs and pages without common server-level logic scenarios.
|
26
|
-
|
27
|
-
You can run the'pnpm run new 'command in the project root directory to enable the "Custom Web Serve" function:
|
17
|
+
Developers can execute the `pnpm run new` command in the project root directory to enable the "Custom Web Server" feature:
|
28
18
|
|
29
19
|
```bash
|
30
|
-
?
|
31
|
-
?
|
20
|
+
? Select operation: Create project element
|
21
|
+
? Select element type: Create "Custom Web Server" source directory
|
32
22
|
```
|
33
23
|
|
34
24
|
After executing the command, register the `@modern-js/plugin-server` plugin in `modern.config.ts`:
|
@@ -41,84 +31,19 @@ export default defineConfig({
|
|
41
31
|
});
|
42
32
|
```
|
43
33
|
|
44
|
-
|
45
|
-
|
46
|
-
### Hook
|
47
|
-
|
48
|
-
The Hook provided by Modern.js is used to control the built-in logic in the Web Server, and all page requests go through the Hook.
|
34
|
+
Once enabled, a `server/index.ts` file will be automatically created in the project directory where custom logic can be implemented.
|
49
35
|
|
50
|
-
|
51
|
-
|
52
|
-
```ts
|
53
|
-
import type {
|
54
|
-
AfterMatchHook,
|
55
|
-
AfterRenderHook,
|
56
|
-
} from '@modern-js/runtime/server';
|
57
|
-
|
58
|
-
export const afterMatch: AfterMatchHook = (ctx, next) => {
|
59
|
-
next();
|
60
|
-
};
|
61
|
-
|
62
|
-
export const afterRender: AfterRenderHook = (ctx, next) => {
|
63
|
-
next();
|
64
|
-
};
|
65
|
-
```
|
66
|
-
|
67
|
-
Projects should follow these best practices when using Hook:
|
68
|
-
|
69
|
-
1. Authentication in afterMatch.
|
70
|
-
2. Do Rewrite and Redirect in afterMatch.
|
71
|
-
3. Inject HTML content in afterRender.
|
72
|
-
|
73
|
-
:::note
|
74
|
-
For more detail, see [Hook](/apis/app/runtime/web-server/hook).
|
75
|
-
:::
|
76
|
-
|
77
|
-
### Middleware
|
78
|
-
|
79
|
-
For some projects, there may be more requirements at the server level, Modern.js provides Middleware to add pre-middleware for Web Server. It can only run in a Node environment, so if the project is deployed to another environment, such as a Worker environment, Middleware cannot be used.
|
80
|
-
|
81
|
-
:::note
|
82
|
-
In the next major release, Modern.js will use new middleware to replace this approach.
|
83
|
-
|
84
|
-
It is recommended to use [UnstableMiddleware](/guides/advanced-features/web-server.html#unstablemiddleware) to handle page requests.
|
85
|
-
:::
|
86
|
-
|
87
|
-
Modern.js provides a set of APIs by default for projects to use:
|
88
|
-
|
89
|
-
```ts
|
90
|
-
import { Middleware } from '@modern-js/runtime/server';
|
91
|
-
|
92
|
-
export const middleware: Middleware = (context, next) => {
|
93
|
-
const {
|
94
|
-
source: { req, res },
|
95
|
-
} = context;
|
96
|
-
console.log(req.url);
|
97
|
-
next();
|
98
|
-
};
|
99
|
-
```
|
100
|
-
|
101
|
-
:::note
|
102
|
-
For more detail, see [Middleware] (/apis/app/runtime/web-server/middleware).
|
103
|
-
:::
|
104
|
-
|
105
|
-
Projects should follow these best practices when using Middleware:
|
106
|
-
|
107
|
-
1. In Middleware, you can directly operate origin request and response objects, do event tracking, and inject Node services (databases, Redis, etc.) that may be used for SSR rendering.
|
108
|
-
2. Operations such as marking and crawler optimization can be done in Middleware.
|
109
|
-
3. In Middleware, you can ignore the default rendering and customize the rendering process.
|
110
|
-
|
111
|
-
**In general, in CSR projects, using Hook can basically meet all the needs of simple scenarios. In SSR projects, Middleware can be used for more complex Node extensions.**
|
36
|
+
## Custom Web Server Capabilities
|
112
37
|
|
113
38
|
### Unstable Middleware
|
114
39
|
|
115
|
-
Modern.js
|
40
|
+
Modern.js supports adding rendering middleware to the Web Server, allowing custom logic execution before and after processing page routes.
|
116
41
|
|
117
42
|
```ts title="server/index.ts"
|
118
43
|
import {
|
119
44
|
UnstableMiddleware,
|
120
45
|
UnstableMiddlewareContext,
|
121
|
-
} from '@
|
46
|
+
} from '@Modern.js/runtime/server';
|
122
47
|
|
123
48
|
const time: UnstableMiddleware = async (c: UnstableMiddlewareContext, next) => {
|
124
49
|
const start = Date.now();
|
@@ -133,30 +58,41 @@ const time: UnstableMiddleware = async (c: UnstableMiddlewareContext, next) => {
|
|
133
58
|
export const unstableMiddleware: UnstableMiddleware[] = [time];
|
134
59
|
```
|
135
60
|
|
136
|
-
:::
|
137
|
-
For detailed API and more usage,
|
61
|
+
:::info
|
62
|
+
For detailed API and more usage, see [UnstableMiddleware](/apis/app/runtime/web-server/unstable_middleware).
|
138
63
|
:::
|
139
64
|
|
140
|
-
|
65
|
+
### Hooks
|
141
66
|
|
142
|
-
|
67
|
+
:::warning
|
68
|
+
We recommend using UnstableMiddleware instead of Hooks.
|
69
|
+
:::
|
143
70
|
|
144
|
-
|
71
|
+
Modern.js provides Hooks to control specific logic in the Web Server. All page requests will pass through Hooks.
|
145
72
|
|
146
|
-
|
73
|
+
Currently, two types of Hooks are available: `AfterMatch` and `AfterRender`. Developers can implement them in `server/index.ts` as follows:
|
147
74
|
|
148
75
|
```ts
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
76
|
+
import type {
|
77
|
+
AfterMatchHook,
|
78
|
+
AfterRenderHook,
|
79
|
+
} from '@modern-js/runtime/server';
|
80
|
+
|
81
|
+
export const afterMatch: AfterMatchHook = (ctx, next) => {
|
82
|
+
next();
|
83
|
+
};
|
84
|
+
|
85
|
+
export const afterRender: AfterRenderHook = (ctx, next) => {
|
86
|
+
next();
|
87
|
+
};
|
154
88
|
```
|
155
89
|
|
156
|
-
|
90
|
+
Best practices when using Hooks:
|
157
91
|
|
158
|
-
|
92
|
+
1. Perform authorization checks in afterMatch.
|
93
|
+
2. Handle Rewrite and Redirect in afterMatch.
|
94
|
+
3. Inject HTML content in afterRender.
|
159
95
|
|
160
|
-
:::
|
161
|
-
|
96
|
+
:::info
|
97
|
+
For detailed API and more usage, see [Hook](/apis/app/runtime/web-server/hook).
|
162
98
|
:::
|
@@ -6,6 +6,10 @@ title: Hook
|
|
6
6
|
|
7
7
|
用于拓展 Modern.js 内置的 Web Server,所有的页面请求都会经过 Hook。
|
8
8
|
|
9
|
+
:::warning
|
10
|
+
推荐使用 [UnstableMiddleware](/apis/app/runtime/web-server/unstable_middleware) 来处理页面请求。
|
11
|
+
:::
|
12
|
+
|
9
13
|
:::note
|
10
14
|
更多内容可以查看[自定义 Web Server](/guides/advanced-features/web-server)。
|
11
15
|
:::
|
@@ -6,11 +6,9 @@ title: Middleware
|
|
6
6
|
|
7
7
|
用于拓展 Modern.js 内置的 Web Server,与 [Hook](/apis/app/runtime/web-server/hook) 不同的是,Middleware 可以直接操作 Node 原生的请求、响应对象,并且可以使用框架拓展。
|
8
8
|
|
9
|
-
:::
|
10
|
-
|
11
|
-
在下一个大版本,Modern.js 将会使用新 Middleware 来替代该写法。
|
9
|
+
:::warning
|
12
10
|
|
13
|
-
|
11
|
+
Middleware 将会在下一个大版本中废弃,推荐使用 [UnstableMiddleware](/apis/app/runtime/web-server/unstable_middleware) 处理页面请求。
|
14
12
|
|
15
13
|
:::
|
16
14
|
|
@@ -119,22 +117,3 @@ export const Middleware = () => async (ctx, next) => {
|
|
119
117
|
ctx.response.locals.rpc = createRpcInstance();
|
120
118
|
};
|
121
119
|
```
|
122
|
-
|
123
|
-
### 框架拓展
|
124
|
-
|
125
|
-
Middleware 还可以和 BFF 一样,使用运行时框架拓展。Modern.js 约定,当使用框架运行时拓展时,类型信息从 `@modern-js/runtime/{namespace}` 下导出,Middleware 可以使用框架语法,例如框架中间件写法,以下是伪代码:
|
126
|
-
|
127
|
-
```ts
|
128
|
-
import { SomeType } from '@modern-js/runtime/{namespace}';
|
129
|
-
|
130
|
-
export const middleware: SomeType = (ctx, next) => {
|
131
|
-
console.log(ctx.url);
|
132
|
-
next();
|
133
|
-
};
|
134
|
-
```
|
135
|
-
|
136
|
-
默认情况下,在安装框架拓展插件后,Web Server 的框架拓展能力是关闭的。如果希望使用框架拓展,可以通过 [`server.enableFrameworkExt`](/configure/app/server/enable-framework-ext.html) 开启。
|
137
|
-
|
138
|
-
:::info
|
139
|
-
框架拓展导出的类型名不一定为 Middleware,命名由框架拓展插件。
|
140
|
-
:::
|
File without changes
|
@@ -63,6 +63,10 @@ export default defineRuntimeConfig(entryName => {
|
|
63
63
|
使用 `src/modern.runtime.ts` 配置方式需要 Modern.js 版本 **MAJOR_VERSION.66.0** 或更高版本。
|
64
64
|
:::
|
65
65
|
|
66
|
+
import RuntimeCliConfig from '@site-docs/components/runtime-cli-config';
|
67
|
+
|
68
|
+
<RuntimeCliConfig />
|
69
|
+
|
66
70
|
## 配置方式演进
|
67
71
|
|
68
72
|
在 MAJOR_VERSION.66.0 之前,运行时配置分散在多个位置:
|
@@ -4,25 +4,17 @@ sidebar_position: 16
|
|
4
4
|
|
5
5
|
# 自定义 Web Server
|
6
6
|
|
7
|
-
Modern.js
|
7
|
+
Modern.js 将大部分项目需要的服务端能力都进行了封装,通常项目无需进行服务端开发。但在有些开发场景下,例如用户鉴权、请求预处理、添加页面渲染骨架等,项目仍需要对服务端进行定制。
|
8
8
|
|
9
|
-
|
9
|
+
Modern.js 提供了 **渲染中间件(Middleware)** 与**生命周期钩子(Hook)** 两类 API 来扩展 Web Server。
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
出于上述原因,Modern.js 提供了三种方式,让项目可以在根据需求,渐进式的定制服务端能力。
|
14
|
-
|
15
|
-
:::warning
|
16
|
-
三种扩展方式无法同时工作,开发者需要根据场景选择合适的方式。
|
11
|
+
:::note
|
12
|
+
Middleware 与 Hook 只会在用户请求页面路由时生效,BFF 路由不会经过这些 API。
|
17
13
|
:::
|
18
14
|
|
19
|
-
##
|
20
|
-
|
21
|
-
第一种方式是通过 Modern.js 提供的服务端运行时 API,在特定的生命周期对服务端进行定制。提供这种方式的目的是在部分情况下,开发者并不需要控制完整的 Web Server,只需要添加服务端逻辑即可。
|
15
|
+
## 开启自定义 Web Server
|
22
16
|
|
23
|
-
|
24
|
-
|
25
|
-
可以在项目根目录执行 `pnpm run new` 命令,开启「自定义 Web Serve」功能:
|
17
|
+
开发者可以在项目根目录执行 `pnpm run new` 命令,开启「自定义 Web Server」功能:
|
26
18
|
|
27
19
|
```bash
|
28
20
|
? 请选择你想要的操作 创建工程元素
|
@@ -39,78 +31,13 @@ export default defineConfig({
|
|
39
31
|
});
|
40
32
|
```
|
41
33
|
|
42
|
-
开启功能后,项目目录下会自动创建 `server/index.ts` 文件,可以在这个文件中编写自定义逻辑。
|
43
|
-
|
44
|
-
### Hook
|
45
|
-
|
46
|
-
Modern.js 提供的 Hook 用于控制 Web Server 中的内置逻辑,所有的页面请求都会经过 Hook。
|
47
|
-
|
48
|
-
目前提供了两种 Hook,分别是 `AfterMatch` 和 `AfterRender`,可以用于修改渲染结果。可以在 `server/index.ts` 中这样写:
|
34
|
+
开启功能后,项目目录下会自动创建 `server/index.ts` 文件,可以在这个文件中编写自定义逻辑。
|
49
35
|
|
50
|
-
|
51
|
-
import type {
|
52
|
-
AfterMatchHook,
|
53
|
-
AfterRenderHook,
|
54
|
-
} from '@modern-js/runtime/server';
|
55
|
-
|
56
|
-
export const afterMatch: AfterMatchHook = (ctx, next) => {
|
57
|
-
next();
|
58
|
-
};
|
59
|
-
|
60
|
-
export const afterRender: AfterRenderHook = (ctx, next) => {
|
61
|
-
next();
|
62
|
-
};
|
63
|
-
```
|
64
|
-
|
65
|
-
项目在使用 Hook 时,应该有以下最佳实践:
|
66
|
-
|
67
|
-
1. 在 afterMatch 中做权限校验。
|
68
|
-
2. 在 afterMatch 做 Rewrite 和 Redirect。
|
69
|
-
3. 在 afterRender 中做 HTML 内容注入。
|
70
|
-
|
71
|
-
:::note
|
72
|
-
详细 API 和更多用法可以查看 [Hook](/apis/app/runtime/web-server/hook)。
|
73
|
-
:::
|
74
|
-
|
75
|
-
### Middleware
|
76
|
-
|
77
|
-
对于某些项目,可能在服务端有更多的需求,Modern.js 提供了 Middleware 为 Web Server 添加前置中间件。它只能运行在 Node 环境下,因此如果项目被部署到其他环境中,如 Worker 环境,则不可以使用 Middleware。
|
78
|
-
|
79
|
-
:::note
|
80
|
-
下一个大版本,Modern.js 将会使用新 Middleware 来替代该写法。
|
81
|
-
|
82
|
-
推荐使用 [UnstableMiddleware](/guides/advanced-features/web-server.html#unstablemiddleware) 处理页面请求。
|
83
|
-
:::
|
84
|
-
|
85
|
-
Modern.js 默认提供了一套 API 供项目使用:
|
86
|
-
|
87
|
-
```ts
|
88
|
-
import { Middleware } from '@modern-js/runtime/server';
|
89
|
-
|
90
|
-
export const middleware: Middleware = (context, next) => {
|
91
|
-
const {
|
92
|
-
source: { req, res },
|
93
|
-
} = context;
|
94
|
-
console.log(req.url);
|
95
|
-
next();
|
96
|
-
};
|
97
|
-
```
|
98
|
-
|
99
|
-
:::note
|
100
|
-
详细 API 和更多用法可以查看 [Middleware](/apis/app/runtime/web-server/middleware)。
|
101
|
-
:::
|
102
|
-
|
103
|
-
项目在使用 Middleware 时,应该有以下最佳实践:
|
104
|
-
|
105
|
-
1. 在 Middleware 中可以直接操作原生的请求、响应对象,做数据打点、注入 SSR 渲染可能用到的 Node 服务(数据库、Redis 等)。
|
106
|
-
2. 在 Middleware 里可以做类似功能打标、爬虫优化等功能。
|
107
|
-
3. 在 Middleware 里可以无视后续默认渲染,自定义渲染流程。
|
108
|
-
|
109
|
-
**总的来说,CSR 项目中,使用 Hook 基本能满足简单场景的所有需求。SSR 项目中,可以使用 Middleware 做更复杂的 Node 扩展。**
|
36
|
+
## 自定义 Web Server 能力
|
110
37
|
|
111
38
|
### Unstable Middleware
|
112
39
|
|
113
|
-
Modern.js
|
40
|
+
Modern.js 支持为 Web Server 添加渲染中间件,支持在处理页面路由的前后执行自定义逻辑。
|
114
41
|
|
115
42
|
```ts title="server/index.ts"
|
116
43
|
import {
|
@@ -131,30 +58,41 @@ const time: UnstableMiddleware = async (c: UnstableMiddlewareContext, next) => {
|
|
131
58
|
export const unstableMiddleware: UnstableMiddleware[] = [time];
|
132
59
|
```
|
133
60
|
|
134
|
-
:::
|
135
|
-
详细 API 和更多用法查看 [UnstableMiddleware](/apis/app/runtime/web-server/unstable_middleware)
|
61
|
+
:::info
|
62
|
+
详细 API 和更多用法查看 [UnstableMiddleware](/apis/app/runtime/web-server/unstable_middleware)。
|
136
63
|
:::
|
137
64
|
|
138
|
-
|
65
|
+
### Hook
|
139
66
|
|
140
|
-
|
67
|
+
:::warning
|
68
|
+
我们推荐使用 UnstableMiddleware 代替 Hook。
|
69
|
+
:::
|
141
70
|
|
142
|
-
|
71
|
+
Modern.js 提供的 Hook 用于控制 Web Server 中的特定逻辑,所有的页面请求都会经过 Hook。
|
143
72
|
|
144
|
-
|
73
|
+
目前提供了两种 Hook,分别是 `AfterMatch` 和 `AfterRender`,开发者可以在 `server/index.ts` 中这样写:
|
145
74
|
|
146
75
|
```ts
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
76
|
+
import type {
|
77
|
+
AfterMatchHook,
|
78
|
+
AfterRenderHook,
|
79
|
+
} from '@modern-js/runtime/server';
|
80
|
+
|
81
|
+
export const afterMatch: AfterMatchHook = (ctx, next) => {
|
82
|
+
next();
|
83
|
+
};
|
84
|
+
|
85
|
+
export const afterRender: AfterRenderHook = (ctx, next) => {
|
86
|
+
next();
|
87
|
+
};
|
152
88
|
```
|
153
89
|
|
154
|
-
|
90
|
+
项目在使用 Hook 时,应该有以下最佳实践:
|
155
91
|
|
156
|
-
|
92
|
+
1. 在 afterMatch 中做权限校验。
|
93
|
+
2. 在 afterMatch 做 Rewrite 和 Redirect。
|
94
|
+
3. 在 afterRender 中做 HTML 内容注入。
|
157
95
|
|
158
|
-
:::
|
159
|
-
|
96
|
+
:::info
|
97
|
+
详细 API 和更多用法可以查看 [Hook](/apis/app/runtime/web-server/hook)。
|
160
98
|
:::
|
package/package.json
CHANGED
@@ -1,49 +0,0 @@
|
|
1
|
-
---
|
2
|
-
title: enableFrameworkExt
|
3
|
-
---
|
4
|
-
|
5
|
-
# server.enableFrameworkExt
|
6
|
-
|
7
|
-
- **Type:** `boolean`
|
8
|
-
- **Default:** `false`
|
9
|
-
|
10
|
-
By default, when the [custom Web Server feature](/guides/advanced-features/web-server) is enabled, the Middleware will use the Modern.js's syntax.
|
11
|
-
|
12
|
-
Enabling `server.enableFrameworkExt` allows the use of syntax extensions from other frameworks.
|
13
|
-
|
14
|
-
```ts title="modern.config.ts"
|
15
|
-
export default defineConfig({
|
16
|
-
server: {
|
17
|
-
enableFrameworkExt: true,
|
18
|
-
},
|
19
|
-
});
|
20
|
-
```
|
21
|
-
|
22
|
-
## Example
|
23
|
-
|
24
|
-
Default usage:
|
25
|
-
|
26
|
-
```ts title="server/index.ts"
|
27
|
-
import { Middleware } from '@modern-js/runtime/server';
|
28
|
-
|
29
|
-
export const middleware: Middleware = (ctx, next) => {
|
30
|
-
console.log(ctx.request.url);
|
31
|
-
next();
|
32
|
-
};
|
33
|
-
```
|
34
|
-
|
35
|
-
After enabling it, the Middleware type will be exported from other namespaces, and syntax extensions from frameworks can be used:
|
36
|
-
|
37
|
-
```ts title="server/index.ts"
|
38
|
-
import { SomeType } from '@modern-js/runtime/{namespace}';
|
39
|
-
|
40
|
-
export const middleware: SomeType = (...args) => {
|
41
|
-
console.log(args[0].url);
|
42
|
-
next();
|
43
|
-
};
|
44
|
-
```
|
45
|
-
|
46
|
-
:::note
|
47
|
-
The above code is pseudocode, and the specific usage needs to refer to the corresponding framework extension.
|
48
|
-
|
49
|
-
:::
|
@@ -1,49 +0,0 @@
|
|
1
|
-
---
|
2
|
-
title: enableFrameworkExt
|
3
|
-
---
|
4
|
-
|
5
|
-
# server.enableFrameworkExt
|
6
|
-
|
7
|
-
- **类型:** `boolean`
|
8
|
-
- **默认值:** `false`
|
9
|
-
|
10
|
-
默认情况下,开启[自定义 Web Server 功能](/guides/advanced-features/web-server)后,Middleware 会使用 Modern.js 本身的语法。
|
11
|
-
|
12
|
-
开启 `server.enableFrameworkExt` 可以使用其他框架扩展的语法。
|
13
|
-
|
14
|
-
```ts title="modern.config.ts"
|
15
|
-
export default defineConfig({
|
16
|
-
server: {
|
17
|
-
enableFrameworkExt: true,
|
18
|
-
},
|
19
|
-
});
|
20
|
-
```
|
21
|
-
|
22
|
-
## 示例
|
23
|
-
|
24
|
-
默认的使用方式:
|
25
|
-
|
26
|
-
```ts title="server/index.ts"
|
27
|
-
import { Middleware } from '@modern-js/runtime/server';
|
28
|
-
|
29
|
-
export const middleware: Middleware = (ctx, next) => {
|
30
|
-
console.log(ctx.request.url);
|
31
|
-
next();
|
32
|
-
};
|
33
|
-
```
|
34
|
-
|
35
|
-
开启后,Middleware 类型将从其他命名空间下导出,并且可以使用框架拓展的语法:
|
36
|
-
|
37
|
-
```ts title="server/index.ts"
|
38
|
-
import { SomeType } from '@modern-js/runtime/{namespace}';
|
39
|
-
|
40
|
-
export const middleware: SomeType = (...args) => {
|
41
|
-
console.log(args[0].url);
|
42
|
-
next();
|
43
|
-
};
|
44
|
-
```
|
45
|
-
|
46
|
-
:::note
|
47
|
-
上述代码为伪代码,具体使用方式需要参考对应的框架拓展。
|
48
|
-
|
49
|
-
:::
|