@modern-js/app-tools 3.6.0 → 3.8.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.
Files changed (306) hide show
  1. package/bin/modern-bundle-docs.js +7 -0
  2. package/dist/cjs/builder/generator/createBuilderProviderConfig.js +5 -1
  3. package/dist/cjs/bundleDocs.js +114 -0
  4. package/dist/cjs/commands/build.js +2 -1
  5. package/dist/cjs/commands/deploy.js +4 -2
  6. package/dist/cjs/commands/index.js +0 -4
  7. package/dist/cjs/config/default.js +2 -0
  8. package/dist/cjs/index.js +52 -14
  9. package/dist/cjs/plugins/analyze/index.js +2 -2
  10. package/dist/cjs/plugins/analyze/utils.js +3 -2
  11. package/dist/cjs/plugins/initialize/index.js +4 -3
  12. package/dist/cjs/plugins/serverBuild.js +3 -2
  13. package/dist/esm/builder/generator/createBuilderProviderConfig.mjs +5 -1
  14. package/dist/esm/bundleDocs.mjs +62 -0
  15. package/dist/esm/commands/build.mjs +2 -1
  16. package/dist/esm/commands/deploy.mjs +4 -2
  17. package/dist/esm/commands/index.mjs +0 -4
  18. package/dist/esm/config/default.mjs +2 -0
  19. package/dist/esm/index.mjs +15 -1
  20. package/dist/esm/plugins/analyze/index.mjs +2 -2
  21. package/dist/esm/plugins/analyze/utils.mjs +3 -2
  22. package/dist/esm/plugins/initialize/index.mjs +4 -3
  23. package/dist/esm/plugins/serverBuild.mjs +3 -2
  24. package/dist/esm-node/builder/generator/createBuilderProviderConfig.mjs +5 -1
  25. package/dist/esm-node/bundleDocs.mjs +63 -0
  26. package/dist/esm-node/commands/build.mjs +2 -1
  27. package/dist/esm-node/commands/deploy.mjs +4 -2
  28. package/dist/esm-node/commands/index.mjs +0 -4
  29. package/dist/esm-node/config/default.mjs +2 -0
  30. package/dist/esm-node/index.mjs +15 -1
  31. package/dist/esm-node/plugins/analyze/index.mjs +2 -2
  32. package/dist/esm-node/plugins/analyze/utils.mjs +3 -2
  33. package/dist/esm-node/plugins/initialize/index.mjs +4 -3
  34. package/dist/esm-node/plugins/serverBuild.mjs +3 -2
  35. package/dist/types/bundleDocs.d.ts +20 -0
  36. package/dist/types/commands/deploy.d.ts +2 -1
  37. package/dist/types/index.d.ts +4 -1
  38. package/dist/types/plugins/analyze/utils.d.ts +1 -1
  39. package/dist/types/types/config/dev.d.ts +6 -0
  40. package/docs/apis/app/commands.md +241 -0
  41. package/docs/apis/app/hooks/api/lambda.md +9 -0
  42. package/docs/apis/app/hooks/config/favicon.md +24 -0
  43. package/docs/apis/app/hooks/config/html.md +5 -0
  44. package/docs/apis/app/hooks/config/icon.md +24 -0
  45. package/docs/apis/app/hooks/config/mock.md +6 -0
  46. package/docs/apis/app/hooks/config/public.md +26 -0
  47. package/docs/apis/app/hooks/config/upload.md +50 -0
  48. package/docs/apis/app/hooks/modern-config.md +5 -0
  49. package/docs/apis/app/hooks/server/server.md +5 -0
  50. package/docs/apis/app/hooks/shared.md +3 -0
  51. package/docs/apis/app/hooks/src/app.md +30 -0
  52. package/docs/apis/app/hooks/src/entry.md +34 -0
  53. package/docs/apis/app/hooks/src/entry.server.md +51 -0
  54. package/docs/apis/app/hooks/src/modern.runtime.md +5 -0
  55. package/docs/apis/app/hooks/src/routes.md +86 -0
  56. package/docs/apis/app/hooks/src/server.md +3 -0
  57. package/docs/apis/app/runtime/bff/use-hono-context.md +27 -0
  58. package/docs/apis/app/runtime/core/create-root.md +19 -0
  59. package/docs/apis/app/runtime/core/render.md +39 -0
  60. package/docs/apis/app/runtime/core/runtime-context.md +156 -0
  61. package/docs/apis/app/runtime/router/router.md +280 -0
  62. package/docs/apis/app/runtime/ssr/no-ssr.md +35 -0
  63. package/docs/apis/app/runtime/ssr/renderStreaming.md +67 -0
  64. package/docs/apis/app/runtime/ssr/renderString.md +60 -0
  65. package/docs/apis/app/runtime/ssr/requestHandler.md +44 -0
  66. package/docs/apis/app/runtime/utility/css-in-js.md +40 -0
  67. package/docs/apis/app/runtime/utility/head.md +35 -0
  68. package/docs/apis/app/runtime/utility/loadable.md +82 -0
  69. package/docs/community/blog/2022-0708-updates.md +91 -0
  70. package/docs/community/blog/2022-0910-updates.md +76 -0
  71. package/docs/community/blog/overview.md +63 -0
  72. package/docs/community/blog/v2-release-note.md +238 -0
  73. package/docs/community/blog/v3-release-note.md +622 -0
  74. package/docs/community/contributing-guide.md +253 -0
  75. package/docs/community/releases.md +27 -0
  76. package/docs/community/showcase.md +34 -0
  77. package/docs/community/team.md +14 -0
  78. package/docs/configure/app/bff/cross-project.md +20 -0
  79. package/docs/configure/app/bff/prefix.md +29 -0
  80. package/docs/configure/app/builder-plugins.md +66 -0
  81. package/docs/configure/app/dev/asset-prefix.md +13 -0
  82. package/docs/configure/app/dev/before-start-url.md +17 -0
  83. package/docs/configure/app/dev/client.md +41 -0
  84. package/docs/configure/app/dev/hmr.md +10 -0
  85. package/docs/configure/app/dev/host.md +18 -0
  86. package/docs/configure/app/dev/https.md +77 -0
  87. package/docs/configure/app/dev/lazy-compilation.md +39 -0
  88. package/docs/configure/app/dev/live-reload.md +10 -0
  89. package/docs/configure/app/dev/mock-dir.md +31 -0
  90. package/docs/configure/app/dev/progress-bar.md +19 -0
  91. package/docs/configure/app/dev/server.md +124 -0
  92. package/docs/configure/app/dev/setup-middlewares.md +32 -0
  93. package/docs/configure/app/dev/start-url.md +48 -0
  94. package/docs/configure/app/dev/watch-files.md +27 -0
  95. package/docs/configure/app/dev/write-to-disk.md +10 -0
  96. package/docs/configure/app/experiments/source-build.md +31 -0
  97. package/docs/configure/app/html/app-icon.md +28 -0
  98. package/docs/configure/app/html/crossorigin.md +10 -0
  99. package/docs/configure/app/html/favicon.md +16 -0
  100. package/docs/configure/app/html/inject.md +10 -0
  101. package/docs/configure/app/html/meta.md +24 -0
  102. package/docs/configure/app/html/mount-id.md +10 -0
  103. package/docs/configure/app/html/output-structure.md +10 -0
  104. package/docs/configure/app/html/script-loading.md +10 -0
  105. package/docs/configure/app/html/tags.md +15 -0
  106. package/docs/configure/app/html/template-parameters.md +33 -0
  107. package/docs/configure/app/html/template.md +10 -0
  108. package/docs/configure/app/html/title.md +18 -0
  109. package/docs/configure/app/output/asset-prefix.md +11 -0
  110. package/docs/configure/app/output/assets-retry.md +77 -0
  111. package/docs/configure/app/output/charset.md +10 -0
  112. package/docs/configure/app/output/clean-dist-path.md +16 -0
  113. package/docs/configure/app/output/convert-to-rem.md +79 -0
  114. package/docs/configure/app/output/copy.md +10 -0
  115. package/docs/configure/app/output/css-modules.md +48 -0
  116. package/docs/configure/app/output/data-uri-limit.md +26 -0
  117. package/docs/configure/app/output/disable-css-module-extension.md +55 -0
  118. package/docs/configure/app/output/disable-inline-runtime-chunk.md +41 -0
  119. package/docs/configure/app/output/disable-svgr.md +16 -0
  120. package/docs/configure/app/output/disable-ts-checker.md +49 -0
  121. package/docs/configure/app/output/dist-path.md +43 -0
  122. package/docs/configure/app/output/enable-asset-manifest.md +36 -0
  123. package/docs/configure/app/output/enable-css-module-tsdeclaration.md +28 -0
  124. package/docs/configure/app/output/enable-inline-route-manifests.md +16 -0
  125. package/docs/configure/app/output/externals.md +20 -0
  126. package/docs/configure/app/output/filename-hash.md +10 -0
  127. package/docs/configure/app/output/filename.md +55 -0
  128. package/docs/configure/app/output/inject-styles.md +10 -0
  129. package/docs/configure/app/output/inline-scripts.md +29 -0
  130. package/docs/configure/app/output/inline-styles.md +29 -0
  131. package/docs/configure/app/output/legal-comments.md +18 -0
  132. package/docs/configure/app/output/minify.md +22 -0
  133. package/docs/configure/app/output/override-browserslist.md +22 -0
  134. package/docs/configure/app/output/polyfill.md +12 -0
  135. package/docs/configure/app/output/source-map.md +30 -0
  136. package/docs/configure/app/output/split-route-chunks.md +16 -0
  137. package/docs/configure/app/output/ssg.md +81 -0
  138. package/docs/configure/app/output/ssgByEntries.md +90 -0
  139. package/docs/configure/app/output/svg-default-export.md +30 -0
  140. package/docs/configure/app/output/temp-dir.md +20 -0
  141. package/docs/configure/app/performance/build-cache.md +39 -0
  142. package/docs/configure/app/performance/chunk-split.md +40 -0
  143. package/docs/configure/app/performance/dns-prefetch.md +15 -0
  144. package/docs/configure/app/performance/preconnect.md +16 -0
  145. package/docs/configure/app/performance/prefetch.md +21 -0
  146. package/docs/configure/app/performance/preload.md +23 -0
  147. package/docs/configure/app/performance/print-file-size.md +40 -0
  148. package/docs/configure/app/performance/profile.md +10 -0
  149. package/docs/configure/app/performance/remove-console.md +10 -0
  150. package/docs/configure/app/performance/remove-moment-locale.md +10 -0
  151. package/docs/configure/app/plugins.md +59 -0
  152. package/docs/configure/app/resolve/alias-strategy.md +10 -0
  153. package/docs/configure/app/resolve/alias.md +9 -0
  154. package/docs/configure/app/resolve/condition-names.md +13 -0
  155. package/docs/configure/app/resolve/dedupe.md +9 -0
  156. package/docs/configure/app/resolve/extensions.md +13 -0
  157. package/docs/configure/app/runtime/0-intro.md +58 -0
  158. package/docs/configure/app/runtime/plugins.md +58 -0
  159. package/docs/configure/app/runtime/router.md +35 -0
  160. package/docs/configure/app/security/check-syntax.md +69 -0
  161. package/docs/configure/app/security/nonce.md +15 -0
  162. package/docs/configure/app/security/sri.md +20 -0
  163. package/docs/configure/app/server/base-url.md +26 -0
  164. package/docs/configure/app/server/port.md +18 -0
  165. package/docs/configure/app/server/public-routes.md +22 -0
  166. package/docs/configure/app/server/routes.md +86 -0
  167. package/docs/configure/app/server/rsc.md +26 -0
  168. package/docs/configure/app/server/ssr-by-entries.md +25 -0
  169. package/docs/configure/app/server/ssr.md +78 -0
  170. package/docs/configure/app/server/tsconfig-path.md +59 -0
  171. package/docs/configure/app/source/alias-strategy.md +14 -0
  172. package/docs/configure/app/source/alias.md +23 -0
  173. package/docs/configure/app/source/config-dir.md +20 -0
  174. package/docs/configure/app/source/decorators.md +25 -0
  175. package/docs/configure/app/source/define.md +16 -0
  176. package/docs/configure/app/source/disable-default-entries.md +28 -0
  177. package/docs/configure/app/source/enable-async-entry.md +54 -0
  178. package/docs/configure/app/source/enable-async-pre-entry.md +26 -0
  179. package/docs/configure/app/source/entries-dir.md +35 -0
  180. package/docs/configure/app/source/entries.md +179 -0
  181. package/docs/configure/app/source/exclude.md +10 -0
  182. package/docs/configure/app/source/global-vars.md +106 -0
  183. package/docs/configure/app/source/include.md +36 -0
  184. package/docs/configure/app/source/main-entry-name.md +24 -0
  185. package/docs/configure/app/source/pre-entry.md +10 -0
  186. package/docs/configure/app/source/react-compiler.md +68 -0
  187. package/docs/configure/app/source/transform-import.md +27 -0
  188. package/docs/configure/app/split-chunks.md +17 -0
  189. package/docs/configure/app/tools/autoprefixer.md +44 -0
  190. package/docs/configure/app/tools/bundler-chain.md +26 -0
  191. package/docs/configure/app/tools/css-extract.md +33 -0
  192. package/docs/configure/app/tools/css-loader.md +17 -0
  193. package/docs/configure/app/tools/dev-server.md +113 -0
  194. package/docs/configure/app/tools/html-plugin.md +41 -0
  195. package/docs/configure/app/tools/less.md +81 -0
  196. package/docs/configure/app/tools/lightningcss-loader.md +35 -0
  197. package/docs/configure/app/tools/minify-css.md +53 -0
  198. package/docs/configure/app/tools/postcss.md +34 -0
  199. package/docs/configure/app/tools/rspack.md +10 -0
  200. package/docs/configure/app/tools/sass.md +78 -0
  201. package/docs/configure/app/tools/style-loader.md +10 -0
  202. package/docs/configure/app/tools/swc.md +65 -0
  203. package/docs/configure/app/tools/ts-checker.md +109 -0
  204. package/docs/configure/app/usage.md +276 -0
  205. package/docs/guides/advanced-features/bff/cross-project.md +109 -0
  206. package/docs/guides/advanced-features/bff/extend-server.md +120 -0
  207. package/docs/guides/advanced-features/bff/frameworks.md +124 -0
  208. package/docs/guides/advanced-features/bff/function.md +314 -0
  209. package/docs/guides/advanced-features/bff/operators.md +554 -0
  210. package/docs/guides/advanced-features/bff/sdk.md +116 -0
  211. package/docs/guides/advanced-features/bff/upload.md +101 -0
  212. package/docs/guides/advanced-features/bff.md +18 -0
  213. package/docs/guides/advanced-features/build-performance.md +130 -0
  214. package/docs/guides/advanced-features/compatibility.md +120 -0
  215. package/docs/guides/advanced-features/international/advanced.md +128 -0
  216. package/docs/guides/advanced-features/international/api.md +231 -0
  217. package/docs/guides/advanced-features/international/best-practices.md +286 -0
  218. package/docs/guides/advanced-features/international/configuration.md +227 -0
  219. package/docs/guides/advanced-features/international/locale-detection.md +126 -0
  220. package/docs/guides/advanced-features/international/quick-start.md +128 -0
  221. package/docs/guides/advanced-features/international/resource-loading.md +154 -0
  222. package/docs/guides/advanced-features/international/routing.md +130 -0
  223. package/docs/guides/advanced-features/international.md +27 -0
  224. package/docs/guides/advanced-features/low-level.md +46 -0
  225. package/docs/guides/advanced-features/page-performance/code-split.md +77 -0
  226. package/docs/guides/advanced-features/page-performance/inline-assets.md +159 -0
  227. package/docs/guides/advanced-features/page-performance/optimize-bundle.md +97 -0
  228. package/docs/guides/advanced-features/page-performance/react-compiler.md +69 -0
  229. package/docs/guides/advanced-features/server-monitor/logger.md +41 -0
  230. package/docs/guides/advanced-features/server-monitor/metrics.md +58 -0
  231. package/docs/guides/advanced-features/server-monitor/monitors.md +242 -0
  232. package/docs/guides/advanced-features/source-build.md +164 -0
  233. package/docs/guides/advanced-features/web-server.md +288 -0
  234. package/docs/guides/basic-features/alias.md +102 -0
  235. package/docs/guides/basic-features/css/css-in-js.md +72 -0
  236. package/docs/guides/basic-features/css/css-modules.md +212 -0
  237. package/docs/guides/basic-features/css/css.md +27 -0
  238. package/docs/guides/basic-features/css/tailwindcss.md +27 -0
  239. package/docs/guides/basic-features/data/data-cache.md +510 -0
  240. package/docs/guides/basic-features/data/data-fetch.md +415 -0
  241. package/docs/guides/basic-features/data/data-write.md +227 -0
  242. package/docs/guides/basic-features/debug/mock.md +109 -0
  243. package/docs/guides/basic-features/debug/proxy.md +21 -0
  244. package/docs/guides/basic-features/debug/rsdoctor.md +62 -0
  245. package/docs/guides/basic-features/debug/using-storybook.md +112 -0
  246. package/docs/guides/basic-features/deploy.md +458 -0
  247. package/docs/guides/basic-features/env-vars.md +177 -0
  248. package/docs/guides/basic-features/html.md +255 -0
  249. package/docs/guides/basic-features/output-files.md +141 -0
  250. package/docs/guides/basic-features/render/before-render.md +108 -0
  251. package/docs/guides/basic-features/render/overview.md +47 -0
  252. package/docs/guides/basic-features/render/rsc.md +525 -0
  253. package/docs/guides/basic-features/render/ssg.md +228 -0
  254. package/docs/guides/basic-features/render/ssr-cache.md +201 -0
  255. package/docs/guides/basic-features/render/ssr.md +321 -0
  256. package/docs/guides/basic-features/render/streaming-ssr.md +264 -0
  257. package/docs/guides/basic-features/routes/config-routes.md +426 -0
  258. package/docs/guides/basic-features/routes/routes.md +498 -0
  259. package/docs/guides/basic-features/static-assets/json-files.md +120 -0
  260. package/docs/guides/basic-features/static-assets/svg-assets.md +168 -0
  261. package/docs/guides/basic-features/static-assets/wasm-assets.md +62 -0
  262. package/docs/guides/basic-features/static-assets.md +160 -0
  263. package/docs/guides/basic-features/testing/playwright.md +120 -0
  264. package/docs/guides/basic-features/testing/rstest.md +251 -0
  265. package/docs/guides/concept/builder.md +37 -0
  266. package/docs/guides/concept/entries.md +319 -0
  267. package/docs/guides/concept/server.md +35 -0
  268. package/docs/guides/get-started/ai-coding-agents.md +58 -0
  269. package/docs/guides/get-started/glossary.md +63 -0
  270. package/docs/guides/get-started/introduction.md +36 -0
  271. package/docs/guides/get-started/quick-start.md +236 -0
  272. package/docs/guides/get-started/tech-stack.md +82 -0
  273. package/docs/guides/get-started/upgrade.md +123 -0
  274. package/docs/guides/topic-detail/module-federation/application.md +116 -0
  275. package/docs/guides/topic-detail/module-federation/deploy.md +104 -0
  276. package/docs/guides/topic-detail/module-federation/i18n.md +670 -0
  277. package/docs/guides/topic-detail/module-federation/introduce.md +35 -0
  278. package/docs/guides/topic-detail/module-federation/ssr.md +118 -0
  279. package/docs/guides/topic-detail/module-federation/usage.md +219 -0
  280. package/docs/guides/troubleshooting/builder.md +110 -0
  281. package/docs/guides/troubleshooting/cli.md +35 -0
  282. package/docs/guides/troubleshooting/dependencies.md +119 -0
  283. package/docs/guides/troubleshooting/hmr.md +144 -0
  284. package/docs/guides/upgrade/config.md +963 -0
  285. package/docs/guides/upgrade/entry.md +463 -0
  286. package/docs/guides/upgrade/other.md +183 -0
  287. package/docs/guides/upgrade/overview.md +33 -0
  288. package/docs/guides/upgrade/tailwindcss.md +91 -0
  289. package/docs/guides/upgrade/web-server.md +109 -0
  290. package/docs/index.md +33 -0
  291. package/docs/llms.txt +285 -0
  292. package/docs/plugin/cli-plugins/api.md +573 -0
  293. package/docs/plugin/cli-plugins/life-cycle.md +2 -0
  294. package/docs/plugin/introduction.md +152 -0
  295. package/docs/plugin/official/cli-plugins/plugin-bff.md +5 -0
  296. package/docs/plugin/official/cli-plugins/plugin-ssg.md +5 -0
  297. package/docs/plugin/official/cli-plugins/plugin-styled-components.md +5 -0
  298. package/docs/plugin/official/cli-plugins.md +4 -0
  299. package/docs/plugin/plugin-system.md +238 -0
  300. package/docs/plugin/runtime-plugins/api.md +194 -0
  301. package/docs/plugin/runtime-plugins/life-cycle.md +2 -0
  302. package/docs/plugin/server-plugins/api.md +209 -0
  303. package/docs/plugin/server-plugins/life-cycle.md +13 -0
  304. package/docs/tutorials/examples/csr-auth.md +9 -0
  305. package/docs/tutorials/foundations/introduction.md +16 -0
  306. package/package.json +48 -15
