@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,498 @@
1
+ # Routing
2
+
3
+ Modern.js routing is based on [React Router v7](https://reactrouter.com/en/main), offering file convention-based routing capabilities and supporting the industry-popular **nested routing** pattern. When an entry is recognized as [conventional routing](/guides/concept/entries.md#conventional-routing), Modern.js automatically generates the corresponding routing structure based on the file system.
4
+
5
+ :::note
6
+
7
+ The routing mentioned in this section all refers to conventional routing.
8
+
9
+ :::
10
+
11
+ ## What is Nested Routing
12
+
13
+ Nested routing is a pattern that couples URL segments with the component hierarchy and data. Typically, URL segments determine:
14
+
15
+ - The layouts to render on the page
16
+ - The data dependencies of those layouts
17
+
18
+ Therefore, when using nested routing, the page's routing and UI structure are in correspondence. We will introduce this routing pattern in detail.
19
+
20
+ ```bash
21
+ /user/johnny/profile /user/johnny/posts
22
+ +------------------+ +-----------------+
23
+ | User | | User |
24
+ | +--------------+ | | +-------------+ |
25
+ | | Profile | | +------------> | | Posts | |
26
+ | | | | | | | |
27
+ | +--------------+ | | +-------------+ |
28
+ +------------------+ +-----------------+
29
+ ```
30
+
31
+ ## Routing File Conventions
32
+
33
+ In the `routes/` directory, subdirectory names are mapped to route URLs. Modern.js has two file conventions: `layout.tsx` and `page.tsx`. These files determine the layout hierarchy of the application:
34
+
35
+ - `page.tsx`: This is the content component. When this file exists in a directory, the corresponding route URL is accessible.
36
+ - `layout.tsx`: This is the layout component and controls the layout of all sub-routes in its directory by using `<Outlet>` to represent child components.
37
+
38
+ :::tip
39
+
40
+ `.ts`, `.js`, `.jsx`, or `.tsx` file extensions can be used for the above convention files.
41
+
42
+ :::
43
+
44
+ ### Page
45
+
46
+ The `<Page>` component refers to all `page.tsx` files in the `routes/` directory and is the leaf component for all routes. All routes should end with a `<Page>` component except for wildcard routes.
47
+
48
+ ```tsx title=routes/page.tsx
49
+ export default () => {
50
+ return <div>Hello world</div>;
51
+ };
52
+ ```
53
+
54
+ When the application has the following directory structure:
55
+
56
+ ```bash
57
+ .
58
+ └── routes
59
+ ├── page.tsx
60
+ └── user
61
+ └── page.tsx
62
+ ```
63
+
64
+ The following two routes will be produced:
65
+
66
+ - `/`
67
+ - `/user`
68
+
69
+ ### Layout
70
+
71
+ The `<Layout>` component refers to all `layout.tsx` files in the `routes/` directory. These represent the layout of their respective route segments, using `<Outlet>` for child components.
72
+
73
+ ```tsx title=routes/layout.tsx
74
+ import { Link, Outlet, useLoaderData } from '@modern-js/runtime/router';
75
+
76
+ export default () => {
77
+ return (
78
+ <>
79
+ <Outlet></Outlet>
80
+ </>
81
+ );
82
+ };
83
+ ```
84
+
85
+ :::note
86
+
87
+ `<Outlet>` is an API provided by React Router v7. For more details, see [Outlet](https://reactrouter.com/en/main/components/outlet#outlet).
88
+
89
+ :::
90
+
91
+ Under different directory structures, the components represented by `<Outlet>` are also different. To illustrate the relationship between `<Layout>` and `<Outlet>`, let's consider the following directory structure:
92
+
93
+ ```bash
94
+ .
95
+ └── routes
96
+ ├── blog
97
+ │ └── page.tsx
98
+ ├── layout.tsx
99
+ ├── page.tsx
100
+ └── user
101
+ ├── layout.tsx
102
+ └── page.tsx
103
+ ```
104
+
105
+ 1. When the route is `/`, the `<Outlet>` in `routes/layout.tsx` represents the component exported from `routes/page.tsx`. The UI structure of the route is:
106
+
107
+ ```tsx
108
+ <Layout>
109
+ <Page />
110
+ </Layout>
111
+ ```
112
+
113
+ 2. When the route is `/blog`, the `<Outlet>` in `routes/layout.tsx` represents the component exported from `routes/blog/page.tsx`. The UI structure of the route is:
114
+
115
+ ```tsx
116
+ <Layout>
117
+ <BlogPage />
118
+ </Layout>
119
+ ```
120
+
121
+ 3. When the route is `/user`, the `<Outlet>` in `routes/layout.tsx` represents the component exported from `routes/user/layout.tsx`. The `<Outlet>` in `routes/user/layout.tsx` represents the component exported from `routes/user/page.tsx`. The UI structure of the route is:
122
+
123
+ ```tsx
124
+ <Layout>
125
+ <UserLayout>
126
+ <UserPage />
127
+ </UserLayout>
128
+ </Layout>
129
+ ```
130
+
131
+ In summary, if there is a `layout.tsx` in the sub-route's directory, the `<Outlet>` in the parent `layout.tsx` corresponds to the `layout.tsx` in the sub-route's directory. Otherwise, it corresponds to the `page.tsx` in the sub-route's directory.
132
+
133
+ ## Dynamic Routes
134
+
135
+ Files and directories named with `[]` are turned into dynamic routes. For instance, consider the following directory structure:
136
+
137
+ ```bash
138
+ .
139
+ └── routes
140
+ ├── [id]
141
+ │ └── page.tsx
142
+ ├── blog
143
+ │ └── page.tsx
144
+ └── page.tsx
145
+ ```
146
+
147
+ The `routes/[id]/page.tsx` file will be converted to the `/:id` route. Apart from the `/blog` route that can be exactly matched, all `/xxx` paths will match this route.
148
+
149
+ In the component, you can use [useParams](/apis/app/runtime/router/router.md#useparams) to get parameters named accordingly.
150
+
151
+ ```tsx
152
+ import { useParams } from '@modern-js/runtime/router';
153
+
154
+ function Blog() {
155
+ const { id } = useParams();
156
+ return <div>current blog ID is: {id}</div>;
157
+ }
158
+ export default Blog;
159
+ ```
160
+
161
+ ## Optional Dynamic Routes
162
+
163
+ Files and directories named with `[$]` are turned into optional dynamic routes. For example, the following directory structure:
164
+
165
+ ```bash
166
+ .
167
+ └── routes
168
+ ├── blog
169
+ │ └── [id$]
170
+ │ └── page.tsx
171
+ └── page.tsx
172
+ ```
173
+
174
+ The `routes/blog/[id$]/page.tsx` file will be converted to the `/blog/:id?` route. All routes under `/blog` will match this route, and the `id` parameter is optional. This route can be used to distinguish between **create** and **edit** actions.
175
+
176
+ ```tsx
177
+ import { useParams } from '@modern-js/runtime/router';
178
+
179
+ function Blog() {
180
+ const { id } = useParams();
181
+ if (id) {
182
+ return <div>current blog ID is: {id}</div>;
183
+ }
184
+
185
+ return <div>create new blog</div>;
186
+ }
187
+ export default Blog;
188
+ ```
189
+
190
+ ## Wildcard Routes
191
+
192
+ If there is a `$.tsx` file in a subdirectory, it acts as a wildcard route component and will be rendered when no other routes match.
193
+
194
+ :::note
195
+ `$.tsx` can be thought of as a special `<Page>` component. If no routes match, `$.tsx` will be rendered as a child component of the `<Layout>`.
196
+ :::
197
+
198
+ :::warning
199
+ If there is no `<Layout>` component in the current directory, `$.tsx` will not have any effect.
200
+ :::
201
+
202
+ For example, consider the following directory structure:
203
+
204
+ ```bash
205
+ .
206
+ └── routes
207
+ ├── blog
208
+ │ ├── $.tsx
209
+ │ └── layout.tsx
210
+ ├── layout.tsx
211
+ └── page.tsx
212
+ ```
213
+
214
+ When you visit `/blog/a` and no routes match, the page will render the `routes/blog/$.tsx` component. The UI structure of the route is:
215
+
216
+ ```tsx
217
+ <RootLayout>
218
+ <BlogLayout>
219
+ <$></$>
220
+ </BlogLayout>
221
+ </RootLayout>
222
+ ```
223
+
224
+ If you want `/blog` to match the `blog/$.tsx` file as well, you need to remove the `blog/layout.tsx` file from the same directory and ensure there are no other sub-routes under `blog`.
225
+
226
+ Similarly, you can use [useParams](/apis/app/runtime/router/router.md#useparams) to capture the remaining part of the URL in the `$.tsx` component.
227
+
228
+ ```ts title="$.tsx"
229
+ import { useParams } from '@modern-js/runtime/router';
230
+
231
+ function Blog() {
232
+ // When the path is `/blog/aaa/bbb`
233
+ const params = useParams();
234
+ console.log(params); // ---> { '*': 'aaa/bbb' }
235
+
236
+ return <div>current blog URL is {params['*']}</div>;
237
+ }
238
+ export default Blog;
239
+ ```
240
+
241
+ ### Custom 404 Page
242
+
243
+ Wildcard routes can be added to any subdirectory in the `routes/` directory. A common use case is to customize a 404 page at any level using a `$.tsx` file.
244
+
245
+ For instance, if you want to show a 404 page for all unmatched routes, you can add a `routes/$.tsx` file:
246
+
247
+ ```bash
248
+ .
249
+ └── routes
250
+ ├── $.tsx
251
+ ├── blog
252
+ │ └── [id$]
253
+ │ └── page.tsx
254
+ ├── layout.tsx
255
+ └── page.tsx
256
+ ```
257
+
258
+ ```tsx
259
+ function Page404() {
260
+ return <div>404 Not Found</div>;
261
+ }
262
+ export default Page404;
263
+ ```
264
+
265
+ At this point, when accessing routes other than `/` or `/blog/*`, they will match the `routes/$.tsx` component and display a 404 page.
266
+
267
+ ## Route Handle Configuration
268
+
269
+ In some scenarios, each route might have its own data which the application needs to access in other components. A common example is retrieving breadcrumb information for the matched route.
270
+
271
+ Modern.js provides a convention where each `Layout`, `$`, or `Page` file can define its own `config` file such as `page.config.ts`. In this file, we conventionally export a named export `handle`, in which you can define any properties:
272
+
273
+ ```ts title="routes/page.config.ts"
274
+ export const handle = {
275
+ breadcrumbName: 'profile',
276
+ };
277
+ ```
278
+
279
+ These defined properties can be accessed using the [`useMatches`](https://reactrouter.com/en/main/hooks/use-matches) hook.
280
+
281
+ ```ts title="routes/layout.ts"
282
+ export default () => {
283
+ const matches = useMatches();
284
+ const breadcrumbs = matches.map(
285
+ matchedRoute => matchedRoute?.handle?.breadcrumbName,
286
+ );
287
+ return <Breadcrumb names={breadcrumbs}></Breadcrumb>;
288
+ };
289
+ ```
290
+
291
+ ## Pathless Layouts
292
+
293
+ When a directory name starts with `__`, the corresponding directory name will not be converted into an actual route path, for example:
294
+
295
+ ```bash
296
+ .
297
+ └── routes
298
+ ├── __auth
299
+ │ ├── layout.tsx
300
+ │ ├── login
301
+ │ │ └── page.tsx
302
+ │ └── sign
303
+ │ └── page.tsx
304
+ ├── layout.tsx
305
+ └── page.tsx
306
+ ```
307
+
308
+ Modern.js will generate `/login` and `/sign` routes, and the `__auth/layout.tsx` component will serve as the layout for `login/page.tsx` and `sign/page.tsx`, but `__auth` will not appear as a path segment in the URL.
309
+
310
+ This feature is useful when you need to create independent layouts or categorize routes without adding additional path segments.
311
+
312
+ ## Pathless File Segments
313
+
314
+ In some cases, a project may need complex routes that do not have independent UI layouts. Creating these routes as regular directories can lead to deeply nested directories.
315
+
316
+ Modern.js supports replacing directory names with `.` to divide route segments. For example, to create a route like `/user/profile/2022/edit`, you can create the following file:
317
+
318
+ ```bash
319
+ └── routes
320
+ ├── user.profile.[id].edit
321
+ │ └── page.tsx
322
+ ├── layout.tsx
323
+ └── page.tsx
324
+ ```
325
+
326
+ When accessed, the resulting route will have the following UI structure:
327
+
328
+ ```tsx
329
+ <RootLayout>
330
+ {/* routes/user.profile.[id].edit/page.tsx */}
331
+ <UserProfileEdit />
332
+ </RootLayout>
333
+ ```
334
+
335
+ ## Route Redirections
336
+
337
+ In some applications, you may need to redirect to another route based on user identity or other data conditions. In Modern.js, you can use a [`Data Loader`](/guides/basic-features/data/data-fetch.md) file to fetch data or use traditional React components with `useEffect`.
338
+
339
+ ### Redirecting in Data Loader
340
+
341
+ Create a `page.data.ts` file in the same directory as `page.tsx`. This file is the Data Loader for that route. In the Data Loader, you can call the `redirect` API to perform route redirections.
342
+
343
+ ```ts title="routes/user/page.data.ts"
344
+ import { redirect } from '@modern-js/runtime/router';
345
+
346
+ export const loader = async () => {
347
+ const user = await getUser();
348
+ if (!user) {
349
+ return redirect('/login');
350
+ }
351
+ return null;
352
+ };
353
+ ```
354
+
355
+ ### Redirecting in a Component
356
+
357
+ To perform a redirection within a component, use the `useNavigate` hook as shown below:
358
+
359
+ ```ts title="routes/user/page.ts"
360
+ import { useNavigate } from '@modern-js/runtime/router';
361
+ import { useEffect } from 'react';
362
+
363
+ export default () => {
364
+ const navigate = useNavigate();
365
+ useEffect(() => {
366
+ getUser().then(user => {
367
+ if (!user) {
368
+ navigate('/login');
369
+ }
370
+ });
371
+ });
372
+
373
+ return <div>Hello World</div>;
374
+ };
375
+ ```
376
+
377
+ ## Error Handling
378
+
379
+ In each directory under `routes/`, developers can define an `error.tsx` file that exports an `<ErrorBoundary>` component. When this component is present, rendering errors in the route directory will be caught by the `ErrorBoundary` component.
380
+
381
+ `<ErrorBoundary>` can return the UI view when an error occurs. If the current level does not declare an `<ErrorBoundary>` component, errors will bubble up to higher-level components until they are caught or thrown. Additionally, when an error occurs within a component, it only affects the route component and its children, leaving the state and view of other components unaffected and interactive.
382
+
383
+
384
+ In the `<ErrorBoundary>` component, you can use [useRouteError](/apis/app/runtime/router/router.md#userouteerror) to obtain specific error information:
385
+
386
+ ```tsx
387
+ import { useRouteError } from '@modern-js/runtime/router';
388
+
389
+ const ErrorBoundary = () => {
390
+ const error = useRouteError();
391
+ return (
392
+ <div>
393
+ <h1>{error.status}</h1>
394
+ <h2>{error.message}</h2>
395
+ </div>
396
+ );
397
+ };
398
+ export default ErrorBoundary;
399
+ ```
400
+
401
+ ## Loading (Experimental)
402
+
403
+ :::info Experimental
404
+ This feature is currently experimental, and its API may change in the future.
405
+ :::
406
+
407
+ In conventional routing, Modern.js automatically splits routes into chunks (each route loads as a separate JS chunk). When users visit a specific route, the corresponding chunk is automatically loaded, effectively reducing the first-screen load time. However, this can lead to a white screen if the route's chunk is not yet loaded.
408
+
409
+ Modern.js supports solving this issue with a `loading.tsx` file. Each directory under `routes/` can create a `loading.tsx` file that exports a `<Loading>` component.
410
+
411
+ :::warning
412
+ If there is no `<Layout>` component in the current directory, `loading.tsx` will not have any effect. To ensure a good user experience, Modern.js recommends adding a root Loading component to each application.
413
+ :::
414
+
415
+ When both this component and a `layout` component exist in the route directory, all child routes under this level will first display the UI from the exported `<Loading>` component until the corresponding JS chunk is fully loaded. For example, with the following file structure:
416
+
417
+ ```bash
418
+ .
419
+ └── routes
420
+ ├── blog
421
+ │ ├── [id]
422
+ │ │ └── page.tsx
423
+ │ └── page.tsx
424
+ ├── layout.tsx
425
+ ├── loading.tsx
426
+ └── page.tsx
427
+ ```
428
+
429
+ When defining a `loading.tsx`, if the route transitions from `/` to `/blog` or from `/blog` to `/blog/123`, and the JS chunk for the route is not yet loaded, the UI from the `<Loading>` component will be displayed first. This results in the following UI structure:
430
+
431
+ ```tsx title=When the route is "/"
432
+ <Layout>
433
+ <Suspense fallback={<Loading />}>
434
+ <Page />
435
+ </Suspense>
436
+ </Layout>
437
+ ```
438
+
439
+ ```tsx title=When the route is "/blog"
440
+ <Layout>
441
+ <Suspense fallback={<Loading />}>
442
+ <BlogPage />
443
+ </Suspense>
444
+ </Layout>
445
+ ```
446
+
447
+ ```tsx title=When the route is "/blog/123"
448
+ <Layout>
449
+ <Suspense fallback={<Loading />}>
450
+ <BlogIdPage />
451
+ </Suspense>
452
+ </Layout>
453
+ ```
454
+
455
+ ## Prefetching
456
+
457
+ Most white screens during route transitions can be optimized by defining a `<Loading>` component. Modern.js also supports preloading static resources and data with the `prefetch` attribute on `<Link>` components.
458
+
459
+ For applications with higher performance requirements, prefetching can further enhance the user experience by reducing the time spent displaying the `<Loading>` component:
460
+
461
+ ```tsx
462
+ <Link prefetch="intent" to="page">
463
+ ```
464
+
465
+ :::tip
466
+
467
+ Data preloading currently only preloads data returned by the [Data Loader](/guides/basic-features/data/data-fetch.md) in SSR projects.
468
+
469
+ :::
470
+
471
+ The `prefetch` attribute has three optional values:
472
+
473
+ - `none`: The default value. No prefetching, no additional behavior.
474
+ - `intent`: This is the recommended value for most scenarios. When you hover over the Link, it will automatically start loading the corresponding chunk and the data defined in the Data Loader. If the mouse moves away, the loading is automatically canceled. In our tests, even quick clicks can reduce load time by approximately 200ms.
475
+ - `render`: When the `<Link>` component is rendered, it begins loading the corresponding chunk and data defined in the Data Loader.
476
+
477
+ :::details Difference Between "render" and Not Using Route Splitting
478
+
479
+ - `render` allows you to control the timing of route splitting, triggering only when the `<Link>` component enters the viewport. You can control the loading timing of the split by adjusting the rendering position of the `<Link>` component.
480
+ - `render` loads static resources only during idle times, thus not occupying the loading time of critical modules.
481
+ - Besides preloading route splits, `render` will also initiate data prefetching in SSR projects.
482
+
483
+ :::
484
+
485
+
486
+ ## FAQ
487
+
488
+ 1. Why is there `@modern-js/runtime/router` to re-export React Router API?
489
+
490
+ Notice that all the code examples in the documentation uses APIs exported from the `@modern-js/runtime/router` package instead of directly using the API exported from the React Router package. So, what is the difference?
491
+
492
+ The API exported from `@modern-js/runtime/router` is the same as the API from the React Router package. If you encounter issues while using an API, check the React Router documentation and issues first.
493
+
494
+ Additionally, when using conventional routing, make sure to use the API from `@modern-js/runtime/router` instead of directly using the React Router API. Modern.js internally installs React Router, and using the React Router API directly in your application may result in two versions of React Router being present, causing unexpected behavior.
495
+
496
+ :::note
497
+ If you must directly use the React Router package's API (e.g., route behavior wrapped in a unified npm package), you can set [`source.alias`](/configure/app/source/alias.md) to point `react-router` and `react-router-dom` to the project's dependencies, avoiding the issue of two versions of React Router.
498
+ :::
@@ -0,0 +1,120 @@
1
+ # Import JSON Files
2
+
3
+ Modern.js supports import JSON files in code by default. You can use Rsbuild plugins to support importing [YAML](https://yaml.org/) and [Toml](https://toml.io/en/) files and converting them to JSON format.
4
+
5
+ ## JSON file
6
+
7
+ You can import JSON files directly in JavaScript files.
8
+
9
+ ### Example
10
+
11
+ ```json title="example.json"
12
+ {
13
+ "name": "foo",
14
+ "items": [1, 2]
15
+ }
16
+ ```
17
+
18
+ ```js title="index.js"
19
+ import example from './example.json';
20
+
21
+ console.log(example.name); // 'foo';
22
+ console.log(example.items); // [1, 2];
23
+ ```
24
+
25
+ ### Named Import
26
+
27
+ Modern.js does not support importing JSON files via named import yet:
28
+
29
+ ```js
30
+ import { name } from './example.json';
31
+ ```
32
+
33
+ ## YAML file
34
+
35
+ YAML is a data serialization language commonly used for writing configuration files.
36
+
37
+ You can configure the [YAML plugin](https://github.com/rstackjs/rsbuild-plugin-yaml) in `modern.config.ts` to support importing `.yaml` or `.yml` files, they will be automatically converted to JSON format.
38
+
39
+ ```ts
40
+ import { defineConfig } from '@modern-js/app-tools';
41
+ import { pluginYaml } from '@rsbuild/plugin-yaml';
42
+
43
+ export default defineConfig({
44
+ plugins: [pluginYaml()],
45
+ });
46
+ ```
47
+
48
+ ### Example
49
+
50
+ ```yaml title="example.yaml"
51
+ ---
52
+ hello: world
53
+ foo:
54
+ bar: baz
55
+ ```
56
+
57
+ ```js
58
+ import example from './example.yaml';
59
+
60
+ console.log(example.hello); // 'world';
61
+ console.log(example.foo); // { bar: 'baz' };
62
+ ```
63
+
64
+ ### Add type declaration
65
+
66
+ When you import a YAML file in your TypeScript code, please create a `src/global.d.ts` file in your project and add the corresponding type declaration:
67
+
68
+ ```ts title="src/global.d.ts"
69
+ declare module '*.yaml' {
70
+ const content: Record<string, any>;
71
+ export default content;
72
+ }
73
+
74
+ declare module '*.yml' {
75
+ const content: Record<string, any>;
76
+ export default content;
77
+ }
78
+ ```
79
+
80
+ ## Toml file
81
+
82
+ Toml is a semantically explicit, easy-to-read configuration file format.
83
+
84
+ You can configure the [TOML plugin](https://github.com/rstackjs/rsbuild-plugin-toml) in `modern.config.ts` to support importing `.toml` files, it will be automatically converted to JSON format.
85
+
86
+ ```ts
87
+ import { defineConfig } from '@modern-js/app-tools';
88
+ import { pluginToml } from '@rsbuild/plugin-toml';
89
+
90
+ export default defineConfig({
91
+ plugins: [pluginToml()],
92
+ });
93
+ ```
94
+
95
+ ### Example
96
+
97
+ ```toml title="example.toml"
98
+ hello = "world"
99
+
100
+ [foo]
101
+ bar = "baz"
102
+ ```
103
+
104
+ ```js
105
+ import example from './example.toml';
106
+
107
+ console.log(example.hello); // 'world';
108
+ console.log(example.foo); // { bar: 'baz' };
109
+ ```
110
+
111
+ ### Add type declaration
112
+
113
+ When you import Toml files in TypeScript code, please create a `src/global.d.ts` file in your project and add the corresponding type declarations:
114
+
115
+ ```ts title="src/global.d.ts"
116
+ declare module '*.toml' {
117
+ const content: Record<string, any>;
118
+ export default content;
119
+ }
120
+ ```