Step 18: Hydration Fixed

The Solution

With hydration, the server pre-renders the HTML and sends it immediately. The page is interactive once JavaScript loads, but users can see content right away.

Don't forget to enable the hydration provider in app.config.ts

Benefits:

  • Immediate content visibility
  • Better First Contentful Paint (FCP)
  • Improved Largest Contentful Paint (LCP)
  • Progressive enhancement

How Hydration Works:

  1. Server pre-renders HTML with initial data
  2. Client receives and displays HTML immediately
  3. JavaScript loads and "hydrates" the page
  4. Application becomes fully interactive

Fixed Code:


// main.ts
bootstrapApplication(AppComponent, {
  providers: [
    provideClientHydration()
  ]
});

// Component
@Component({
  template:
    <div class="cards-container">
    @for (card of cards; track card.id) {
        <div class="card">...</div>
      }
    </div>

})
          

Refresh the page and notice how content is visible immediately, even though the application is not yet interactive.

Status: Non-Interactive
Hydrating in 3s
Card 1

Card 1

Non-Interactive Content

Card 2

Card 2

Non-Interactive Content

Card 3

Card 3

Non-Interactive Content

Card 4

Card 4

Non-Interactive Content

Card 5

Card 5

Non-Interactive Content

Card 6

Card 6

Non-Interactive Content

Card 7

Card 7

Non-Interactive Content

Card 8

Card 8

Non-Interactive Content

Card 9

Card 9

Non-Interactive Content

Card 10

Card 10

Non-Interactive Content

Card 11

Card 11

Non-Interactive Content

Card 12

Card 12

Non-Interactive Content

Card 13

Card 13

Non-Interactive Content

Card 14

Card 14

Non-Interactive Content

Card 15

Card 15

Non-Interactive Content

Card 16

Card 16

Non-Interactive Content

Card 17

Card 17

Non-Interactive Content

Card 18

Card 18

Non-Interactive Content

Card 19

Card 19

Non-Interactive Content

Card 20

Card 20

Non-Interactive Content