Repository navigation
fix(vue): release a page's DOM after navigating away from it #31871
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
ShaneK
wants to merge
2
commits into
main
Choose a base branch
from
FW-6445
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.
+228
−6
Open
Changes from all commits
Commits
Show all changes
2 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
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
188 changes: 188 additions & 0 deletions
188
packages/vue/test/base/tests/unit/detached-pages.spec.ts
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,188 @@ | ||
| import { enableAutoUnmount, mount } from '@vue/test-utils'; | ||
| import { afterEach, describe, expect, it } from 'vitest'; | ||
| import { setFlagsFromString } from 'v8'; | ||
| import { runInNewContext } from 'vm'; | ||
| import { createRouter, createWebHistory } from '@ionic/vue-router'; | ||
| import { IonicVue, IonApp, IonRouterOutlet, IonPage } from '@ionic/vue'; | ||
| import { waitForRouter } from './utils'; | ||
|
|
||
| /** | ||
| * Exposes `gc()` so the tests can check that a page is | ||
| * released once it leaves the DOM. | ||
| */ | ||
| setFlagsFromString('--expose-gc'); | ||
| const gc: () => void = runInNewContext('gc'); | ||
|
|
||
| const collectGarbage = async () => { | ||
| for (let i = 0; i < 5; i++) { | ||
| gc(); | ||
| await new Promise((r) => setTimeout(r, 0)); | ||
| } | ||
| }; | ||
|
|
||
| const App = { | ||
| components: { IonApp, IonRouterOutlet }, | ||
| template: '<ion-app><ion-router-outlet /></ion-app>', | ||
| }; | ||
|
|
||
| const Home = { | ||
| components: { IonPage }, | ||
| template: '<ion-page>Home</ion-page>', | ||
| }; | ||
|
|
||
| const Detail = { | ||
| components: { IonPage }, | ||
| props: ['id'], | ||
| template: '<ion-page class="detail">Detail {{ id }}</ion-page>', | ||
| }; | ||
|
|
||
| const createGuardedDetail = (leftPages: string[]) => ({ | ||
| ...Detail, | ||
| beforeRouteLeave(this: { id: string }) { | ||
| leftPages.push(this.id); | ||
| }, | ||
| }); | ||
|
|
||
| const Settings = { | ||
| components: { IonPage }, | ||
| template: '<ion-page>Settings</ion-page>', | ||
| }; | ||
|
|
||
| const createAppRouter = (detail = Detail) => | ||
| createRouter({ | ||
| history: createWebHistory(process.env.BASE_URL), | ||
| routes: [ | ||
| { path: '/', component: Home }, | ||
| { path: '/message/:id', component: detail, props: true }, | ||
| { path: '/settings', component: Settings }, | ||
| ], | ||
| }); | ||
|
|
||
| const mountApp = async (router: ReturnType<typeof createAppRouter>) => { | ||
| router.push('/'); | ||
| await router.isReady(); | ||
| return mount(App, { | ||
| global: { plugins: [router, IonicVue] }, | ||
| attachTo: document.body, | ||
| }); | ||
| }; | ||
|
|
||
| enableAutoUnmount(afterEach); | ||
|
|
||
| describe('Detached pages', () => { | ||
| it('should release a page after navigating back from it', async () => { | ||
| const router = createAppRouter(); | ||
| await mountApp(router); | ||
|
|
||
| router.push('/message/1'); | ||
| await waitForRouter(); | ||
| const detailPage = new WeakRef(document.querySelector('.detail')!); | ||
|
|
||
| router.back(); | ||
| await waitForRouter(); | ||
| expect(document.querySelector('.detail')).toBeNull(); | ||
|
|
||
| await collectGarbage(); | ||
| expect(detailPage.deref()).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should release a page after it is replaced', async () => { | ||
| const router = createAppRouter(); | ||
| await mountApp(router); | ||
|
|
||
| router.push('/message/1'); | ||
| await waitForRouter(); | ||
| const detailPage = new WeakRef(document.querySelector('.detail')!); | ||
|
|
||
| router.replace('/settings'); | ||
| await waitForRouter(); | ||
| expect(document.querySelector('.detail')).toBeNull(); | ||
|
|
||
| await collectGarbage(); | ||
| expect(detailPage.deref()).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should release a page after navigating back to a page of the same route', async () => { | ||
| const router = createAppRouter(); | ||
| await mountApp(router); | ||
|
|
||
| router.push('/message/1'); | ||
| await waitForRouter(); | ||
| router.push('/message/2'); | ||
| await waitForRouter(); | ||
| const secondDetailPage = new WeakRef(document.querySelectorAll('.detail')[1]); | ||
|
|
||
| router.back(); | ||
| await waitForRouter(); | ||
| expect(document.querySelectorAll('.detail').length).toBe(1); | ||
|
|
||
| await collectGarbage(); | ||
| expect(secondDetailPage.deref()).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should run the leave guard of the page navigated back to', async () => { | ||
| const leftPages: string[] = []; | ||
| const router = createAppRouter(createGuardedDetail(leftPages)); | ||
| await mountApp(router); | ||
|
|
||
| router.push('/message/1'); | ||
| await waitForRouter(); | ||
| router.push('/message/2'); | ||
| await waitForRouter(); | ||
|
|
||
| router.back(); | ||
| await waitForRouter(); | ||
| expect(router.currentRoute.value.path).toBe('/message/1'); | ||
|
|
||
| router.push('/settings'); | ||
| await waitForRouter(); | ||
|
|
||
| expect(leftPages).toEqual(['1']); | ||
| }); | ||
|
|
||
| it('should run the leave guard of the page navigated back to with router.go()', async () => { | ||
| const leftPages: string[] = []; | ||
| const router = createAppRouter(createGuardedDetail(leftPages)); | ||
| await mountApp(router); | ||
|
|
||
| router.push('/message/1'); | ||
| await waitForRouter(); | ||
| router.push('/message/2'); | ||
| await waitForRouter(); | ||
| router.push('/message/3'); | ||
| await waitForRouter(); | ||
|
|
||
| router.go(-2); | ||
| await waitForRouter(); | ||
| expect(router.currentRoute.value.path).toBe('/message/1'); | ||
|
|
||
| router.push('/settings'); | ||
| await waitForRouter(); | ||
|
|
||
| expect(leftPages).toEqual(['1']); | ||
| }); | ||
|
|
||
| it('should run the leave guard of the page navigated forward to with router.go()', async () => { | ||
| const leftPages: string[] = []; | ||
| const router = createAppRouter(createGuardedDetail(leftPages)); | ||
| await mountApp(router); | ||
|
|
||
| router.push('/message/1'); | ||
| await waitForRouter(); | ||
| router.push('/message/2'); | ||
| await waitForRouter(); | ||
| router.push('/message/3'); | ||
| await waitForRouter(); | ||
|
|
||
| router.go(-2); | ||
| await waitForRouter(); | ||
| router.go(2); | ||
| await waitForRouter(); | ||
| expect(router.currentRoute.value.path).toBe('/message/3'); | ||
|
|
||
| router.push('/settings'); | ||
| await waitForRouter(); | ||
|
|
||
| expect(leftPages).toEqual(['3']); | ||
| }); | ||
| }); | ||
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.
--expose-gcat runtime doesn't addgcto the current global, only to contexts created afterward, so the comment doesn't explain whygccomes fromrunInNewContextrunInNewContext('gc')looks like a roundaboutglobalThis.gc, and simplifying it breaks every GC assertion in the file