Skip to content

Hydration behavior when client and server render different branches of an if-else block #58670

Closed
1 of 1 issue completed

Description

@e-oz

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

compiler

Is this a regression?

No

Description

I don't know how to create a reproduction with SSR on Stackblitz, so please read it first and check the videos - perhaps you'll see what I'm talking about and what could cause that and perhaps it is a duplicate (I tried searching).

So, in the template, we have:

...
@if (store.$isAuthenticated()) {
      <div class="nav-btn" (click)="...">
        <mat-icon svgIcon="account"/>
        <span i18n="@navbar.account" class="btn-label">Account</span>
      </div>
    } @else {
      <div class="nav-btn" (click)="...">
        <mat-icon svgIcon="account"/>
        <span i18n="@navbar.signup" class="btn-label">Sign Up</span>
      </div>
}
...

As you can see, they can never be rendered together.
But if I use withI18nSupport():

with.mov

And if I remove withI18nSupport():

without.mov

This is ng serve with "ssr": true, but deployed versions behave the same.

Full app config:

export const appConfig: ApplicationConfig = {
  providers: [
    provideExperimentalZonelessChangeDetection(),
    provideRouter(appRoutes,
      withComponentInputBinding(),
      withInMemoryScrolling({
        anchorScrolling: 'enabled',
        scrollPositionRestoration: 'top',
      }),
      withRouterConfig({
        onSameUrlNavigation: 'ignore',
        urlUpdateStrategy: 'eager'
      }),
      withPreloading(CrPreloadingStrategy)
    ),
    provideAnimationsAsync(),
    provideHttpClient(withInterceptors([authInterceptor]), withFetch()),
    provideClientHydration(
      withIncrementalHydration(),
      withEventReplay(),
    ),
    provideNgxStripe(),
    // provideServiceWorker('ngsw-worker.js', {
    //   enabled: !isDevMode(),
    //   registrationStrategy: 'registerWhenStable:30000'
    // }),
    {
      provide: IMAGE_LOADER,
      useValue: (config: ImageLoaderConfig) => cloudflareImgLoader(config, environment.imgDomain)
    },
....

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw


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

Angular 19.0.0-rc.2 and rc.1 (checked).

Anything else?

No response

Sub-issues

Sub-issues

1 of 1 Issues completed
Loading 1 sub-issues

Activity

  1. e-oz commented on Nov 14, 2024

    @e-oz
    Author

    Forgot to mention: this also causes significant layout shifts and drops the Lighthouse score from 98 to 72. Just one single thing.

  2. added
    area: core
    on Nov 14, 2024
  3. modified the milestone: needsTriage on Nov 14, 2024
  4. thePunderWoman commented on Nov 14, 2024

    @thePunderWoman
    Contributor

    @e-oz This is a really nice report! Thanks for that. Any chance you can create a very minimal reproduction for us so we have something to start with for debugging purposes?

  5. e-oz commented on Nov 14, 2024

    @e-oz
    Author

    @thePunderWoman I will try tomorrow. Just checked if it's not a known issue.

  6. AndrewKushnir commented on Nov 14, 2024

    @AndrewKushnir
    Contributor

    @e-oz do you see a different behavior if you remove i18n attributes from elements in your template? I'm also wondering if you see any warnings in developer console (we output a warning if post-hydration cleanup didn't happen within 10 seconds)?

  7. added
    needs reproduction
    on Nov 14, 2024
  8. modified the milestones: needsTriage, Backlog on Nov 14, 2024
  9. e-oz commented on Nov 14, 2024

    @e-oz
    Author

    @AndrewKushnir

    do you see a different behavior if you remove i18n attributes from elements in your template?

    no, same behavior. And when I run ng serve, it prints this:

    Localization (localize option) has been disabled. The development server only supports localizing a single locale per build.

    The deployed version uses i18n, so i18n attributes make no difference.

    any warnings in developer console

    the only thing in console:

    Angular hydrated 194 component(s) and 2476 node(s), 0 component(s) were skipped. Learn more at https://angular.dev/guide/hydration.

    it happens during 1-3 seconds without network throttling, and during ~6 seconds with "fast 4G" throttling.

  10. AndrewKushnir commented on Nov 14, 2024

    @AndrewKushnir
    Contributor

    @e-oz thanks for additional information. As @thePunderWoman mentioned, we'd need a minimal repro to investigate the issue more.

    it happens during 1-3 seconds without network throttling, and during ~6 seconds with "fast 4G" throttling.

    The warning that I was referring to appears after 10 seconds (source code).

  11. e-oz commented on Nov 15, 2024

    @e-oz
    Author
  12. removed
    needs reproduction
    on Nov 20, 2024
  13. 27 remaining items

  14. added a commit that references this issue on Mar 21, 2025
    13d1c8a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area: corecore: hydration

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions