Skip to content

Zoneless SSR: usage of forkJoin to get multiple responses prevents server rendering #59352

Closed

Description

@jnizet

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

Don't known / other

Is this a regression?

No

Description

Using forkJoin to load multiple data using the HttpClient prevents the component to be rendered on the server. Using combineLatest instead works as expected.

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/stackblitz-starters-fud9dwup?file=demo%2Fsrc%2Fapp%2Fapp.component.ts

Please provide the exception or error you saw

NA

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

Angular CLI: 19.0.6
Node: 18.20.3
Package Manager: npm 10.2.3
OS: linux x64

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

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1900.6
@angular-devkit/build-angular   19.0.6
@angular-devkit/core            19.0.6
@angular-devkit/schematics      19.0.6
@angular/cli                    19.0.6
@angular/ssr                    19.0.6
@schematics/angular             19.0.6
rxjs                            7.8.1
typescript                      5.6.3
zone.js                         0.15.0

Anything else?

To reproduce, using the provided stackblitz:

  • cd demo
  • npm i
  • ng serve

Then open the network dev tools, clear the requests, and refresh the stackblitz demo page. Inspect the HTML returned by the server. It contains an empty app-root element, and it also contains the transfer state for the http response:

<app-root _nghost-ng-c1660245559="" ng-version="19.0.5" ngh="0" ng-server-context="ssr"><!--container--></app-root>

<script id="ng-state" type="application/json">{"3838178545":{"b":{"message":"hello"},"h":{},"s":200,"st":"OK","u":"/foo.json","rt":"json"},"__nghData__":[{"t":{"0":"t0"},"c":{"0":[]}}]}</script></body></html>

Then comment the code of the app component loading the data using forkJoin, and replace it with the commented code using combineLatest. Then refresh the stackblitz demo page. Inspect the HTML returned by the server. It now contains a filled app-root element, as expected:

<app-root _nghost-ng-c1660245559="" ng-version="19.0.5" ngh="0" ng-server-context="ssr"><h1 _ngcontent-ng-c1660245559="">Hello from app</h1><!--container--></app-root>

Activity

  1. JeanMeche commented on Jan 2, 2025

    @JeanMeche
    Member

    This looks like an implementation quirk of forkJoin. The app becomes stable before the value is passed through the pipe.
    It's probably due to the fact that forkJoin waits for the completion of the observables to send its own value.

    The transfer cache is correctly populated as it doesn't depend on forkJoin itself but is set via an interceptor.

  2. changed the title [-]SSR: usage of forkJoin to get multiple responses prevents server rendering[/-] [+]Zoneless SSR: usage of forkJoin to get multiple responses prevents server rendering[/+] on Jan 2, 2025
  3. added
    core: zoneless
    on Jan 5, 2025
  4. added
    area: core
    area: server
    on Jan 6, 2025
  5. added this to the needsTriage milestone on Jan 6, 2025
  6. added
    area: common/http
    on Jan 16, 2025
  7. atscott commented on Jan 16, 2025

    @atscott
    Contributor

    TL;DR Use the pendingUntilEvent operator here.

    This is also generally due to how the HttpClient removes the task in the finalize (

    this.backend.handle(downstreamRequest),
    ), which would cause the application to become stable if it's the last task, before the forkJoin is able to observe the complete event of the request.

    There is an argument for not having HttpClient contribute to stability automatically. Requests may or may not be tied to rendering at all so it should be up to developers to indicate which ones should do this, or install an interceptor themselves (while understanding the limitations).

    The short (and likely long term) solution for this would be to use the pendingUntilEvent operator here.

  8. jnizet commented on Jan 17, 2025

    @jnizet
    ContributorAuthor

    Thanks @atscott.

    I thought the HttpClient would remove the pensing task on the next tick following the last pending request and thus allowing operators such as forkJoin to work fine (or any other piece of code acting synchronously after a response have been received). Couldn't that be the default behavior (or an optional one)?

    That said, I couldn't figure out what forkJoin does. I thought it was itself using finalize(), thus executing code after the HttpClient's finalizer which removed the pending task, but when I do that in my own code, it works fine.

  9. angular-automatic-lock-bot commented on May 4, 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 May 4, 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: common/httparea: corearea: servercore: zoneless

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions