I built a 3D product microsite as a portfolio project — a scroll-driven page where a speaker rotates and comes apart as you move down it. The product and the brand are both fictional; the point of the build was the rendering, not the marketing.
WebGL is the risky part of a page like this. It can fail, and when it fails on a page whose entire hero section is the 3D scene, the visitor gets a blank rectangle where the product should be. So most of the work went into a question that isn’t really about 3D at all: how do you guarantee the user always sees the product, no matter what the GPU does?
Three ways a WebGL scene fails, and only two of them tell you
1. WebGL isn’t available. Disabled in settings, blocked by policy, ancient hardware, or a browser running with a software renderer that refuses. This one is easy — you find out immediately when the context won’t create.
2. The context is lost mid-flight. The GPU driver resets, the OS reclaims resources, a mobile browser evicts a background tab, another tab hogs the GPU. The browser fires a webglcontextlost event and your scene freezes. Detectable, if you’re listening.
3. The context is created, and the first frame never arrives. No error. No event. Nothing throws. The canvas exists, it’s just permanently blank — a wedged driver, or a software fallback so slow it may as well be hung.
The third one is the nasty one, and it’s the one most fallback code misses. You can’t catch an event that never fires. You have to time it out.
The shape of the solution: three states, and the poster is already there
export type SceneStatus = "pending" | "ready" | "failed";
The important design decision is what pending looks like. The poster image — a pre-rendered still of the product — is server-rendered and on screen from the very first paint. The canvas mounts on top of it. The poster only fades out once the scene has proven it’s actually rendering.
That ordering matters more than it sounds. The intuitive approach is the opposite: mount the canvas, and if something goes wrong, swap in a poster. That gives you a blank rectangle during the window between mount and first frame, plus a visible pop when the fallback appears. Starting with the poster means there is no moment where the user sees nothing — the worst case is that they keep seeing a nice static image forever.
failed is terminal. No retry, no flicker between broken canvas and poster. On a page like this, a permanent graceful downgrade beats a scene that keeps trying and keeps failing.
Catching failure mode 2: the event, plus a per-frame check
useEffect(() => {
const canvas = gl.domElement;
const handleContextLost = (event: Event) => {
event.preventDefault();
onFailure("webgl-context-lost");
};
canvas.addEventListener("webglcontextlost", handleContextLost, false);
invalidate();
return () => {
canvas.removeEventListener("webglcontextlost", handleContextLost, false);
};
}, [gl, invalidate, onFailure]);
Then, separately, inside the render loop:
useFrame(() => {
if (reportedReady.current) return;
renderedFrames.current += 1;
const context = gl.getContext();
if (context.isContextLost()) {
onFailure("webgl-context-lost");
return;
}
// ...
});
Why both? Because the event only helps if you were listening when it fired. Loss can happen during setup, before the effect attaches the listener — and then you’d sit in pending forever waiting for an event that already came and went. Polling isContextLost() each frame catches the case the listener structurally cannot.
Belt and braces, deliberately. These are cheap checks guarding a failure that produces a blank page.
Catching failure mode 3: an 8-second timeout
useEffect(() => {
if (sceneStatus !== "pending" || !rendererAttemptAllowed) return;
const timeout = window.setTimeout(
() => onSceneFailure("first-frame-timeout"),
8_000,
);
return () => window.clearTimeout(timeout);
}, [sceneStatus, rendererAttemptAllowed, onSceneFailure]);
The whole idea: if the scene hasn’t proven itself in eight seconds, stop waiting for it. The timeout is armed only while pending, and it’s cleared the moment the status changes — so a scene that becomes ready never trips it.
This is the only defence against a silent hang, and it costs eleven lines.
The readiness check has to be strict
The counterpart to the timeout is deciding when the scene has earned the right to remove the poster. Getting this wrong is how you end up hiding a good poster to reveal a black box.
if (
renderedFrames.current >= 2 &&
gl.domElement.width > 0 &&
gl.domElement.height > 0 &&
gl.info.render.frame > 0
) {
reportedReady.current = true;
onReady();
return;
}
invalidate();
Four conditions, each ruling out a specific way of being wrong:
renderedFrames >= 2— one frame can be a partially-initialised fluke; two means the loop is actually turning over.width > 0 && height > 0— a canvas can mount with zero size while layout settles. Zero-size means nothing is visible, whatever the renderer thinks.gl.info.render.frame > 0— Three.js’s own counter. This is the difference between “my loop ran” and “the renderer actually drew something”.
“The component mounted” is not evidence that the user can see anything. Each of these turns an assumption into a check.
Note the trailing invalidate(). The scene runs on demand rather than a continuous loop, so nothing schedules the next frame automatically — while waiting for readiness, each frame has to explicitly request the one after it. Otherwise the readiness check runs once and the scene sits there, un-ready forever, waiting for a frame it never asked for.
What I’d flag as weak
- 8 seconds is a guess. It’s generous enough for a slow phone on a cold cache, but a genuinely slow device could get downgraded when it would have made it. A more careful version would key off whether any frame had rendered rather than wall-clock alone.
- No recovery. I never listen for
webglcontextrestored. A long-lived app should — for a scroll-through page, permanent downgrade is the better trade. - The poster has to be genuinely good, because for some visitors it is the product shot. If your fallback is an obvious placeholder, all this machinery just delivers disappointment reliably.
Takeaway
The reusable pattern isn’t WebGL-specific: start in the degraded state, and require the enhanced state to prove itself before you switch. Poster first, canvas has to earn its way in. Combine that with a timeout for failures that produce no error at all, and the worst outcome is a static image instead of a blank hole.
Detecting failure is the easy half. The hard half is admitting a failure can be silent, and putting a clock on it.
ORBIT ONE and NOVA FORM are fictional concepts built for a portfolio project — no real product, no commerce. Built with AI-assisted development: I define requirements and acceptance criteria, iterate on the implementation, then review, test and debug it.
