You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CHANGELOG.md
+176Lines changed: 176 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -407,6 +407,182 @@ In order to use your build-time loader data during pre-rendering, we now also ex
407
407
-`@react-router/dev` - Add unstable support for splitting route modules in framework mode via `future.unstable_splitRouteModules` ([#11871](https://github.com/remix-run/react-router/pull/11871))
408
408
-`@react-router/dev` - Add `future.unstable_viteEnvironmentApi` flag to enable experimental Vite Environment API support ([#12936](https://github.com/remix-run/react-router/pull/12936))
409
409
410
+
#### Split Route Modules (unstable)
411
+
412
+
> ⚠️ This feature is currently [unstable](https://reactrouter.com/community/api-development-strategy#unstable-flags), enabled by the `future.unstable_splitRouteModules` flag. We’d love any interested users to play with it locally and provide feedback, but we do not recommend using it in production yet.
413
+
>
414
+
> If you do choose to adopt this flag in production, please ensure you do sufficient testing against your production build to ensure that the optimization is working as expected.
415
+
416
+
One of the conveniences of the [Route Module API](https://reactrouter.com/start/framework/route-module) is that everything a route needs is in a single file. Unfortunately this comes with a performance cost in some cases when using the `clientLoader`, `clientAction`, and `HydrateFallback` APIs.
In this example we have a minimal `clientLoader` export that makes a basic fetch call, whereas the default component export is much larger. This is a problem for performance because it means that if we want to navigate to this route client-side, the entire route module must be downloaded before the client loader can start running.
435
+
436
+
To visualize this as a timeline:
437
+
438
+
<docs-info>In the following timeline diagrams, different characters are used within the Route Module bars to denote the different Route Module APIs being exported.</docs-info>
439
+
440
+
```
441
+
Get Route Module: |--=======|
442
+
Run clientLoader: |-----|
443
+
Render: |-|
444
+
```
445
+
446
+
Instead, we want to optimize this to the following:
447
+
448
+
```
449
+
Get clientLoader: |--|
450
+
Get Component: |=======|
451
+
Run clientLoader: |-----|
452
+
Render: |-|
453
+
```
454
+
455
+
To achieve this optimization, React Router will split the route module into multiple smaller modules during the production build process. In this case, we'll end up with two separate [virtual modules](https://vite.dev/guide/api-plugin#virtual-modules-convention) — one for the client loader and one for the component and its dependencies.
> 💡 This optimization is automatically applied in framework mode, but you can also implement it in library mode via `route.lazy` and authoring your route in multiple files as covered in our blog post on [lazy loading route modules.](https://remix.run/blog/lazy-loading-routes#advanced-usage-and-optimizations)
474
+
475
+
Now that these are available as separate modules, the client loader and the component can be downloaded in parallel. This means that the client loader can be executed as soon as it's ready without having to wait for the component.
476
+
477
+
This optimization is even more pronounced when more Route Module APIs are used. For example, when using `clientLoader`, `clientAction` and `HydrateFallback`, the timeline for a single route module during a client-side navigation might look like this:
478
+
479
+
```
480
+
Get Route Module: |--~~++++=======|
481
+
Run clientLoader: |-----|
482
+
Render: |-|
483
+
```
484
+
485
+
This would instead be optimized to the following:
486
+
487
+
```
488
+
Get clientLoader: |--|
489
+
Get clientAction: |~~|
490
+
Get HydrateFallback: SKIPPED
491
+
Get Component: |=======|
492
+
Run clientLoader: |-----|
493
+
Render: |-|
494
+
```
495
+
496
+
Note that this optimization only works when the Route Module APIs being split don't share code within the same file. For example, the following route module can't be split:
This route will still work, but since both the client loader and the component depend on the `shared` function defined within the same file, it will be de-optimized into a single route module.
517
+
518
+
To avoid this, you can extract any code shared between exports into a separate file. For example:
519
+
520
+
```tsx filename=routes/example/shared.tsx
521
+
exportconst shared = () =>console.log("hello");
522
+
```
523
+
524
+
You can then import this shared code in your route module without triggering the de-optimization:
This export could not be split into its own chunk because it shares code with other exports. You should extract any shared code into its own module and then import it within the route module.
After building for the browser, only the `Component` will still be in the bundle, so you can use server-only code in the other module exports.
55
55
56
-
## Splitting Route Modules
57
-
58
-
<docs-info>
59
-
60
-
This feature is only enabled when setting the `unstable_splitRouteModules` future flag:
61
-
62
-
```tsx filename=react-router-config.ts
63
-
exportdefault {
64
-
future: {
65
-
unstable_splitRouteModules: true,
66
-
},
67
-
};
68
-
```
69
-
70
-
</docs-info>
71
-
72
-
One of the conveniences of the [Route Module API][route-module] is that everything a route needs is in a single file. Unfortunately this comes with a performance cost in some cases when using the `clientLoader`, `clientAction`, and `HydrateFallback` APIs.
73
-
74
-
As a basic example, consider this route module:
75
-
76
-
```tsx filename=routes/example.tsx
77
-
import { MassiveComponent } from"~/components";
78
-
79
-
exportasyncfunction clientLoader() {
80
-
returnawaitfetch("https://example.com/api").then(
81
-
(response) =>response.json()
82
-
);
83
-
}
84
-
85
-
exportdefaultfunction Component({ loaderData }) {
86
-
return <MassiveComponentdata={loaderData} />;
87
-
}
88
-
```
89
-
90
-
In this example we have a minimal `clientLoader` export that makes a basic fetch call, whereas the default component export is much larger. This is a problem for performance because it means that if we want to navigate to this route client-side, the entire route module must be downloaded before the client loader can start running.
91
-
92
-
To visualize this as a timeline:
93
-
94
-
<docs-info>In the following timeline diagrams, different characters are used within the Route Module bars to denote the different Route Module APIs being exported.</docs-info>
95
-
96
-
```
97
-
Get Route Module: |--=======|
98
-
Run clientLoader: |-----|
99
-
Render: |-|
100
-
```
101
-
102
-
Instead, we want to optimize this to the following:
103
-
104
-
```
105
-
Get clientLoader: |--|
106
-
Get Component: |=======|
107
-
Run clientLoader: |-----|
108
-
Render: |-|
109
-
```
110
-
111
-
To achieve this optimization, React Router will split the route module into multiple smaller modules during the production build process. In this case, we'll end up with two separate [virtual modules][virtual-modules] — one for the client loader and one for the component and its dependencies.
<docs-info>This optimization is automatically applied in framework mode, but you can also implement it in library mode via `route.lazy` and authoring your route in multiple files as covered in our blog post on [lazy loading route modules.][blog-lazy-loading-routes]</docs-info>
130
-
131
-
Now that these are available as separate modules, the client loader and the component can be downloaded in parallel. This means that the client loader can be executed as soon as it's ready without having to wait for the component.
132
-
133
-
This optimization is even more pronounced when more Route Module APIs are used. For example, when using `clientLoader`, `clientAction` and `HydrateFallback`, the timeline for a single route module during a client-side navigation might look like this:
134
-
135
-
```
136
-
Get Route Module: |--~~++++=======|
137
-
Run clientLoader: |-----|
138
-
Render: |-|
139
-
```
140
-
141
-
This would instead be optimized to the following:
142
-
143
-
```
144
-
Get clientLoader: |--|
145
-
Get clientAction: |~~|
146
-
Get HydrateFallback: SKIPPED
147
-
Get Component: |=======|
148
-
Run clientLoader: |-----|
149
-
Render: |-|
150
-
```
151
-
152
-
Note that this optimization only works when the Route Module APIs being split don't share code within the same file. For example, the following route module can't be split:
153
-
154
-
```tsx filename=routes/example.tsx
155
-
import { MassiveComponent } from"~/components";
156
-
157
-
const shared = () =>console.log("hello");
158
-
159
-
exportasyncfunction clientLoader() {
160
-
shared();
161
-
returnawaitfetch("https://example.com/api").then(
162
-
(response) =>response.json()
163
-
);
164
-
}
165
-
166
-
exportdefaultfunction Component({ loaderData }) {
167
-
shared();
168
-
return <MassiveComponentdata={loaderData} />;
169
-
}
170
-
```
171
-
172
-
This route will still work, but since both the client loader and the component depend on the `shared` function defined within the same file, it will be de-optimized into a single route module.
173
-
174
-
To avoid this, you can extract any code shared between exports into a separate file. For example:
175
-
176
-
```tsx filename=routes/example/shared.tsx
177
-
exportconst shared = () =>console.log("hello");
178
-
```
179
-
180
-
You can then import this shared code in your route module without triggering the de-optimization:
181
-
182
-
```tsx filename=routes/example/route.tsx
183
-
import { MassiveComponent } from"~/components";
184
-
import { shared } from"./shared";
185
-
186
-
exportasyncfunction clientLoader() {
187
-
shared();
188
-
returnawaitfetch("https://example.com/api").then(
189
-
(response) =>response.json()
190
-
);
191
-
}
192
-
193
-
exportdefaultfunction Component({ loaderData }) {
194
-
shared();
195
-
return <MassiveComponentdata={loaderData} />;
196
-
}
197
-
```
198
-
199
-
Since the shared code is in its own module, React Router is now able to split this route module into two separate virtual modules:
This export could not be split into its own chunk because it shares code with other exports. You should extract any shared code into its own module and then import it within the route module.
0 commit comments