From c8c072ccd18d81dc6c039ec292c83bf236e54265 Mon Sep 17 00:00:00 2001 From: Artem Zakharchenko Date: Sun, 30 Aug 2026 14:00:12 +0200 Subject: [PATCH] fix(persist): use `visibilitychange` instead of deprecated `unload` event --- playwright.config.ts | 11 ++++++++++- src/extensions/persist.ts | 12 ++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/playwright.config.ts b/playwright.config.ts index e588d7d..faf774c 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -6,7 +6,16 @@ export default defineConfig({ workers: 1, projects: [ { - use: devices['Desktop Chrome'], + use: { + ...devices['Desktop Chrome'], + launchOptions: { + /** + * @note Emulate Chrome's `unload` deprecation so the tests + * fail if any code relies on the `unload` event. + */ + args: ['--enable-features=DeprecateUnload'], + }, + }, }, ], }) diff --git a/src/extensions/persist.ts b/src/extensions/persist.ts index 3380c31..70ade94 100644 --- a/src/extensions/persist.ts +++ b/src/extensions/persist.ts @@ -50,8 +50,16 @@ export function persist() { const logger = new Logger('extension').extend('persist') const COLLECTION_KEY = `${STORAGE_KEY}/${collection[kCollectionId]}` - // Flush the collection's on page unload. - window.addEventListener('unload', () => { + /** + * @note Flush the collection whenever the page becomes hidden. + * This covers reloads, navigations, and closing the page. + * The `unload` event is deprecated and blocked by Chrome. + */ + document.addEventListener('visibilitychange', () => { + if (document.visibilityState !== 'hidden') { + return + } + localStorage.setItem( COLLECTION_KEY, /**