Skip to content

Control flow with defer inside does not clean up before httpClient ends its request #56992

@zip-fa

Description

@zip-fa

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

core

Is this a regression?

Yes

Description

Hi. I noticed that defer's @Loading block is not cleaned up before HttpClient completes its request. In v17 it doesn't wait for it.
Demo video:

ng-bug.mov

Full component code which reproduces the issue:

import {Component, inject, PLATFORM_ID, signal} from '@angular/core';
import {HttpClient} from "@angular/common/http";
import {isPlatformServer} from "@angular/common";
import {LoggedCmp} from "./logged-cmp";
import {TestCmp} from "./test-cmp";
import {User} from "./types";

@Component({
  selector: 'app-home-cmp',
  standalone: true,
  imports: [
    LoggedCmp,
    TestCmp
  ],
  template: `
    @if (user()) {
      <p>
        @defer (when user()) {
          <app-logged-cmp [user]="user()!" />
        }
      </p>
    } @else {
      <div>
        @defer (on immediate) {
          <app-test-cmp />
        } @loading {
          <div>Loading....</div>
        } @placeholder {
          <div>Loading....</div>
        }
      </div>
    }
  `
})
export class HomeCmp {
  private readonly httpClient = inject(HttpClient);
  private readonly platformId = inject(PLATFORM_ID);

  user = signal<User | null>({id: 1, nickname: 'admin'});

  constructor() {
    if (isPlatformServer(this.platformId)) {
      this.user.set(null);
    } else {
      this.httpClient.get(`https://dummyjson.com/RESOURCE/?delay=5000&t=${ Date.now() }`)
        .subscribe();
    }
  }
}

Please provide a link to a minimal reproduction of the bug

https://github.com/zip-fa/ng18-defer-cleanup-issue

Please provide the exception or error you saw

"Loading..." block is not removed from DOM-tree before HttpClient completes request

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

Angular CLI: 18.1.0
Node: 22.4.1
Package Manager: npm 10.8.1
OS: darwin arm64

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

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1801.0
@angular-devkit/build-angular   18.1.0
@angular-devkit/core            18.1.0
@angular-devkit/schematics      18.1.0
@schematics/angular             18.1.0
rxjs                            7.8.1
typescript                      5.4.5
zone.js                         0.14.7

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: coreIssues related to the framework runtimecore: hydrationneeds: clarificationThis issue needs additional clarification from the reporter before the team can investigate.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions