-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(vue-router): path useMatch memory leak #8001
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
Sheraff
wants to merge
1
commit into
main
Choose a base branch
from
fix-vue-router-use-match-leak
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.
+172
−2
Open
Changes from all commits
Commits
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
127 changes: 127 additions & 0 deletions
127
packages/vue-router/tests/match-subscription-cleanup.test.tsx
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 |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| import { afterEach, expect, test } from 'vitest' | ||
| import { cleanup, fireEvent, render, screen } from '@testing-library/vue' | ||
| import { | ||
| Outlet, | ||
| RouterProvider, | ||
| createMemoryHistory, | ||
| createRootRoute, | ||
| createRoute, | ||
| createRouter, | ||
| } from '../src' | ||
|
|
||
| afterEach(() => { | ||
| cleanup() | ||
| }) | ||
|
|
||
| test('releases match-store subscriptions when route params replace a match', async () => { | ||
| const observedIds: Array<string> = [] | ||
| const rootRoute = createRootRoute({ | ||
| validateSearch: (search: Record<string, unknown>) => ({ | ||
| q: typeof search.q === 'string' ? search.q : '', | ||
| }), | ||
| loaderDeps: ({ search }) => ({ q: search.q }), | ||
| loader: ({ deps }) => `root:${deps.q}`, | ||
| component: RootComponent, | ||
| }) | ||
| function RootComponent() { | ||
| const rootData = rootRoute.useLoaderData() | ||
| return ( | ||
| <section data-testid="root-data"> | ||
| {rootData.value} | ||
| <Outlet /> | ||
| </section> | ||
| ) | ||
| } | ||
| const itemRoute = createRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: '/items/$id', | ||
| validateSearch: (search: Record<string, unknown>) => ({ | ||
| q: typeof search.q === 'string' ? search.q : '', | ||
| }), | ||
| loaderDeps: ({ search }) => ({ q: search.q }), | ||
| loader: ({ params, deps }) => `${params.id}:${deps.q}`, | ||
| component: ItemComponent, | ||
| }) | ||
| function ItemComponent() { | ||
| const id = itemRoute.useLoaderData() | ||
| const rootData = rootRoute.useLoaderData() | ||
| return ( | ||
| <button | ||
| data-testid="item-id" | ||
| onClick={() => observedIds.push(`${id.value}|${rootData.value}`)} | ||
| > | ||
| {`${id.value}|${rootData.value}`} | ||
| </button> | ||
| ) | ||
| } | ||
| const router = createRouter({ | ||
| routeTree: rootRoute.addChildren([itemRoute]), | ||
| history: createMemoryHistory({ initialEntries: ['/items/initial'] }), | ||
| defaultGcTime: 0, | ||
| }) | ||
| const matchStore = router.stores.getMatchStore('/items/$id') | ||
| const originalSubscribe = matchStore.subscribe.bind(matchStore) | ||
| let activeSubscriptions = 0 | ||
| let subscriptions = 0 | ||
| let unsubscriptions = 0 | ||
|
|
||
| matchStore.subscribe = (observer) => { | ||
| subscriptions++ | ||
| activeSubscriptions++ | ||
| const subscription = Reflect.apply(originalSubscribe, matchStore, [ | ||
| observer, | ||
| ]) as ReturnType<typeof matchStore.subscribe> | ||
| let active = true | ||
|
|
||
| return { | ||
| unsubscribe() { | ||
| if (active) { | ||
| active = false | ||
| activeSubscriptions-- | ||
| unsubscriptions++ | ||
| } | ||
| subscription.unsubscribe() | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| render(<RouterProvider router={router} />) | ||
| expect(await screen.findByTestId('item-id')).toHaveTextContent( | ||
| 'initial:|root:', | ||
| ) | ||
| const initialSubscriptions = activeSubscriptions | ||
|
|
||
| for (let index = 0; index < 50; index++) { | ||
| await router.navigate({ | ||
| to: '/items/$id', | ||
| params: { id: `item-${index}` }, | ||
| search: { q: `query-${index}` }, | ||
| replace: true, | ||
| }) | ||
| } | ||
| expect(screen.getByTestId('item-id')).toHaveTextContent( | ||
| 'item-49:query-49|root:query-49', | ||
| ) | ||
| expect(activeSubscriptions).toBe(initialSubscriptions) | ||
|
|
||
| const subscriptionsAfterParamChanges = subscriptions | ||
| for (let index = 0; index < 50; index++) { | ||
| await router.navigate({ | ||
| to: '/items/$id', | ||
| params: { id: 'item-49' }, | ||
| search: { q: `same-param-query-${index}` }, | ||
| replace: true, | ||
| }) | ||
| } | ||
| expect(screen.getByTestId('item-id')).toHaveTextContent( | ||
| 'item-49:same-param-query-49|root:same-param-query-49', | ||
| ) | ||
| await fireEvent.click(screen.getByTestId('item-id')) | ||
| expect(observedIds).toEqual([ | ||
| 'item-49:same-param-query-49|root:same-param-query-49', | ||
| ]) | ||
|
|
||
| expect(activeSubscriptions).toBe(initialSubscriptions) | ||
| expect(subscriptions - unsubscriptions).toBe(initialSubscriptions) | ||
| expect(subscriptions).toBe(subscriptionsAfterParamChanges) | ||
| }) |
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.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: TanStack/router
Length of output: 50372
Remove
anyfrom the cached ref type.Vue.Ref<any>disables type checking for every cached store value. Useunknownfor the cache boundary, then keep the precise cast at the helper return boundary.🤖 Prompt for AI Agents
Source: Coding guidelines