@bleedingdev/modern-js-app-tools 3.5.0-ultramodern.98 → 3.8.1-ultramodern.1
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/bin/modern-bundle-docs.js +7 -0
- package/dist/cjs/builder/generator/createBuilderProviderConfig.js +5 -1
- package/dist/cjs/builder/generator/getBuilderEnvironments.js +80 -11
- package/dist/cjs/bundleDocs.js +114 -0
- package/dist/cjs/commands/build.js +2 -1
- package/dist/cjs/commands/deploy.js +4 -2
- package/dist/cjs/commands/index.js +0 -4
- package/dist/cjs/config/default.js +2 -0
- package/dist/cjs/index.js +56 -15
- package/dist/cjs/plugins/analyze/index.js +2 -2
- package/dist/cjs/plugins/analyze/utils.js +3 -2
- package/dist/cjs/plugins/deploy/platforms/cloudflare-output-verifier/index.js +1 -1
- package/dist/cjs/plugins/deploy/platforms/cloudflare-output-verifier/worker-bundles.js +94 -18
- package/dist/cjs/plugins/deploy/platforms/templates/cloudflare-entry.001-bootstrap-security.mjs +1 -0
- package/dist/cjs/plugins/deploy/platforms/templates/cloudflare-worker-mf-ssr-runtime-plugin.mjs +14 -0
- package/dist/cjs/plugins/initialize/index.js +4 -3
- package/dist/cjs/plugins/serverBuild.js +3 -2
- package/dist/cjs/ultramodern-release-envelope/framework-output.js +109 -57
- package/dist/esm/builder/generator/createBuilderProviderConfig.mjs +5 -1
- package/dist/esm/builder/generator/getBuilderEnvironments.mjs +70 -10
- package/dist/esm/bundleDocs.mjs +62 -0
- package/dist/esm/commands/build.mjs +2 -1
- package/dist/esm/commands/deploy.mjs +4 -2
- package/dist/esm/commands/index.mjs +0 -4
- package/dist/esm/config/default.mjs +2 -0
- package/dist/esm/index.mjs +15 -1
- package/dist/esm/plugins/analyze/index.mjs +2 -2
- package/dist/esm/plugins/analyze/utils.mjs +3 -2
- package/dist/esm/plugins/deploy/platforms/cloudflare-output-verifier/index.mjs +1 -1
- package/dist/esm/plugins/deploy/platforms/cloudflare-output-verifier/worker-bundles.mjs +93 -18
- package/dist/esm/plugins/deploy/platforms/templates/cloudflare-entry.001-bootstrap-security.mjs +1 -0
- package/dist/esm/plugins/deploy/platforms/templates/cloudflare-worker-mf-ssr-runtime-plugin.mjs +14 -0
- package/dist/esm/plugins/initialize/index.mjs +4 -3
- package/dist/esm/plugins/serverBuild.mjs +3 -2
- package/dist/esm/ultramodern-release-envelope/framework-output.mjs +107 -58
- package/dist/esm-node/builder/generator/createBuilderProviderConfig.mjs +5 -1
- package/dist/esm-node/builder/generator/getBuilderEnvironments.mjs +70 -10
- package/dist/esm-node/bundleDocs.mjs +63 -0
- package/dist/esm-node/commands/build.mjs +2 -1
- package/dist/esm-node/commands/deploy.mjs +4 -2
- package/dist/esm-node/commands/index.mjs +0 -4
- package/dist/esm-node/config/default.mjs +2 -0
- package/dist/esm-node/index.mjs +15 -1
- package/dist/esm-node/plugins/analyze/index.mjs +2 -2
- package/dist/esm-node/plugins/analyze/utils.mjs +3 -2
- package/dist/esm-node/plugins/deploy/platforms/cloudflare-output-verifier/index.mjs +1 -1
- package/dist/esm-node/plugins/deploy/platforms/cloudflare-output-verifier/worker-bundles.mjs +93 -18
- package/dist/esm-node/plugins/deploy/platforms/templates/cloudflare-entry.001-bootstrap-security.mjs +1 -0
- package/dist/esm-node/plugins/deploy/platforms/templates/cloudflare-worker-mf-ssr-runtime-plugin.mjs +14 -0
- package/dist/esm-node/plugins/initialize/index.mjs +4 -3
- package/dist/esm-node/plugins/serverBuild.mjs +3 -2
- package/dist/esm-node/ultramodern-release-envelope/framework-output.mjs +107 -58
- package/dist/types/builder/generator/getBuilderEnvironments.d.ts +29 -0
- package/dist/types/bundleDocs.d.ts +20 -0
- package/dist/types/commands/deploy.d.ts +2 -1
- package/dist/types/index.d.ts +4 -1
- package/dist/types/plugins/analyze/utils.d.ts +1 -1
- package/dist/types/plugins/deploy/platforms/cloudflare-output-verifier/worker-bundles.d.ts +1 -1
- package/dist/types/plugins/deploy/platforms/templates/cloudflare-entry.001-bootstrap-security.d.mts +1 -1
- package/dist/types/plugins/deploy/platforms/templates/cloudflare-worker-mf-ssr-runtime-plugin.d.mts +13 -0
- package/dist/types/types/config/dev.d.ts +6 -0
- package/dist/types/ultramodern-release-envelope/framework-output.d.ts +1 -0
- package/docs/apis/app/commands.md +231 -0
- package/docs/apis/app/hooks/api/lambda.md +9 -0
- package/docs/apis/app/hooks/config/favicon.md +24 -0
- package/docs/apis/app/hooks/config/html.md +5 -0
- package/docs/apis/app/hooks/config/icon.md +24 -0
- package/docs/apis/app/hooks/config/mock.md +6 -0
- package/docs/apis/app/hooks/config/public.md +26 -0
- package/docs/apis/app/hooks/config/upload.md +50 -0
- package/docs/apis/app/hooks/modern-config.md +5 -0
- package/docs/apis/app/hooks/server/server.md +5 -0
- package/docs/apis/app/hooks/shared.md +3 -0
- package/docs/apis/app/hooks/src/app.md +30 -0
- package/docs/apis/app/hooks/src/entry.md +34 -0
- package/docs/apis/app/hooks/src/entry.server.md +51 -0
- package/docs/apis/app/hooks/src/modern.runtime.md +5 -0
- package/docs/apis/app/hooks/src/routes.md +86 -0
- package/docs/apis/app/hooks/src/server.md +3 -0
- package/docs/apis/app/runtime/bff/use-backend-context.md +27 -0
- package/docs/apis/app/runtime/core/create-root.md +19 -0
- package/docs/apis/app/runtime/core/render.md +39 -0
- package/docs/apis/app/runtime/core/runtime-context.md +156 -0
- package/docs/apis/app/runtime/router/router.md +283 -0
- package/docs/apis/app/runtime/ssr/no-ssr.md +35 -0
- package/docs/apis/app/runtime/ssr/renderStreaming.md +67 -0
- package/docs/apis/app/runtime/ssr/renderString.md +60 -0
- package/docs/apis/app/runtime/ssr/requestHandler.md +44 -0
- package/docs/apis/app/runtime/utility/css-in-js.md +40 -0
- package/docs/apis/app/runtime/utility/head.md +35 -0
- package/docs/apis/app/runtime/utility/loadable.md +82 -0
- package/docs/community/blog/2022-0708-updates.md +91 -0
- package/docs/community/blog/2022-0910-updates.md +76 -0
- package/docs/community/blog/overview.md +63 -0
- package/docs/community/blog/v2-release-note.md +238 -0
- package/docs/community/blog/v3-release-note.md +622 -0
- package/docs/community/contributing-guide.md +252 -0
- package/docs/community/releases.md +23 -0
- package/docs/community/showcase.md +24 -0
- package/docs/community/team.md +14 -0
- package/docs/configure/app/bff/cross-project.md +20 -0
- package/docs/configure/app/bff/effect.md +313 -0
- package/docs/configure/app/bff/prefix.md +29 -0
- package/docs/configure/app/bff/runtime-framework.md +26 -0
- package/docs/configure/app/builder-plugins.md +66 -0
- package/docs/configure/app/dev/asset-prefix.md +13 -0
- package/docs/configure/app/dev/before-start-url.md +17 -0
- package/docs/configure/app/dev/client.md +41 -0
- package/docs/configure/app/dev/hmr.md +10 -0
- package/docs/configure/app/dev/host.md +18 -0
- package/docs/configure/app/dev/https.md +77 -0
- package/docs/configure/app/dev/lazy-compilation.md +39 -0
- package/docs/configure/app/dev/live-reload.md +10 -0
- package/docs/configure/app/dev/mock-dir.md +31 -0
- package/docs/configure/app/dev/progress-bar.md +19 -0
- package/docs/configure/app/dev/server.md +124 -0
- package/docs/configure/app/dev/setup-middlewares.md +32 -0
- package/docs/configure/app/dev/start-url.md +48 -0
- package/docs/configure/app/dev/watch-files.md +27 -0
- package/docs/configure/app/dev/write-to-disk.md +10 -0
- package/docs/configure/app/experiments/source-build.md +31 -0
- package/docs/configure/app/html/app-icon.md +28 -0
- package/docs/configure/app/html/crossorigin.md +10 -0
- package/docs/configure/app/html/favicon.md +16 -0
- package/docs/configure/app/html/inject.md +10 -0
- package/docs/configure/app/html/meta.md +24 -0
- package/docs/configure/app/html/mount-id.md +10 -0
- package/docs/configure/app/html/output-structure.md +10 -0
- package/docs/configure/app/html/script-loading.md +10 -0
- package/docs/configure/app/html/tags.md +15 -0
- package/docs/configure/app/html/template-parameters.md +33 -0
- package/docs/configure/app/html/template.md +10 -0
- package/docs/configure/app/html/title.md +18 -0
- package/docs/configure/app/output/asset-prefix.md +11 -0
- package/docs/configure/app/output/assets-retry.md +77 -0
- package/docs/configure/app/output/charset.md +10 -0
- package/docs/configure/app/output/clean-dist-path.md +16 -0
- package/docs/configure/app/output/convert-to-rem.md +79 -0
- package/docs/configure/app/output/copy.md +10 -0
- package/docs/configure/app/output/css-modules.md +48 -0
- package/docs/configure/app/output/data-uri-limit.md +26 -0
- package/docs/configure/app/output/disable-css-module-extension.md +55 -0
- package/docs/configure/app/output/disable-inline-runtime-chunk.md +41 -0
- package/docs/configure/app/output/disable-svgr.md +18 -0
- package/docs/configure/app/output/disable-ts-checker.md +49 -0
- package/docs/configure/app/output/dist-path.md +43 -0
- package/docs/configure/app/output/enable-asset-manifest.md +36 -0
- package/docs/configure/app/output/enable-css-module-tsdeclaration.md +28 -0
- package/docs/configure/app/output/enable-inline-route-manifests.md +16 -0
- package/docs/configure/app/output/externals.md +20 -0
- package/docs/configure/app/output/filename-hash.md +10 -0
- package/docs/configure/app/output/filename.md +55 -0
- package/docs/configure/app/output/inject-styles.md +10 -0
- package/docs/configure/app/output/inline-scripts.md +29 -0
- package/docs/configure/app/output/inline-styles.md +29 -0
- package/docs/configure/app/output/legal-comments.md +18 -0
- package/docs/configure/app/output/minify.md +22 -0
- package/docs/configure/app/output/override-browserslist.md +22 -0
- package/docs/configure/app/output/polyfill.md +12 -0
- package/docs/configure/app/output/source-map.md +30 -0
- package/docs/configure/app/output/split-route-chunks.md +16 -0
- package/docs/configure/app/output/ssg.md +81 -0
- package/docs/configure/app/output/ssgByEntries.md +90 -0
- package/docs/configure/app/output/svg-default-export.md +30 -0
- package/docs/configure/app/output/temp-dir.md +20 -0
- package/docs/configure/app/performance/build-cache.md +44 -0
- package/docs/configure/app/performance/chunk-split.md +40 -0
- package/docs/configure/app/performance/dns-prefetch.md +15 -0
- package/docs/configure/app/performance/preconnect.md +16 -0
- package/docs/configure/app/performance/prefetch.md +21 -0
- package/docs/configure/app/performance/preload.md +23 -0
- package/docs/configure/app/performance/print-file-size.md +40 -0
- package/docs/configure/app/performance/profile.md +10 -0
- package/docs/configure/app/performance/remove-console.md +10 -0
- package/docs/configure/app/performance/remove-moment-locale.md +10 -0
- package/docs/configure/app/performance/rsdoctor.md +38 -0
- package/docs/configure/app/plugins.md +59 -0
- package/docs/configure/app/resolve/alias-strategy.md +10 -0
- package/docs/configure/app/resolve/alias.md +9 -0
- package/docs/configure/app/resolve/condition-names.md +13 -0
- package/docs/configure/app/resolve/dedupe.md +9 -0
- package/docs/configure/app/resolve/extensions.md +13 -0
- package/docs/configure/app/runtime/0-intro.md +58 -0
- package/docs/configure/app/runtime/plugins.md +58 -0
- package/docs/configure/app/runtime/router.md +35 -0
- package/docs/configure/app/security/check-syntax.md +69 -0
- package/docs/configure/app/security/nonce.md +15 -0
- package/docs/configure/app/security/sri.md +20 -0
- package/docs/configure/app/server/base-url.md +26 -0
- package/docs/configure/app/server/port.md +18 -0
- package/docs/configure/app/server/public-routes.md +22 -0
- package/docs/configure/app/server/routes.md +86 -0
- package/docs/configure/app/server/rsc.md +22 -0
- package/docs/configure/app/server/ssr-by-entries.md +25 -0
- package/docs/configure/app/server/ssr.md +80 -0
- package/docs/configure/app/server/tsconfig-path.md +59 -0
- package/docs/configure/app/source/alias-strategy.md +14 -0
- package/docs/configure/app/source/alias.md +23 -0
- package/docs/configure/app/source/config-dir.md +20 -0
- package/docs/configure/app/source/decorators.md +25 -0
- package/docs/configure/app/source/define.md +16 -0
- package/docs/configure/app/source/disable-default-entries.md +28 -0
- package/docs/configure/app/source/enable-async-entry.md +54 -0
- package/docs/configure/app/source/enable-async-pre-entry.md +26 -0
- package/docs/configure/app/source/entries-dir.md +35 -0
- package/docs/configure/app/source/entries.md +179 -0
- package/docs/configure/app/source/exclude.md +10 -0
- package/docs/configure/app/source/global-vars.md +106 -0
- package/docs/configure/app/source/include.md +36 -0
- package/docs/configure/app/source/main-entry-name.md +24 -0
- package/docs/configure/app/source/pre-entry.md +10 -0
- package/docs/configure/app/source/react-compiler.md +68 -0
- package/docs/configure/app/source/transform-import.md +27 -0
- package/docs/configure/app/split-chunks.md +17 -0
- package/docs/configure/app/tools/autoprefixer.md +44 -0
- package/docs/configure/app/tools/bundler-chain.md +26 -0
- package/docs/configure/app/tools/css-extract.md +33 -0
- package/docs/configure/app/tools/css-loader.md +17 -0
- package/docs/configure/app/tools/dev-server.md +113 -0
- package/docs/configure/app/tools/html-plugin.md +41 -0
- package/docs/configure/app/tools/less.md +81 -0
- package/docs/configure/app/tools/lightningcss-loader.md +35 -0
- package/docs/configure/app/tools/minify-css.md +53 -0
- package/docs/configure/app/tools/postcss.md +34 -0
- package/docs/configure/app/tools/rspack.md +10 -0
- package/docs/configure/app/tools/sass.md +78 -0
- package/docs/configure/app/tools/style-loader.md +10 -0
- package/docs/configure/app/tools/swc.md +65 -0
- package/docs/configure/app/tools/ts-checker.md +116 -0
- package/docs/configure/app/usage.md +276 -0
- package/docs/guides/advanced-features/bff/cross-project.md +109 -0
- package/docs/guides/advanced-features/bff/data-platform.md +116 -0
- package/docs/guides/advanced-features/bff/extend-server.md +120 -0
- package/docs/guides/advanced-features/bff/frameworks.md +243 -0
- package/docs/guides/advanced-features/bff/function.md +318 -0
- package/docs/guides/advanced-features/bff/operators.md +554 -0
- package/docs/guides/advanced-features/bff/sdk.md +116 -0
- package/docs/guides/advanced-features/bff/upload.md +101 -0
- package/docs/guides/advanced-features/bff.md +18 -0
- package/docs/guides/advanced-features/build-performance.md +130 -0
- package/docs/guides/advanced-features/compatibility.md +120 -0
- package/docs/guides/advanced-features/international/advanced.md +128 -0
- package/docs/guides/advanced-features/international/api.md +267 -0
- package/docs/guides/advanced-features/international/best-practices.md +286 -0
- package/docs/guides/advanced-features/international/configuration.md +228 -0
- package/docs/guides/advanced-features/international/locale-detection.md +126 -0
- package/docs/guides/advanced-features/international/quick-start.md +128 -0
- package/docs/guides/advanced-features/international/resource-loading.md +154 -0
- package/docs/guides/advanced-features/international/routing.md +173 -0
- package/docs/guides/advanced-features/international.md +27 -0
- package/docs/guides/advanced-features/low-level.md +46 -0
- package/docs/guides/advanced-features/page-performance/code-split.md +77 -0
- package/docs/guides/advanced-features/page-performance/inline-assets.md +159 -0
- package/docs/guides/advanced-features/page-performance/optimize-bundle.md +97 -0
- package/docs/guides/advanced-features/page-performance/react-compiler.md +51 -0
- package/docs/guides/advanced-features/server-monitor/logger.md +41 -0
- package/docs/guides/advanced-features/server-monitor/metrics.md +58 -0
- package/docs/guides/advanced-features/server-monitor/monitors.md +242 -0
- package/docs/guides/advanced-features/source-build.md +164 -0
- package/docs/guides/advanced-features/web-server.md +288 -0
- package/docs/guides/basic-features/alias.md +102 -0
- package/docs/guides/basic-features/css/css-in-js.md +72 -0
- package/docs/guides/basic-features/css/css-modules.md +212 -0
- package/docs/guides/basic-features/css/css.md +27 -0
- package/docs/guides/basic-features/css/tailwindcss.md +27 -0
- package/docs/guides/basic-features/data/data-cache.md +510 -0
- package/docs/guides/basic-features/data/data-fetch.md +415 -0
- package/docs/guides/basic-features/data/data-write.md +227 -0
- package/docs/guides/basic-features/debug/mock.md +109 -0
- package/docs/guides/basic-features/debug/proxy.md +21 -0
- package/docs/guides/basic-features/debug/rsdoctor.md +47 -0
- package/docs/guides/basic-features/debug/using-storybook.md +112 -0
- package/docs/guides/basic-features/deploy.md +458 -0
- package/docs/guides/basic-features/env-vars.md +177 -0
- package/docs/guides/basic-features/html.md +255 -0
- package/docs/guides/basic-features/output-files.md +141 -0
- package/docs/guides/basic-features/render/before-render.md +108 -0
- package/docs/guides/basic-features/render/overview.md +47 -0
- package/docs/guides/basic-features/render/rsc.md +516 -0
- package/docs/guides/basic-features/render/ssg.md +228 -0
- package/docs/guides/basic-features/render/ssr-cache.md +201 -0
- package/docs/guides/basic-features/render/ssr.md +321 -0
- package/docs/guides/basic-features/render/streaming-ssr.md +264 -0
- package/docs/guides/basic-features/routes/config-routes.md +426 -0
- package/docs/guides/basic-features/routes/routes.md +524 -0
- package/docs/guides/basic-features/static-assets/json-files.md +120 -0
- package/docs/guides/basic-features/static-assets/svg-assets.md +168 -0
- package/docs/guides/basic-features/static-assets/wasm-assets.md +62 -0
- package/docs/guides/basic-features/static-assets.md +160 -0
- package/docs/guides/basic-features/testing/playwright.md +120 -0
- package/docs/guides/basic-features/testing/rstest.md +251 -0
- package/docs/guides/concept/builder.md +37 -0
- package/docs/guides/concept/entries.md +319 -0
- package/docs/guides/concept/server.md +35 -0
- package/docs/guides/get-started/ai-coding-agents.md +58 -0
- package/docs/guides/get-started/glossary.md +63 -0
- package/docs/guides/get-started/introduction.md +40 -0
- package/docs/guides/get-started/quick-start.md +269 -0
- package/docs/guides/get-started/tech-stack.md +91 -0
- package/docs/guides/get-started/ultramodern.md +440 -0
- package/docs/guides/get-started/upgrade.md +118 -0
- package/docs/guides/topic-detail/module-federation/application.md +116 -0
- package/docs/guides/topic-detail/module-federation/deploy.md +104 -0
- package/docs/guides/topic-detail/module-federation/i18n.md +670 -0
- package/docs/guides/topic-detail/module-federation/introduce.md +35 -0
- package/docs/guides/topic-detail/module-federation/ssr.md +182 -0
- package/docs/guides/topic-detail/module-federation/usage.md +219 -0
- package/docs/guides/troubleshooting/builder.md +110 -0
- package/docs/guides/troubleshooting/cli.md +35 -0
- package/docs/guides/troubleshooting/dependencies.md +119 -0
- package/docs/guides/troubleshooting/hmr.md +144 -0
- package/docs/guides/upgrade/config.md +954 -0
- package/docs/guides/upgrade/entry.md +463 -0
- package/docs/guides/upgrade/other.md +183 -0
- package/docs/guides/upgrade/overview.md +33 -0
- package/docs/guides/upgrade/tailwindcss.md +91 -0
- package/docs/guides/upgrade/web-server.md +109 -0
- package/docs/index.md +33 -0
- package/docs/llms.txt +290 -0
- package/docs/plugin/cli-plugins/api.md +573 -0
- package/docs/plugin/cli-plugins/life-cycle.md +2 -0
- package/docs/plugin/introduction.md +152 -0
- package/docs/plugin/official/cli-plugins/plugin-bff.md +5 -0
- package/docs/plugin/official/cli-plugins/plugin-ssg.md +5 -0
- package/docs/plugin/official/cli-plugins/plugin-styled-components.md +5 -0
- package/docs/plugin/official/cli-plugins.md +4 -0
- package/docs/plugin/plugin-system.md +238 -0
- package/docs/plugin/runtime-plugins/api.md +194 -0
- package/docs/plugin/runtime-plugins/life-cycle.md +2 -0
- package/docs/plugin/server-plugins/api.md +209 -0
- package/docs/plugin/server-plugins/life-cycle.md +13 -0
- package/docs/tutorials/examples/csr-auth.md +9 -0
- package/docs/tutorials/foundations/introduction.md +16 -0
- package/package.json +63 -22
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# render
|
|
2
|
+
|
|
3
|
+
It is used to render project components.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { render } from '@modern-js/runtime/browser';
|
|
9
|
+
|
|
10
|
+
render(<ModernRoot />);
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Function Signature
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
export function render(App: React.ReactElement, id?: HTMLElement | string): Promise<any>;
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
### Parameters
|
|
20
|
+
|
|
21
|
+
- `App`:An instance of ReactElement created through [`createRoot`](/apis/app/runtime/core/create-root.md).
|
|
22
|
+
- `id`:The id of the DOM root element to be mounted, such as "root".
|
|
23
|
+
|
|
24
|
+
## Example
|
|
25
|
+
|
|
26
|
+
```tsx
|
|
27
|
+
import { createRoot } from '@modern-js/runtime/react';
|
|
28
|
+
import { render } from '@modern-js/runtime/browser';
|
|
29
|
+
|
|
30
|
+
const ModernRoot = createRoot();
|
|
31
|
+
|
|
32
|
+
async function beforeRender() {
|
|
33
|
+
// todo
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
beforeRender().then(() => {
|
|
37
|
+
render(<ModernRoot />);
|
|
38
|
+
});
|
|
39
|
+
```
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
# RuntimeContext
|
|
2
|
+
|
|
3
|
+
`RuntimeContext` is a React Context used to get Runtime context information in components. This Context can be accessed through React's `use` or `useContext` API.
|
|
4
|
+
|
|
5
|
+
:::warning
|
|
6
|
+
|
|
7
|
+
The `useRuntimeContext` Hook has been deprecated. Please use `use(RuntimeContext)` instead. `useRuntimeContext` is internally implemented as `useContext(RuntimeContext)`, while `use(RuntimeContext)` is the recommended new approach in React 19.
|
|
8
|
+
|
|
9
|
+
:::
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```tsx
|
|
14
|
+
import { use } from 'react';
|
|
15
|
+
import { RuntimeContext } from '@modern-js/runtime';
|
|
16
|
+
|
|
17
|
+
export function App() {
|
|
18
|
+
const runtimeContext = use(RuntimeContext); // You can also use useContext(RuntimeContext)
|
|
19
|
+
return <div>Hello World</div>
|
|
20
|
+
}
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Type Definition
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
type TRuntimeContext = {
|
|
27
|
+
initialData?: Record<string, unknown>;
|
|
28
|
+
isBrowser: boolean;
|
|
29
|
+
routes?: RouteObject[];
|
|
30
|
+
requestContext: RequestContext;
|
|
31
|
+
/**
|
|
32
|
+
* @deprecated Use `requestContext` instead
|
|
33
|
+
*/
|
|
34
|
+
context: RequestContext;
|
|
35
|
+
[key: string]: unknown;
|
|
36
|
+
};
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Property Description
|
|
40
|
+
|
|
41
|
+
- `isBrowser`: Indicates whether the current runtime environment is a browser
|
|
42
|
+
- `routes`: Route configuration information
|
|
43
|
+
- `requestContext`: Request context, containing `request` and `response` objects
|
|
44
|
+
- `context`: Deprecated, please use `requestContext` instead
|
|
45
|
+
|
|
46
|
+
### RequestContext
|
|
47
|
+
|
|
48
|
+
`RequestContext` is the type of the `requestContext` property, containing request and response related information:
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
type RequestContext = {
|
|
52
|
+
request: {
|
|
53
|
+
params: Record<string, string>; // Route parameters
|
|
54
|
+
pathname: string; // Pathname
|
|
55
|
+
query: Record<string, string>; // Query parameters
|
|
56
|
+
headers: IncomingHttpHeaders; // Request headers
|
|
57
|
+
host: string; // Hostname
|
|
58
|
+
url: string; // Full URL
|
|
59
|
+
referer?: string; // Referrer page
|
|
60
|
+
userAgent?: string; // User agent
|
|
61
|
+
cookie?: string; // Cookie string
|
|
62
|
+
cookieMap?: Record<string, string>; // Cookie map object
|
|
63
|
+
};
|
|
64
|
+
response: {
|
|
65
|
+
setHeader: (key: string, value: string) => void; // Set response header (server-side only)
|
|
66
|
+
status: (code: number) => void; // Set status code (server-side only)
|
|
67
|
+
locals: Record<string, any>; // Local data
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
**Notes**:
|
|
73
|
+
|
|
74
|
+
- `response.setHeader` and `response.status` are only available on the server-side (SSR)
|
|
75
|
+
- On the browser side, the `response` object may not contain these methods
|
|
76
|
+
- The `request` object is available on both server and browser, but server-side information is more complete
|
|
77
|
+
|
|
78
|
+
## Usage Examples
|
|
79
|
+
|
|
80
|
+
### Distinguishing Runtime Environment
|
|
81
|
+
|
|
82
|
+
```tsx
|
|
83
|
+
import { use } from 'react';
|
|
84
|
+
import { RuntimeContext } from '@modern-js/runtime';
|
|
85
|
+
|
|
86
|
+
function App() {
|
|
87
|
+
const { context, isBrowser } = use(RuntimeContext);
|
|
88
|
+
|
|
89
|
+
if (isBrowser === true) {
|
|
90
|
+
// Browser-side execution logic
|
|
91
|
+
console.log('browser render')
|
|
92
|
+
} else {
|
|
93
|
+
// Server-side execution logic
|
|
94
|
+
// Note: Functions like logger need to be injected into context through Runtime plugins
|
|
95
|
+
console.log('server render')
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Getting Request Context
|
|
101
|
+
|
|
102
|
+
When SSR is enabled, isomorphic request context can be obtained in both Node environment and browser environment.
|
|
103
|
+
|
|
104
|
+
The slight difference is that the Node environment also supports setting response headers, response codes, and provides Logger logging and Metrics tracking.
|
|
105
|
+
|
|
106
|
+
:::tip
|
|
107
|
+
|
|
108
|
+
When SSR is not enabled, only the information that can be obtained on the browser side is included.
|
|
109
|
+
|
|
110
|
+
:::
|
|
111
|
+
|
|
112
|
+
```tsx
|
|
113
|
+
import { use } from 'react';
|
|
114
|
+
import { RuntimeContext } from '@modern-js/runtime';
|
|
115
|
+
|
|
116
|
+
function App() {
|
|
117
|
+
const runtimeContext = use(RuntimeContext);
|
|
118
|
+
const { request, response } = runtimeContext.requestContext || {};
|
|
119
|
+
|
|
120
|
+
// Access request information
|
|
121
|
+
const userAgent = request?.userAgent || request?.headers?.['user-agent'];
|
|
122
|
+
const url = request?.url;
|
|
123
|
+
const query = request?.query;
|
|
124
|
+
const params = request?.params;
|
|
125
|
+
|
|
126
|
+
// Server-side can set response headers
|
|
127
|
+
if (!runtimeContext.isBrowser && response) {
|
|
128
|
+
response.setHeader('X-Custom-Header', 'value');
|
|
129
|
+
response.status(200);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return (
|
|
133
|
+
<div>
|
|
134
|
+
<div>User Agent: {userAgent}</div>
|
|
135
|
+
<div>URL: {url}</div>
|
|
136
|
+
<div>Query: {JSON.stringify(query)}</div>
|
|
137
|
+
</div>
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Accessing Injected Global Data
|
|
143
|
+
|
|
144
|
+
Global data can be injected into `RuntimeContext` through the `onBeforeRender` hook of Runtime plugins:
|
|
145
|
+
|
|
146
|
+
```tsx
|
|
147
|
+
import { use } from 'react';
|
|
148
|
+
import { RuntimeContext } from '@modern-js/runtime';
|
|
149
|
+
|
|
150
|
+
export default function MyComponent() {
|
|
151
|
+
const context = use(RuntimeContext);
|
|
152
|
+
const { message } = context;
|
|
153
|
+
|
|
154
|
+
return <div>{message}</div>;
|
|
155
|
+
}
|
|
156
|
+
```
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
# router
|
|
2
|
+
|
|
3
|
+
:::info
|
|
4
|
+
The router solution based on [react-router](https://reactrouter.com/).
|
|
5
|
+
|
|
6
|
+
This page documents the React Router runtime export (`@modern-js/runtime/router`).\
|
|
7
|
+
If your app uses TanStack Router (`--router tanstack`), use `@modern-js/plugin-tanstack/runtime` and refer to TanStack Router API docs.
|
|
8
|
+
|
|
9
|
+
:::
|
|
10
|
+
|
|
11
|
+
## hooks
|
|
12
|
+
|
|
13
|
+
### useNavigate
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
declare function useNavigate(): NavigateFunction;
|
|
17
|
+
|
|
18
|
+
interface NavigateFunction {
|
|
19
|
+
(
|
|
20
|
+
to: To,
|
|
21
|
+
options?: {
|
|
22
|
+
replace?: boolean;
|
|
23
|
+
state?: any;
|
|
24
|
+
relative?: RelativeRoutingType;
|
|
25
|
+
},
|
|
26
|
+
): void;
|
|
27
|
+
(delta: number): void;
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
The `useNavigate` hook returns a function that lets you navigate programmatically.
|
|
32
|
+
|
|
33
|
+
```tsx
|
|
34
|
+
import { useNavigate } from '@modern-js/runtime/router';
|
|
35
|
+
|
|
36
|
+
export function HomeButton() {
|
|
37
|
+
let navigate = useNavigate();
|
|
38
|
+
|
|
39
|
+
function handleClick() {
|
|
40
|
+
navigate('/home');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return (
|
|
44
|
+
<button type="button" onClick={handleClick}>
|
|
45
|
+
Go home
|
|
46
|
+
</button>
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### useLocation
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
declare function useLocation(): Location;
|
|
55
|
+
|
|
56
|
+
interface Location extends Path {
|
|
57
|
+
state: unknown;
|
|
58
|
+
key: Key;
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
The `useLocation` hook returns the current [location](https://reactrouter.com/web/api/location) object. A new location object would be returned whenever the current location changes.
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import { useLocation } from '@modern-js/runtime/router';
|
|
66
|
+
|
|
67
|
+
function usePageViews() {
|
|
68
|
+
let location = useLocation();
|
|
69
|
+
React.useEffect(() => {
|
|
70
|
+
ga.send(["pageview", location.pathname]);
|
|
71
|
+
}, [location]);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function App() {
|
|
75
|
+
usePageViews();
|
|
76
|
+
return (
|
|
77
|
+
//...
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### useParams
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
declare function useParams<K extends string = string>(): Readonly<Params<K>>;
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
The `useParams` hook returns an object of key/value pairs of the dynamic params from the current URL that were matched by the `<Route path>`.
|
|
89
|
+
|
|
90
|
+
```tsx
|
|
91
|
+
import { Routes, Route, useParams } from '@modern-js/runtime/router';
|
|
92
|
+
|
|
93
|
+
function BlogPost() {
|
|
94
|
+
const { slug } = useParams();
|
|
95
|
+
return <div>Now showing post {slug}</div>;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function App() {
|
|
99
|
+
return (
|
|
100
|
+
<Routes>
|
|
101
|
+
<Route path="/" element={<div>home</div>} />
|
|
102
|
+
<Route path="/blog/:slug" element={<BlogPost />} />
|
|
103
|
+
</Routes>
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### useRouteError
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
export declare function useRouteError(): unknown;
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
`useRouteError` returns the nearest ancestor Route error。
|
|
115
|
+
|
|
116
|
+
```tsx
|
|
117
|
+
import { useRouteError } from '@modern-js/runtime/router';
|
|
118
|
+
const ErrorBoundary = () => {
|
|
119
|
+
const error = useRouteError();
|
|
120
|
+
return (
|
|
121
|
+
<div>
|
|
122
|
+
<h1>{error.status}</h1>
|
|
123
|
+
<h2>{error.message}</h2>
|
|
124
|
+
</div>
|
|
125
|
+
);
|
|
126
|
+
};
|
|
127
|
+
export default ErrorBoundary;
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Components
|
|
131
|
+
|
|
132
|
+
### Link
|
|
133
|
+
|
|
134
|
+
```ts
|
|
135
|
+
declare function Link(props: LinkProps): React.ReactElement;
|
|
136
|
+
|
|
137
|
+
interface LinkProps
|
|
138
|
+
extends Omit<React.AnchorHTMLAttributes<HTMLAnchorElement>, 'href'> {
|
|
139
|
+
replace?: boolean;
|
|
140
|
+
state?: any;
|
|
141
|
+
to: To;
|
|
142
|
+
reloadDocument?: boolean;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
type To = string | Partial<Path>;
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
A `<Link>` is an element that lets the user navigate to another page by clicking or tapping on it.
|
|
149
|
+
|
|
150
|
+
```ts
|
|
151
|
+
<Link to="/about">About</Link>
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### NavLink
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
declare function NavLink(props: NavLinkProps): React.ReactElement;
|
|
158
|
+
|
|
159
|
+
interface NavLinkProps
|
|
160
|
+
extends Omit<LinkProps, 'className' | 'style' | 'children'> {
|
|
161
|
+
caseSensitive?: boolean;
|
|
162
|
+
children?:
|
|
163
|
+
| React.ReactNode
|
|
164
|
+
| ((props: { isActive: boolean }) => React.ReactNode);
|
|
165
|
+
className?: string | ((props: { isActive: boolean }) => string | undefined);
|
|
166
|
+
end?: boolean;
|
|
167
|
+
style?:
|
|
168
|
+
| React.CSSProperties
|
|
169
|
+
| ((props: { isActive: boolean }) => React.CSSProperties);
|
|
170
|
+
}
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
A `<NavLink>` is a special kind of `<Link>` that knows whether or not it is "active".
|
|
174
|
+
|
|
175
|
+
### Outlet
|
|
176
|
+
|
|
177
|
+
```ts
|
|
178
|
+
interface OutletProps {
|
|
179
|
+
context?: unknown;
|
|
180
|
+
}
|
|
181
|
+
declare function Outlet(props: OutletProps): React.ReactElement | null;
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
An `<Outlet>` should be used in parent route elements to render their child route elements. This allows nested UI to show up when child routes are rendered.
|
|
185
|
+
|
|
186
|
+
```tsx
|
|
187
|
+
function Dashboard() {
|
|
188
|
+
return (
|
|
189
|
+
<div>
|
|
190
|
+
<h1>Dashboard</h1>
|
|
191
|
+
|
|
192
|
+
{/* This element will render either <DashboardMessages> when the URL is
|
|
193
|
+
"/messages", <DashboardTasks> at "/tasks", or null if it is "/"
|
|
194
|
+
*/}
|
|
195
|
+
<Outlet />
|
|
196
|
+
</div>
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function App() {
|
|
201
|
+
return (
|
|
202
|
+
<Routes>
|
|
203
|
+
<Route path="/" element={<Dashboard />}>
|
|
204
|
+
<Route path="messages" element={<DashboardMessages />} />
|
|
205
|
+
<Route path="tasks" element={<DashboardTasks />} />
|
|
206
|
+
</Route>
|
|
207
|
+
</Routes>
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### Route
|
|
213
|
+
|
|
214
|
+
```ts
|
|
215
|
+
interface RouteObject {
|
|
216
|
+
path?: string;
|
|
217
|
+
index?: boolean;
|
|
218
|
+
children?: React.ReactNode;
|
|
219
|
+
caseSensitive?: boolean;
|
|
220
|
+
id?: string;
|
|
221
|
+
loader?: LoaderFunction;
|
|
222
|
+
action?: ActionFunction;
|
|
223
|
+
element?: React.ReactNode | null;
|
|
224
|
+
errorElement?: React.ReactNode | null;
|
|
225
|
+
handle?: RouteObject['handle'];
|
|
226
|
+
shouldRevalidate?: ShouldRevalidateFunction;
|
|
227
|
+
}
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
`Route` represents the route information. A `Route` object couples URL segments to components, data loading and data mutations.
|
|
231
|
+
|
|
232
|
+
`Route` can be used as a plain object, passing to the router creation functions:
|
|
233
|
+
|
|
234
|
+
```ts
|
|
235
|
+
const router = createBrowserRouter([
|
|
236
|
+
{
|
|
237
|
+
// it renders this element
|
|
238
|
+
element: <Team />,
|
|
239
|
+
|
|
240
|
+
// when the URL matches this segment
|
|
241
|
+
path: 'teams/:teamId',
|
|
242
|
+
|
|
243
|
+
// with this data loaded before rendering
|
|
244
|
+
loader: async ({ request, params }) => {
|
|
245
|
+
return fetch(`/fake/api/teams/${params.teamId}.json`, {
|
|
246
|
+
signal: request.signal,
|
|
247
|
+
});
|
|
248
|
+
},
|
|
249
|
+
|
|
250
|
+
// performing this mutation when data is submitted to it
|
|
251
|
+
action: async ({ request }) => {
|
|
252
|
+
return updateFakeTeam(await request.formData());
|
|
253
|
+
},
|
|
254
|
+
|
|
255
|
+
// and renders this element in case something went wrong
|
|
256
|
+
errorElement: <ErrorBoundary />,
|
|
257
|
+
},
|
|
258
|
+
]);
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
You can also declare your routes with JSX and `createRoutesFromElements`, the props to the element are identical to the properties of the route objects:
|
|
262
|
+
|
|
263
|
+
```ts
|
|
264
|
+
const router = createBrowserRouter(
|
|
265
|
+
createRoutesFromElements(
|
|
266
|
+
<Route
|
|
267
|
+
element={<Team />}
|
|
268
|
+
path="teams/:teamId"
|
|
269
|
+
loader={async ({ params }) => {
|
|
270
|
+
return fetch(`/fake/api/teams/${params.teamId}.json`);
|
|
271
|
+
}}
|
|
272
|
+
action={async ({ request }) => {
|
|
273
|
+
return updateFakeTeam(await request.formData());
|
|
274
|
+
}}
|
|
275
|
+
errorElement={<ErrorBoundary />}
|
|
276
|
+
/>,
|
|
277
|
+
),
|
|
278
|
+
);
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
## More
|
|
282
|
+
|
|
283
|
+
You can access to [React Router](https://reactrouter.com/) to get the full API information.
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# NoSSR
|
|
2
|
+
|
|
3
|
+
The content wrapped by NoSSR will not be rendered at the server, nor will it be rendered during the client side hydrate. it will only be rendered immediately after the entire app is rendered.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```tsx
|
|
8
|
+
import { NoSSR } from '@modern-js/runtime/ssr';
|
|
9
|
+
|
|
10
|
+
export default () => <NoSSR>...</NoSSR>;
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Example
|
|
14
|
+
|
|
15
|
+
In the following code, the `Time` component is used to display the current time. Since the time obtained by server-side rendering and client side hydrate are diff, React will throw an exception. For this case, you can use `NoSSR` to optimize:
|
|
16
|
+
|
|
17
|
+
```tsx
|
|
18
|
+
import { NoSSR } from '@modern-js/runtime/ssr';
|
|
19
|
+
|
|
20
|
+
function Time() {
|
|
21
|
+
return (
|
|
22
|
+
<NoSSR>
|
|
23
|
+
<div>Time: {Date.now()}</div>
|
|
24
|
+
</NoSSR>
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Scene
|
|
30
|
+
|
|
31
|
+
In CSR, it is often necessary to render different content according to the browser UA, or a parameter of the current page URL. If the application switches directly to SSR at this time, it is very likely that the results will not meet the expectations.
|
|
32
|
+
|
|
33
|
+
Modern.js provides complete browser side information in the SSR context, which can be used to determine the rendering result of the component on the server side.
|
|
34
|
+
|
|
35
|
+
Even so, if there is too much logic in the application, or the developer wants to use the context later, or does not want some content to be rendered at the server side. developer can use the NoSSR component to exclude this part from server-side rendering.
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# renderStreaming
|
|
2
|
+
|
|
3
|
+
Used for `React v18` + `Streaming SSR` to render readable streams, used in conjunction with `createRequestHandler`.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```ts title="src/entry.server.tsx"
|
|
8
|
+
import {
|
|
9
|
+
renderStreaming,
|
|
10
|
+
createRequestHandler,
|
|
11
|
+
type HandleRequest,
|
|
12
|
+
} from '@modern-js/runtime/ssr/server';
|
|
13
|
+
|
|
14
|
+
const handleRequest: HandleRequest = async (request, ServerRoot, options) => {
|
|
15
|
+
const stream = await renderStreaming(request, <ServerRoot />, options);
|
|
16
|
+
|
|
17
|
+
return new Response(stream, {
|
|
18
|
+
headers: {
|
|
19
|
+
'content-type': 'text/html; charset=utf-8',
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export default createRequestHandler(handleRequest);
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Function Signature
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
export type RenderStreaming = (
|
|
31
|
+
request: Request,
|
|
32
|
+
serverRoot: React.ReactElement,
|
|
33
|
+
optinos: RenderOptions,
|
|
34
|
+
) => Promise<ReadableStream>;
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Example
|
|
38
|
+
|
|
39
|
+
```tsx title="src/entry.server.tsx"
|
|
40
|
+
import {
|
|
41
|
+
renderStreaming,
|
|
42
|
+
createRequestHandler,
|
|
43
|
+
type HandleRequest,
|
|
44
|
+
} from '@modern-js/runtime/ssr/server';
|
|
45
|
+
|
|
46
|
+
const handleRequest: HandleRequest = async (request, ServerRoot, options) => {
|
|
47
|
+
// do something before render
|
|
48
|
+
const stream = await renderStreaming(request, <ServerRoot />, options);
|
|
49
|
+
|
|
50
|
+
// docs: https://developer.mozilla.org/en-US/docs/Web/API/TransformStream
|
|
51
|
+
const transformStream = new TransformStream({
|
|
52
|
+
transform(chunk, controller) {
|
|
53
|
+
// do some transform
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
stream.pipeThrough(transformStream);
|
|
58
|
+
|
|
59
|
+
return new Response(transformStream.readable, {
|
|
60
|
+
headers: {
|
|
61
|
+
'content-type': 'text/html; charset=utf-8',
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export default createRequestHandler(handleRequest);
|
|
67
|
+
```
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# renderString
|
|
2
|
+
|
|
3
|
+
Used for React String SSR to render strings, used in conjunction with `createRequestHandler`.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```tsx title="src/entry.server.tsx"
|
|
8
|
+
import {
|
|
9
|
+
renderString,
|
|
10
|
+
createRequestHandler,
|
|
11
|
+
type HandleRequest,
|
|
12
|
+
} from '@modern-js/runtime/ssr/server';
|
|
13
|
+
|
|
14
|
+
const handleRequest: HandleRequest = async (request, ServerRoot, options) => {
|
|
15
|
+
const body = await renderString(request, <ServerRoot />, options);
|
|
16
|
+
|
|
17
|
+
return new Response(body, {
|
|
18
|
+
headers: {
|
|
19
|
+
'content-type': 'text/html; charset=utf-8',
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export default createRequestHandler(handleRequest);
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Function Signature
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
export type RenderString = (
|
|
31
|
+
request: Request,
|
|
32
|
+
serverRoot: React.ReactElement,
|
|
33
|
+
optinos: RenderOptions,
|
|
34
|
+
) => Promise<string>;
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Example
|
|
38
|
+
|
|
39
|
+
```tsx title="src/entry.server.tsx"
|
|
40
|
+
import {
|
|
41
|
+
renderString,
|
|
42
|
+
createRequestHandler,
|
|
43
|
+
type HandleRequest,
|
|
44
|
+
} from '@modern-js/runtime/ssr/server';
|
|
45
|
+
|
|
46
|
+
const handleRequest: HandleRequest = async (request, ServerRoot, options) => {
|
|
47
|
+
// do something before render
|
|
48
|
+
const body = await renderString(request, <ServerRoot />, options);
|
|
49
|
+
|
|
50
|
+
const newBody = body + '<div>Byte-Dance</div>';
|
|
51
|
+
|
|
52
|
+
return new Response(newBody, {
|
|
53
|
+
headers: {
|
|
54
|
+
'content-type': 'text/html; charset=utf-8',
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export default createRequestHandler(handleRequest);
|
|
60
|
+
```
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# createRequestHandler
|
|
2
|
+
|
|
3
|
+
Used to customize the Server-Side Rendering entry to return the `requestHandler`.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```tsx title="src/entry.server.tsx"
|
|
8
|
+
import {
|
|
9
|
+
renderString,
|
|
10
|
+
createRequestHandler,
|
|
11
|
+
type HandleRequest,
|
|
12
|
+
} from '@modern-js/runtime/ssr/server';
|
|
13
|
+
|
|
14
|
+
const handleRequest: HandleRequest = async (request, ServerRoot, options) => {
|
|
15
|
+
const body = await renderString(request, <ServerRoot />, options);
|
|
16
|
+
|
|
17
|
+
return new Response(body, {
|
|
18
|
+
headers: {
|
|
19
|
+
'content-type': 'text/html; charset=utf-8',
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export default createRequestHandler(handleRequest);
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Function Signature
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
export type HandleRequest = (
|
|
31
|
+
request: Request,
|
|
32
|
+
ServerRoot: React.ComponentType,
|
|
33
|
+
options: HandleRequestOptions,
|
|
34
|
+
) => Promise<Response>;
|
|
35
|
+
|
|
36
|
+
export type RequestHandler = (
|
|
37
|
+
request: Request,
|
|
38
|
+
options: RequestHandlerOptions,
|
|
39
|
+
) => Promise<Response>;
|
|
40
|
+
|
|
41
|
+
export type CreateRequestHandler = (
|
|
42
|
+
handleRequest: HandleRequest,
|
|
43
|
+
) => Promise<RequestHandler>;
|
|
44
|
+
```
|