@modern-js/main-doc 2.0.1 → 2.0.3-alpha.0
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/.turbo/turbo-build.log +1 -1
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/model/Provider.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/components/debug-app.md +17 -0
- package/en/docusaurus-plugin-content-docs/current/components/enable-bff.md +6 -6
- package/en/docusaurus-plugin-content-docs/current/components/prerequisites.md +19 -0
- package/en/docusaurus-plugin-content-docs/current/configure/app/runtime/router.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/css/less-sass.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/css/tailwindcss.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/guides/get-started/quick-start.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c01-start.md +17 -17
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c02-component.md +11 -11
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c03-css.md +48 -49
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c04-routes.md +22 -22
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c05-loader.md +10 -10
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c06-model.md +28 -28
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c07-container.md +20 -21
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c08-entries.md +31 -32
- package/en/docusaurus-plugin-content-docs/current.json +3 -47
- package/package.json +4 -4
- package/zh/components/enable-bff.md +6 -6
- package/zh/configure/app/runtime/intro.md +27 -0
- package/zh/guides/basic-features/routes.md +84 -0
@@ -283,6 +283,90 @@ const ErrorBoundary = () => {
|
|
283
283
|
export default ErrorBoundary;
|
284
284
|
```
|
285
285
|
|
286
|
+
### 运行时配置
|
287
|
+
|
288
|
+
在每个根 `Layout` 组件中(`routes/layout.ts`),可以动态地定义应用运行时配置:
|
289
|
+
|
290
|
+
```ts title="src/routes/layout.tsx"
|
291
|
+
// 定义运行时配置
|
292
|
+
import type { AppConfig } from '@modern-js/runtime';
|
293
|
+
|
294
|
+
export const config = (): AppConfig => {
|
295
|
+
return {
|
296
|
+
router: {
|
297
|
+
supportHtml5History: false
|
298
|
+
}
|
299
|
+
}
|
300
|
+
};
|
301
|
+
```
|
302
|
+
|
303
|
+
### 渲染前钩子
|
304
|
+
|
305
|
+
在有些场景下,需要在应用渲染前做一些操作,可以在 `routes/layout.tsx` 中定义 `init` 钩子,`init` 在客户端和服务端均会执行,基本使用示例如下:
|
306
|
+
|
307
|
+
```ts title="src/routes/layout.tsx"
|
308
|
+
import type { RuntimeContext } from '@modern-js/runtime';
|
309
|
+
|
310
|
+
export const init = (context: RuntimeContext) => {
|
311
|
+
// do something
|
312
|
+
};
|
313
|
+
```
|
314
|
+
|
315
|
+
通过 `init` 钩子可以挂载一些全局的数据,在应用的其他地方可以访问 `runtimeContext` 变量:
|
316
|
+
|
317
|
+
:::note
|
318
|
+
该功能在应用需要页面前置的数据、自定义数据注入或是框架迁移(如 Next.js)时会非常有用。
|
319
|
+
:::
|
320
|
+
|
321
|
+
```ts title="src/routes/layout.tsx"
|
322
|
+
import {
|
323
|
+
RuntimeContext,
|
324
|
+
} from '@modern-js/runtime';
|
325
|
+
|
326
|
+
export const init = (context: RuntimeContext) => {
|
327
|
+
return {
|
328
|
+
message: 'Hello World',
|
329
|
+
}
|
330
|
+
}
|
331
|
+
```
|
332
|
+
|
333
|
+
```tsx title="src/routes/page.tsx"
|
334
|
+
import { useRuntimeContext } from '@modern-js/runtime';
|
335
|
+
|
336
|
+
export default () => {
|
337
|
+
const { context } = useRuntimeContext();
|
338
|
+
const { message } = context.getInitData();
|
339
|
+
|
340
|
+
return <div>{message}</div>;
|
341
|
+
}
|
342
|
+
```
|
343
|
+
|
344
|
+
配合 SSR 功能时,浏览器端可以获取到 SSR 时 App.init 返回的数据,开发者可以自行判断是否要在浏览器端重新获取数据来覆盖 SSR 数据,例如:
|
345
|
+
|
346
|
+
```tsx title="src/routes/layout.tsx"
|
347
|
+
import {
|
348
|
+
RuntimeContext,
|
349
|
+
} from '@modern-js/runtime';
|
350
|
+
|
351
|
+
export const init = (context: RuntimeContext) => {
|
352
|
+
if (process.env.JUPITER_TARGET === 'node') {
|
353
|
+
return {
|
354
|
+
message: 'Hello World By Server',
|
355
|
+
}
|
356
|
+
} else {
|
357
|
+
const { context } = runtimeContext;
|
358
|
+
const data = context.getInitData();
|
359
|
+
// 如果没有获取到期望的数据
|
360
|
+
if (!data.message) {
|
361
|
+
return {
|
362
|
+
message: 'Hello World By Client'
|
363
|
+
}
|
364
|
+
}
|
365
|
+
}
|
366
|
+
}
|
367
|
+
```
|
368
|
+
|
369
|
+
|
286
370
|
## 自控式路由
|
287
371
|
|
288
372
|
以 `src/App.tsx` 为约定的入口,Modern.js 不会多路由做额外的操作,开发者可以自行使用 React Router 6 的 API 进行开发,例如:
|