@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.
@@ -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
- :::note
9
+ :::warning
10
10
 
11
- In the next major release, Modern.js will use new middleware to replace this approach.
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:
@@ -2,7 +2,7 @@
2
2
  title: router
3
3
  ---
4
4
 
5
- # runtime.router
5
+ # router
6
6
 
7
7
  import RouterLegacyTip from "@site-docs-en/components/router-legacy-tip"
8
8
 
@@ -2,7 +2,7 @@
2
2
  title: state
3
3
  ---
4
4
 
5
- # runtime.state
5
+ # state
6
6
 
7
7
  - **Type:** `boolean | object`
8
8
  - **Default:** `false`
@@ -4,31 +4,21 @@ sidebar_position: 16
4
4
 
5
5
  # Custom Web Server
6
6
 
7
- As a client-centric development framework, Modern.js has limited customization capabilities on the server side. However, in some development scenarios, special server-level logic needs to be customized, such as user authentication, request preprocessing, and adding page rendering skeletons.
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
- Some developers may be wondering, Modern.js already provides [BFF](/guides/advanced-features/bff/function.html), why you need **Custom Web Server**.
9
+ Modern.js provides two types of APIs to extend the Web Server: **Middleware** and **Lifecycle Hooks**.
10
10
 
11
- The reason is that by default, page routing does not go through BFF, it has no way to provide server-side custom logic for page access. The reason for this design is that we do not want the service that controls the page to be bound to the BFF service, this is to avoid the BFF framework restricting how the page is deployed.
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
- ## Extending Web Server with API
15
+ ## Enabling Custom Web Server
22
16
 
23
- The first way is to customize the server-side at a specific lifecycle through the server-side runtime API provided by Modern.js. The purpose of providing this way is that in some cases, developers do not need to control the full Web Server, but only need to add server-level logic.
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
- ? Please select the operation you want: Create Element
31
- ? Please select the type of element to create: New "Custom Web Server" source code directory
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
- After the function is turned on, the `server/index.ts` file will be automatically created in the project directory, and custom logic can be written in this file. Modern.js provides two types of APIs, **Hook** and **Middleware**, to extend Web Server.
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
- Currently, two Hooks are provided: `AfterMatch` and `AfterRender`, which can be used to modify the rendering results. It can be written in `server/index.ts` as follows:
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 will provide new Middleware to add pre-processing middleware to the Web Server, supporting the execution of custom logic before and after handling the page.
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 '@modern-js/runtime/server';
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
- :::note
137
- For detailed API and more usage, please refer to [UnstableMiddleware](/apis/app/runtime/web-server/unstable_middleware)
61
+ :::info
62
+ For detailed API and more usage, see [UnstableMiddleware](/apis/app/runtime/web-server/unstable_middleware).
138
63
  :::
139
64
 
140
- ## Managed Page Requests with BFF
65
+ ### Hooks
141
66
 
142
- The second way is to use BFF to Managed page rendering. In this way, all requests will first hit the BFF service.
67
+ :::warning
68
+ We recommend using UnstableMiddleware instead of Hooks.
69
+ :::
143
70
 
144
- This method can uniformly control the server-level logic of all requests through BFF. Therefore, it is suitable for scenarios where the server-level logic is complex, and BFF and pages need common server-level logic. But it still relies on the Web Server of Modern.js as a whole, and cannot run the logic on existing services.
71
+ Modern.js provides Hooks to control specific logic in the Web Server. All page requests will pass through Hooks.
145
72
 
