Skip to content

SSG: when hydrating Angular renders contradicting state in @if-else, @switch, [ngSwitch], *ngTemplateOutlet #60218

Closed

Description

@KonstantinasL

Which @angular/* package(s) are the source of the bug?

Don't known / other

Is this a regression?

Yes

Description

When hydrating a prerendered page (SSG) and there are differences in states, Angular displays both the content that was pre-rendered and the live content at the same time, resulting in duplicated content which impacts very negatively UX and CWV metrics (like CLS).

@Component({
  selector: 'app-demo',
  template: `
  <div> <!-- for a short while both A and B will be visible, when app becomes stable A will disappear -->
    @if (variant === 'A') {
      <span>A</span>
    } @else {
      <span>B</span>
    }
  </div>

  <div> <!-- for a short while both A and B will be visible, when app becomes stable A will disappear -->
    @switch (variant) {
      @case('A') { <span>A</span> }
      @case('B') { <span>B</span> }
    }
  </div>

  <div><!-- for a short while both A and B will be visible, when app becomes stable A will disappear -->
    <ng-container [ngSwitch]="variant">
      <span *ngSwithCase="A">A</span>
      <span *ngSwithCase="B">B</span>
    </ng-container>
  </div>

  <div><!-- for a short while both A and B will be visible, when app becomes stable A will disappear -->
    <ng-container *ngTemplateOutlet="variant === 'A' ? templateA : templateB" />
    <ng-template #templateA>
       <span>A</span>
    </ng-template>
    <ng-template #templateB>
       <span>B</span>
    </ng-template>
  </div>

  <div><!-- this will work fine, only A or B will be visible at any time -->
    <ng-container *ngIf="variant === 'A'; else templateB">
       <span>A</span>
    </ng-container>
    <ng-template #templateB>
       <span>B</span>
    </ng-template>
  </div>
  `,
  imports: [CommonModule],
})
export class DemoComponent implements OnInit {
  private readonly platformId = inject(PLATFORM_ID);
  variant = 'A';
  
  ngOnInit(): void {
    // simulating different state, in the browser than it was in the server
    // IRL, this could be any state change: user permissions, authentication, query parameters, etc.
    if (isPlatformBrowser(this.platformId)) {
       this.variant = 'B';
    }
  }
}

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

No error

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 19.2.0
Node: 22.7.0
Package Manager: npm 10.8.2
OS: win32 x64

Angular: 19.2.0
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, localize, platform-browser
... platform-browser-dynamic, platform-server, router, ssr

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1902.0
@angular-devkit/build-angular   19.2.0
@angular-devkit/core            19.2.0
@angular-devkit/schematics      19.2.0
@angular/cdk                    19.2.1
@schematics/angular             19.0.6
ng-packagr                      19.0.1
rxjs                            7.8.2
typescript                      5.6.3
zone.js                         0.15.0

Anything else?

This is a duplicate for a 58670 issue (which is now closed but the bug is still present), the 58670 has some live examples.

This issue seems to be related to "withI18nSupport" hydration option even though none of the elements has i18n properties on them.

Activity

  1. added
    area: core
    on Mar 5, 2025
  2. modified the milestone: needsTriage on Mar 5, 2025
  3. thePunderWoman commented on Mar 5, 2025

    @thePunderWoman
    Contributor

    Thanks for the report. We are actively working on this issue and due to that, I've actually re-opened #58670.

  4. KonstantinasL commented on Mar 12, 2025

    @KonstantinasL
    Author

    @thePunderWoman Hi, first of all, thank you for taking a quick action, but are there any updates on this issue?

  5. thePunderWoman commented on Mar 12, 2025

    @thePunderWoman
    Contributor

    @KonstantinasL We're working on it, but it's not a quick fix. So please be patient.

  6. KonstantinasL commented on Mar 20, 2025

    @KonstantinasL
    Author

    @thePunderWoman Thank you, for the update 👍

  7. 7 remaining items

  8. added a commit that references this issue on Mar 21, 2025
    13d1c8a
  9. angular-automatic-lock-bot commented on Apr 21, 2025

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  10. locked and limited conversation to collaborators on Apr 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area: corebugcore: hydration

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions