-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Trigger a new router.routes identity during fog of war route patching #11740
Merged
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
73ff819
Trigger a new router.routes identity during fog of war route patching
brophdawg11 650559b
Update comments
brophdawg11 650c58b
Batch patch
brophdawg11 ef611a9
Update TS signature
brophdawg11 3ce8d73
Get experimental releases working
brophdawg11 2fb9a7a
Revert "Get experimental releases working"
brophdawg11 0f1a4d3
Remove batch patchRotues API for now - React batches for us
brophdawg11 078ef8d
Get experimental releases working
brophdawg11 248a7fc
Revert "Get experimental releases working"
brophdawg11 0b4f1bd
Remove batched API from changeset
brophdawg11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
"@remix-run/router": patch | ||
--- | ||
|
||
Trigger a new `router.routes` identity/reflow during fog of war route patching | ||
|
||
- This also adds a new batched API for `router.patchRoutes` so you can perform multiple patches but only a single reflow at the end: | ||
|
||
```js | ||
// Apply one patch and reflow | ||
router.patchRoutes(parentId, children); | ||
|
||
// Apply multiples patches and a single reflow | ||
router.patchRoutes((patch) => { | ||
patch(parentId, children); | ||
patch(parentId2, children2); | ||
patch(parentId3, children3); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -249,10 +249,16 @@ export interface Router { | |
* PRIVATE DO NOT USE | ||
* | ||
* Patch additional children routes into an existing parent route | ||
* @param routeId The parent route id | ||
* @param routeId The parent route id or a callback function accepting `patch` | ||
* to perform batch patching | ||
* @param children The additional children routes | ||
*/ | ||
patchRoutes(routeId: string | null, children: AgnosticRouteObject[]): void; | ||
patchRoutes( | ||
cb: ( | ||
patch: (r: string | null, children: AgnosticRouteObject[]) => void | ||
) => void | ||
): void; | ||
|
||
/** | ||
* @internal | ||
|
@@ -1222,6 +1228,7 @@ export function createRouter(init: RouterInit): Router { | |
isMutationMethod(state.navigation.formMethod) && | ||
location.state?._isRedirect !== true); | ||
|
||
// Commit any in-flight routes at the end of the HMR revalidation "navigation" | ||
if (inFlightDataRoutes) { | ||
dataRoutes = inFlightDataRoutes; | ||
inFlightDataRoutes = undefined; | ||
|
@@ -3204,26 +3211,37 @@ export function createRouter(init: RouterInit): Router { | |
? partialMatches[partialMatches.length - 1].route | ||
: null; | ||
while (true) { | ||
let isNonHMR = inFlightDataRoutes == null; | ||
let routesToUse = inFlightDataRoutes || dataRoutes; | ||
try { | ||
await loadLazyRouteChildren( | ||
patchRoutesOnMissImpl!, | ||
pathname, | ||
partialMatches, | ||
dataRoutes || inFlightDataRoutes, | ||
routesToUse, | ||
manifest, | ||
mapRouteProperties, | ||
pendingPatchRoutes, | ||
signal | ||
); | ||
} catch (e) { | ||
return { type: "error", error: e, partialMatches }; | ||
} finally { | ||
// If we are not in the middle of an HMR revalidation and we changed the | ||
// routes, provide a new identity so when we `updateState` at the end of | ||
// this navigation/fetch `router.routes` will be a new identity and | ||
// trigger a re-run of memoized `router.routes` dependencies. | ||
// HMR will already update the identity and reflow when it lands | ||
// `inFlightDataRoutes` in `completeNavigation` | ||
if (isNonHMR) { | ||
dataRoutes = [...dataRoutes]; | ||
} | ||
} | ||
|
||
if (signal.aborted) { | ||
return { type: "aborted" }; | ||
} | ||
|
||
let routesToUse = inFlightDataRoutes || dataRoutes; | ||
let newMatches = matchRoutes(routesToUse, pathname, basename); | ||
let matchedSplat = false; | ||
if (newMatches) { | ||
|
@@ -3284,6 +3302,51 @@ export function createRouter(init: RouterInit): Router { | |
); | ||
} | ||
|
||
// single-patch implementation: | ||
// router.patchRoutes(parentId, children); | ||
// | ||
// batch-patch implementation: | ||
// router.patchRoutes((patch) => { | ||
// patch(parentId1, children1); | ||
// patch(parentId2, children2); | ||
// patch(parentId3, children3); | ||
// }); | ||
function patchRoutes( | ||
routeIdOrCb: | ||
| string | ||
| null | ||
| (( | ||
patch: (r: string | null, children: AgnosticRouteObject[]) => void | ||
) => void), | ||
children?: AgnosticRouteObject[] | ||
): void { | ||
let isNonHMR = inFlightDataRoutes == null; | ||
let routesToUse = inFlightDataRoutes || dataRoutes; | ||
if (typeof routeIdOrCb === "function") { | ||
routeIdOrCb((r, c) => | ||
patchRoutesImpl(r, c, routesToUse, manifest, mapRouteProperties) | ||
); | ||
} else if (typeof routeIdOrCb === "string" && children != null) { | ||
patchRoutesImpl( | ||
routeIdOrCb, | ||
children, | ||
routesToUse, | ||
manifest, | ||
mapRouteProperties | ||
); | ||
} | ||
|
||
// If we are not in the middle of an HMR revalidation and we changed the | ||
// routes, provide a new identity and trigger a reflow via `updateState` | ||
// to re-run memoized `router.routes` dependencies. | ||
// HMR will already update the identity and reflow when it lands | ||
// `inFlightDataRoutes` in `completeNavigation` | ||
if (isNonHMR) { | ||
dataRoutes = [...dataRoutes]; | ||
updateState({}); | ||
} | ||
Comment on lines
+3319
to
+3322
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update |
||
} | ||
|
||
router = { | ||
get basename() { | ||
return basename; | ||
|
@@ -3315,15 +3378,7 @@ export function createRouter(init: RouterInit): Router { | |
dispose, | ||
getBlocker, | ||
deleteBlocker, | ||
patchRoutes(routeId, children) { | ||
return patchRoutes( | ||
routeId, | ||
children, | ||
dataRoutes || inFlightDataRoutes, | ||
manifest, | ||
mapRouteProperties | ||
); | ||
}, | ||
patchRoutes, | ||
_internalFetchControllers: fetchControllers, | ||
_internalActiveDeferreds: activeDeferreds, | ||
// TODO: Remove setRoutes, it's temporary to avoid dealing with | ||
|
@@ -4488,7 +4543,7 @@ function shouldRevalidateLoader( | |
} | ||
|
||
/** | ||
* Idempotent utility to execute route.children() method to lazily load route | ||
* Idempotent utility to execute patchRoutesOnMiss() to lazily load route | ||
* definitions and update the routes/routeManifest | ||
*/ | ||
async function loadLazyRouteChildren( | ||
|
@@ -4510,7 +4565,7 @@ async function loadLazyRouteChildren( | |
matches, | ||
patch: (routeId, children) => { | ||
if (!signal.aborted) { | ||
patchRoutes( | ||
patchRoutesImpl( | ||
routeId, | ||
children, | ||
routes, | ||
|
@@ -4531,10 +4586,10 @@ async function loadLazyRouteChildren( | |
} | ||
} | ||
|
||
function patchRoutes( | ||
function patchRoutesImpl( | ||
routeId: string | null, | ||
children: AgnosticRouteObject[], | ||
routes: AgnosticDataRouteObject[], | ||
routesToUse: AgnosticDataRouteObject[], | ||
manifest: RouteManifest, | ||
mapRouteProperties: MapRoutePropertiesFunction | ||
) { | ||
|
@@ -4559,10 +4614,10 @@ function patchRoutes( | |
let dataChildren = convertRoutesToDataRoutes( | ||
children, | ||
mapRouteProperties, | ||
["patch", String(routes.length || "0")], | ||
["patch", String(routesToUse.length || "0")], | ||
manifest | ||
); | ||
routes.push(...dataChildren); | ||
routesToUse.push(...dataChildren); | ||
} | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update
router.routes
identity during normal navigation discovery viaunstable_patchRoutesOnMiss