@modern-js/main-doc 0.0.0-nightly-20250514160342 → 0.0.0-nightly-20250518160324
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/hooks/server/server.mdx +10 -0
- package/docs/en/apis/app/hooks/src/routes.mdx +3 -3
- package/docs/en/apis/app/runtime/bff/use-hono-context.mdx +30 -0
- package/docs/en/components/enable-bff.mdx +1 -27
- package/docs/en/components/rsbuild-config-tooltip.mdx +2 -2
- package/docs/en/components/tech-stack-node-framework.mdx +1 -1
- package/docs/en/configure/app/dev/asset-prefix.mdx +2 -3
- package/docs/en/configure/app/dev/client.mdx +2 -3
- package/docs/en/configure/app/dev/hmr.mdx +2 -3
- package/docs/en/configure/app/dev/live-reload.mdx +2 -3
- package/docs/en/configure/app/dev/progress-bar.mdx +2 -3
- package/docs/en/configure/app/dev/setup-middlewares.mdx +2 -3
- package/docs/en/configure/app/dev/watch-files.mdx +2 -3
- package/docs/en/configure/app/dev/write-to-disk.mdx +2 -3
- package/docs/en/configure/app/html/app-icon.mdx +2 -3
- package/docs/en/configure/app/html/crossorigin.mdx +2 -3
- package/docs/en/configure/app/html/favicon.mdx +2 -3
- package/docs/en/configure/app/html/inject.mdx +2 -3
- package/docs/en/configure/app/html/meta.mdx +2 -3
- package/docs/en/configure/app/html/mount-id.mdx +2 -3
- package/docs/en/configure/app/html/output-structure.mdx +2 -3
- package/docs/en/configure/app/html/script-loading.mdx +2 -3
- package/docs/en/configure/app/html/tags.mdx +2 -3
- package/docs/en/configure/app/html/template-parameters.mdx +2 -3
- package/docs/en/configure/app/html/template.mdx +2 -3
- package/docs/en/configure/app/html/title.mdx +2 -3
- package/docs/en/configure/app/output/asset-prefix.mdx +2 -3
- package/docs/en/configure/app/output/charset.mdx +2 -3
- package/docs/en/configure/app/output/copy.mdx +2 -3
- package/docs/en/configure/app/output/css-modules.mdx +2 -3
- package/docs/en/configure/app/output/data-uri-limit.mdx +2 -3
- package/docs/en/configure/app/output/dist-path.mdx +2 -3
- package/docs/en/configure/app/output/externals.mdx +2 -3
- package/docs/en/configure/app/output/filename-hash.mdx +2 -3
- package/docs/en/configure/app/output/filename.mdx +2 -3
- package/docs/en/configure/app/output/inject-styles.mdx +2 -3
- package/docs/en/configure/app/output/inline-scripts.mdx +2 -3
- package/docs/en/configure/app/output/inline-styles.mdx +2 -3
- package/docs/en/configure/app/output/legal-comments.mdx +2 -3
- package/docs/en/configure/app/output/minify.mdx +2 -3
- package/docs/en/configure/app/output/override-browserslist.mdx +2 -3
- package/docs/en/configure/app/output/polyfill.mdx +2 -3
- package/docs/en/configure/app/output/source-map.mdx +2 -3
- package/docs/en/configure/app/performance/build-cache.mdx +2 -3
- package/docs/en/configure/app/performance/bundle-analyze.mdx +2 -3
- package/docs/en/configure/app/performance/chunk-split.mdx +2 -3
- package/docs/en/configure/app/performance/dns-prefetch.mdx +2 -3
- package/docs/en/configure/app/performance/preconnect.mdx +2 -3
- package/docs/en/configure/app/performance/prefetch.mdx +2 -3
- package/docs/en/configure/app/performance/preload.mdx +2 -3
- package/docs/en/configure/app/performance/print-file-size.mdx +2 -3
- package/docs/en/configure/app/performance/profile.mdx +2 -3
- package/docs/en/configure/app/performance/remove-console.mdx +2 -3
- package/docs/en/configure/app/performance/remove-moment-locale.mdx +2 -3
- package/docs/en/configure/app/plugins.mdx +13 -33
- package/docs/en/configure/app/runtime/master-app.mdx +1 -5
- package/docs/en/configure/app/runtime/plugins.mdx +58 -0
- package/docs/en/configure/app/security/nonce.mdx +2 -3
- package/docs/en/configure/app/security/sri.mdx +0 -1
- package/docs/en/configure/app/server/port.mdx +2 -3
- package/docs/en/configure/app/source/alias-strategy.mdx +2 -3
- package/docs/en/configure/app/source/alias.mdx +2 -3
- package/docs/en/configure/app/source/decorators.mdx +2 -3
- package/docs/en/configure/app/source/define.mdx +2 -3
- package/docs/en/configure/app/source/exclude.mdx +2 -3
- package/docs/en/configure/app/source/include.mdx +2 -3
- package/docs/en/configure/app/source/pre-entry.mdx +2 -3
- package/docs/en/configure/app/source/transform-import.mdx +2 -3
- package/docs/en/configure/app/tools/css-extract.mdx +2 -3
- package/docs/en/configure/app/tools/css-loader.mdx +2 -2
- package/docs/en/configure/app/tools/html-plugin.mdx +2 -3
- package/docs/en/configure/app/tools/lightningcss-loader.mdx +2 -3
- package/docs/en/configure/app/tools/postcss.mdx +2 -3
- package/docs/en/configure/app/tools/rspack.mdx +2 -3
- package/docs/en/configure/app/tools/style-loader.mdx +2 -3
- package/docs/en/guides/advanced-features/bff/extend-server.mdx +33 -82
- package/docs/en/guides/advanced-features/bff/frameworks.mdx +12 -68
- package/docs/en/guides/advanced-features/custom-server.mdx +13 -9
- package/docs/en/plugin/cli-plugins/api.mdx +6 -0
- package/docs/en/plugin/runtime-plugins/api.mdx +6 -0
- package/docs/en/tutorials/first-app/c04-routes.mdx +4 -2
- package/docs/en/tutorials/first-app/c05-loader.mdx +4 -1
- package/docs/zh/apis/app/hooks/server/server.mdx +10 -0
- package/docs/zh/apis/app/hooks/src/routes.mdx +3 -3
- package/docs/zh/apis/app/runtime/bff/use-hono-context.mdx +31 -0
- package/docs/zh/components/enable-bff.mdx +2 -27
- package/docs/zh/components/rsbuild-config-tooltip.mdx +2 -2
- package/docs/zh/components/tech-stack-node-framework.mdx +1 -1
- package/docs/zh/configure/app/dev/asset-prefix.mdx +2 -3
- package/docs/zh/configure/app/dev/client.mdx +2 -3
- package/docs/zh/configure/app/dev/hmr.mdx +2 -3
- package/docs/zh/configure/app/dev/live-reload.mdx +2 -3
- package/docs/zh/configure/app/dev/progress-bar.mdx +2 -3
- package/docs/zh/configure/app/dev/setup-middlewares.mdx +2 -3
- package/docs/zh/configure/app/dev/watch-files.mdx +2 -3
- package/docs/zh/configure/app/dev/write-to-disk.mdx +2 -3
- package/docs/zh/configure/app/html/app-icon.mdx +2 -3
- package/docs/zh/configure/app/html/crossorigin.mdx +2 -3
- package/docs/zh/configure/app/html/favicon.mdx +2 -3
- package/docs/zh/configure/app/html/inject.mdx +2 -3
- package/docs/zh/configure/app/html/meta.mdx +2 -3
- package/docs/zh/configure/app/html/mount-id.mdx +2 -3
- package/docs/zh/configure/app/html/output-structure.mdx +2 -3
- package/docs/zh/configure/app/html/script-loading.mdx +2 -3
- package/docs/zh/configure/app/html/tags.mdx +2 -3
- package/docs/zh/configure/app/html/template-parameters.mdx +2 -3
- package/docs/zh/configure/app/html/template.mdx +2 -3
- package/docs/zh/configure/app/html/title.mdx +2 -3
- package/docs/zh/configure/app/output/asset-prefix.mdx +2 -3
- package/docs/zh/configure/app/output/charset.mdx +2 -3
- package/docs/zh/configure/app/output/copy.mdx +2 -3
- package/docs/zh/configure/app/output/css-modules.mdx +2 -3
- package/docs/zh/configure/app/output/data-uri-limit.mdx +2 -3
- package/docs/zh/configure/app/output/dist-path.mdx +2 -3
- package/docs/zh/configure/app/output/externals.mdx +2 -3
- package/docs/zh/configure/app/output/filename-hash.mdx +2 -3
- package/docs/zh/configure/app/output/filename.mdx +2 -3
- package/docs/zh/configure/app/output/inject-styles.mdx +2 -3
- package/docs/zh/configure/app/output/inline-scripts.mdx +2 -3
- package/docs/zh/configure/app/output/inline-styles.mdx +2 -3
- package/docs/zh/configure/app/output/legal-comments.mdx +2 -3
- package/docs/zh/configure/app/output/minify.mdx +2 -3
- package/docs/zh/configure/app/output/override-browserslist.mdx +2 -3
- package/docs/zh/configure/app/output/polyfill.mdx +2 -3
- package/docs/zh/configure/app/output/source-map.mdx +2 -3
- package/docs/zh/configure/app/performance/build-cache.mdx +2 -3
- package/docs/zh/configure/app/performance/bundle-analyze.mdx +2 -3
- package/docs/zh/configure/app/performance/chunk-split.mdx +2 -3
- package/docs/zh/configure/app/performance/dns-prefetch.mdx +2 -3
- package/docs/zh/configure/app/performance/preconnect.mdx +2 -3
- package/docs/zh/configure/app/performance/prefetch.mdx +2 -3
- package/docs/zh/configure/app/performance/preload.mdx +2 -3
- package/docs/zh/configure/app/performance/print-file-size.mdx +2 -3
- package/docs/zh/configure/app/performance/profile.mdx +2 -3
- package/docs/zh/configure/app/performance/remove-console.mdx +2 -3
- package/docs/zh/configure/app/performance/remove-moment-locale.mdx +2 -3
- package/docs/zh/configure/app/plugins.mdx +3 -24
- package/docs/zh/configure/app/runtime/master-app.mdx +1 -5
- package/docs/zh/configure/app/runtime/plugins.mdx +58 -0
- package/docs/zh/configure/app/security/nonce.mdx +2 -3
- package/docs/zh/configure/app/security/sri.mdx +0 -1
- package/docs/zh/configure/app/server/port.mdx +2 -3
- package/docs/zh/configure/app/source/alias-strategy.mdx +2 -3
- package/docs/zh/configure/app/source/alias.mdx +2 -3
- package/docs/zh/configure/app/source/decorators.mdx +2 -3
- package/docs/zh/configure/app/source/define.mdx +2 -3
- package/docs/zh/configure/app/source/exclude.mdx +2 -3
- package/docs/zh/configure/app/source/include.mdx +2 -3
- package/docs/zh/configure/app/source/pre-entry.mdx +2 -3
- package/docs/zh/configure/app/source/transform-import.mdx +2 -3
- package/docs/zh/configure/app/tools/css-extract.mdx +2 -3
- package/docs/zh/configure/app/tools/css-loader.mdx +2 -3
- package/docs/zh/configure/app/tools/html-plugin.mdx +2 -3
- package/docs/zh/configure/app/tools/lightningcss-loader.mdx +2 -3
- package/docs/zh/configure/app/tools/postcss.mdx +2 -3
- package/docs/zh/configure/app/tools/rspack.mdx +2 -3
- package/docs/zh/configure/app/tools/style-loader.mdx +2 -3
- package/docs/zh/guides/advanced-features/bff/extend-server.mdx +28 -76
- package/docs/zh/guides/advanced-features/bff/frameworks.mdx +6 -66
- package/docs/zh/guides/advanced-features/custom-server.mdx +12 -8
- package/docs/zh/plugin/cli-plugins/api.mdx +6 -0
- package/docs/zh/plugin/runtime-plugins/api.mdx +6 -0
- package/docs/zh/tutorials/first-app/c04-routes.mdx +4 -2
- package/docs/zh/tutorials/first-app/c05-loader.mdx +4 -1
- package/package.json +6 -4
- package/rspress.config.ts +6 -1
- package/src/components/RsbuildLink/index.tsx +2 -2
- package/src/index.ts +1 -5
- package/src/pages/index.tsx +3 -3
- package/docs/en/apis/app/hooks/api/middleware.mdx +0 -11
- package/docs/en/apis/app/runtime/bff/hook.mdx +0 -44
- package/docs/en/apis/app/runtime/bff/use-context.mdx +0 -38
- package/docs/zh/apis/app/hooks/api/middleware.mdx +0 -11
- package/docs/zh/apis/app/runtime/bff/hook.mdx +0 -44
- package/docs/zh/apis/app/runtime/bff/use-context.mdx +0 -38
|
@@ -6,14 +6,20 @@ sidebar_position: 16
|
|
|
6
6
|
|
|
7
7
|
Modern.js 将大部分项目需要的服务端能力都进行了封装,通常项目无需进行服务端开发。但在有些开发场景下,例如用户鉴权、请求预处理、添加页面渲染骨架等,项目仍需要对服务端进行定制。
|
|
8
8
|
|
|
9
|
+
## 开启自定义 Server
|
|
10
|
+
|
|
11
|
+
开启自定义 Server 功能,需要执行以下步骤:
|
|
12
|
+
1. devDependencies 增加 `@modern-js/server-runtime` 及 `ts-node`依赖并安装。
|
|
13
|
+
2. `tsconfig` 的 `include` 中添加 `server`。
|
|
14
|
+
3. 项目目录下创建 `server/modern.server.ts` 文件,可以在这个文件中编写自定义逻辑。
|
|
15
|
+
|
|
9
16
|
## 自定义 Server 能力
|
|
10
17
|
|
|
11
|
-
|
|
18
|
+
`server/modern.server.ts` 文件中添加如下配置来扩展 Server:
|
|
12
19
|
- **中间件(Middleware)**
|
|
13
20
|
- **渲染中间件(RenderMiddleware)**
|
|
14
21
|
- **服务端插件(Plugin)**
|
|
15
22
|
|
|
16
|
-
|
|
17
23
|
其中 **Plugin** 中可以定义 **Middleware** 与 **RenderMiddleware**。 中间件加载流程如下图所示:
|
|
18
24
|
|
|
19
25
|
<img
|
|
@@ -58,20 +64,18 @@ type ServerConfig = {
|
|
|
58
64
|
### Middleware
|
|
59
65
|
|
|
60
66
|
Middleware 支持在 Modern.js 服务的**请求处理**与**页面路由**的流程前后,执行自定义逻辑。
|
|
61
|
-
即自定义逻辑既要处理接口路由,也要作用于页面路由,那么 Middleware
|
|
67
|
+
即自定义逻辑既要处理接口路由,也要作用于页面路由,那么 Middleware 是不二选择。
|
|
68
|
+
|
|
69
|
+
如果仅需要处理 BFF 接口路由,可以通过检查 `req.path` 是否以 BFF `prefix` 开头,来判断是否为 BFF 接口请求。
|
|
62
70
|
|
|
63
|
-
:::note
|
|
64
|
-
BFF 场景只有[运行时框架](/guides/advanced-features/bff/frameworks.html)为 Hono 时,BFF 路由才会经过 Middleware。
|
|
65
|
-
:::
|
|
66
71
|
|
|
67
72
|
#### 使用姿势
|
|
68
73
|
|
|
69
74
|
```ts title="server/modern.server.ts"
|
|
70
75
|
import { defineServerConfig, type MiddlewareHandler } from '@modern-js/server-runtime';
|
|
71
|
-
import { getMonitors } from '@modern-js/runtime';
|
|
72
76
|
|
|
73
77
|
export const handler: MiddlewareHandler = async (c, next) => {
|
|
74
|
-
const monitors =
|
|
78
|
+
const monitors = c.get('monitors');
|
|
75
79
|
const start = Date.now();
|
|
76
80
|
|
|
77
81
|
await next();
|
|
@@ -2,6 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
Modern.js 的 Runtime 插件允许您在应用程序的 React 代码运行时扩展和修改其行为。通过 Runtime 插件,您可以轻松地执行初始化任务、实现 React 高阶组件(HOC)封装等功能。
|
|
4
4
|
|
|
5
|
+
:::info
|
|
6
|
+
|
|
7
|
+
Runtime 插件需要通过 `src/modern.runtime.ts` 中的 [`plugins`](/configure/app/runtime/plugins) 字段配置。
|
|
8
|
+
|
|
9
|
+
:::
|
|
10
|
+
|
|
5
11
|
## 插件结构
|
|
6
12
|
|
|
7
13
|
一个典型的 Runtime 插件如下所示:
|
|
@@ -118,8 +118,9 @@ import { Radio } from 'antd';
|
|
|
118
118
|
|
|
119
119
|
然后将 UI 最顶部进行修改,增加一组单选框
|
|
120
120
|
|
|
121
|
-
```tsx
|
|
121
|
+
```tsx
|
|
122
122
|
export default function Layout() {
|
|
123
|
+
// [!code highlight:8]
|
|
123
124
|
return (
|
|
124
125
|
<div>
|
|
125
126
|
<div className="h-16 p-2 flex items-center justify-center">
|
|
@@ -146,8 +147,9 @@ import { Outlet, useLocation, useNavigate } from '@modern-js/runtime/router';
|
|
|
146
147
|
|
|
147
148
|
最后在 Layout 组件里增加局部状态和相关逻辑:
|
|
148
149
|
|
|
149
|
-
```tsx
|
|
150
|
+
```tsx
|
|
150
151
|
export default function Layout() {
|
|
152
|
+
// [!code highlight:8]
|
|
151
153
|
const navigate = useNavigate();
|
|
152
154
|
const location = useLocation();
|
|
153
155
|
const [currentList, setList] = useState(location.pathname || '/');
|
|
@@ -56,11 +56,13 @@ Data Loader 并非只为 SSR 工作。在 CSR 项目中,Data Loader 也可以
|
|
|
56
56
|
|
|
57
57
|
Modern.js 也提供了一个叫 `useLoaderData` 的 hooks API,我们修改 `src/routes/page.tsx` 导出的组件:
|
|
58
58
|
|
|
59
|
-
```tsx
|
|
59
|
+
```tsx
|
|
60
|
+
// [!code highlight:2]
|
|
60
61
|
import { useLoaderData } from '@modern-js/runtime/router';
|
|
61
62
|
import type { LoaderData } from './page.data';
|
|
62
63
|
|
|
63
64
|
function Index() {
|
|
65
|
+
// [!code highlight:1]
|
|
64
66
|
const { data } = useLoaderData() as LoaderData;
|
|
65
67
|
|
|
66
68
|
return (
|
|
@@ -68,6 +70,7 @@ function Index() {
|
|
|
68
70
|
<Helmet>
|
|
69
71
|
<title>All</title>
|
|
70
72
|
</Helmet>
|
|
73
|
+
// [!code highlight:1]
|
|
71
74
|
<List
|
|
72
75
|
dataSource={data}
|
|
73
76
|
renderItem={info => <Item key={info.name} info={info} />}
|
package/package.json
CHANGED
|
@@ -15,17 +15,19 @@
|
|
|
15
15
|
"modern",
|
|
16
16
|
"modern.js"
|
|
17
17
|
],
|
|
18
|
-
"version": "0.0.0-nightly-
|
|
18
|
+
"version": "0.0.0-nightly-20250518160324",
|
|
19
19
|
"publishConfig": {
|
|
20
20
|
"registry": "https://registry.npmjs.org/",
|
|
21
21
|
"access": "public"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"mermaid": "^11.4.1",
|
|
25
|
-
"@modern-js/sandpack-react": "0.0.0-nightly-
|
|
25
|
+
"@modern-js/sandpack-react": "0.0.0-nightly-20250518160324"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"@
|
|
28
|
+
"@rsbuild/plugin-sass": "1.3.1",
|
|
29
|
+
"@shikijs/transformers": "^3.4.1",
|
|
30
|
+
"@rspress/shared": "2.0.0-beta.5",
|
|
29
31
|
"@types/fs-extra": "9.0.13",
|
|
30
32
|
"@types/node": "^16",
|
|
31
33
|
"classnames": "^2.5.1",
|
|
@@ -33,7 +35,7 @@
|
|
|
33
35
|
"fs-extra": "^10",
|
|
34
36
|
"react": "^18.3.1",
|
|
35
37
|
"react-dom": "^18.3.1",
|
|
36
|
-
"rspress": "
|
|
38
|
+
"rspress": "2.0.0-beta.5",
|
|
37
39
|
"ts-node": "^10.9.1",
|
|
38
40
|
"typescript": "^5"
|
|
39
41
|
},
|
package/rspress.config.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
|
+
import { pluginSass } from '@rsbuild/plugin-sass';
|
|
3
|
+
import { transformerNotationHighlight } from '@shikijs/transformers';
|
|
2
4
|
import { defineConfig } from 'rspress/config';
|
|
3
5
|
|
|
4
6
|
const docPath = path.join(__dirname, 'docs');
|
|
@@ -12,7 +14,9 @@ export default defineConfig({
|
|
|
12
14
|
themeDir: path.join(__dirname, 'src'),
|
|
13
15
|
markdown: {
|
|
14
16
|
checkDeadLinks: true,
|
|
15
|
-
|
|
17
|
+
shiki: {
|
|
18
|
+
transformers: [transformerNotationHighlight()],
|
|
19
|
+
},
|
|
16
20
|
},
|
|
17
21
|
search: {
|
|
18
22
|
codeBlocks: true,
|
|
@@ -91,5 +95,6 @@ export default defineConfig({
|
|
|
91
95
|
'@site': require('path').resolve(__dirname),
|
|
92
96
|
},
|
|
93
97
|
},
|
|
98
|
+
plugins: [pluginSass()],
|
|
94
99
|
},
|
|
95
100
|
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { useLang } from 'rspress/runtime';
|
|
3
3
|
|
|
4
|
-
const
|
|
4
|
+
const RsbuildLink = ({ configName }: { configName: string }) => {
|
|
5
5
|
const lang = useLang();
|
|
6
6
|
const href = `https://rsbuild.dev/${lang === 'zh' ? 'zh/' : ''}config/${configName
|
|
7
7
|
.split('.')
|
|
@@ -16,4 +16,4 @@ const RsbuildLInk = ({ configName }: { configName: string }) => {
|
|
|
16
16
|
);
|
|
17
17
|
};
|
|
18
18
|
|
|
19
|
-
export default
|
|
19
|
+
export default RsbuildLink;
|
package/src/index.ts
CHANGED
package/src/pages/index.tsx
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import clsx from 'clsx';
|
|
2
2
|
import { useEffect } from 'react';
|
|
3
|
-
import {
|
|
3
|
+
import { Head, useLocation } from 'rspress/runtime';
|
|
4
4
|
import ContentCard from '../components/ContentCard';
|
|
5
5
|
import { FeatureLayout } from '../components/FeatureLayout';
|
|
6
6
|
import Footer from '../components/Footer';
|
|
@@ -94,7 +94,7 @@ export default function Home() {
|
|
|
94
94
|
|
|
95
95
|
return (
|
|
96
96
|
<div>
|
|
97
|
-
<
|
|
97
|
+
<Head>
|
|
98
98
|
<html className="dark" />
|
|
99
99
|
<script>window.MODERN_THEME = 'dark';</script>
|
|
100
100
|
<style type="text/css">{`
|
|
@@ -102,7 +102,7 @@ export default function Home() {
|
|
|
102
102
|
display: none!important;
|
|
103
103
|
}
|
|
104
104
|
`}</style>
|
|
105
|
-
</
|
|
105
|
+
</Head>
|
|
106
106
|
<HomepageHeader />
|
|
107
107
|
<main className={styles['homepage-main']}>
|
|
108
108
|
<FeatureLayout>
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: _app.[tj]s
|
|
3
|
-
sidebar_position: 2
|
|
4
|
-
---
|
|
5
|
-
# _app.[tj]s
|
|
6
|
-
|
|
7
|
-
This file can add pre-middleware to BFF functions. For detailed information, refer to [Extend BFF Server](/guides/advanced-features/bff/extend-server).
|
|
8
|
-
|
|
9
|
-
:::note
|
|
10
|
-
For specific examples, please refer to [hook](/apis/app/runtime/bff/hook).
|
|
11
|
-
:::
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: hook
|
|
3
|
-
---
|
|
4
|
-
# hook
|
|
5
|
-
|
|
6
|
-
Used to add framework middleware under BFF function mode, the middleware will execute before BFF function.
|
|
7
|
-
|
|
8
|
-
## Usage
|
|
9
|
-
|
|
10
|
-
according to the framework extend plugin, export from the corresponding namespace:
|
|
11
|
-
|
|
12
|
-
```ts
|
|
13
|
-
import { hook } from '@modern-js/runtime/{namespace}';
|
|
14
|
-
```
|
|
15
|
-
|
|
16
|
-
## Function Signature
|
|
17
|
-
|
|
18
|
-
```ts
|
|
19
|
-
type HookOptions = {
|
|
20
|
-
addMiddleware: string | function;
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
function hook(options: HookOptions): void;
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
### Input
|
|
27
|
-
|
|
28
|
-
- `options`: a range of hooks provided by Modern.js.
|
|
29
|
-
- `addMiddleware`: add middlewares for BFF.
|
|
30
|
-
|
|
31
|
-
## Example
|
|
32
|
-
|
|
33
|
-
middleware for different frameworks should be different(an example is when using the koa framework):
|
|
34
|
-
|
|
35
|
-
```ts title=api/_app.ts
|
|
36
|
-
import { hook } from '@modern-js/runtime/koa';
|
|
37
|
-
|
|
38
|
-
export default hook(({ addMiddleware }) => {
|
|
39
|
-
addMiddleware(async (ctx, next) => {
|
|
40
|
-
ctx.req.query.id = 'koa';
|
|
41
|
-
await next();
|
|
42
|
-
});
|
|
43
|
-
});
|
|
44
|
-
```
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: useContext
|
|
3
|
-
---
|
|
4
|
-
# useContext
|
|
5
|
-
|
|
6
|
-
Used to get the request context in the BFF function.
|
|
7
|
-
|
|
8
|
-
## Usage
|
|
9
|
-
|
|
10
|
-
according to the framework extend plugin, export from the corresponding namespace:
|
|
11
|
-
|
|
12
|
-
```ts
|
|
13
|
-
import { useContext } from '@modern-js/runtime/{namespace}';
|
|
14
|
-
```
|
|
15
|
-
|
|
16
|
-
## Function Signature
|
|
17
|
-
|
|
18
|
-
`function useContext(): any`
|
|
19
|
-
|
|
20
|
-
## Example
|
|
21
|
-
|
|
22
|
-
Developers can get more request information through `context`, such as browser UA(an example is when using the koa framework):
|
|
23
|
-
|
|
24
|
-
```ts
|
|
25
|
-
import { useContext } from '@modern-js/runtime/koa';
|
|
26
|
-
|
|
27
|
-
export async function get() {
|
|
28
|
-
const ctx = useContext();
|
|
29
|
-
return ctx.req.headers['user-agent'];
|
|
30
|
-
}
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
:::caution
|
|
34
|
-
only in BFF function, `useContext` API can be used.
|
|
35
|
-
|
|
36
|
-
:::
|
|
37
|
-
|
|
38
|
-
Although the `useContext` API is supported in any framework extend plugin, the types of return values are different.
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: hook
|
|
3
|
-
---
|
|
4
|
-
# hook
|
|
5
|
-
|
|
6
|
-
用于在 BFF 函数写法下添加框架中间件,添加的中间件的执行会在 BFF 函数定义的路由之前。
|
|
7
|
-
|
|
8
|
-
## 使用姿势
|
|
9
|
-
|
|
10
|
-
根据使用的框架拓展插件,从对应的命名空间中导出:
|
|
11
|
-
|
|
12
|
-
```ts
|
|
13
|
-
import { hook } from '@modern-js/runtime/{namespace}';
|
|
14
|
-
```
|
|
15
|
-
|
|
16
|
-
## 函数签名
|
|
17
|
-
|
|
18
|
-
```ts
|
|
19
|
-
type HookOptions = {
|
|
20
|
-
addMiddleware: string | function;
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
function hook(options: HookOptions): void;
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
### 参数
|
|
27
|
-
|
|
28
|
-
- `options`: Modern.js 提供的一系列钩子。
|
|
29
|
-
- `addMiddleware`: 添加 BFF 中间件的钩子。
|
|
30
|
-
|
|
31
|
-
## 示例
|
|
32
|
-
|
|
33
|
-
使用不同的框架,应添加不同框架的中间件(示例为使用 koa 框架时):
|
|
34
|
-
|
|
35
|
-
```ts title=api/_app.ts
|
|
36
|
-
import { hook } from '@modern-js/runtime/koa';
|
|
37
|
-
|
|
38
|
-
export default hook(({ addMiddleware }) => {
|
|
39
|
-
addMiddleware(async (ctx, next) => {
|
|
40
|
-
ctx.req.query.id = 'koa';
|
|
41
|
-
await next();
|
|
42
|
-
});
|
|
43
|
-
});
|
|
44
|
-
```
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: useContext
|
|
3
|
-
---
|
|
4
|
-
# useContext
|
|
5
|
-
|
|
6
|
-
用于在一体化 BFF 函数中获取请求上下文。
|
|
7
|
-
|
|
8
|
-
## 使用姿势
|
|
9
|
-
|
|
10
|
-
根据使用的框架拓展插件,从对应的命名空间中导出:
|
|
11
|
-
|
|
12
|
-
```ts
|
|
13
|
-
import { useContext } from '@modern-js/runtime/{namespace}';
|
|
14
|
-
```
|
|
15
|
-
|
|
16
|
-
## 函数签名
|
|
17
|
-
|
|
18
|
-
`function useContext(): any`
|
|
19
|
-
|
|
20
|
-
## 示例
|
|
21
|
-
|
|
22
|
-
开发者可以通过 `context` 获取更多的请求信息,例如获取请求 UA(示例为使用 koa 框架时):
|
|
23
|
-
|
|
24
|
-
```ts
|
|
25
|
-
import { useContext } from '@modern-js/runtime/koa';
|
|
26
|
-
|
|
27
|
-
export async function get() {
|
|
28
|
-
const ctx = useContext();
|
|
29
|
-
return ctx.req.headers['user-agent'];
|
|
30
|
-
}
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
:::caution 注意
|
|
34
|
-
只有在一体化 BFF 函数中,你才可以使用 `useContext` API 。
|
|
35
|
-
|
|
36
|
-
:::
|
|
37
|
-
|
|
38
|
-
使用不同的运行时框架时,虽然均支持 `useContext` API,但它们的返回值的类型是不同的。
|