published fresh until

Why does my Chrome extension forget its data after a while?

Summary When an MV3 extension loses state after sitting idle or its timer stops, persist state in chrome.storage and use alarms; Chrome's service worker stops after about 30 seconds idle, loses globals, and has no localStorage.

Not for content script timing; Web Store review

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

The problem

An extension (often agent-written, often ported from Manifest V2) works right after you reload it, then later the popup shows empty or stale data, a polling setInterval never fires, or the background script throws that localStorage is not defined.

What Chrome's docs say

On the service worker lifecycle page (read 18 October 2026):

  • The worker is terminated after about 30 seconds of inactivity, when one event or API call runs past 5 minutes, or when a fetch() takes over 30 seconds to answer. Receiving an event or calling an extension API resets the idle timer. WebSocket message traffic (Chrome 116), active debugger sessions (118), native messaging connections (105) and long-lived messaging (114, where sending a message keeps it alive but merely opening a port does not) extend its lifetime.
  • Global variables are lost when it shuts down; save state to storage.
  • Use chrome.storage.session or local for JSON state, IndexedDB for structured data and blobs. localStorage is not available in extension service workers.
  • Use alarms for scheduled work; timers die with the worker. Alarms can have a minimum period of 30 seconds since Chrome 120.
  • Events wake a stopped worker, so register your listeners when the script starts, not inside a later callback.

How to ask your agent

Review background.js for state kept in module variables, setTimeout or setInterval, and localStorage. Move state into chrome.storage.session (or local if it must survive restart), replace timers with chrome.alarms, and register all listeners at the top level. Then add a test that stops the service worker and checks the popup still shows the data.

Pitfalls

  • Reading a variable right after the worker restarts gives undefined, which looks like a logic bug.
  • Keeping the worker alive with a ping loop treats the symptom; the docs list what legitimately extends its life.
  • Values in local storage are exposed to content scripts by default, and access can be restricted with setAccessLevel(); keep secrets out. See Chrome's storage API docs.

Limits

From Chrome's documentation, not from a reproduced bug. Version numbers are as the docs state them.

Sources

Primary source: Chrome's extension service worker lifecycle page, read on 18 October 2026.

  • Confirms: the 30-second idle shutdown, the 5-minute limit for one event or call, the 30-second fetch() limit, events and extension API calls resetting the timer, lost globals, chrome.storage and IndexedDB for state, no localStorage, and the 30-second minimum alarm period since Chrome 120.
  • Corrected above: the keep-alive versions differ by feature. Native messaging is Chrome 105, long-lived messaging 114 (sending a message keeps the worker alive, merely opening a port does not), WebSocket message traffic 116 and debugger sessions 118. This page does not discuss registering listeners at the top level, so that advice is not confirmed here.

Started from a post by @Chrome for Developers, packaged by the editor. Original post

Subjects

More in documentation

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Why does my Chrome extension forget its data after a while?
Summary: When an MV3 extension loses state after sitting idle or its timer stops, persist state in `chrome.storage` and use alarms; Chrome's service worker stops after about 30 seconds idle, loses globals, and has no `localStorage`.
Copy for agent
get the whatworks thread chrome-extension-mv3-forgets-state-service-worker-storage-alarms and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread chrome-extension-mv3-forgets-state-service-worker-storage-alarms
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks