DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
  1. Home
  2. Articles
  3. Frontend Engineering
  4. How to Detect a Web Page Crash
XLinkedInReddit
MediumFrontend Engineering

How to Detect a Web Page Crash

D
DevPrep Team
February 9, 2026·2 min read·0
Table of Contents
  • Why is This Hard?
  • Method 1: Service Worker Heartbeat
  • Method 2: sessionStorage Flag
  • Method 3: Reporting API
  • Common Causes of Tab Crashes
  • Prevention Best Practices
  • Summary

By Rahul — Google Frontend Engineer

Why is This Hard?

When a tab crashes, your JavaScript stops running. You cannot run code to report the crash because... the code is not running anymore. It is like asking a dead person to call 911. So how do we detect it?

Method 1: Service Worker Heartbeat

The most reliable approach. Service workers run in a separate thread — they survive tab crashes.

// In your page
const HEARTBEAT_INTERVAL = 5000; // 5 seconds

// Send heartbeats to service worker
setInterval(() => {
  navigator.serviceWorker.controller?.postMessage({
    type: 'HEARTBEAT',
    tabId: sessionId
  });
}, HEARTBEAT_INTERVAL);

// In service worker
const activeTabs = new Map();

self.addEventListener('message', (event) => {
  if (event.data.type === 'HEARTBEAT') {
    activeTabs.set(event.data.tabId, Date.now());
  }
});

// Check for dead tabs
setInterval(() => {
  const now = Date.now();
  for (const [tabId, lastBeat] of activeTabs) {
    if (now - lastBeat > 15000) { // No heartbeat for 15s
      // Tab probably crashed
      reportCrash(tabId);
      activeTabs.delete(tabId);
    }
  }
}, 10000);

Method 2: sessionStorage Flag

// On page load, check if previous session ended cleanly
if (sessionStorage.getItem('loaded') === 'true' 
    && !sessionStorage.getItem('unloaded')) {
  // Page was loaded but never unloaded = crash!
  reportCrash();
}

// Set loaded flag
sessionStorage.setItem('loaded', 'true');
sessionStorage.removeItem('unloaded');

// Set unloaded flag on clean exit
window.addEventListener('beforeunload', () => {
  sessionStorage.setItem('unloaded', 'true');
});

This has false positives (force quit browser, power loss) but is simple and requires no service worker.

Method 3: Reporting API

// Modern browsers support crash reports natively
// Set the Reporting-Endpoints header
Reporting-Endpoints: crash-reports="https://example.com/reports"

// Or use the ReportingObserver API
const observer = new ReportingObserver((reports) => {
  for (const report of reports) {
    if (report.type === 'crash') {
      sendToAnalytics(report);
    }
  }
}, { types: ['crash'], buffered: true });
observer.observe();

Common Causes of Tab Crashes

  • Memory leaks: Unbounded arrays, retained DOM references, forgotten event listeners
  • Infinite loops: Recursive rendering, while loops with wrong conditions
  • Massive DOM: 100,000+ nodes cause the renderer to crash
  • WebGL/Canvas: GPU memory exhaustion
  • Out of memory: Loading huge files into memory, large base64 strings

Prevention Best Practices

  • Monitor memory usage with performance.measureUserAgentSpecificMemory()
  • Use virtual scrolling for large lists
  • Clean up event listeners and intervals on unmount
  • Set limits on user-uploaded file sizes
  • Use Web Workers for heavy computation

Summary

Detecting crashes is hard because your code dies with the tab. Use service worker heartbeats for the most reliable detection, sessionStorage flags for a simpler approach, or the Reporting API for modern browsers. Prevention is better — monitor memory and avoid unbounded growth.

Related Articles

MediumFrontend Engineering

System Design #12: Design a Multi-Step Form Wizard

7 min read
MediumFrontend Engineering

Mastering Senior-Level JavaScript Interview Concepts

2 min read
MediumFrontend Engineering

System Design #9: Design a Collaborative Text Editor

9 min read

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Table of Contents

  • Why is This Hard?
  • Method 1: Service Worker Heartbeat
  • Method 2: sessionStorage Flag
  • Method 3: Reporting API
  • Common Causes of Tab Crashes
  • Prevention Best Practices
  • Summary

Series

View all Frontend Engineering articles →

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.