DEV Community

Cover image for Autopsy #06: The Search Bar That Ate the First Three Letters
frontend.rocks
frontend.rocks

Posted on Edited on Originally published at frontend.rocks

Autopsy #06: The Search Bar That Ate the First Three Letters

One small timestamp above the search bar

Thomas asked for a small "Updated today at 14:02" note in the catalog header, sitting right above <input name="q">, so buyers could tell the inventory feed was fresh. Julie wrote new Date().toLocaleTimeString() directly in the header component and centered it above the search input.

Our production servers run in UTC. Julie's laptop in London during winter also runs in UTC. Zero warnings in local dev, zero warnings in staging, and every Playwright check passed on the CI runner, which also runs in UTC. We merged the PR before lunch.

Thirty minutes after deploy, Nadia posted a screen recording from a customer in New York. The page appeared at 320 milliseconds, the customer clicked the search bar and typed cha, and at 1.4 seconds the search bar blinked, lost focus, and came back completely empty. It had eaten the first three letters and left the cursor sitting nowhere.

Hiding the warning, breaking the layout

Julie spotted the hydration mismatch warning the moment she switched her browser timezone to America/New_York in DevTools Sensors. Her first fix was two edits: suppressHydrationWarning on the timestamp <span>, and a mounted guard around the search bar so the client would not fight the server.

const [mounted, setMounted] = useState(false);
useEffect(() => setMounted(true), []);
if (!mounted) return null;
Enter fullscreen mode Exit fullscreen mode

The console was spotless. Not a single warning remained.

In production, CLS jumped to 0.28 and mobile search abandonment climbed 9%. Because !mounted returned null during SSR, the search bar was no longer in the initial HTML at all. At 1.4 seconds, when the JavaScript bundle finished booting, the search bar popped into existence above the catalog and shoved the entire page downward just as people were reaching for the first product card.

What happened between 320 ms and 1.4 s

Our streaming SSR pipeline flushed the header HTML chunk at 320 milliseconds. That HTML contained a real, working browser <input> element, so the user could focus it and type cha long before the JavaScript bundle finished downloading over a mobile connection.

Unlike resumable architectures that serialize event listeners into HTML attributes and resume from where the server left off, React's hydration has to re-execute the component tree on the client at 1.4 seconds and compare the virtual tree against the existing DOM node by node.

Two things diverged on that first client pass. First, the timestamp string differed between 14:02:00 in UTC on the server and 9:02:00 AM in New York or 15:02:00 in Paris on the client. Second, the parent header component also read window.location.search directly instead of reading URL state passed from the server, which sent the client down a different conditional branch whenever query parameters were present.

When React hit the mismatch during hydration reconciliation, the client-side router wrapper and hydration boundary bailed out of reusing the subtree, tore down the existing DOM nodes (including the <input> holding cha and the user's focus), and mounted fresh empty nodes from scratch.

Aligning the server and client trees

Interactive Failure Simulator — Tap to step through the live model

Why controlled inputs still wiped the text

First try after removing the mismatch: when we tested on Slow 4G and typed cha at 500 ms before the bundle loaded, the letters still vanished at 1.4 seconds.

// First try: controlled input initialized to "" overwrites pre-hydration typing
const [query, setQuery] = useState(initialQuery ?? '');
return <input name="q" value={query} onChange={(e) => setQuery(e.target.value)} />;
Enter fullscreen mode Exit fullscreen mode

Because value={query} was controlled and initialQuery was '', React's hydration pass synced the DOM .value property to '' and erased everything typed during the 1-second gap. Switching to defaultValue={initialQuery} (and reading inputRef.current.value on mount to seed client state if the user had already typed before hydration) left the user's early keystrokes untouched.

What stayed in the codebase

Fast SSR gives the user a one-second head start on your JavaScript; hydration should not confiscate what they typed during that second.

Thomas still has his "Updated today" badge in the header, and in Paris it even shows Paris time. More importantly, CLS dropped back to 0.01 and buyers on mobile connections no longer lose their search query mid-word.

Top comments (0)