Dev.to WebDev 🛠 Dev 👁 0 📖 2 min read

Service workers can't createObjectURL — how I made a Chrome extension auto-download its daily backup anyway

Service workers can't createObjectURL — how I made a Chrome extension auto-download its daily backup anyway SessionSnap is a small Edge/Chrome extension I build in public: it snapshots your open tabs so you can restore

Service workers can't createObjectURL — how I made a Chrome extension auto-download its daily backup anyway

SessionSnap is a small Edge/Chrome extension I build in public: it snapshots your open tabs so you can restore a session later, and it keeps a closed-tab log. One feature sounded trivial — every day, automatically download a JSON backup of your sessions, no click needed. It took three download paths to make it work, because the obvious one doesn't exist in a Manifest V3 service worker.

The feature

A daily alarm fires backupNow(), which serialises the saved sessions and downloads sessionsnap-backup-YYYY-MM-DD.json (no save-as dialog, overwrite silently). If the download fails, the stored copy stays put and the options page says so honestly, with a manual Download button as the fallback. Backups are far below the ~1.5 MB guard; anything larger is recorded as a clean failure instead of being forced through.

Rung 1: blob URL — works everywhere except where it runs

The first implementation did the obvious thing in the service worker:

const url = URL.createObjectURL(new Blob([json], { type: 'application/json' }));
chrome.downloads.download({ url, filename, saveAs: false });

My first live test of v0.1.2 caught it immediately: in an MV3 service worker, URL.createObjectURL is not a function. The fallback still worked as designed — the stored copy stayed, the failure was recorded, the options page showed it — but the auto-download itself failed every time. A unit test wouldn't have caught this; the function exists in pages, just not in workers.

Rung 2: an offscreen document

Chrome's answer for worker-only gaps is an offscreen document: a hidden extension page that can create blob URLs. The worker asks for one (reason BLOBS), sends it the JSON, and the page does the createObjectURL + chrome.downloads.download dance, then reports back. If the offscreen document already exists, Chrome throws "Only a single offscreen document may be created" — which is fine, it just means "use the one that's there".

One wrinkle: Firefox's background is a real page, not a worker, so the blob path works there and the offscreen permission is meaningless — the packager strips it from the Firefox build, and the lint warnings it used to produce went away with it.

Rung 3: a base64 data URL, with an honest limitation

If the offscreen route fails too, the last resort is a data: URL. It works, but Chrome ignores the filename for data-URL downloads, so the file lands as download.json. That's recorded in the result (filename ignored for data URLs; offscreen failed: …) rather than silently pretending the named file arrived.

Verified, not assumed

The re-test after the fix, against the real unpacked v0.1.2 Chrome package in headless Chrome: daily backup on, two restorable tab snapshots, backupNow() returned downloaded: true, the file sessionsnap-backup-2026-11-19.json really landed in the download directory — 913 bytes, parsed back with the right format, kind, and session/tab counts — and the options page showed "Auto-downloaded". Tests went 182 → 204.

The order matters: try the cheap path first, keep the honest record of which rung actually ran, and never let a silent failure look like a success. The same pattern — first live test finds the real defect, fallback keeps the data safe — is what the backup feature itself is for.

SessionSnap is live on Edge (v0.1.7) — the backup has been auto-downloading daily since v0.1.2.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.