@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.
Files changed (175) hide show
  1. package/docs/en/apis/app/hooks/server/server.mdx +10 -0
  2. package/docs/en/apis/app/hooks/src/routes.mdx +3 -3
  3. package/docs/en/apis/app/runtime/bff/use-hono-context.mdx +30 -0
  4. package/docs/en/components/enable-bff.mdx +1 -27
  5. package/docs/en/components/rsbuild-config-tooltip.mdx +2 -2
  6. package/docs/en/components/tech-stack-node-framework.mdx +1 -1
  7. package/docs/en/configure/app/dev/asset-prefix.mdx +2 -3
  8. package/docs/en/configure/app/dev/client.mdx +2 -3
  9. package/docs/en/configure/app/dev/hmr.mdx +2 -3
  10. package/docs/en/configure/app/dev/live-reload.mdx +2 -3
  11. package/docs/en/configure/app/dev/progress-bar.mdx +2 -3
  12. package/docs/en/configure/app/dev/setup-middlewares.mdx +2 -3
  13. package/docs/en/configure/app/dev/watch-files.mdx +2 -3
  14. package/docs/en/configure/app/dev/write-to-disk.mdx +2 -3
  15. package/docs/en/configure/app/html/app-icon.mdx +2 -3
  16. package/docs/en/configure/app/html/crossorigin.mdx +2 -3
  17. package/docs/en/configure/app/html/favicon.mdx +2 -3
  18. package/docs/en/configure/app/html/inject.mdx +2 -3
  19. package/docs/en/configure/app/html/meta.mdx +2 -3
  20. package/docs/en/configure/app/html/mount-id.mdx +2 -3
  21. package/docs/en/configure/app/html/output-structure.mdx +2 -3
  22. package/docs/en/configure/app/html/script-loading.mdx +2 -3
  23. package/docs/en/configure/app/html/tags.mdx +2 -3
  24. package/docs/en/configure/app/html/template-parameters.mdx +2 -3
  25. package/docs/en/configure/app/html/template.mdx +2 -3
  26. package/docs/en/configure/app/html/title.mdx +2 -3
  27. package/docs/en/configure/app/output/asset-prefix.mdx +2 -3
  28. package/docs/en/configure/app/output/charset.mdx +2 -3
  29. package/docs/en/configure/app/output/copy.mdx +2 -3
  30. package/docs/en/configure/app/output/css-modules.mdx +2 -3
  31. package/docs/en/configure/app/output/data-uri-limit.mdx +2 -3
  32. package/docs/en/configure/app/output/dist-path.mdx +2 -3
  33. package/docs/en/configure/app/output/externals.mdx +2 -3
  34. package/docs/en/configure/app/output/filename-hash.mdx +2 -3
  35. package/docs/en/configure/app/output/filename.mdx +2 -3
  36. package/docs/en/configure/app/output/inject-styles.mdx +2 -3
  37. package/docs/en/configure/app/output/inline-scripts.mdx +2 -3
  38. package/docs/en/configure/app/output/inline-styles.mdx +2 -3
  39. package/docs/en/configure/app/output/legal-comments.mdx +2 -3
  40. package/docs/en/configure/app/output/minify.mdx +2 -3
  41. package/docs/en/configure/app/output/override-browserslist.mdx +2 -3
  42. package/docs/en/configure/app/output/polyfill.mdx +2 -3
  43. package/docs/en/configure/app/output/source-map.mdx +2 -3
  44. package/docs/en/configure/app/performance/build-cache.mdx +2 -3
  45. package/docs/en/configure/app/performance/bundle-analyze.mdx +2 -3
  46. package/docs/en/configure/app/performance/chunk-split.mdx +2 -3
  47. package/docs/en/configure/app/performance/dns-prefetch.mdx +2 -3
  48. package/docs/en/configure/app/performance/preconnect.mdx +2 -3
  49. package/docs/en/configure/app/performance/prefetch.mdx +2 -3
  50. package/docs/en/configure/app/performance/preload.mdx +2 -3
  51. package/docs/en/configure/app/performance/print-file-size.mdx +2 -3
  52. package/docs/en/configure/app/performance/profile.mdx +2 -3
  53. package/docs/en/configure/app/performance/remove-console.mdx +2 -3
  54. package/docs/en/configure/app/performance/remove-moment-locale.mdx +2 -3
  55. package/docs/en/configure/app/plugins.mdx +13 -33
  56. package/docs/en/configure/app/runtime/master-app.mdx +1 -5
  57. package/docs/en/configure/app/runtime/plugins.mdx +58 -0
  58. package/docs/en/configure/app/security/nonce.mdx +2 -3
  59. package/docs/en/configure/app/security/sri.mdx +0 -1
  60. package/docs/en/configure/app/server/port.mdx +2 -3
  61. package/docs/en/configure/app/source/alias-strategy.mdx +2 -3
  62. package/docs/en/configure/app/source/alias.mdx +2 -3
  63. package/docs/en/configure/app/source/decorators.mdx +2 -3
  64. package/docs/en/configure/app/source/define.mdx +2 -3
  65. package/docs/en/configure/app/source/exclude.mdx +2 -3
  66. package/docs/en/configure/app/source/include.mdx +2 -3
  67. package/docs/en/configure/app/source/pre-entry.mdx +2 -3
  68. package/docs/en/configure/app/source/transform-import.mdx +2 -3
  69. package/docs/en/configure/app/tools/css-extract.mdx +2 -3
  70. package/docs/en/configure/app/tools/css-loader.mdx +2 -2
  71. package/docs/en/configure/app/tools/html-plugin.mdx +2 -3
  72. package/docs/en/configure/app/tools/lightningcss-loader.mdx +2 -3
  73. package/docs/en/configure/app/tools/postcss.mdx +2 -3
  74. package/docs/en/configure/app/tools/rspack.mdx +2 -3
  75. package/docs/en/configure/app/tools/style-loader.mdx +2 -3
  76. package/docs/en/guides/advanced-features/bff/extend-server.mdx +33 -82
  77. package/docs/en/guides/advanced-features/bff/frameworks.mdx +12 -68
  78. package/docs/en/guides/advanced-features/custom-server.mdx +13 -9
  79. package/docs/en/plugin/cli-plugins/api.mdx +6 -0
  80. package/docs/en/plugin/runtime-plugins/api.mdx +6 -0
  81. package/docs/en/tutorials/first-app/c04-routes.mdx +4 -2
  82. package/docs/en/tutorials/first-app/c05-loader.mdx +4 -1
  83. package/docs/zh/apis/app/hooks/server/server.mdx +10 -0
  84. package/docs/zh/apis/app/hooks/src/routes.mdx +3 -3
  85. package/docs/zh/apis/app/runtime/bff/use-hono-context.mdx +31 -0
  86. package/docs/zh/components/enable-bff.mdx +2 -27
  87. package/docs/zh/components/rsbuild-config-tooltip.mdx +2 -2
  88. package/docs/zh/components/tech-stack-node-framework.mdx +1 -1
  89. package/docs/zh/configure/app/dev/asset-prefix.mdx +2 -3
  90. package/docs/zh/configure/app/dev/client.mdx +2 -3
  91. package/docs/zh/configure/app/dev/hmr.mdx +2 -3
  92. package/docs/zh/configure/app/dev/live-reload.mdx +2 -3
  93. package/docs/zh/configure/app/dev/progress-bar.mdx +2 -3
  94. package/docs/zh/configure/app/dev/setup-middlewares.mdx +2 -3
  95. package/docs/zh/configure/app/dev/watch-files.mdx +2 -3
  96. package/docs/zh/configure/app/dev/write-to-disk.mdx +2 -3
  97. package/docs/zh/configure/app/html/app-icon.mdx +2 -3
  98. package/docs/zh/configure/app/html/crossorigin.mdx +2 -3
  99. package/docs/zh/configure/app/html/favicon.mdx +2 -3
  100. package/docs/zh/configure/app/html/inject.mdx +2 -3
  101. package/docs/zh/configure/app/html/meta.mdx +2 -3
  102. package/docs/zh/configure/app/html/mount-id.mdx +2 -3
  103. package/docs/zh/configure/app/html/output-structure.mdx +2 -3
  104. package/docs/zh/configure/app/html/script-loading.mdx +2 -3
  105. package/docs/zh/configure/app/html/tags.mdx +2 -3
  106. package/docs/zh/configure/app/html/template-parameters.mdx +2 -3
  107. package/docs/zh/configure/app/html/template.mdx +2 -3
  108. package/docs/zh/configure/app/html/title.mdx +2 -3
  109. package/docs/zh/configure/app/output/asset-prefix.mdx +2 -3
  110. package/docs/zh/configure/app/output/charset.mdx +2 -3
  111. package/docs/zh/configure/app/output/copy.mdx +2 -3
  112. package/docs/zh/configure/app/output/css-modules.mdx +2 -3
  113. package/docs/zh/configure/app/output/data-uri-limit.mdx +2 -3
  114. package/docs/zh/configure/app/output/dist-path.mdx +2 -3
  115. package/docs/zh/configure/app/output/externals.mdx +2 -3
  116. package/docs/zh/configure/app/output/filename-hash.mdx +2 -3
  117. package/docs/zh/configure/app/output/filename.mdx +2 -3
  118. package/docs/zh/configure/app/output/inject-styles.mdx +2 -3
  119. package/docs/zh/configure/app/output/inline-scripts.mdx +2 -3
  120. package/docs/zh/configure/app/output/inline-styles.mdx +2 -3
  121. package/docs/zh/configure/app/output/legal-comments.mdx +2 -3
  122. package/docs/zh/configure/app/output/minify.mdx +2 -3
  123. package/docs/zh/configure/app/output/override-browserslist.mdx +2 -3
  124. package/docs/zh/configure/app/output/polyfill.mdx +2 -3
  125. package/docs/zh/configure/app/output/source-map.mdx +2 -3
  126. package/docs/zh/configure/app/performance/build-cache.mdx +2 -3
  127. package/docs/zh/configure/app/performance/bundle-analyze.mdx +2 -3
  128. package/docs/zh/configure/app/performance/chunk-split.mdx +2 -3
  129. package/docs/zh/configure/app/performance/dns-prefetch.mdx +2 -3
  130. package/docs/zh/configure/app/performance/preconnect.mdx +2 -3
  131. package/docs/zh/configure/app/performance/prefetch.mdx +2 -3
  132. package/docs/zh/configure/app/performance/preload.mdx +2 -3
  133. package/docs/zh/configure/app/performance/print-file-size.mdx +2 -3
  134. package/docs/zh/configure/app/performance/profile.mdx +2 -3
  135. package/docs/zh/configure/app/performance/remove-console.mdx +2 -3
  136. package/docs/zh/configure/app/performance/remove-moment-locale.mdx +2 -3
  137. package/docs/zh/configure/app/plugins.mdx +3 -24
  138. package/docs/zh/configure/app/runtime/master-app.mdx +1 -5
  139. package/docs/zh/configure/app/runtime/plugins.mdx +58 -0
  140. package/docs/zh/configure/app/security/nonce.mdx +2 -3
  141. package/docs/zh/configure/app/security/sri.mdx +0 -1
  142. package/docs/zh/configure/app/server/port.mdx +2 -3
  143. package/docs/zh/configure/app/source/alias-strategy.mdx +2 -3
  144. package/docs/zh/configure/app/source/alias.mdx +2 -3
  145. package/docs/zh/configure/app/source/decorators.mdx +2 -3
  146. package/docs/zh/configure/app/source/define.mdx +2 -3
  147. package/docs/zh/configure/app/source/exclude.mdx +2 -3
  148. package/docs/zh/configure/app/source/include.mdx +2 -3
  149. package/docs/zh/configure/app/source/pre-entry.mdx +2 -3
  150. package/docs/zh/configure/app/source/transform-import.mdx +2 -3
  151. package/docs/zh/configure/app/tools/css-extract.mdx +2 -3
  152. package/docs/zh/configure/app/tools/css-loader.mdx +2 -3
  153. package/docs/zh/configure/app/tools/html-plugin.mdx +2 -3
  154. package/docs/zh/configure/app/tools/lightningcss-loader.mdx +2 -3
  155. package/docs/zh/configure/app/tools/postcss.mdx +2 -3
  156. package/docs/zh/configure/app/tools/rspack.mdx +2 -3
  157. package/docs/zh/configure/app/tools/style-loader.mdx +2 -3
  158. package/docs/zh/guides/advanced-features/bff/extend-server.mdx +28 -76
  159. package/docs/zh/guides/advanced-features/bff/frameworks.mdx +6 -66
  160. package/docs/zh/guides/advanced-features/custom-server.mdx +12 -8
  161. package/docs/zh/plugin/cli-plugins/api.mdx +6 -0
  162. package/docs/zh/plugin/runtime-plugins/api.mdx +6 -0
  163. package/docs/zh/tutorials/first-app/c04-routes.mdx +4 -2
  164. package/docs/zh/tutorials/first-app/c05-loader.mdx +4 -1
  165. package/package.json +6 -4
  166. package/rspress.config.ts +6 -1
  167. package/src/components/RsbuildLink/index.tsx +2 -2
  168. package/src/index.ts +1 -5
  169. package/src/pages/index.tsx +3 -3
  170. package/docs/en/apis/app/hooks/api/middleware.mdx +0 -11
  171. package/docs/en/apis/app/runtime/bff/hook.mdx +0 -44
  172. package/docs/en/apis/app/runtime/bff/use-context.mdx +0 -38
  173. package/docs/zh/apis/app/hooks/api/middleware.mdx +0 -11
  174. package/docs/zh/apis/app/runtime/bff/hook.mdx +0 -44
  175. 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
