@modern-js/main-doc 2.67.4 → 2.67.5

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 (29) hide show
  1. package/docs/en/configure/app/plugins.mdx +13 -33
  2. package/docs/en/configure/app/runtime/master-app.mdx +1 -5
  3. package/docs/en/configure/app/runtime/plugins.mdx +58 -0
  4. package/docs/en/configure/app/usage.mdx +1 -1
  5. package/docs/en/guides/advanced-features/_meta.json +1 -0
  6. package/docs/en/guides/advanced-features/bff.mdx +1 -1
  7. package/docs/en/guides/advanced-features/compatibility.mdx +1 -1
  8. package/docs/en/guides/advanced-features/custom-server.mdx +218 -0
  9. package/docs/en/guides/advanced-features/page-performance/inline-assets.mdx +2 -0
  10. package/docs/en/guides/advanced-features/page-performance/optimize-bundle.mdx +1 -1
  11. package/docs/en/guides/advanced-features/web-server.mdx +174 -1
  12. package/docs/en/guides/basic-features/data/data-fetch.mdx +2 -1
  13. package/docs/en/guides/basic-features/html.mdx +3 -3
  14. package/docs/en/guides/basic-features/render/ssr.mdx +2 -2
  15. package/docs/en/guides/concept/entries.mdx +1 -1
  16. package/docs/en/plugin/cli-plugins/api.mdx +6 -0
  17. package/docs/en/plugin/runtime-plugins/api.mdx +37 -12
  18. package/docs/en/tutorials/first-app/c05-loader.mdx +1 -1
  19. package/docs/zh/configure/app/plugins.mdx +3 -24
  20. package/docs/zh/configure/app/runtime/master-app.mdx +1 -5
  21. package/docs/zh/configure/app/runtime/plugins.mdx +58 -0
  22. package/docs/zh/configure/app/usage.mdx +1 -1
  23. package/docs/zh/guides/advanced-features/_meta.json +1 -0
  24. package/docs/zh/guides/advanced-features/custom-server.mdx +216 -0
  25. package/docs/zh/guides/advanced-features/web-server.mdx +174 -1
  26. package/docs/zh/plugin/cli-plugins/api.mdx +6 -0
  27. package/docs/zh/plugin/runtime-plugins/api.mdx +37 -12
  28. package/package.json +2 -2
  29. package/src/i18n/index.ts +1 -1
@@ -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 插件如下所示:
@@ -151,16 +157,35 @@ await hooks.onBeforeRender.call(myContext);
151
157
  您可以组合使用多个钩子来实现更复杂的功能。例如,您可以使用 `onBeforeRender` 获取数据,然后使用 `wrapRoot` 将数据通过 Context 传递给整个应用:
152
158
 
153
159
  ```ts
154
- api.onBeforeRender(async (context) => {
155
- const data = await fetchData(context.req);
156
- context.data = data;
157
- });
158
-
159
- api.wrapRoot((App) => {
160
- return (props) => (
161
- <DataContext.Provider value={props.data}>
162
- <App {...props} />
163
- </DataContext.Provider>
164
- );
165
- });
160
+ import { RuntimePluginFuture, RuntimeReactContext } from '@modern-js/runtime';
161
+ import { useContext, createContext } from 'react';
162
+
163
+ export const ThemeContext = createContext<{ theme: string } | null>(null);
164
+
165
+ export const themePlugin = (): RuntimePluginFuture => {
166
+ return {
167
+ name: 'theme-plugin',
168
+ setup: api => {
169
+ api.onBeforeRender(async context => {
170
+ const userPreference = await fetch('/api/user/theme-settings').then(
171
+ res => res.json(),
172
+ );
173
+ context.data = {
174
+ theme: userPreference.theme,
175
+ };
176
+ });
177
+
178
+ api.wrapRoot(App => {
179
+ return props => {
180
+ const context = useContext(RuntimeReactContext);
181
+ return (
182
+ <ThemeContext.Provider value={context.data}>
183
+ <App {...props} />
184
+ </ThemeContext.Provider>
185
+ );
186
+ };
187
+ });
188
+ },
189
+ };
190
+ };
166
191
  ```
package/package.json CHANGED
@@ -15,14 +15,14 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.67.4",
18
+ "version": "2.67.5",
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.4"
25
+ "@modern-js/sandpack-react": "2.67.5"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@rspress/shared": "1.43.11",
package/src/i18n/index.ts CHANGED
@@ -9,7 +9,7 @@ const translations = {
9
9
 
10
10
  export function useUrl(url: string) {
11
11
  const lang = useLang();
12
- return withBase(lang === 'zh' ? url : `/en${url}`);
12
+ return withBase(lang === 'zh' ? `/zh${url}` : url);
13
13
  }
14
14
 
15
15
  export function useI18n() {