Skip to content

Defer is keeping application unstable, hydration cleanup not done until defer finished #60373

Closed

Description

@gboutte

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

Don't known / other

Is this a regression?

No

Description

Problem

If you have an application with ssr enabled and your ssr is not rendering the same as the browser side, during the defer, you will have both information.

Details

I provided a git repository that show the problem.
To simplify I used the isBrowser to show an information.

<div>Is Browser = {{ isBrowserSide }}</div>
@if(isBrowserSide){
  <div>Inside if: browser bool = {{ isBrowserSide }}</div>
} @else{
  <div>Inside else: browser bool = {{ isBrowserSide }}</div>
}

If anywhere in the application there is a @defer, during the timing both parts ssr/ browser version will be shown.

In my example I used a timing of 20s

@defer {
  <app-defered/>
} @placeholder (minimum 20s){
  placeholder shown during 20s
}

And during the 20s I will see this:

Is Browser = true
Inside if: browser bool = true
Inside else: browser bool = false

Use case

This apply to some application that does not handle the user auth server side and only browser side.

For example your navbar is showing a "login" button when not connected. And when connected it's an "my account" button.

If you want to defer some heavy components while the components is loading both button will be show. Which is not ideal.

Explanation

I think it's because the hydration wait for any defered action started initially to be finished before cleaning up. It seem to be designed that way, so I don't know if it's a bug.

Maybe a solution would be a way to ask the defer to be ran after hydration / application stable.

Workaround

I found a workaround without using the defer, I waited for the application to be stable and then with a setTimeout waited some seconds to update a boolean to true, and my defered component is juste wrapped inside a @if

I made a stack overflow post about it: https://stackoverflow.com/questions/79500651/angular-hydration-waiting-for-defer-to-be-done-and-show-the-if-and-the-else

Please provide a link to a minimal reproduction of the bug

https://github.com/gboutte/angular-defer-keep-unstable.git

Please provide the exception or error you saw


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

Angular CLI: 18.2.14
Node: 20.18.3
Package Manager: npm 10.8.2
OS: darwin arm64

Angular: 18.2.13
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, platform-server
... router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1802.14
@angular-devkit/build-angular   18.2.14
@angular-devkit/core            18.2.14
@angular-devkit/schematics      18.2.14
@angular/cli                    18.2.14
@angular/ssr                    18.2.14
@schematics/angular             18.2.14
rxjs                            7.8.2
typescript                      5.4.5
zone.js                         0.14.10

Anything else?

No response

Activity

  1. JoostK commented on Mar 13, 2025

    @JoostK
    Member

    I believe this is essentially a duplicate of #58670 and #60218

  2. added
    area: core
    on Mar 13, 2025
  3. added this to the needsTriage milestone on Mar 13, 2025
  4. thePunderWoman commented on Mar 13, 2025

    @thePunderWoman
    Contributor

    I don't think it actually is a duplicate of those things, but it may be related. We currently rely on zones for timers and it's blocking of stability. So there's a potential fix for that part.

  5. added a commit that references this issue on Mar 14, 2025
    768a9e1
  6. added a commit that references this issue on Mar 14, 2025
    296aded
  7. added a commit that references this issue on Mar 14, 2025
    b8daf47
  8. angular-automatic-lock-bot commented on Apr 14, 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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: corecore: hydration

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions