From c73981da2d2fa0749c9a2004c7979c7c4359bb6f Mon Sep 17 00:00:00 2001 From: Bendik Aagaard Lynghaug Date: Wed, 5 Aug 2026 14:55:55 +0200 Subject: [PATCH] yes.js: guard against the canvas not being in the DOM yet The Rust-side NodeRef gate (previous commit) didn't actually close the race on fast client-side re-navigation back to / - reproduced the same crash again after that fix shipped. Harden the actual failure point directly instead of chasing the exact Leptos/wasm-bindgen timing: skip setup (not throw) if either canvas is missing. --- yes.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/yes.js b/yes.js index 4a02419..0d746b2 100644 --- a/yes.js +++ b/yes.js @@ -11,6 +11,22 @@ export class RasterizedYES { constructor() { this.rasterCanvas = document.getElementById('rasterCanvas'); this.lineCanvas = document.getElementById('lineCanvas'); + + // The Rust side only constructs this once it's confirmed (via a + // NodeRef) that the canvas is mounted, but that guard has shown + // a real gap on fast client-side re-navigation back to `/` - + // this is the actual failure point, so it gets its own defense + // rather than depending on getting that timing exactly right + // from the other side of the wasm boundary. Leaving the + // instance otherwise-inert (no crash, no animation) rather than + // throwing mid-render - `stop()` already tolerates a partially + // (non-)initialized instance. + if (!this.rasterCanvas || !this.lineCanvas) { + console.warn('RasterizedYES: canvas not in DOM yet, skipping'); + this.destroyed = true; + return; + } + this.rasterCtx = this.rasterCanvas.getContext('2d'); this.lineCtx = this.lineCanvas.getContext('2d');