Full Stack Engineer Guide: Chrome Extensions in Manifest V3, Ephemeral Service Workers & Side Panels

Migrating legacy Manifest V2 browser extensions or building enterprise CRM/AI copilot extensions under Chrome Manifest V3 (MV3) breaks every architecture that relies on persistent background pages. In Manifest V3, the background Service Worker terminates after 30 seconds of inactivity—wiping all in-memory global variables—and strict Content Security Policies (CSP) forbid remotely hosted code. As a full stack engineer, I architect resilient Manifest V3 Chrome Extensions in TypeScript and React using chrome.storage.session, Side Panel APIs, and typed message buses.

1.Surviving Ephemeral Service Worker Termination in MV3

Never store auth tokens, WebSocket state, or active tab queues in top-level JavaScript variables inside background.ts. Register all event listeners synchronously at the top level of the Service Worker and persist hot runtime state in chrome.storage.session (in-memory encrypted session storage that survives Service Worker restarts):

// src/background/serviceWorker.ts
export interface CopilotRequestMessage {
  type: "ANALYZE_SELECTION";
  selectionText: string;
  pageUrl: string;
}

// 1. Always register listeners synchronously at top-level
chrome.runtime.onInstalled.addListener(() => {
  chrome.sidePanel.setPanelBehavior({ openPanelOnActionClick: true });
});

chrome.runtime.onMessage.addListener(
  (message: CopilotRequestMessage, sender, sendResponse) => {
    if (message.type === "ANALYZE_SELECTION") {
      handleAnalyzeSelection(message, sender.tab?.id)
        .then((result) => sendResponse({ ok: true, data: result }))
        .catch((err: Error) => sendResponse({ ok: false, error: err.message }));
      return true; // Required to keep the message channel open for async sendResponse
    }
    return false;
  }
);

async function handleAnalyzeSelection(msg: CopilotRequestMessage, tabId?: number) {
  const { apiSessionToken } = await chrome.storage.session.get("apiSessionToken");
  if (!apiSessionToken) {
    throw new Error("Session expired. Please sign in via the extension side panel.");
  }
  const resp = await fetch("https://api.example.com/v1/copilot/summarize", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${apiSessionToken}`,
    },
    body: JSON.stringify({ text: msg.selectionText, sourceUrl: msg.pageUrl, tabId }),
  });
  return resp.json();
}

2. Shadow DOM Isolation for Injected Content Script UI

When injecting floating toolbars or inline annotations into third-party web pages, mount your React root inside a closed or open attachShadow({ mode: "open" }) Shadow DOM container with bundled CSS so host-page Tailwind or Bootstrap rules never distort your extension UI.

3. Related Full-Stack & AI Copilot Engineering Resources


About the Author — Hire Aldian Fazrihady (Cloud Engineer, GCP Professional Cloud Architect, GCP Professional Data Engineer, Backend, AI & Full Stack Engineer)

Aldian Fazrihady is a Top-Rated Senior Cloud Engineer, GCP Professional Cloud Architect (Google Cloud Certified), GCP Professional Data Engineer (Google Cloud Certified), Backend Engineer (Python, Django, FastAPI, Go, PostgreSQL), AI Application Engineer (LLMs, RAG, MCP, Google ADK, LangGraph), and Full Stack Engineer (React, Next.js, TypeScript) with 15+ years of production software engineering experience.

Hire Aldian Fazrihady on Upwork | Book 1-Day Programming Service ($100 USD / Rp 1.000.000) | View Live AI Portfolio | Send a Direct Message

Related Engineering Articles

Engineering Role Pillar Guides & Category Hubs