@modern-js/main-doc 2.67.5 → 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/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/guides/advanced-features/_meta.json +0 -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/page-performance/_meta.json +1 -1
- package/docs/en/guides/advanced-features/page-performance/react-compiler.mdx +44 -0
- package/docs/en/guides/advanced-features/web-server.mdx +211 -20
- package/docs/en/guides/basic-features/deploy.mdx +3 -3
- package/docs/en/guides/basic-features/output-files.mdx +0 -28
- 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 -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/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/guides/advanced-features/_meta.json +0 -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 +209 -25
- package/docs/zh/guides/basic-features/deploy.mdx +4 -3
- package/docs/zh/guides/basic-features/output-files.mdx +0 -28
- 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/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/en/guides/advanced-features/custom-server.mdx +0 -218
- 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/docs/zh/guides/advanced-features/custom-server.mdx +0 -216
package/package.json
CHANGED
@@ -15,17 +15,20 @@
|
|
15
15
|
"modern",
|
16
16
|
"modern.js"
|
17
17
|
],
|
18
|
-
"version": "2.67.
|
18
|
+
"version": "2.67.6",
|
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": "2.67.
|
25
|
+
"@modern-js/sandpack-react": "2.67.6"
|
26
26
|
},
|
27
27
|
"devDependencies": {
|
28
|
-
"@
|
28
|
+
"@rsbuild/plugin-sass": "1.3.1",
|
29
|
+
"@shikijs/transformers": "^3.4.2",
|
30
|
+
"@rspress/shared": "2.0.0-beta.6",
|
31
|
+
"@rspress/plugin-llms": "2.0.0-beta.6",
|
29
32
|
"@types/fs-extra": "9.0.13",
|
30
33
|
"@types/node": "^16",
|
31
34
|
"classnames": "^2.5.1",
|
@@ -33,7 +36,7 @@
|
|
33
36
|
"fs-extra": "^10",
|
34
37
|
"react": "^18.3.1",
|
35
38
|
"react-dom": "^18.3.1",
|
36
|
-
"rspress": "
|
39
|
+
"rspress": "2.0.0-beta.6",
|
37
40
|
"ts-node": "^10.9.1",
|
38
41
|
"typescript": "^5"
|
39
42
|
},
|
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/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,218 +0,0 @@
|
|
1
|
-
---
|
2
|
-
sidebar_position: 16
|
3
|
-
---
|
4
|
-
|
5
|
-
# Custom Server
|
6
|
-
|
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
|
-
|
9
|
-
## Custom Server Capabilities
|
10
|
-
|
11
|
-
Create the `server/modern.server.ts` file in the project directory, and you can add the following configurations to extend the Server:
|
12
|
-
- **Middleware**
|
13
|
-
- **Render Middleware**
|
14
|
-
- **Server Plugin**
|
15
|
-
|
16
|
-
In the **Plugin**, you can define **Middleware** and **RenderMiddleware**. The middleware loading process is illustrated in the following diagram:
|
17
|
-
|
18
|
-
<img
|
19
|
-
src="https://lf3-static.bytednsdoc.com/obj/eden-cn/10eh7nuhpenuhog/server-md-wf.png"
|
20
|
-
style={{ width: '100%', maxWidth: '540px' }}
|
21
|
-
/>
|
22
|
-
|
23
|
-
### Basic Configuration
|
24
|
-
|
25
|
-
```ts title="server/modern.server.ts"
|
26
|
-
import { defineServerConfig } from '@modern-js/server-runtime';
|
27
|
-
|
28
|
-
export default defineServerConfig({
|
29
|
-
middlewares: [],
|
30
|
-
renderMiddlewares: [],
|
31
|
-
plugins: [],
|
32
|
-
});
|
33
|
-
```
|
34
|
-
|
35
|
-
|
36
|
-
### Type Definition
|
37
|
-
|
38
|
-
`defineServerConfig` type definition is as follows:
|
39
|
-
|
40
|
-
```ts
|
41
|
-
import type { MiddlewareHandler } from 'hono';
|
42
|
-
|
43
|
-
type MiddlewareOrder = 'pre' | 'post' | 'default';
|
44
|
-
type MiddlewareObj = {
|
45
|
-
name: string;
|
46
|
-
path?: string;
|
47
|
-
method?: 'options' | 'get' | 'post' | 'put' | 'delete' | 'patch' | 'all';
|
48
|
-
handler: MiddlewareHandler | MiddlewareHandler[];
|
49
|
-
before?: Array<MiddlewareObj['name']>;
|
50
|
-
order?: MiddlewareOrder;
|
51
|
-
};
|
52
|
-
type ServerConfig = {
|
53
|
-
middlewares?: MiddlewareObj[];
|
54
|
-
renderMiddlewares?: MiddlewareObj[];
|
55
|
-
plugins?: (ServerPlugin | ServerPluginLegacy)[];
|
56
|
-
}
|
57
|
-
```
|
58
|
-
|
59
|
-
|
60
|
-
### Middleware
|
61
|
-
|
62
|
-
Middleware supports executing custom logic before and after the **request handling** and **page routing** processes in Modern.js services.
|
63
|
-
That is, if custom logic needs to handle both API routes and affect page routes, then Middleware is the obvious choice. If you only need to handle BFF API routes, this can be achieved by configuring the `path` to the BFF's `prefix`.
|
64
|
-
|
65
|
-
:::note
|
66
|
-
In the BFF scenario, BFF routing will only go through Middleware when the [runtime framework](/guides/advanced-features/bff/frameworks.html) is Hono.
|
67
|
-
:::
|
68
|
-
|
69
|
-
#### Using Posture
|
70
|
-
|
71
|
-
```ts title="server/modern.server.ts"
|
72
|
-
import { defineServerConfig, type MiddlewareHandler } from '@modern-js/server-runtime';
|
73
|
-
import { getMonitors } from '@modern-js/runtime';
|
74
|
-
|
75
|
-
export const handler: MiddlewareHandler = async (c, next) => {
|
76
|
-
const monitors = getMonitors();
|
77
|
-
const start = Date.now();
|
78
|
-
|
79
|
-
await next();
|
80
|
-
|
81
|
-
const end = Date.now();
|
82
|
-
// Report Duration
|
83
|
-
monitors.timing('request_timing', end - start);
|
84
|
-
};
|
85
|
-
|
86
|
-
export default defineServerConfig({
|
87
|
-
middlewares: [
|
88
|
-
{
|
89
|
-
name: 'request-timing',
|
90
|
-
handler,
|
91
|
-
},
|
92
|
-
],
|
93
|
-
});
|
94
|
-
```
|
95
|
-
|
96
|
-
:::warning
|
97
|
-
You must execute the `next` function to proceed with the subsequent Middleware.
|
98
|
-
:::
|
99
|
-
|
100
|
-
|
101
|
-
### RenderMiddleware
|
102
|
-
|
103
|
-
If you only need to handle the logic before and after page rendering, modern.js also provides rendering middleware.
|
104
|
-
|
105
|
-
#### Using Posture
|
106
|
-
|
107
|
-
```ts title="server/modern.server.ts"
|
108
|
-
import { defineServerConfig, type MiddlewareHandler } from '@modern-js/server-runtime';
|
109
|
-
|
110
|
-
// Inject render performance metrics
|
111
|
-
const renderTiming: MiddlewareHandler = async (c, next) => {
|
112
|
-
const start = Date.now();
|
113
|
-
|
114
|
-
await next();
|
115
|
-
|
116
|
-
const end = Date.now();
|
117
|
-
c.res.headers.set('server-timing', `render; dur=${end - start}`);
|
118
|
-
};
|
119
|
-
|
120
|
-
// Modify the Response Body
|
121
|
-
const modifyResBody: MiddlewareHandler = async (c, next) => {
|
122
|
-
await next();
|
123
|
-
|
124
|
-
const { res } = c;
|
125
|
-
const text = await res.text();
|
126
|
-
const newText = text.replace('<body>', '<body> <h3>bytedance</h3>');
|
127
|
-
|
128
|
-
c.res = c.body(newText, {
|
129
|
-
status: res.status,
|
130
|
-
headers: res.headers,
|
131
|
-
});
|
132
|
-
};
|
133
|
-
|
134
|
-
export default defineServerConfig({
|
135
|
-
renderMiddlewares: [
|
136
|
-
{
|
137
|
-
name: 'render-timing',
|
138
|
-
handler: renderTiming,
|
139
|
-
},
|
140
|
-
{
|
141
|
-
name: 'modify-res-body',
|
142
|
-
handler: modifyResBody,
|
143
|
-
},
|
144
|
-
],
|
145
|
-
});
|
146
|
-
```
|
147
|
-
|
148
|
-
|
149
|
-
### Plugin
|
150
|
-
|
151
|
-
Modern.js supports adding the aforementioned middleware and rendering middleware for the Server in custom plugins.
|
152
|
-
|
153
|
-
#### Using Posture
|
154
|
-
|
155
|
-
|
156
|
-
```ts title="server/plugins/server.ts"
|
157
|
-
import type { ServerPluginLegacy } from '@modern-js/server-runtime';
|
158
|
-
|
159
|
-
export default (): ServerPluginLegacy => ({
|
160
|
-
name: 'serverPlugin',
|
161
|
-
setup(api) {
|
162
|
-
return {
|
163
|
-
prepare(serverConfig) {
|
164
|
-
const { middlewares, renderMiddlewares } = api.useAppContext();
|
165
|
-
|
166
|
-
// Inject server-side data for page dataLoader consumption
|
167
|
-
middlewares?.push({
|
168
|
-
name: 'server-plugin-middleware',
|
169
|
-
handler: async (c, next) => {
|
170
|
-
c.set('message', 'hi modern.js');
|
171
|
-
await next();
|
172
|
-
// ...
|
173
|
-
},
|
174
|
-
});
|
175
|
-
|
176
|
-
// redirect
|
177
|
-
renderMiddlewares?.push({
|
178
|
-
name: 'server-plugin-render-middleware',
|
179
|
-
handler: async (c, next) => {
|
180
|
-
const user = getUser(c.req);
|
181
|
-
if (!user) {
|
182
|
-
return c.redirect('/login');
|
183
|
-
}
|
184
|
-
|
185
|
-
await next();
|
186
|
-
},
|
187
|
-
});
|
188
|
-
return serverConfig;
|
189
|
-
},
|
190
|
-
};
|
191
|
-
},
|
192
|
-
});
|
193
|
-
```
|
194
|
-
|
195
|
-
|
196
|
-
```ts title="server/modern.server.ts"
|
197
|
-
import { defineServerConfig } from '@modern-js/server-runtime';
|
198
|
-
import serverPlugin from './plugins/serverPlugin';
|
199
|
-
|
200
|
-
export default defineServerConfig({
|
201
|
-
plugins: [serverPlugin()],
|
202
|
-
});
|
203
|
-
```
|
204
|
-
|
205
|
-
|
206
|
-
```ts title="src/routes/page.data.ts"
|
207
|
-
import { useHonoContext } from '@modern-js/server-runtime';
|
208
|
-
import { defer } from '@modern-js/runtime/router';
|
209
|
-
|
210
|
-
export default () => {
|
211
|
-
const ctx = useHonoContext();
|
212
|
-
// Consuming Data Injected by the Server-Side
|
213
|
-
const message = ctx.get('message');
|
214
|
-
|
215
|
-
// ...
|
216
|
-
};
|
217
|
-
|
218
|
-
```
|
@@ -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
|
-
```
|