|
| 1 | +import { createSSRApp } from 'vue' |
| 2 | +import { renderToString } from '../src/renderToString' |
| 3 | + |
| 4 | +const gc = () => |
| 5 | + new Promise<void>(resolve => { |
| 6 | + setTimeout(() => { |
| 7 | + global.gc!() |
| 8 | + resolve() |
| 9 | + }) |
| 10 | + }) |
| 11 | + |
| 12 | +const Card = { |
| 13 | + props: { tag: { type: String, default: 'div' } }, |
| 14 | + template: `<component :is="tag" class="card"><slot /></component>`, |
| 15 | +} |
| 16 | + |
| 17 | +const Page = { |
| 18 | + components: { Card }, |
| 19 | + props: ['payload'], |
| 20 | + template: `<main><Card>{{ payload.title }}</Card></main>`, |
| 21 | +} |
| 22 | + |
| 23 | +describe.skipIf(!global.gc)('ssr: render error leak', () => { |
| 24 | + // #15070 |
| 25 | + test('should GC apps created after a render error in a forwarded slot', async () => { |
| 26 | + const createApp = (payload: any) => createSSRApp(Page, { payload }) |
| 27 | + |
| 28 | + expect(await renderToString(createApp({ title: 'ok' }))).toContain('ok') |
| 29 | + |
| 30 | + await expect(renderToString(createApp(null))).rejects.toThrow( |
| 31 | + `Cannot read properties of null`, |
| 32 | + ) |
| 33 | + |
| 34 | + const weakRefs: { deref(): unknown | undefined }[] = [] |
| 35 | + const renderOnce = async () => { |
| 36 | + const app = createApp({ title: 'ok' }) |
| 37 | + // @ts-expect-error ES2021 API |
| 38 | + weakRefs.push(new WeakRef(app)) |
| 39 | + expect(await renderToString(app)).toContain('ok') |
| 40 | + } |
| 41 | + for (let i = 0; i < 20; i++) { |
| 42 | + await renderOnce() |
| 43 | + } |
| 44 | + |
| 45 | + for (let i = 0; i < 5; i++) { |
| 46 | + await gc() |
| 47 | + } |
| 48 | + |
| 49 | + expect(weakRefs.filter(ref => ref.deref()).length).toBe(0) |
| 50 | + }) |
| 51 | + |
| 52 | + test('should GC apps created after a render error in an inline block (`v-if`)', async () => { |
| 53 | + const PageIf = { |
| 54 | + props: ['payload'], |
| 55 | + template: `<component :is="'div'"><div v-if="payload.list">{{ payload.list.missing.x }}</div></component>`, |
| 56 | + } |
| 57 | + const createApp = (payload: any) => createSSRApp(PageIf, { payload }) |
| 58 | + |
| 59 | + expect( |
| 60 | + await renderToString(createApp({ list: { missing: { x: 'ok' } } })), |
| 61 | + ).toContain('ok') |
| 62 | + |
| 63 | + await expect(renderToString(createApp({ list: {} }))).rejects.toThrow( |
| 64 | + `Cannot read properties of undefined`, |
| 65 | + ) |
| 66 | + |
| 67 | + const weakRefs: { deref(): unknown | undefined }[] = [] |
| 68 | + const renderOnce = async () => { |
| 69 | + const app = createApp({ list: { missing: { x: 'ok' } } }) |
| 70 | + // @ts-expect-error ES2021 API |
| 71 | + weakRefs.push(new WeakRef(app)) |
| 72 | + expect(await renderToString(app)).toContain('ok') |
| 73 | + } |
| 74 | + for (let i = 0; i < 20; i++) { |
| 75 | + await renderOnce() |
| 76 | + } |
| 77 | + |
| 78 | + for (let i = 0; i < 5; i++) { |
| 79 | + await gc() |
| 80 | + } |
| 81 | + |
| 82 | + expect(weakRefs.filter(ref => ref.deref()).length).toBe(0) |
| 83 | + }) |
| 84 | +}) |
0 commit comments