Skip to content

Hydrate the menu bar - #43

Merged
gpbsumido merged 5 commits into
ps/ng-11-testingfrom
ps/ng-12-ssr
Nov 21, 2026
Merged

gpbsumido merged 5 commits into
ps/ng-11-testingfrom
ps/ng-12-ssr

Conversation

@gpbsumido

@gpbsumido gpbsumido commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner

Twelfth lesson in my Angular guide: server-side rendering and hydration. The feature slice makes the menu bar hydrate instead of being thrown away.

What shipped

Prerendered pages no longer show the build time. The menu bar clock rendered on the server, so every Thoughts page shipped the clock as it read during the build, until the JavaScript loaded. From the 1.4.10 build:

<span class="menu-bar-clock">Sat, Nov 21, 11:24 AM</span>
  • The clock is null until the browser takes over, and starts in afterNextRender, which never runs on the server.
  • That replaces an effect with no signal dependencies, guarded by isPlatformBrowser, that only existed to start a setInterval.

The menu bar hydrates. It was marked ngSkipHydration to hide the server/browser difference, so the browser discarded the server DOM for the whole bar and re-created it. With the difference gone, the attribute is gone. In the new build the bar carries a hydration annotation and an empty clock:

<app-menu-bar … ngh="2">
<span class="menu-bar-clock"></span>

menu-bar.server.spec.ts renders the bar through renderApplication, the real server platform, and asserts both. TestBed runs browser-only hooks, so it can't show what the server sends. Both tests failed before the fix:

AssertionError: server-rendered clock text: expected 'Sat, Nov 21, 11:29 AM' to be ''
AssertionError: expected '<app-menu-bar ngskiphydration="true" …' not to contain 'ngskiphydration'

Lesson 12: SSR and hydration (/thoughts/angular-12-ssr).

  • The demo says whether the page was prerendered or rendered in the browser. On the server the lesson writes its render time into TransferState; the browser reads it back (and removes it, so a later client-side visit doesn't claim to be server-rendered). In the build: "lesson-12-rendered-at":"2026-11-21T16:33:07.430Z".
  • Covers render modes per route, hydration and what breaks it, browser-only code, the HTTP transfer cache, event replay (with the real jsaction markup from the lesson 11 page), incremental hydration, PendingTasks and ngSkipHydration, then pitfalls and interview Q&A.

Bundle. My first version formatted the demo's time with DatePipe, and the initial bundle went to 407.07 kB, over the 400 kB budget: the same @angular/common trap as lesson 03. Intl.DateTimeFormat brought it back to 395.26 kB (394.92 kB before this PR).

Commits

  1. Add failing specs for a hydratable menu bar and lesson 12
  2. Start the menu bar clock after render so the bar can hydrate
  3. Write lesson 12 with a where-was-this-rendered demo
  4. Format lesson 12's render time with Intl to keep DatePipe out of the initial bundle
  5. Bump to 1.4.11 with the lesson 12 changelog entry

Verification

  • ng test --watch=false: 68 files, 471 tests, exit 0
  • npm run lint: exit 0
  • tsc --noEmit -p tsconfig.app.json and -p tsconfig.spec.json: exit 0
  • ng build: exit 0, 81 routes prerendered, initial 395.26 kB
  • The prerendered thoughts/angular-11-testing and thoughts/angular-12-ssr pages: empty clock, ngh on the menu bar, no ngskiphydration, the TransferState stamp present

TO-DOs for this PR

  • Hydration in a real browser is unverified: Chromium won't launch in my sandbox. Load a prerendered page with the console open and confirm there's no NG05xx hydration error and the clock fills in.
  • Before/after screenshots of the menu bar on a prerendered page, light and dark. Blocked on the same Chromium issue; capture script is in my local plan folder.

@gpbsumido
gpbsumido marked this pull request as ready for review November 21, 2026 16:34
@gpbsumido
gpbsumido added this pull request to stack #46 November 21, 2026 17:22
@gpbsumido
gpbsumido merged commit a9b1a9a 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