diff --git a/src/linking/LinkingHandler.test.ts b/src/linking/LinkingHandler.test.ts index cb35b4c8da..18d9119c4f 100644 --- a/src/linking/LinkingHandler.test.ts +++ b/src/linking/LinkingHandler.test.ts @@ -292,6 +292,41 @@ describe('LinkingHandler', () => { uut.configure(baseConfig); expect(remove).toHaveBeenCalled(); }); + + it('ignores an initial URL resolved by a previous configuration', async () => { + let resolveInitialURL!: (url: string | null) => void; + mockLinking.getInitialURL + .mockImplementationOnce( + () => + new Promise((resolve) => { + resolveInitialURL = resolve; + }) + ) + .mockResolvedValueOnce(null); + const firstOnLink = jest.fn(); + const secondOnLink = jest.fn(); + + uut.configure({ ...baseConfig, onLink: firstOnLink }); + uut.setRootReady(); + uut.configure({ ...baseConfig, onLink: secondOnLink }); + resolveInitialURL('myapp://home'); + await Promise.resolve(); + + expect(firstOnLink).not.toHaveBeenCalled(); + expect(secondOnLink).not.toHaveBeenCalled(); + }); + + it('continues handling URL events when the initial URL lookup fails', async () => { + mockLinking.getInitialURL.mockRejectedValueOnce(new Error('lookup failed')); + uut.configure(baseConfig); + uut.setRootReady(); + + await Promise.resolve(); + await Promise.resolve(); + urlListener?.({ url: 'myapp://home' }); + + expect(mockShowModal).toHaveBeenCalledTimes(1); + }); }); describe('setRootReady', () => { diff --git a/src/linking/LinkingHandler.ts b/src/linking/LinkingHandler.ts index 7db0a50172..79b01c9b1a 100644 --- a/src/linking/LinkingHandler.ts +++ b/src/linking/LinkingHandler.ts @@ -46,6 +46,7 @@ export class LinkingHandler { private config: LinkingConfig | null = null; private linkingSubscription: { remove: () => void } | null = null; + private subscriptionGeneration = 0; private rootReady = false; private userReadyOverride: boolean | null = null; @@ -114,6 +115,7 @@ export class LinkingHandler { } public teardown(): void { + this.subscriptionGeneration++; if (this.linkingSubscription) { this.linkingSubscription.remove(); this.linkingSubscription = null; @@ -124,13 +126,19 @@ export class LinkingHandler { } private subscribe(): void { + const subscriptionGeneration = this.subscriptionGeneration; this.linkingSubscription = this.linkingAPI.addEventListener('url', (event) => { this.handleURL(event.url); }); - this.linkingAPI.getInitialURL().then((url) => { - if (url) this.handleURL(url); - }); + this.linkingAPI + .getInitialURL() + .then((url) => { + if (url && subscriptionGeneration === this.subscriptionGeneration) { + this.handleURL(url); + } + }) + .catch(() => {}); } private processURL(url: string): void {