Repository navigation
Closed
Closed
Copy link
Labels
area: common/httpIssues related to HTTP and HTTP Clientarea: coreIssues related to the framework runtimearea: serverIssues related to server-side renderingcore: zonelessIssues related to running Angular without zone.js
Milestone
Description
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
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 demonpm ing 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>Reacted by Evgeniy OZ
Metadata
Metadata
Assignees
Labels
area: common/httpIssues related to HTTP and HTTP Clientarea: coreIssues related to the framework runtimearea: serverIssues related to server-side renderingcore: zonelessIssues related to running Angular without zone.js
Activity
JeanMeche commentedon Jan 2, 2025
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
forkJoinwaits for the completion of the observables to send its own value.The transfer cache is correctly populated as it doesn't depend on
forkJoinitself but is set via an interceptor.[-]SSR: usage of forkJoin to get multiple responses prevents server rendering[/-][+]Zoneless SSR: usage of forkJoin to get multiple responses prevents server rendering[/+]atscott commentedon Jan 16, 2025
TL;DR Use the
pendingUntilEventoperator here.This is also generally due to how the
HttpClientremoves the task in the finalize (angular/packages/common/http/src/interceptor.ts
Line 328 in 01fffdb
forkJoinis able to observe thecompleteevent of the request.There is an argument for not having
HttpClientcontribute 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
pendingUntilEventoperator here.jnizet commentedon Jan 17, 2025
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.
fix(http): Delay stabilization until next app synchronization (angula…
angular-automatic-lock-bot commentedon May 4, 2025
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.