- 项目目录下创建 `server/modern.server.ts` 文件,可以在这个文件中添加如下配置来扩展 Server:
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 是不二选择。如果仅需要处理 BFF 接口路由,可以通过配置 `path` 为 BFF 的 `prefix` 来实现。
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 = getMonitors();
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 CLI 插件的 API。CLI 插件允许您在 Modern.js 项目的构建和开发过程中扩展和定制功能。
4
4
 
5
+ :::info
6
+
7
+ CLI 插件需要通过 `modern.config.ts` 中的 [`plugins`](/configure/app/plugins) 字段配置。
8
+
9
+ :::
10
+
5
11
  ## 插件基础结构
6
12
 
7
13
  一个典型的 CLI 插件结构如下:
@@ -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 {4-9}
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 {2-9}
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 {1,2,5,13}
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-20250514160342",
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-20250514160342"
25
+ "@modern-js/sandpack-react": "0.0.0-nightly-20250518160324"
26
26
  },
27
27
  "devDependencies": {
28
- "@rspress/shared": "1.43.11",
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": "1.43.11",
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
- experimentalMdxRs: true,
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 RsbuildLInk = ({ configName }: { configName: string }) => {
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 RsbuildLInk;
19
+ export default RsbuildLink;
package/src/index.ts CHANGED
@@ -1,9 +1,5 @@
1
- import Theme from 'rspress/theme';
2
1
  import HomeLayout from './pages';
3
2
 
4
- export default {
5
- ...Theme,
6
- HomeLayout,
7
- };
3
+ export { HomeLayout };
8
4
 
9
5
  export * from 'rspress/theme';
@@ -1,6 +1,6 @@
1
1
  import clsx from 'clsx';
2
2
  import { useEffect } from 'react';
3
- import { Helmet, useLocation } from 'rspress/runtime';
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
- <Helmet>
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
- </Helmet>
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,11 +0,0 @@
1
- ---
2
- title: _app.[tj]s
3
- sidebar_position: 2
4
- ---
5
- # _app.[tj]s
6
-
7
- 该文件可以为 BFF 函数添加前置中间件,详细内容参考 [扩展 BFF Server](/guides/advanced-features/bff/extend-server)。
8
-
9
- :::note
10
- 具体示例请参考 [hook](/apis/app/runtime/bff/hook)。
11
- :::
@@ -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,但它们的返回值的类型是不同的。