From 646d5a51d7420511760ffa0fe4f9fe184477365f Mon Sep 17 00:00:00 2001 From: willow <42willow@pm.me> Date: Mon, 15 Dec 2025 18:06:17 +1100 Subject: [PATCH 1/2] fix(storage): set version number on init * https://github.com/wxt-dev/wxt/pull/1996 * prevents migrations running when unneeded if storage is initiated at a value other than 1 --- packages/storage/src/index.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/storage/src/index.ts b/packages/storage/src/index.ts index 76e438251..3f9799302 100644 --- a/packages/storage/src/index.ts +++ b/packages/storage/src/index.ts @@ -436,6 +436,9 @@ function createStorage(): WxtStorage { const getOrInitValue = () => initMutex.runExclusive(async () => { const value = await driver.getItem(driverKey); + if (value == null && targetVersion > 1) + await setMeta(driver, driverKey, { v: targetVersion }); + // Don't init value if it already exists or the init function isn't provided if (value != null || opts?.init == null) return value; From d0b720c60cdaac2ac3fcb8706f1fde653fe070fb Mon Sep 17 00:00:00 2001 From: willow <42willow@pm.me> Date: Wed, 17 Dec 2025 11:22:06 +1100 Subject: [PATCH 2/2] add tests --- packages/storage/src/__tests__/index.test.ts | 31 +++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/packages/storage/src/__tests__/index.test.ts b/packages/storage/src/__tests__/index.test.ts index 1ba64e3cd..6501a1ea3 100644 --- a/packages/storage/src/__tests__/index.test.ts +++ b/packages/storage/src/__tests__/index.test.ts @@ -706,6 +706,35 @@ describe('Storage Utils', () => { expect(migrateToV3).toBeCalledWith(4); }); + it('should not run migrations if initialized on a version greater than 1', async () => { + const migrateToV2 = vi.fn((oldValue) => oldValue * 2); + + const item = storage.defineItem(`local:count`, { + defaultValue: 0, + version: 2, + migrations: { + 2: migrateToV2, + }, + }); + // runs getOrInitValue, sets meta + await item.migrate(); + await waitForMigrations(); + + await item.setValue(1); + + // should do nothing + await item.migrate(); + await waitForMigrations(); + + const actualValue = await item.getValue(); + const actualMeta = await item.getMeta(); + + expect(actualValue).toEqual(1); + expect(actualMeta).toEqual({ v: 2 }); + + expect(migrateToV2).not.toBeCalled(); + }); + it('should call onMigrationComplete callback function if defined', async () => { await fakeBrowser.storage.local.set({ count: 2, @@ -748,7 +777,7 @@ describe('Storage Utils', () => { const actualMeta = await item.getMeta(); expect(actualValue).toEqual(0); - expect(actualMeta).toEqual({}); + expect(actualMeta).toEqual({ v: 3 }); expect(migrateToV2).not.toBeCalled(); expect(migrateToV3).not.toBeCalled();