@@ -0,0 +1,525 @@
1
+ # React Server Components (RSC)
2
+
3
+ React Server Components (RSC) is a new component type that allows components to be rendered in a server environment, bringing better performance and developer experience to modern web applications.
4
+
5
+ ## Core Advantages
6
+
7
+ - **Zero Client JavaScript**: Server Components code is not bundled to the client, significantly reducing client bundle size
8
+ - **Direct Access to Server Resources**: Can directly access server resources such as databases, file systems, and internal APIs without additional API layers
9
+ - **Better Performance**: Data fetching is closer to the data source, reducing client-side data waterfalls and improving first-screen load speed
10
+ - **Automatic Code Splitting**: Code splitting based on actual rendered data, not just routes, enabling more granular code optimization
11
+ - **Higher Cohesion**: Logic closely related to data, permissions, caching, etc., can remain in Server Components, improving component cohesion and reducing state lifting and cross-level passing
12
+
13
+ :::tip Prerequisite
14
+ Before starting, we recommend reading React's official [Server Components documentation](https://react.dev/reference/rsc/server-components) to get a basic understanding of Server Components.
15
+
16
+ :::
17
+
18
+ ## Quick Start
19
+
20
+ 1. **Ensure React and React DOM are upgraded to version 19** (recommended version 19.2.4 or above)
21
+
22
+ 2. **Install the `react-server-dom-rspack@0.0.2` dependency**
23
+
24
+ ```bash
25
+ npm install react-server-dom-rspack@0.0.2
26
+ ```
27
+
28
+ :::warning Notes
29
+
30
+ 1. Currently, [Server Functions](https://react.dev/reference/rsc/server-functions) are not supported in SPA projects
31
+ 2. Currently, when building with Rspack, the output chunks and bundle size are not yet optimal. We will further optimize this in the near future
32
+
33
+ :::
34
+
35
+ 3. Set [`server.rsc`](/configure/app/server/rsc.md) to `true`:
36
+
37
+ ```ts title="modern.config.ts"
38
+ import { defineConfig } from '@modern-js/app-tools';
39
+
40
+ export default defineConfig({
41
+ server: {
42
+ rsc: true,
43
+ },
44
+ });
45
+ ```
46
+
47
+ :::info Migrating from Legacy CSR Projects
48
+ If you have a CSR project that uses Modern.js Data Loaders, after enabling RSC, Data Loaders will execute on the server by default.
49
+ For detailed migration guide, please refer to [CSR Project Migration to RSC](#csr-project-migration-guide).
50
+
51
+ :::
52
+
53
+ ## Usage Guide
54
+
55
+ ### Default Behavior
56
+
57
+ By default, when RSC is enabled, all components in Modern.js are **Server Components** by default. Server Components allow you to fetch data on the server and render UI. When you need interactivity (such as event handling, state management) or use browser APIs, you can use the `"use client"` directive to mark components as **Client Components**.
58
+
59
+ ### Component Type Selection
60
+
61
+ #### When to Use Client Component
62
+
63
+ When a component needs the following features, you need to use the `"use client"` directive to mark it as a Client Component:
64
+
65
+ - **Interactivity**: Using [State](https://react.dev/learn/managing-state) and [event handlers](https://react.dev/learn/responding-to-events), such as `onClick`, `onChange`, `onSubmit`
66
+ - **Lifecycle**: Using [lifecycle](https://react.dev/learn/lifecycle-of-reactive-effects)-related hooks, such as `useEffect`, `useLayoutEffect`
67
+ - **Browser APIs**: Using browser APIs (such as `window`, `document`, `localStorage`, `navigator`, etc.)
68
+ - **Custom Hooks**: Using [custom hooks](https://react.dev/learn/reusing-logic-with-custom-hooks), especially those that depend on client-side features
69
+
70
+ #### When to Use Server Component
71
+
72
+ The following scenarios should use Server Components (default behavior, no additional marking required):
73
+
74
+ 1. **Accessing Server Resources**: Using APIs available only on the server (such as Node.js APIs, file systems, consul, RPC, etc.)
75
+ 2. **Data Fetching**: Fetching data on the server to optimize performance and reduce client requests
76
+ 3. **Security**: Accessing private environment variables or API keys, avoiding exposure to the client
77
+ 4. **Reducing Bundle Size**: Using large dependency libraries that don't need to be included in the client bundle
78
+ 5. **Static Content**: Rendering static or infrequently changing content
79
+
80
+ ### Client Boundary
81
+
82
+ Once a file is marked with `"use client"`, all other modules it imports (if they haven't been marked with `"use client"` yet) will also be considered client code and included in the client JavaScript bundle. This is the concept of **Client Boundary**.
83
+
84
+ :::tip Understanding Client Boundary
85
+ The `"use client"` directive creates a boundary: all code within the boundary will be bundled to the client. This means that even if the `Button` and `Tooltip` components don't have the `"use client"` directive themselves, they will become client code because they are imported by `InteractiveCard`.
86
+
87
+ :::
88
+
89
+ ```tsx title="components/InteractiveCard.tsx"
90
+ 'use client'; // <--- This is where the Client Boundary starts
91
+
92
+ import { useState } from 'react';
93
+ import Button from './Button'; // Button.tsx doesn't have "use client", but will be included in the client bundle
94
+ import Tooltip from './Tooltip'; // Tooltip.tsx also doesn't have "use client", and will be included
95
+
96
+ export default function InteractiveCard() {
97
+ const [isActive, setIsActive] = useState(false);
98
+
99
+ return (
100
+ <div onClick={() => setIsActive(!isActive)}>
101
+ <p>Click me!</p>
102
+ <Button />
103
+ <Tooltip text="This is a card" />
104
+ </div>
105
+ );
106
+ }
107
+ ```
108
+
109
+ ### How to Combine Both Component Types
110
+
111
+ Server Components and Client Components don't exist in isolation; they need to work together. Remember the following two rules:
112
+
113
+ #### Server Component Can Import Client Component
114
+
115
+ This is the most common pattern. Your page body is a Server Component responsible for data fetching and layout, while interactive parts are embedded as Client Components.
116
+
117
+ ```tsx title="routes/page.tsx"
118
+ // Server Component (default, no marking needed)
119
+ import CounterButton from './CounterButton'; // This is a Client Component
120
+
121
+ async function getPageData() {
122
+ // Fetch data on the server
123
+ const res = await fetch('https://api.example.com/data');
124
+ return res.json();
125
+ }
126
+
127
+ export default async function Page() {
128
+ const data = await getPageData();
129
+
130
+ return (
131
+ <div>
132
+ <h1>{data.title}</h1> {/* Server-side rendered */}
133
+ <p>This part is static.</p>
134
+ {/* Client Component can be seamlessly embedded in Server Component */}
135
+ <CounterButton />
136
+ </div>
137
+ );
138
+ }
139
+ ```
140
+
141
+ ```tsx title="routes/CounterButton.tsx"
142
+ 'use client'; // Client Component
143
+
144
+ import { useState } from 'react';
145
+
146
+ export default function CounterButton() {
147
+ const [count, setCount] = useState(0);
148
+ return <button onClick={() => setCount(c => c + 1)}>Count: {count}</button>;
149
+ }
150
+ ```
151
+
152
+ #### Client Component Cannot Directly Import Server Component
153
+
154
+ This may seem counterintuitive at first. The reason is that Server Component code doesn't exist on the client at all. When a Client Component renders in the browser, it cannot execute a function that only exists on the server.
155
+
156
+ However, there are two patterns to work around this limitation:
157
+
158
+ **1. Pass Server Component via `children` Prop**
159
+
160
+ You can pass Server Components as the `children` Prop to a Client Component. For example, an animated Tabs component where the tab switching logic is client-side, but the content of each tab might be static and fetched from the server.
161
+
162
+ ```tsx title="app/components/Tabs.tsx"
163
+ 'use client'; // Client Component
164
+
165
+ import React, { useState } from 'react';
166
+
167
+ interface TabsProps {
168
+ tabLabels: string[];
169
+ children: React.ReactNode;
170
+ }
171
+
172
+ export default function Tabs({ tabLabels, children }: TabsProps) {
173
+ const [activeTab, setActiveTab] = useState(0);
174
+
175
+ return (
176
+ <div>
177
+ <nav>
178
+ {tabLabels.map((label, index) => (
179
+ <button key={label} onClick={() => setActiveTab(index)}>
180
+ {label}
181
+ </button>
182
+ ))}
183
+ </nav>
184
+ {/* React.Children.toArray ensures only the active child component is rendered */}
185
+ <div>{React.Children.toArray(children)[activeTab]}</div>
186
+ </div>
187
+ );
188
+ }
189
+ ```
190
+
191
+ ```tsx title="app/dashboard/page.tsx"
192
+ // Server Component (default)
193
+ import Tabs from '../components/Tabs';
194
+ import Analytics from '../components/Analytics'; // Server Component
195
+ import UserSettings from '../components/UserSettings'; // Server Component
196
+
197
+ export default function DashboardPage() {
198
+ const labels = ['Analytics', 'Settings'];
199
+
200
+ return (
201
+ <main>
202
+ <h1>Dashboard</h1>
203
+ {/*
204
+ Here, Tabs is a Client Component (handling interactive logic),
205
+ but Analytics and UserSettings are Server Components rendered on the server,
206
+ passed to the Tabs component as children props.
207
+ This maintains interactivity while maximizing the advantages of server-side rendering.
208
+ */}
209
+ <Tabs tabLabels={labels}>
210
+ <Analytics />
211
+ <UserSettings />
212
+ </Tabs>
213
+ </main>
214
+ );
215
+ }
216
+ ```
217
+
218
+ Through this pattern, you can keep components on the server to the maximum extent while maintaining interactivity, achieving optimal performance. This is one of the most powerful composition patterns in RSC.
219
+
220
+ **2. Route Components Can Independently Choose Component Type**
221
+
222
+ Each level of route components (such as `layout.tsx`, `page.tsx`) can independently choose to be a Client Component or Server Component:
223
+
224
+ ```ts
225
+ -routes -
226
+ layout.tsx - // Can be a Client Component
227
+ page.tsx; // Can be a Server Component
228
+ ```
229
+
230
+ For example, if `layout.tsx` is a Client Component (requiring client-side interactivity), you can still set `page.tsx` as a Server Component (for data fetching and rendering). This approach provides great flexibility and allows non-RSC projects to gradually migrate to RSC projects.
231
+
232
+ ## Server Component and Data Loader
233
+
234
+ In RSC projects, you have two ways to fetch data: directly in Server Components, or using [Data Loader](/guides/basic-features/data/data-fetch.md). Both approaches have their advantages, and you can choose flexibly based on your scenario.
235
+
236
+ ### Comparison of Two Data Fetching Approaches
237
+
238
+ | Feature | Fetching in Server Component | Fetching in Data Loader |
239
+ | ----------------------- | ----------------------------------- | ---------------------------------------- |
240
+ | Avoid Request Waterfall | Requires manual optimization | ✅ Automatically parallel execution |
241
+ | Component Cohesion | ✅ Data and UI in the same place | Data logic separated into separate files |
242
+ | Maintainability | ✅ Easier to understand and maintain | Requires maintaining additional files |
243
+ | Type Safety | ✅ Natural type inference | Requires manual type management |
244
+
245
+ **Generally, we recommend fetching data in Server Components**, because waterfall requests have less performance impact on the server, and this approach makes components more cohesive, with data fetching logic and UI rendering in the same place, making it easier to understand and maintain. However, if your page has multiple independent data sources and you want to completely avoid request waterfall issues, Data Loader's parallel execution feature will be more advantageous.
246
+
247
+ ### Data Loader Execution Environment in RSC Projects
248
+
249
+ In RSC projects, the execution environment of Data Loaders is related to file naming:
250
+
251
+ - **`*.data.ts`**: Executes only on the **server**, and data can be consumed by both Client Components and Server Components
252
+ - **`*.data.client.ts`**: Executes only on the **client**
253
+
254
+ ```bash
255
+ .
256
+ └── routes
257
+ └── user
258
+ ├── page.tsx # Route component (can be Server or Client Component)
259
+ ├── page.data.ts # Executes on server, data can be consumed by any component
260
+ └── page.data.client.ts # Executes on client
261
+ ```
262
+
263
+ In Modern.js RSC projects, Server Components can receive data returned by Data Loaders through the `loaderData` prop:
264
+
265
+ ```tsx title="routes/user/page.tsx"
266
+ // Server Component receives loaderData through props
267
+ export default function UserPage({ loaderData }: { loaderData: { name: string } }) {
268
+ return <div>Welcome, {loaderData.name}</div>;
269
+ }
270
+ ```
271
+
272
+ ```tsx title="routes/user/page.tsx"
273
+ 'use client';
274
+ // Client Component can also receive loaderData through props
275
+ export default function UserPage({ loaderData }: { loaderData: { name: string } }) {
276
+ return <div>Welcome, {loaderData.name}</div>;
277
+ }
278
+ ```
279
+
280
+ ### Data Loader Returning Server Component
281
+
282
+ In Modern.js RSC projects, Data Loaders have a powerful feature: **they can return Server Components**. This is very helpful for gradual migration, allowing you to render server-generated content in Client Components.
283
+
284
+ ```tsx title="routes/user/layout.data.tsx"
285
+ // Server Component defined in data loader file
286
+ function UserProfile() {
287
+ return <div>User Profile (Server Rendered)</div>;
288
+ }
289
+
290
+ export const loader = async () => {
291
+ const userData = await fetchUserData();
292
+
293
+ return {
294
+ user: userData,
295
+ // Return a Server Component as part of the data
296
+ ProfileComponent: <UserProfile />,
297
+ };
298
+ };
299
+ ```
300
+
301
+ ```tsx title="routes/user/layout.tsx"
302
+ 'use client';
303
+ import { Outlet } from '@modern-js/runtime/router';
304
+
305
+ export default function UserLayout({
306
+ loaderData,
307
+ }: {
308
+ loaderData: { user: any; ProfileComponent: React.ReactNode };
309
+ }) {
310
+ const { user, ProfileComponent } = loaderData;
311
+
312
+ return (
313
+ <div>
314
+ {/* Directly render Server Component in Client Component */}
315
+ {ProfileComponent}
316
+ <div>User: {user.name}</div>
317
+ <Outlet />
318
+ </div>
319
+ );
320
+ }
321
+ ```
322
+
323
+ ## CSR Project Migration Guide
324
+
325
+ Modern.js's RSC capability supports both SSR and CSR projects. For existing CSR projects, if you want to gradually migrate to RSC, we recommend following these steps:
326
+
327
+ 1. **Enable RSC Configuration**
328
+
329
+ ```ts title="modern.config.ts"
330
+ import { defineConfig } from '@modern-js/app-tools';
331
+
332
+ export default defineConfig({
333
+ server: {
334
+ rsc: true,
335
+ },
336
+ });
337
+ ```
338
+
339
+ 2. **Mark All Route Components with `'use client'`**
340
+
341
+ This ensures that existing components maintain their behavior and continue to run as Client Components.
342
+
343
+ ```tsx title="routes/page.tsx"
344
+ 'use client';
345
+
346
+ export default function Page() {
347
+ // Existing client-side logic remains unchanged
348
+ }
349
+ ```
350
+
351
+ 3. **Rename All `*.data.ts` to `*.data.client.ts`**
352
+
353
+ Since `*.data.ts` in RSC projects executes on the server by default, to maintain consistency with CSR project behavior (Data Loaders execute on the client), you need to rename the files.
354
+
355
+ ```bash
356
+ # Before renaming
357
+ routes/user/page.data.ts
358
+
359
+ # After renaming
360
+ routes/user/page.data.client.ts
361
+ ```
362
+
363
+ After completing these steps, you can gradually migrate components to Server Components and enjoy the performance benefits of RSC.
364
+
365
+ ## Notes
366
+
367
+ ### Projects Using Streaming SSR
368
+
369
+ If you're using both Streaming SSR and RSC, in React 19 you need to use `use` instead of the `Await` component:
370
+
371
+ ```typescript
372
+ function NonCriticalUI({ p }: { p: Promise<string> }) {
373
+ let value = React.use(p);
374
+ return <h3>Non critical value {value}</h3>;
375
+ }
376
+
377
+ <React.Suspense fallback={<div>Loading...</div>}>
378
+ <NonCriticalUI p={nonCriticalData} />
379
+ </React.Suspense>;
380
+ ```
381
+
382
+ ## Best Practices
383
+
384
+ ### Data Fetching
385
+
386
+ 1. Whether it's an SSR or RSC project, it's recommended to use the `cache` function provided by Modern.js for data fetching logic executed on the server by default. This ensures that for each server-side render, no matter how many times the function is called, it will only execute once.
387
+
388
+ > This is also the recommended usage by React.js, which provides the [cache](https://react.dev/reference/react/cache) function. Modern.js's [cache](/guides/basic-features/data/data-cache.md) can be considered a superset of it.
389
+
390
+ ```typescript
391
+ import { cache } from '@modern-js/runtime/cache';
392
+
393
+ const getCriticalCached = cache(getCritical);
394
+ ```
395
+
396
+ - Based on using the `cache` function, you no longer need to manage server-side state through `props`, `context`, etc. We recommend fetching data in the nearest Server Component where it's needed. With the `cache` function, even if the same function is called multiple times, this makes project state management, business logic, and performance optimization simpler.
397
+
398
+ ```typescript
399
+ // layout.tsx
400
+ export default async function Layout() {
401
+ const criticalData = await getCriticalCached();
402
+ }
403
+
404
+ export default async function Page() {
405
+ const criticalData = await getCriticalCached();
406
+ }
407
+ ```
408
+
409
+ ### Optimal Performance
410
+
411
+ To leverage the advantages of RSC or Streaming SSR, we need to make as many components as possible flow. A core principle is to make the area wrapped by Suspense as small as possible (this is also one of the reasons we recommend using the `cache` function).
412
+
413
+ For Server Components that directly consume data, we recommend wrapping them with Suspense at a higher level:
414
+
415
+ > In this scenario, Server Components are often asynchronous. There's another case where Server Components are synchronous and data is consumed by Client Components, described below.
416
+
417
+ ```javascript
418
+ // profile/components/PostsList.tsx
419
+ export default async function PostsList() {
420
+ const posts = await getUserPosts();
421
+
422
+ return (
423
+ <ul>
424
+ {posts.map(post => (
425
+ <li key={post.id}>{post.title}</li>
426
+ ))}
427
+ </ul>
428
+ );
429
+ }
430
+ ```
431
+
432
+ ```javascript
433
+ // profile/page.tsx
434
+ import { Suspense } from 'react';
435
+ import UserInfo from './components/UserInfo';
436
+ import PostsList from './components/PostsList';
437
+ import PostsSkeleton from './components/PostsSkeleton';
438
+
439
+ export default function ProfilePage() {
440
+ return (
441
+ <div>
442
+ <UserInfo />
443
+
444
+ <hr />
445
+
446
+ {/*
447
+ We wrap the slow PostsList in Suspense.
448
+ While PostsList is fetching data, users will see PostsSkeleton.
449
+ Once PostsList data is ready, it will automatically replace the skeleton.
450
+ */}
451
+ <Suspense fallback={<PostsSkeleton />}>
452
+ <PostsList posts={postsPromise} />
453
+ </Suspense>
454
+ </div>
455
+ );
456
+ }
457
+ ```
458
+
459
+ There's another scenario where data is consumed in Client Components. In this case, we should avoid using `await` in Server Components to avoid blocking rendering:
460
+
461
+ ```javascript
462
+ // profile/components/PostsList.tsx
463
+ 'use client';
464
+ export default function PostsList({ postsPromise }) {
465
+ const posts = use(postsPromise);
466
+
467
+ return (
468
+ <ul>
469
+ {posts.map(post => (
470
+ <li key={post.id}>{post.title}</li>
471
+ ))}
472
+ </ul>
473
+ );
474
+ }
475
+ ```
476
+
477
+ ```javascript
478
+ // profile/page.tsx
479
+ import { Suspense } from 'react';
480
+ import UserInfo from './components/UserInfo';
481
+ import PostsList from './components/PostsList'; // Now a Client Component
482
+ import PostsSkeleton from './components/PostsSkeleton';
483
+ import { getUserPosts } from '../lib/data'; // Import data fetching function
484
+
485
+ // Note: This component is not async
486
+ export default function ProfilePage() {
487
+ // 1. Call the data fetching function on the server, but don't await it
488
+ // This immediately returns a Promise
489
+ const postsPromise = getUserPosts();
490
+
491
+ return (
492
+ <div>
493
+ <UserInfo />
494
+
495
+ <hr />
496
+
497
+ {/* 2. Suspense boundary is still required. It will catch
498
+ the Promise thrown by the `use` hook inside PostsList */}
499
+ <Suspense fallback={<PostsSkeleton />}>
500
+ {/* 3. Pass the Promise object itself as a prop to the client component */}
501
+ <PostsList postsPromise={postsPromise} />
502
+ </Suspense>
503
+ </div>
504
+ );
505
+ }
506
+ ```
507
+
508
+ ### Helmet
509
+
510
+ When using React 19, you no longer need to use Helmet. We recommend directly using the [components](https://react.dev/reference/react-dom/components) provided by React.
511
+
512
+
513
+ ## Common Issues
514
+
515
+ ### `This entry point is not yet supported outside of experimental channels`
516
+
517
+ The project's bundle has introduced a non-19 React version, commonly seen in monorepos. Please ensure all dependencies use React 19.
518
+
519
+ ## Related Links
520
+
521
+ - [React Server Components Official Documentation](https://react.dev/reference/rsc/server-components)
522
+ - [React Server Functions Documentation](https://react.dev/reference/rsc/server-functions)
523
+ - [Data Cache](/guides/basic-features/data/data-cache.md)
524
+ - [Server-Side Rendering (SSR)](/guides/basic-features/render/ssr.md)
525
+ - [Streaming SSR](/guides/basic-features/render/streaming-ssr.md)