Web Worker
Runs JavaScript in a separate thread.
Workers, IndexedDB and Cache API support heavier browser applications: background work, structured storage and offline-capable caching.
const worker = new Worker("/worker.js", { type: "module" });
if ("caches" in window) {
const cache = await caches.open("app-v1");
}
Browser APIs & BOM
Web Workers run JavaScript off the main thread. They are useful for CPU-heavy work that would otherwise freeze the interface.
IndexedDB stores larger structured data asynchronously in the browser. It is more capable than localStorage but more complex.
The Cache API stores Request and Response pairs and is commonly used with service workers for offline and performance strategies.
Runs JavaScript in a separate thread.
Async structured browser database.
Stores request/response pairs.
Network proxy layer for offline and caching strategies.
Examples
if ("Worker" in window) {
const worker = new Worker("/worker.js", { type: "module" });
}
for (let index = 0; index < 1_000_000_000; index += 1) {
calculate(index);
}
// The interface freezes while this runs.
Code patterns
These examples focus on practical browser APIs: URL state, storage, cookies, clipboard, permissions, observers and offline-capable platform tools.
Move heavy work off the main thread.
const worker = new Worker("/worker.js", { type: "module" });
worker.postMessage({ type: "start" });
Workers communicate by messages.
worker.addEventListener("message", event => {
console.log(event.data);
});
Cache API is promise-based.
const cache = await caches.open("app-v1");
await cache.add("/styles.css");
Advanced APIs need support checks.
if ("indexedDB" in window && "caches" in window) {
enableOfflineFeatures();
}
Rules that matter
Browser APIs are powerful because they touch privacy, navigation, storage, permissions and performance. Use them deliberately.
Keep the main thread responsive.
Do not force large data into localStorage.
It pairs naturally with service workers.
Old cached assets need update strategies.
Support varies by browser and context.
Offline data needs cleanup and migration.
Production thinking
Advanced browser apps need background work, durable storage and reliable caching to feel native-level fast.
Background work should not block input, focus or visible feedback.
Production offline features need versioning, cache invalidation and clear fallback behavior.
Service workers and caches should not serve stale or broken content to users or crawlers.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
If you can explain support, permissions, fallback and cleanup, you are ready to use the API responsibly.
Senior audit upgrade
Chapter checkpoint
Finish this chapter by turning the lessons into a small practical proof.
Build a small example that combines three lessons from this chapter.
Can you explain the important tradeoff without reading from the page?