AleannLab

Our Site Crashed Only in Safari: The history.replaceState Limit Nobody Tells You About

AleannLab team3 min read

Safari allows 100 calls to history.replaceState() per 10 seconds. Call it more often and Safari throws SecurityError: Attempt to use history.replaceState() more than 100 times per 10 seconds. If that call sits in a scroll handler, a fast scroll is enough to crash a React or Next.js page.

On this page
  1. What the code did
  2. The fix
  3. Where else this bites
  4. Why nobody noticed
  5. Checklist

This happened on our own site in November 2026. Chrome and Firefox were fine. A colleague scrolled our homepage in Safari, the page went black, and Next.js showed its "Application error: a client-side exception has occurred" screen. She sent a 9-second screen recording. The answer was in the Web Inspector console in the corner of the video.

What the code did

The homepage updates the URL hash as you scroll, so #services or #team appears in the address bar when that section reaches the middle of the screen. The handler looked reasonable at first glance:

JavaScript
const handleScroll = () => {
  if (ticking) return;
  ticking = true;
  requestAnimationFrame(() => {
    for (const section of document.querySelectorAll('section[id]')) {
      const top = section.getBoundingClientRect().top;
      if (top >= 0 && top <= window.innerHeight / 2) {
        window.history.replaceState(null, '', `#${section.id}`);
        break;
      }
    }
    ticking = false;
  });
};

The requestAnimationFrame throttle limits the work to one run per frame. One run per frame still means up to 60 replaceState calls a second, and the code called it even when the hash had not changed. Safari's limit is 100 calls in 10 seconds. A two-second scroll was enough.

Next.js App Router also patches history.replaceState to keep its own router state in sync. The exception escaped from inside that patched call, so React never got a chance to recover and the whole page unmounted.

The fix

Two rules: only call replaceState when the hash actually changes, and never more than once every 300 ms.

JavaScript
const MIN_INTERVAL_MS = 300;
let lastHash = window.location.hash;
let lastUpdate = 0;
let pending = null;

function updateHash(hash) {
  if (hash === lastHash) return;
  const wait = MIN_INTERVAL_MS - (Date.now() - lastUpdate);
  if (wait > 0) {
    clearTimeout(pending);
    pending = setTimeout(() => updateHash(hash), wait);
    return;
  }
  try {
    window.history.replaceState(window.history.state, '', hash);
    lastHash = hash;
    lastUpdate = Date.now();
  } catch {
    // Rate-limited by the browser. Skip this update.
  }
}

Two details matter here:

  • Pass window.history.state instead of null. Next.js stores router data in that state object, and wiping it can break back-button behaviour.
  • The try/catch is a safety net, not the fix. The dedupe and the interval are what keep you under the limit.

We simulated 10 seconds of fast scrolling across eight sections. The old handler made 84 calls; real momentum scrolling in Safari goes well past 100. The new one made 7, one per section change.

Where else this bites

Any code that writes to the URL in a loop can hit the same wall:

  • Scroll-spy navigation that syncs the active section into the hash.
  • Search inputs that push every keystroke into ?q=.
  • Map or chart components that store zoom and position in the query string on every move.
  • Infinite lists that update ?page= as you scroll.

The same pattern fixes all of them: compare before you write, and debounce. For search inputs, update the URL on a 300-500 ms debounce or on submit.

Why nobody noticed

Our site had no error monitoring in the browser. The crash only happened in Safari, only on a fast scroll, and only on pages with the scroll handler. Nobody on the team uses Safari as their main browser. Without a colleague who happened to try it, we would have kept losing every Safari visitor who scrolled quickly.

The same week we added Sentry to the site. Three days later it caught a different production error that nobody had reported. We wrote that up separately: a 30-minute Sentry setup for Next.js.

Checklist

  1. Search your codebase for replaceState and pushState.
  2. For each call, check whether it can run more than a few times a second.
  3. Add a "did the value change" check before the call.
  4. Debounce or throttle to 300 ms or more.
  5. Test in Safari with a fast trackpad scroll, with the console open.
  6. Add browser error monitoring so the next one reaches you before a user does.

If your app crashes for some users and you cannot reproduce it, that is the kind of problem our app rescue and security audit starts with.

All articles →

Gotowy, aby omówić swój projekt z nami?

idea
*
*
*
Interesuje mnie...
Tworzenie stron internetowych
Tworzenie aplikacji
Projektowanie UX/UI
DevOps
Sztuczna inteligencja i uczenie maszynowe
Testowanie QA
Tworzenie aplikacji mobilnych
Konsultacja
Outsourcing
Budżet projektu (USD)
≤ $5K
$10K-$20K
$30K-$40K
$50K-$60K
>$100K
Prześlij plik