Skip to content

Teach change detection with OnPush - #40

Merged
gpbsumido merged 5 commits into
ps/ng-08-formsfrom
ps/ng-09-change-detection
Nov 21, 2026
Merged

gpbsumido merged 5 commits into
ps/ng-08-formsfrom
ps/ng-09-change-detection

Conversation

@gpbsumido

@gpbsumido gpbsumido commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner

Ninth lesson in my Angular guide: change detection. The feature slice puts every component on ChangeDetectionStrategy.OnPush and adds a test that keeps it there.

What shipped

Every component is OnPush. None of the 35 components set a strategy before this, so all of them were checked on every pass.

Before switching, I audited each one for state that changes outside signals and template events, the kind OnPush stops rendering. I found none, so the switch is a one-line change per decorator.

  • The closest call is the contact form's form.reset() inside a setTimeout. It renders only because submissionStatus.set('success') sits next to it. It now has a comment saying so.

A fitness test keeps it that way. architecture.spec.ts reads every @Component decorator with the TypeScript parser. It fails, naming the class, on any component that doesn't choose OnPush or an explicit Eager.

  • The RED version split files on the text @Component(, which also matched five code samples printed inside the lessons.
  • A second test pins that the parser skips those samples.

Lesson 09: change detection (/thoughts/angular-09-change-detection).

  • Covers the material at three levels (simple, intermediate, expert), then pitfalls and interview Q&A:
    • zoneless scheduling
    • what marks an OnPush view dirty
    • signals and local change detection
    • NG0100
    • markForCheck vs detectChanges vs detach
    • the 21.2 rename of Default to Eager
  • The live demo passes one plain object to an Eager card and an OnPush card:
    • mutating it updates only the Eager card
    • replacing it updates both
    • a shared signal reaches the OnPush card with no input change at all

Version and changelog. 1.4.8, with a matching changelog entry.

Correction, made in #42

The contact form comment in this PR is wrong. form.reset() doesn't need the signal write next to it to re-render: reactive form controls keep touched and status in signals, and NgControlStatus reads them in its host bindings. A spec in #42 resets the form with no other change and no detectChanges(), and the errors clear. The comment and the lesson 09 intro are corrected there, on top of this branch.

A build hang I hit on the way

At first the two demo cards shared one styles constant. With two components carrying byte-identical inline styles, @angular/build 21.2.18 left its esbuild service process referenced. ng build and ng test both finished their work (77 routes prerendered, 426 tests passed) and then never exited.

I bisected it in a separate worktree:

  • HEAD exits 0.
  • The OnPush switch alone exits 0.
  • Any tree containing the lesson file hangs.
  • Making the two style strings differ exits 0.

A handle dump on the hung process showed the culprit:

ACTIVE RESOURCES [ 'ProcessWrap' ]
HANDLE ChildProcess [ '.../@esbuild/darwin-arm64/bin/esbuild', '--service=0.28.1', '--ping' ]

The fix is to stop sharing the styles: the lesson component styles both card hosts, and the cards have no styles of their own. A comment at the template constant records why.

Commits

  1. Add a failing fitness spec for OnPush everywhere and lesson 09
  2. Find components with the TypeScript parser in the OnPush fitness test
  3. Switch every component to OnPush
  4. Write lesson 09 with an Eager vs OnPush demo
  5. Bump to 1.4.8 with the lesson 09 changelog entry

Verification

  • RED first. The fitness spec failed, listing every component without a strategy. The lesson 09 spec didn't compile until the lesson existed.
  • Tests. ng test: 60 files, 426 tests, exit 0. That covers the demo's three behaviours and axe on the lesson page. No existing spec needed changes for OnPush.
  • Static checks. Lint and both tsc --noEmit configs are clean.
  • Production build. Exit 0, no budget warnings. Initial bundle 392.09 → 392.64 kB. 77 routes prerendered, up from 75 with the lesson page and the change-detection tag page.
  • Prerendered output. The prerendered lesson shows "Part 9 of 9" and both cards at 0 visits.

TO-DOs for this PR

  • Before/after screenshots of the lesson 09 demo after pressing "mutate", light and dark. Chromium couldn't launch in the environment I built this in. The OnPush switch itself has no visual change.

Splitting files on '@component(' also matched the code samples the lessons
print, so five strings counted as components. The parser only sees real
class decorators, and a second test pins that.
All 35 components were checked on every pass. None of them holds state
that changes outside signals or template events, so the switch needs no
other code changes. The contact form's reset gets a comment: it renders
only because a signal write sits next to it.
One plain object goes to an Eager card and an OnPush card. Mutating it
updates only the Eager one; replacing it updates both; a shared signal
reaches the OnPush card with no input change at all.

The cards are styled from the lesson component. Sharing one styles
constant between them left an esbuild context open in @angular/build
21.2.18, and ng build and ng test never exited.
@gpbsumido
gpbsumido marked this pull request as ready for review November 21, 2026 16:13
@gpbsumido
gpbsumido added this pull request to stack #46 November 21, 2026 17:00
@gpbsumido
gpbsumido merged commit 530c68f into develop Nov 21, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant