From b5956a2acc44db91d1250bcef6fca9b9e2d96e85 Mon Sep 17 00:00:00 2001 From: Jason Nall Date: Sat, 7 Dec 2024 17:00:51 -0500 Subject: [PATCH] Fix soft navigation subscription cleanup. --- .changeset/breezy-rockets-allow.md | 5 ++++ .../src/main/createIPCHandler.ts | 23 +++++++++++-------- 2 files changed, 18 insertions(+), 10 deletions(-) create mode 100644 .changeset/breezy-rockets-allow.md diff --git a/.changeset/breezy-rockets-allow.md b/.changeset/breezy-rockets-allow.md new file mode 100644 index 00000000..af79ada8 --- /dev/null +++ b/.changeset/breezy-rockets-allow.md @@ -0,0 +1,5 @@ +--- +'electron-trpc': minor +--- + +Fix bug where soft-navigation events would cause subscription cleanup diff --git a/packages/electron-trpc/src/main/createIPCHandler.ts b/packages/electron-trpc/src/main/createIPCHandler.ts index 0ee086db..ea4c56a0 100644 --- a/packages/electron-trpc/src/main/createIPCHandler.ts +++ b/packages/electron-trpc/src/main/createIPCHandler.ts @@ -79,16 +79,19 @@ class IPCHandler { } #attachSubscriptionCleanupHandlers(win: BrowserWindow) { - win.webContents.on('did-start-navigation', ({ frame }) => { - debug( - 'Handling webContents `did-start-navigation` event', - `webContentsId: ${win.webContents.id}`, - `frameRoutingId: ${frame.routingId}` - ); - this.#cleanUpSubscriptions({ - webContentsId: win.webContents.id, - frameRoutingId: frame.routingId, - }); + win.webContents.on('did-start-navigation', ({ isSameDocument, frame }) => { + // Check if it's a hard navigation + if (!isSameDocument) { + debug( + 'Handling hard navigation event', + `webContentsId: ${win.webContents.id}`, + `frameRoutingId: ${frame.routingId}` + ); + this.#cleanUpSubscriptions({ + webContentsId: win.webContents.id, + frameRoutingId: frame.routingId, + }); + } }); win.webContents.on('destroyed', () => { debug('Handling webContents `destroyed` event');