@modern-js/main-doc 2.67.4 → 2.67.6
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 -4
- 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 +7 -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/configure/app/tools/swc.mdx +1 -1
- package/docs/en/configure/app/usage.mdx +1 -1
- 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/bff.mdx +1 -1
- package/docs/en/guides/advanced-features/compatibility.mdx +1 -1
- package/docs/en/guides/advanced-features/page-performance/_meta.json +1 -1
- package/docs/en/guides/advanced-features/page-performance/inline-assets.mdx +2 -0
- package/docs/en/guides/advanced-features/page-performance/optimize-bundle.mdx +1 -1
- package/docs/en/guides/advanced-features/page-performance/react-compiler.mdx +44 -0
- package/docs/en/guides/advanced-features/web-server.mdx +378 -14
- package/docs/en/guides/basic-features/data/data-fetch.mdx +2 -1
- package/docs/en/guides/basic-features/deploy.mdx +3 -3
- package/docs/en/guides/basic-features/html.mdx +3 -3
- package/docs/en/guides/basic-features/output-files.mdx +0 -28
- package/docs/en/guides/basic-features/render/ssr.mdx +2 -2
- package/docs/en/guides/concept/entries.mdx +1 -1
- package/docs/en/plugin/cli-plugins/api.mdx +6 -0
- package/docs/en/plugin/runtime-plugins/api.mdx +37 -12
- package/docs/en/tutorials/first-app/c04-routes.mdx +4 -2
- package/docs/en/tutorials/first-app/c05-loader.mdx +5 -2
- 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 -4
- 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 +6 -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/configure/app/tools/swc.mdx +1 -1
- package/docs/zh/configure/app/usage.mdx +1 -1
- 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/page-performance/_meta.json +1 -1
- package/docs/zh/guides/advanced-features/page-performance/react-compiler.mdx +44 -0
- package/docs/zh/guides/advanced-features/web-server.mdx +375 -18
- package/docs/zh/guides/basic-features/deploy.mdx +4 -3
- package/docs/zh/guides/basic-features/output-files.mdx +0 -28
- package/docs/zh/plugin/cli-plugins/api.mdx +6 -0
- package/docs/zh/plugin/runtime-plugins/api.mdx +37 -12
- 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 +7 -4
- package/rspress.config.ts +16 -1
- package/src/components/RsbuildLink/index.tsx +2 -2
- package/src/i18n/index.ts +1 -1
- 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/en/configure/app/bff/enable-handle-web.mdx +0 -24
- 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
- package/docs/zh/configure/app/bff/enable-handle-web.mdx +0 -24
package/rspress.config.ts
CHANGED
@@ -1,10 +1,16 @@
|
|
1
1
|
import path from 'path';
|
2
|
+
import { pluginSass } from '@rsbuild/plugin-sass';
|
3
|
+
import { pluginLlms } from '@rspress/plugin-llms';
|
4
|
+
import { transformerNotationHighlight } from '@shikijs/transformers';
|
2
5
|
import { defineConfig } from 'rspress/config';
|
3
6
|
|
4
7
|
const docPath = path.join(__dirname, 'docs');
|
5
8
|
|
6
9
|
export default defineConfig({
|
7
10
|
root: docPath,
|
11
|
+
title: 'Modern.js',
|
12
|
+
description:
|
13
|
+
'The Modern.js framework is a progressive web framework based on React. At ByteDance, we use Modern.js to build upper-level frameworks that have supported the development of thousands of web applications.',
|
8
14
|
base: '/',
|
9
15
|
logo: 'https://lf-cdn-tos.bytescm.com/obj/static/webinfra/modern-js-website/assets/images/images/modernjs-logo.svg',
|
10
16
|
icon: 'https://lf3-static.bytednsdoc.com/obj/eden-cn/uhbfnupenuhf/favicon.ico',
|
@@ -12,8 +18,11 @@ export default defineConfig({
|
|
12
18
|
themeDir: path.join(__dirname, 'src'),
|
13
19
|
markdown: {
|
14
20
|
checkDeadLinks: true,
|
15
|
-
|
21
|
+
shiki: {
|
22
|
+
transformers: [transformerNotationHighlight()],
|
23
|
+
},
|
16
24
|
},
|
25
|
+
plugins: [pluginLlms()],
|
17
26
|
search: {
|
18
27
|
codeBlocks: true,
|
19
28
|
},
|
@@ -58,6 +67,11 @@ export default defineConfig({
|
|
58
67
|
text: 'Edit this page on GitHub',
|
59
68
|
},
|
60
69
|
socialLinks: [
|
70
|
+
{
|
71
|
+
icon: 'discord',
|
72
|
+
mode: 'link',
|
73
|
+
content: 'https://discord.gg/qPCqYg38De',
|
74
|
+
},
|
61
75
|
{
|
62
76
|
icon: 'github',
|
63
77
|
mode: 'link',
|
@@ -91,5 +105,6 @@ export default defineConfig({
|
|
91
105
|
'@site': require('path').resolve(__dirname),
|
92
106
|
},
|
93
107
|
},
|
108
|
+
plugins: [pluginSass()],
|
94
109
|
},
|
95
110
|
});
|
@@ -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/i18n/index.ts
CHANGED
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,24 +0,0 @@
|
|
1
|
-
---
|
2
|
-
title: enableHandleWeb
|
3
|
-
---
|
4
|
-
|
5
|
-
# bff.enableHandleWeb
|
6
|
-
|
7
|
-
- **Type:** `boolean`
|
8
|
-
- **Default:** `false`
|
9
|
-
|
10
|
-
import EnableBFFCaution from "@site-docs-en/components/enable-bff-caution";
|
11
|
-
|
12
|
-
<EnableBFFCaution />
|
13
|
-
|
14
|
-
By default, the BFF service can only handle requests for BFF APIs.
|
15
|
-
|
16
|
-
When this value is set to `true`, page request will also pass through BFF, and the default logic for page rendering built in Modern.js will run as the last middleware of the BFF service.
|
17
|
-
|
18
|
-
```ts title="modern.config.ts"
|
19
|
-
export default defineConfig({
|
20
|
-
bff: {
|
21
|
-
enableHandleWeb: true,
|
22
|
-
},
|
23
|
-
});
|
24
|
-
```
|
@@ -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,但它们的返回值的类型是不同的。
|
@@ -1,24 +0,0 @@
|
|
1
|
-
---
|
2
|
-
title: enableHandleWeb
|
3
|
-
---
|
4
|
-
|
5
|
-
# bff.enableHandleWeb
|
6
|
-
|
7
|
-
- **类型:** `boolean`
|
8
|
-
- **默认值:** `false`
|
9
|
-
|
10
|
-
import EnableBFFCaution from "@site-docs/components/enable-bff-caution";
|
11
|
-
|
12
|
-
<EnableBFFCaution />
|
13
|
-
|
14
|
-
默认情况下,BFF 服务只能处理 BFF API 的请求。
|
15
|
-
|
16
|
-
当该值设置为 `true` 时,页面请求流量也会经过 BFF,并且 Modern.js 内置的页面渲染的逻辑默认会作为 BFF 服务的最后一个中间件运行。
|
17
|
-
|
18
|
-
```ts title="modern.config.ts"
|
19
|
-
export default defineConfig({
|
20
|
-
bff: {
|
21
|
-
enableHandleWeb: true,
|
22
|
-
},
|
23
|
-
});
|
24
|
-
```
|