[Bug] Blockly tool crashes on load — ViewChild resolves undefined under *transloco (same class as diagram #314 finding) #342

Closed
opened 2026-07-03 18:08:46 +00:00 by spikerj · 1 comment
Owner

Found by the P1 anonymous walk after tightening the console-error assertion (#314, epic #307) — Angular's ErrorHandler swallows this into console.error, so the previous pageerror-only assertion missed it.

Symptom

Opening /tools/(tools:blockly) logs:

ERROR TypeError: Cannot read properties of undefined (reading 'nativeElement')
    at s.initializeBlockly (...)
    at s.ngAfterViewInit (...)

The workspace never initializes.

Root cause

blockly.component.ts queries #blocklyDiv with a decorator @ViewChild and calls initializeBlockly() from ngAfterViewInit. The element sits inside the *transloco structural directive, whose embedded view renders only after translations load — at ngAfterViewInit the query is still undefined. Same class as the diagram tool bug fixed in spikersoft-angular#104, but a bare guard isn't enough here: the view can render after ngAfterViewInit, so guarding would leave the tool permanently uninitialized.

Fix

Signal viewChild query + an effect that runs initializeBlockly(div) exactly once when the element actually renders (idempotence via the existing workspace signal).

Found by the P1 anonymous walk after tightening the console-error assertion (#314, epic #307) — Angular's ErrorHandler swallows this into `console.error`, so the previous pageerror-only assertion missed it. ## Symptom Opening `/tools/(tools:blockly)` logs: ``` ERROR TypeError: Cannot read properties of undefined (reading 'nativeElement') at s.initializeBlockly (...) at s.ngAfterViewInit (...) ``` The workspace never initializes. ## Root cause `blockly.component.ts` queries `#blocklyDiv` with a decorator `@ViewChild` and calls `initializeBlockly()` from `ngAfterViewInit`. The element sits inside the `*transloco` structural directive, whose embedded view renders only after translations load — at `ngAfterViewInit` the query is still `undefined`. Same class as the diagram tool bug fixed in spikersoft-angular#104, but a bare guard isn't enough here: the view can render *after* `ngAfterViewInit`, so guarding would leave the tool permanently uninitialized. ## Fix Signal `viewChild` query + an `effect` that runs `initializeBlockly(div)` exactly once when the element actually renders (idempotence via the existing `workspace` signal).
spikerj added the bug label 2026-07-03 18:09:00 +00:00
Author
Owner

Resolved in spikersoft-angular PR #106 (merged to master). Replaced the decorator @ViewChild with a signal viewChild + a run-once init effect, so Blockly initializes whenever the *transloco view actually renders. Verified by the tightened anonymous walk (tools spec green). Closing.

Resolved in spikersoft-angular PR #106 (merged to `master`). Replaced the decorator `@ViewChild` with a signal `viewChild` + a run-once init effect, so Blockly initializes whenever the `*transloco` view actually renders. Verified by the tightened anonymous walk (tools spec green). Closing.
Sign in to join this conversation.