-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
optimize bundle-size & perf #7053
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
Open
schiller-manuel
wants to merge
3
commits into
main
Choose a base branch
from
chore-perf-bundlesize
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+197
−84
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 |
|---|---|---|
|
|
@@ -1248,11 +1248,11 @@ export class RouterCore< | |
| } | ||
|
|
||
| emit: EmitFn = (routerEvent) => { | ||
| this.subscribers.forEach((listener) => { | ||
| for (const listener of this.subscribers) { | ||
| if (listener.eventType === routerEvent.type) { | ||
| listener.fn(routerEvent) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -1765,24 +1765,24 @@ export class RouterCore< | |
| } | ||
|
|
||
| cancelMatches = () => { | ||
| this.stores.pendingMatchesId.state.forEach((matchId) => { | ||
| for (const matchId of this.stores.pendingMatchesId.state) { | ||
| this.cancelMatch(matchId) | ||
| }) | ||
| } | ||
|
|
||
| this.stores.matchesId.state.forEach((matchId) => { | ||
| for (const matchId of this.stores.matchesId.state) { | ||
| if (this.stores.pendingMatchStoresById.has(matchId)) { | ||
| return | ||
| continue | ||
| } | ||
|
|
||
| const match = this.stores.activeMatchStoresById.get(matchId)?.state | ||
| if (!match) { | ||
| return | ||
| continue | ||
| } | ||
|
|
||
| if (match.status === 'pending' || match.isFetching === 'loader') { | ||
| this.cancelMatch(matchId) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -1915,7 +1915,7 @@ export class RouterCore< | |
| let nextSearch = fromSearch | ||
| if (opts._includeValidateSearch && this.options.search?.strict) { | ||
| const validatedSearch = {} | ||
| destRoutes.forEach((route) => { | ||
| for (const route of destRoutes) { | ||
| if (route.options.validateSearch) { | ||
| try { | ||
| Object.assign( | ||
|
|
@@ -1929,7 +1929,7 @@ export class RouterCore< | |
| // ignore errors here because they are already handled in matchRoutes | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| nextSearch = validatedSearch | ||
| } | ||
|
|
||
|
|
@@ -2093,13 +2093,13 @@ export class RouterCore< | |
| '__TSR_index', | ||
| '__hashScrollIntoViewOptions', | ||
| ] as const | ||
| ignoredProps.forEach((prop) => { | ||
| for (const prop of ignoredProps) { | ||
| ;(next.state as any)[prop] = this.latestLocation.state[prop] | ||
| }) | ||
| } | ||
| const isEqual = deepEqual(next.state, this.latestLocation.state) | ||
| ignoredProps.forEach((prop) => { | ||
| for (const prop of ignoredProps) { | ||
| delete next.state[prop] | ||
| }) | ||
| } | ||
| return isEqual | ||
| } | ||
|
|
||
|
|
@@ -2418,54 +2418,42 @@ export class RouterCore< | |
| // exitingMatches uses match.id (routeId + params + loaderDeps) so | ||
| // navigating /foo?page=1 → /foo?page=2 correctly caches the page=1 entry. | ||
| let exitingMatches: Array<AnyRouteMatch> | null = null | ||
|
|
||
| // Lifecycle-hook identity uses routeId only so that navigating between | ||
| // different params/deps of the same route fires onStay (not onLeave+onEnter). | ||
| let hookExitingMatches: Array<AnyRouteMatch> | null = null | ||
| let hookEnteringMatches: Array<AnyRouteMatch> | null = null | ||
| let hookStayingMatches: Array<AnyRouteMatch> | null = null | ||
| let pendingMatches: Array<AnyRouteMatch> = [] | ||
| let currentMatches: Array<AnyRouteMatch> = [] | ||
| let pendingRouteIds: Set<string> | null = null | ||
| let activeRouteIds: Set<string> | null = null | ||
|
|
||
| this.batch(() => { | ||
| const pendingMatches = | ||
| this.stores.pendingMatchesSnapshot.state | ||
| const mountPending = pendingMatches.length | ||
| const currentMatches = | ||
| this.stores.activeMatchesSnapshot.state | ||
|
|
||
| exitingMatches = mountPending | ||
| ? currentMatches.filter( | ||
| (match) => | ||
| !this.stores.pendingMatchStoresById.has(match.id), | ||
| ) | ||
| : null | ||
|
|
||
| // Lifecycle-hook identity: routeId only (route presence in tree) | ||
| // Build routeId sets from pools to avoid derived stores. | ||
| const pendingRouteIds = new Set<string>() | ||
| for (const s of this.stores.pendingMatchStoresById.values()) { | ||
| if (s.routeId) pendingRouteIds.add(s.routeId) | ||
| } | ||
| const activeRouteIds = new Set<string>() | ||
| for (const s of this.stores.activeMatchStoresById.values()) { | ||
| if (s.routeId) activeRouteIds.add(s.routeId) | ||
| pendingMatches = this.stores.pendingMatchesSnapshot.state | ||
| currentMatches = this.stores.activeMatchesSnapshot.state | ||
|
|
||
| if (pendingMatches.length) { | ||
| activeRouteIds = new Set<string>() | ||
| for (const match of currentMatches) { | ||
| activeRouteIds.add(match.routeId) | ||
| } | ||
|
|
||
| pendingRouteIds = new Set<string>() | ||
| exitingMatches = [] | ||
|
|
||
| for (const match of pendingMatches) { | ||
| pendingRouteIds.add(match.routeId) | ||
| } | ||
|
|
||
| for (const match of currentMatches) { | ||
| if ( | ||
| !this.stores.pendingMatchStoresById.has(match.id) && | ||
| match.status !== 'error' && | ||
| match.status !== 'notFound' && | ||
| match.status !== 'redirected' | ||
| ) { | ||
| exitingMatches.push(match) | ||
| } | ||
| } | ||
| } else { | ||
| exitingMatches = null | ||
| } | ||
|
|
||
| hookExitingMatches = mountPending | ||
| ? currentMatches.filter( | ||
| (match) => !pendingRouteIds.has(match.routeId), | ||
| ) | ||
| : null | ||
| hookEnteringMatches = mountPending | ||
| ? pendingMatches.filter( | ||
| (match) => !activeRouteIds.has(match.routeId), | ||
| ) | ||
| : null | ||
| hookStayingMatches = mountPending | ||
| ? pendingMatches.filter((match) => | ||
| activeRouteIds.has(match.routeId), | ||
| ) | ||
| : currentMatches | ||
|
|
||
| this.stores.isLoading.setState(() => false) | ||
| this.stores.loadedAt.setState(() => Date.now()) | ||
| /** | ||
|
|
@@ -2474,31 +2462,47 @@ export class RouterCore< | |
| * deliberately excluded from `cachedMatches` so that subsequent invalidations | ||
| * or reloads re-run their loaders instead of reusing the failed/not-found data. | ||
| */ | ||
| if (mountPending) { | ||
| if (pendingMatches.length) { | ||
| this.stores.setActiveMatches(pendingMatches) | ||
| this.stores.setPendingMatches([]) | ||
| this.stores.setCachedMatches([ | ||
| ...this.stores.cachedMatchesSnapshot.state, | ||
| ...exitingMatches!.filter( | ||
| (d) => | ||
| d.status !== 'error' && | ||
| d.status !== 'notFound' && | ||
| d.status !== 'redirected', | ||
| ), | ||
| ...exitingMatches!, | ||
| ]) | ||
| this.clearExpiredCache() | ||
| } | ||
| }) | ||
|
|
||
| // | ||
| for (const [matches, hook] of [ | ||
| [hookExitingMatches, 'onLeave'], | ||
| [hookEnteringMatches, 'onEnter'], | ||
| [hookStayingMatches, 'onStay'], | ||
| ] as const) { | ||
| if (!matches) continue | ||
| for (const match of matches as Array<AnyRouteMatch>) { | ||
| this.looseRoutesById[match.routeId]!.options[hook]?.( | ||
| // Lifecycle-hook identity uses routeId only so that navigating between | ||
| // different params/deps of the same route fires onStay (not onLeave+onEnter). | ||
| const nextPendingRouteIds = | ||
| pendingRouteIds as Set<string> | null | ||
| const nextActiveRouteIds = | ||
| activeRouteIds as Set<string> | null | ||
|
Comment on lines
+2478
to
+2481
Contributor
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. This doesn't seem necessary, |
||
|
|
||
| if (nextPendingRouteIds && nextActiveRouteIds) { | ||
| for (const match of currentMatches) { | ||
| if (!nextPendingRouteIds.has(match.routeId)) { | ||
| this.looseRoutesById[match.routeId]!.options.onLeave?.( | ||
| match, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| for (const match of pendingMatches) { | ||
| if (nextActiveRouteIds.has(match.routeId)) { | ||
| this.looseRoutesById[match.routeId]!.options.onStay?.( | ||
| match, | ||
| ) | ||
| } else { | ||
| this.looseRoutesById[match.routeId]!.options.onEnter?.( | ||
| match, | ||
| ) | ||
| } | ||
| } | ||
| } else { | ||
| for (const match of currentMatches) { | ||
| this.looseRoutesById[match.routeId]!.options.onStay?.( | ||
| match, | ||
| ) | ||
| } | ||
|
|
@@ -2525,9 +2529,7 @@ export class RouterCore< | |
| ? redirect.status | ||
| : notFound | ||
| ? 404 | ||
| : this.stores.activeMatchesSnapshot.state.some( | ||
| (d) => d.status === 'error', | ||
| ) | ||
| : this.hasErrorMatch() | ||
| ? 500 | ||
| : 200 | ||
|
|
||
|
|
@@ -2561,9 +2563,7 @@ export class RouterCore< | |
| let newStatusCode: number | undefined = undefined | ||
| if (this.hasNotFoundMatch()) { | ||
| newStatusCode = 404 | ||
| } else if ( | ||
| this.stores.activeMatchesSnapshot.state.some((d) => d.status === 'error') | ||
| ) { | ||
| } else if (this.hasErrorMatch()) { | ||
| newStatusCode = 500 | ||
| } | ||
| if (newStatusCode !== undefined) { | ||
|
|
@@ -2930,10 +2930,24 @@ export class RouterCore< | |
|
|
||
| serverSsr?: ServerSsr | ||
|
|
||
| hasErrorMatch = () => { | ||
| for (const match of this.stores.activeMatchesSnapshot.state) { | ||
| if (match.status === 'error') { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| hasNotFoundMatch = () => { | ||
| return this.stores.activeMatchesSnapshot.state.some( | ||
| (d) => d.status === 'notFound' || d.globalNotFound, | ||
| ) | ||
| for (const match of this.stores.activeMatchesSnapshot.state) { | ||
| if (match.status === 'notFound' || match.globalNotFound) { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| return false | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or 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
Oops, something went wrong.
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.
elsenot necessary,exitingMatchesis alreadynull