Why a bug happens in production and not locally
A bug that only shows up in production, or only some of the time, depends on a condition your machine lacks or meets only now and then. The table lists the usual ones and how to recreate each.
| In production | What it can break | How to recreate it |
|---|---|---|
| A slow, uneven network | An older response overwrites a newer one, or a slow request invites a second click. | Slow down one request on purpose (below). |
| Real data | Long lists, empty fields, unusual characters, other time zones. | A fixture with the failing record's shape, minus personal data. |
| The production build | React's Strict Mode runs an extra setup and cleanup of every Effect in development only (React docs). Minifiers often rename functions, which breaks code that checks a function's name (MDN). | Build for production and serve that build locally. |
| Environment variables and flags | Vite bakes import.meta.env values into the bundle at build time (Vite docs). Flags can differ per user. | Build with the production values and the user's flags. |
| Caches and the CDN | A tab opened before a deploy, or a page whose HTML was cached without Cache-Control: no-cache, runs the old code, which can ask for files the host has deleted (Vite docs). Next.js lists a CDN that changes the HTML among the causes of hydration errors (Next.js docs). | Keep using a tab opened before a deploy, and check the HTML's cache headers. |
| Browsers and extensions | Extensions that change the page's HTML are on the same list (Next.js docs). | Match the user's browser version and extensions. |
| How fast people click | Double submits, and clicks before the page has loaded. | Double-click in a test, or click while requests are in flight. |
The network and click rows are the hardest to spot, because nothing throws. The guides to out-of-order responses and duplicate submissions cover them.
What to ask the person who saw it
Many reports arrive without steps. Ask for facts you can match against your logs:
- When it happened, with the time zone, and the page address.
- The browser and its version, the device, and whether extensions were on.
- What they clicked or typed just before, and how quickly.
- A HAR file of the network log. Chrome exports a sanitized HAR by default, without cookies or Authorization headers; one with sensitive data needs a setting turned on first (Chrome DevTools).
Read production stack traces with source maps
A production stack trace points into minified code, such as index-4f2a.js:1:20931, and a source map leads back to your file, line and function name. In Vite, build.sourcemap: "hidden" writes the maps without the comment that points browsers to them (Vite docs). Upload them to your error tracker with the build's version, and leave them out of what you deploy. Many production-only bugs throw nothing, though, and for those you need the order of events.
Put a request id in every log line
To match a user's report with your server logs, give every request an id, send it as a header and log it on both sides. Also log the order in which requests were sent and answered: in a timing bug, that order is the evidence.
let sent = 0;
let arrived = 0;
export async function apiFetch(url, init = {}) {
const id = crypto.randomUUID();
const headers = new Headers(init.headers);
headers.set("X-Request-Id", id);
const entry = { id, path: new URL(url, location.href).pathname, sent: ++sent };
const started = performance.now();
try {
const res = await fetch(url, { ...init, headers });
entry.status = res.status;
return res;
} catch (error) {
entry.error = error.name;
throw error;
} finally {
entry.arrived = ++arrived;
entry.ms = Math.round(performance.now() - started);
log(entry); // your logging call; the server logs the same id
}
}
Two lines from one page load with sent: 1, arrived: 2 and sent: 2, arrived: 1 mean the older answer came back last, the order a stale-results bug needs. Log the path, not the full address: query strings can hold search terms and tokens. A cross-origin API has to allow the header in Access-Control-Allow-Headers (MDN). With distributed tracing, the W3C Trace Context header traceparent carries one trace id across services.
Slow the network down unevenly
A throttling preset such as Slow 4G slows the whole connection (Chrome DevTools), so requests sent in order still tend to come back in order. Many timing bugs need one request to be slower than the next:
- In Chrome DevTools, right-click a request in the Network panel and choose Throttle request; closing DevTools turns the rule off (Chrome DevTools).
- With Mock Service Worker in the browser,
delay()with no argument waits a random time of about 100 to 400 ms before the response (MSW docs). - In a Playwright test, a route handler can wait before
route.fallback()lets the request go on (Playwright docs). Derive the waits from a seed, so a failing run can be repeated with the same waits.
Repeat the steps until the bug shows up and note which request was slow. The race condition simulator shows the effect in your browser.
What a session replay can show
A session replay shows what happened in a user's browser. Sentry's Session Replay plays back the page's recorded DOM state, masks all text, images and user input by default, and records request and response bodies only if you opt in (Sentry docs). A replay fills in the steps a report left out, such as where the user clicked and which requests ran then. It cannot show that a fix works, because the recorded session ran your old code.
Turn it into a test that fails every time
Once you know the condition, write it into a test with fixed data and a fixed order. This Playwright test, for a search box that sends a request as you type, holds back the answer for the older query until the newer results are on screen.
import { test, expect } from "@playwright/test";
const isQuery = (q) => (url) =>
url.pathname === "/api/search" && url.searchParams.get("q") === q;
// two animation frames: time for the page to apply a response that just arrived
const twoFrames = () =>
new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r)));
test("results match the last query when the older answer lands last", async ({ page }) => {
let asked, release, delivered;
const paAsked = new Promise((resolve) => (asked = resolve));
const held = new Promise((resolve) => (release = resolve));
const done = new Promise((resolve) => (delivered = resolve));
// Fixed data (in your API's shape), and the answer for "pa" held back.
await page.route(isQuery("pa"), async (route) => {
asked();
await held;
await route.fulfill({ json: [{ name: "Panama" }] });
delivered();
});
await page.route(isQuery("paris"), (route) =>
route.fulfill({ json: [{ name: "Paris" }] }),
);
await page.goto("/search");
const box = page.getByRole("searchbox");
await box.fill("pa");
await paAsked; // the page asked for "pa" (after any debounce)
await box.fill("paris");
await expect(page.getByRole("listitem")).toHaveText(["Paris"]);
release(); // now the older answer arrives
await done;
await page.evaluate(twoFrames);
await expect(page.getByRole("listitem")).toHaveText(["Paris"]); // fails while the bug is there
});
Waiting for the request for "pa" covers a search box with a debounce, and the two animation frames let the page apply the late answer before the last check. Run it with --repeat-each 10 to see it fail every time, then keep only the answer to the latest query and run it again. More patterns: testing race conditions with Playwright.
Where GenClass fits
GenClass's MCP server, @genclass/mcp, gives your coding agent each click and request on your app's development build, the order responses were delivered in and what each wrote into the app's state. A response written over newer data is a stale-write finding. While the agent or a test repeats the steps, genclass_explore delivers the responses in seeded orders and reports what ends differently from the in-order run. genclass_replay delivers one recorded order again and says whether the bug was reproduced, before and after your fix, and genclass_explain writes a Playwright test that fails while the bug is there. On the server's end-to-end test (one search page, headless Chromium), explore found a stale-results race in 5 of 6 reordered runs and 0 of 6 on the fixed page: one page and one bug class, not a detection rate. Steps: debug race conditions with MCP.
It cannot replay a real visitor's response order. If your live site runs GenClass, npx genclass agent link gives the MCP server your dashboard's private link, so the agent can read the counts by diagnosis and page and explore that page locally. Explore does not reorder WebSocket or EventSource messages or hold failure responses, and a replay covers only the order it delivers.
In CI, @genclass/playwright runs GenClass in observe mode on every page your tests open and, with chaos on, holds matched requests for seeded times without changing them. To set up the MCP server, run npx genclass in your app's folder.
Questions
Why does a bug happen in production but not locally?
Production differs from your machine in network speed and response order, data, the build, settings, caches and browsers. Find the difference the bug needs and recreate it locally.
How do I reproduce an intermittent bug?
Repeat the steps under changing conditions, such as one slow request, until it fails, and note what was different. Then hold that condition in a test with set data and a set order.
Why does the bug go away when I add console.log or a breakpoint?
A breakpoint pauses your code while responses keep arriving, and logging adds a little time, so the order the bug needs may stop happening. Record the order of events in every run and read it afterwards instead.
Can I reproduce a bug from a HAR file?
Playwright's routeFromHAR serves the recorded responses, matched by URL and method, and by body for POST requests. It does not replay the recorded timing, so if the order matters, hold the older response back in your test.