跪拜 Guibai
← All articles
Frontend · JavaScript · Browsers

Build Passed, Browser Still White: When Missing APIs Like findLast and randomUUID Blank the Screen

By Wilson王艺谋 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Frontend teams shipping to older browsers and embedded WebViews pay for missing API coverage at render time, not build time. A hand-rolled polyfill can return a valid-looking `-1` on legal input, and a secure-context API fails on plain HTTP long after CI reports success.

Summary

Two production incidents pin down what a green build can miss. In one, a component called `items.findLast(...)` and old browsers threw `TypeError: items.findLast is not a function`; in another, client initialization crashed because `crypto.randomUUID` was not available. Both failures happen during execution, after the build tooling has already succeeded, and both blank the first screen.

Fixing them means sorting failures into three buckets. Syntax the target browser can't parse, like optional chaining, needs transpilation. Missing built-ins, like `findLast`, need a polyfill loaded before first use; `core-js` provides targeted entry points such as `core-js/actual/array/find-last`. A handwritten fallback can look fine on a normal array and still be wrong: on an array-like object with `length` set to `2 ** 32`, the `>>> 0` coercion turns the length to `0` and the loop never runs, so the function returns `-1` where the spec returns `4294967295`. For environment-dependent capabilities, detect at runtime and degrade deliberately. `randomUUID()` only exists in secure contexts, so a fallback can feed `getRandomValues()` bytes into the `uuid` library and return `undefined` when neither source is available.

The decision table maps each failure mode to a treatment and a verification step: transpile and parse the real artifact, polyfill and simulate the method missing before first call, or feature-detect and exercise each fallback path. Load order matters for React, Vue, and Next.js entry points, prototype patches are scoped to an execution context so iframes and Workers need their own, and TypeScript's `lib` does not tell you what a runtime exposes.

Takeaways
— Build success only means the toolchain produced an artifact; the browser can still white-screen on parse errors or missing methods during execution.
— Array.prototype.findLast first arrives in Chrome 97, Firefox 104, and Safari 15.4; optional chaining needs Chrome 80, Firefox 74, or Safari 13.1.
— A handwritten findLastIndex fallback using `>>> 0` fails on an array-like with length `2 ** 32` because the coercion yields `0`; the native method returns `4294967295`.
— `core-js` offers targeted imports like `core-js/actual/array/find-last` and `core-js/actual/array/find-last-index` to load before first call, and `core-js-pure` for local implementations in libraries.
— `crypto.randomUUID()` is only available in secure contexts; environments with `getRandomValues()` can generate UUID v4 using that, and a generator should return `undefined` when neither is present.
— Transpile syntax the target cannot parse, polyfill missing APIs, and feature-detect with runtime fallbacks for environment-dependent capabilities.
— TypeScript's `lib` only affects type checking; runtime availability must be verified in the actual browser or WebView.
Conclusions

Build success and runtime failure live in different layers, so verification needs to happen in the actual target runtime rather than in CI or TypeScript's type checker.

A reverse loop that looks like a correct implementation of `findLastIndex` can produce a legal but wrong return value on a valid array-like object, which makes hand-rolled polyfills riskier than spec-tested libraries.

A missing method is not one problem: `findLast` is a pure JS built-in that a polyfill can replace, while `randomUUID` is gated by secure context and requires a capability-detection fallback, so the same-looking `TypeError` forces different fixes.

Concepts & terms
polyfill
Code that supplies a compatible implementation for a missing or incomplete browser API, loaded before the application calls it so older engines don't throw on first use.
secure context
A browser context considered trustworthy, generally HTTPS or localhost, where APIs like Web Crypto's `randomUUID()` are exposed; plain HTTP pages do not get those methods.
array-like object
An object with a numeric `length` and indexed properties but not an `Array`; standard array methods can run on it via `.call()`, and implementations must handle lengths up to `2 ** 32 - 1`.
conditional exports
A `package.json` mechanism that lets a library expose different entry points per environment, so browser and Node.js builds pick the right dependencies without bundling incompatible ones.
From the discussion
Featured comments
卷土的土 1 likes

Patch loading order also needs to cover first-screen testing.

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