146
- To use this method, we first need to enable the "BFF" function through `pnpm new`. Then add [`bff.enableHandleWeb`](/configure/app/bff/enable-handle-web.html) configuration in the configuration file:
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
- export default defineConfig({
150
- bff: {
151
- enableHandleWeb: true,
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
- When this value is set to `true`, page request traffic also goes through the BFF, and the logic built into Modern.js for page rendering defaults to running as the last middleware for the BFF service.
90
+ Best practices when using Hooks:
157
91
 
158
- ## Fully Customized Web Server
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
- :::note
161
- Comming soon..
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
- :::note
10
-
11
- 在下一个大版本,Modern.js 将会使用新 Middleware 来替代该写法。
9
+ :::warning
12
10
 
13
- 推荐使用 [UnstableMiddleware](/apis/app/runtime/web-server/unstable_middleware) 处理页面请求。
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 之前,运行时配置分散在多个位置:
@@ -2,7 +2,7 @@
2
2
  title: router
3
3
  ---
4
4
 
5
- # runtime.router
5
+ # router
6
6
 
7
7
  import RouterLegacyTip from "@site-docs/components/router-legacy-tip"
8
8
 
@@ -2,7 +2,7 @@
2
2
  title: state
3
3
  ---
4
4
 
5
- # runtime.state
5
+ # state
6
6
 
7
7
  - **类型:** `boolean | object`
8
8
  - **默认值:** `false`
@@ -4,25 +4,17 @@ sidebar_position: 16
4
4
 
5
5
  # 自定义 Web Server
6
6
 
7
- Modern.js 作为以客户端为中心的开发框架,对服务端的定制能力较弱。而在有些开发场景下,需要定制特殊的服务端逻辑,例如用户鉴权、请求预处理、添加页面渲染骨架等。
7
+ Modern.js 将大部分项目需要的服务端能力都进行了封装,通常项目无需进行服务端开发。但在有些开发场景下,例如用户鉴权、请求预处理、添加页面渲染骨架等,项目仍需要对服务端进行定制。
8
8
 
9
- 一些开发者可能会有疑惑,Modern.js 已经提供了 [BFF 能力](/guides/advanced-features/bff/function.html),为什么还需要**自定义 Web Server**。
9
+ Modern.js 提供了 **渲染中间件(Middleware)** 与**生命周期钩子(Hook)** 两类 API 来扩展 Web Server
10
10
 
11
- 因为在默认情况下,页面路由并不会经过 BFF,它没有办法为页面访问提供服务端的定制逻辑。之所以这样设计,是因为我们不希望控制页面的服务与 BFF 服务绑定在一起,避免 BFF 的框架限制页面的部署方式。例如将页面与 BFF 分开托管、将页面服务部署到非 Node 的环境上,或是针对部署平台做定制等。
12
-
13
- 出于上述原因,Modern.js 提供了三种方式,让项目可以在根据需求,渐进式的定制服务端能力。
14
-
15
- :::warning
16
- 三种扩展方式无法同时工作,开发者需要根据场景选择合适的方式。
11
+ :::note
12
+ Middleware 与 Hook 只会在用户请求页面路由时生效,BFF 路由不会经过这些 API。
17
13
  :::
18
14
 
19
- ## 使用 API 扩展 Web Server
20
-
21
- 第一种方式是通过 Modern.js 提供的服务端运行时 API,在特定的生命周期对服务端进行定制。提供这种方式的目的是在部分情况下,开发者并不需要控制完整的 Web Server,只需要添加服务端逻辑即可。
15
+ ## 开启自定义 Web Server
22
16
 
23
- 这种方式无法控制完整的 Web Server,并且扩展逻辑**只在请求页面时生效**。因此,它适用于服务端逻辑相对简单,不希望额外创建 BFF 或者 BFF 和页面无需公用服务端逻辑场景。
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` 文件,可以在这个文件中编写自定义逻辑。Modern.js 提供了 **Hook** 与 **Middleware** 两类 API 来扩展 Web Server。
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
- ```ts
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 将提供新 Middleware 为 Web Server 添加前置中间件,支持在处理页面的前后执行自定义逻辑。
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
- :::note
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
- ## 通过 BFF 托管页面请求
65
+ ### Hook
139
66
 
140
- 第二种方式,是利用 BFF 来托管页面渲染,这种方式下,所有的请求都会先打到 BFF 的服务。
67
+ :::warning
68
+ 我们推荐使用 UnstableMiddleware 代替 Hook。
69
+ :::
141
70
 
142
- 因为这种方式可以通过 BFF 统一控制所有请求的服务端逻辑。因此,它适用于服务端逻辑复杂,BFF 和页面需要公用服务端逻辑的场景。但它整体还是依托于 Modern.js Web Server 运行,无法将逻辑运行在已有的服务上。
71
+ Modern.js 提供的 Hook 用于控制 Web Server 中的特定逻辑,所有的页面请求都会经过 Hook。
143
72
 
144
- 使用这种方式,我们需要先通过 `pnpm new` 开启「BFF」功能。然后在配置文件中添加 [`bff.enableHandleWeb`](/configure/app/bff/enable-handle-web.html) 配置:
73
+ 目前提供了两种 Hook,分别是 `AfterMatch` `AfterRender`,开发者可以在 `server/index.ts` 中这样写:
145
74
 
146
75
  ```ts
147
- export default defineConfig({
148
- bff: {
149
- enableHandleWeb: true,
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
- 当该值设置为 `true` 时,页面请求流量也会经过 BFF,并且 Modern.js 内置的页面渲染的逻辑默认会作为 BFF 服务的最后一个中间件运行。
90
+ 项目在使用 Hook 时,应该有以下最佳实践:
155
91
 
156
- ## 完全自定义的 Web Server
92
+ 1. afterMatch 中做权限校验。
93
+ 2. 在 afterMatch 做 Rewrite 和 Redirect。
94
+ 3. 在 afterRender 中做 HTML 内容注入。
157
95
 
158
- :::note
159
- 敬请期待
96
+ :::info
97
+ 详细 API 和更多用法可以查看 [Hook](/apis/app/runtime/web-server/hook)。
160
98
  :::
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.66.1-alpha.0",
18
+ "version": "2.66.1-alpha.2",
19
19
  "publishConfig": {
20
20
  "registry": "https://registry.npmjs.org/",
21
21
  "access": "public"
@@ -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
- :::