Manifest v3 : Migrate user data from localStorage in background page?

274 views
Skip to first unread message

Jerry Krinock

unread,
Sep 27, 2023, 12:41:38 AM9/27/23
to Chromium Extensions
Our manifest v2 extension uses the localStorage of a background page.  We understand that, after migrating our extension to manifest v3, which requires migrating our background page to a service worker, we must replace this storage with either (a) the localStorage of an offscreen document, or (b) chrome.storage.local of our  service worker.

But how about our users' data?  Is there a way to migrate users' data from the localStorage in the old background page to either of these new storage places during the first run of our new extension version?

Pavel Aronovich

unread,
Sep 27, 2023, 7:03:39 AM9/27/23
to Chromium Extensions, Jerry Krinock
You can implement pre-migration of data to MV2 or MV3 using OffscreenDocument.

среда, 27 сентября 2023 г. в 07:41:38 UTC+3, Jerry Krinock:

Jerry Krinock

unread,
Sep 27, 2023, 9:13:28 AM9/27/23
to Chromium Extensions, Pavel Aronovich, Jerry Krinock
Thank you Pavel.  Do you mean that we should publish a manifest v2 update which migrates the user's data to a offscreen document, wait a few months, assuming that 99.9%  users will have used Chrome at least once during that period to migrate thier data, and then publish a manifest our v3 update?

Pavel Aronovich

unread,
Sep 27, 2023, 2:44:07 PM9/27/23
to Chromium Extensions, Jerry Krinock, Pavel Aronovich
You can now transfer data to chrome.storage.local in mv2. 
Or in mv3, implement the transfer using Offscreen Document.

среда, 27 сентября 2023 г. в 16:13:28 UTC+3, Jerry Krinock:

Jerry Krinock

unread,
Sep 29, 2023, 9:21:05 AM9/29/23
to Chromium Extensions, Pavel Aronovich, Jerry Krinock
Thank you, Pavel.  Your first alternative implied that maybe an offscreen document in a manifest v3 update would automagically access the same .localStorage that was written by the background page of a previous manifest v2 version.  If this is true, then no migration is necessary – it just works.

I thought this was highly unlikely, but I tried it and, to my amazement, it seems to work!   Here is what I did:

• Load as Unpacked my old manifest v2 extension from a directory.
• Perform an operation to write some data to .localStorage.
• Replace all files in that directory with files from my new manifest v3 extension which accesses .localData via an offscreen document.
• In Chrome, Reload the extension.
• Perform an operation to read data from .localStorage – Voila, the previously written data appears.

Since I don't see this documented anywhere that I can find, could you please confirm that this is expected and supported?  Did the Chrome developers code this magic to make migration just work, or is it an accident?  It is not obvious to me that a background page and an offscreen window would share their .localStorage.

Patrick Kettner

unread,
Sep 29, 2023, 9:36:00 AM9/29/23
to Jerry Krinock, Chromium Extensions, Pavel Aronovich
both pages would be from the same origin (i.e. chrome-extension://{{hash}}), so this is expected behavior.

--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/f50b7e41-6e9c-4b8e-be80-05ccb611baa0n%40chromium.org.
Reply all
Reply to author
Forward
0 new messages