跪拜 Guibai
← All articles
Frontend

A Missing Null Check Caused a Full-Site White Screen: The Hidden Cost of 'Defensive' Frontend Code

By 涛涛ing ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

A single unhandled null can take down an entire SPA, and 'defensive' patterns like try...catch can mask the root cause until users see white screens or, worse, duplicate charges. This incident provides a concrete playbook for building frontends that survive dirty data, stale caches, and silent render errors.

Summary

On a Friday afternoon, a senior frontend developer at a Chinese company watched production go down: every API returned 200, the console was clean, no Sentry errors, but the page was a dead white screen. Rolling back the day's release fixed it instantly, pointing to the new code. The culprit was a missing null check: a colleague's bug fix cleared the `ui_preferences` key from localStorage, so `JSON.parse(null)` returned null, and reading `.theme` off it threw a TypeError during React's render phase.

The silent failure happened because production error boundaries swallowed the exception, a deliberate choice to prevent sensitive data leakage. The incident became a case study in how defensive programming often backfires. Wrapping `JSON.parse` in try...catch can interrupt business logic, causing duplicate orders or dirty data when users retry. Scattering optional chaining (`?.`) prevents undefined errors but leaves UI in empty states that confuse users.

The author's team rebuilt its approach around three lines of defense: validate all external data with a safe parse that applies defaults; use error boundaries that report errors and show fallback UI; and add global listeners for resource loading failures that auto-reload the page. The takeaway: frontend engineering is the last line of defense for user experience, and the most valuable code is often the defensive code users never see.

Takeaways
— A missing null check on localStorage data caused a full-site white screen; JSON.parse(null) returns null, and reading a property off it throws a TypeError during render.
— Production error boundaries that suppress errors to prevent data leaks can silently kill the whole app, leaving no Sentry report.
— Wrapping complex logic in try...catch and logging can hide failures, leading users to retry and create duplicate orders or dirty data.
— Optional chaining prevents undefined errors but doesn't handle business empty states; a UI fallback like a skeleton screen or 'unnamed user' is the correct treatment.
— Data boundary validation should always include defaults: if localStorage is empty or corrupted, return a safe default object and merge parsed values.
— React ErrorBoundary and Vue's errorHandler should report production errors and show a degraded UI card instead of a blank page.
— Global listeners for unhandledrejection and resource loading failures can auto-reload the page, preventing stuck white screens from chunk load errors.
— The post-incident refactor produced a three-tier defense: data probing, error boundaries, and release-phase safety nets.
Conclusions

The real failure was not the null check itself but the production configuration that suppressed error reports; any error boundary that hides exceptions from monitoring is a liability, not a safeguard.

The pattern of swallowing exceptions in try...catch blocks is a systemic risk: it treats symptoms as acceptable outcomes while letting business logic break silently, and users end up paying for it.

The 'defense' mindset often focuses on code-level tactics like optional chaining without addressing data contracts; this incident shows why validating external inputs at the boundary is more robust than scattering defensive operators.

A single line of defensive code can be worth more than a full-featured monitoring tool, since monitoring only helps when errors are allowed to surface.

The article's framing of 'useless work' is a useful counterweight to the AI-hype narrative: stable code that handles edge cases remains the core value of frontend engineering.

Concepts & terms
Error Boundary
A React component that catches errors in its child component tree during rendering, lifecycle methods, and constructors, allowing the app to display a fallback UI instead of crashing the whole page.
Optional chaining (?.)
A JavaScript syntax that safely accesses nested object properties without throwing an error if a reference is null or undefined; it returns undefined instead, but does not handle business logic fallbacks.
Graceful degradation
Designing a system so that when a component or feature fails, it falls back to a reduced but still usable state (e.g., a retry card) rather than a blank screen or total crash.
Chunk load error
A runtime error that occurs when a JavaScript chunk (often from code splitting) fails to load, typically because the deployed file hash changed after an update, causing the browser to request an already-removed file.
From the discussion

The discussion centers on data boundary validation as the true first line of defense, with one reader detailing a `readJson(key, schema)` pattern that ties schema validation to local storage reads. A second comment reinforces that swallowing errors is more dangerous than any visible outage, agreeing that silent failures are the greater risk.

— LocalStorage defaults must not naively fall back to an object; validation should be centralized in a readJson helper that checks null, parses, and applies schema-based defaults.
— ErrorBoundary is a final safety net, not a substitute for data boundary validation, since it cannot recover already corrupted data.
— Schema validation raises a practical question: zod vs handwritten assertions, and whether dirty data from older versions should be discarded or migrated.
— On error handling, white screens are acceptable compared to silent crashes, which hide the real failure and make debugging harder.
Featured comments
badhope

I agree with the boundary validation part. The default value for localStorage should not fall back to an object. My current approach is to funnel all local reads through a `readJson(key, schema)` function: first check for null, then parse, then use the schema to fill in defaults. ErrorBoundary should only be the last line of defense, not a substitute for data boundaries. I'd like to ask: in your first line of defense, is the schema defined with zod or handwritten assertions? And when you encounter dirty data from an old version, do you discard it directly or migrate it?

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