Skip to content

Commit ddf0d4e

Browse files
alan-agius4thePunderWoman
authored andcommitted
fix(http): HTTP cache was being disabled prematurely (#49826)
This commit fixes an issue were on the server the HTTP cache was being disabled prematurely which caused HTTP calls performed in `ngOnInit` life cycle hooks not to be cached. PR Close #49826
1 parent 7fd0b67 commit ddf0d4e

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

integration/platform-server/src/http-transferstate-lazy/transfer-state.component.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {HttpClient} from '@angular/common/http';
10-
import {Component} from '@angular/core';
10+
import {Component, OnInit} from '@angular/core';
1111

1212
@Component({
1313
selector: 'transfer-state-app-http',
@@ -16,18 +16,22 @@ import {Component} from '@angular/core';
1616
<div class="two">{{ responseTwo }}</div>
1717
`,
1818
})
19-
export class TransferStateComponent {
19+
export class TransferStateComponent implements OnInit {
2020
responseOne: string = '';
2121
responseTwo: string = '';
2222

2323
constructor(
2424
private readonly httpClient: HttpClient,
2525
) {
26-
this.httpClient.get<any>(`http://localhost:4206/api`).subscribe((response) => {
26+
// Test that HTTP cache works when HTTP call is made in the constructor.
27+
this.httpClient.get<any>('http://localhost:4206/api').subscribe((response) => {
2728
this.responseOne = response.data;
2829
});
30+
}
2931

30-
this.httpClient.get<any>(`http://localhost:4206/api-2`).subscribe((response) => {
32+
ngOnInit(): void {
33+
// Test that HTTP cache works when HTTP call is made in a lifecycle hook.
34+
this.httpClient.get<any>('http://localhost:4206/api-2').subscribe((response) => {
3135
this.responseTwo = response.data;
3236
});
3337
}

packages/common/http/src/transfer_cache.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {APP_INITIALIZER, ApplicationRef, inject, InjectionToken, makeStateKey, Provider, StateKey, TransferState, ɵENABLED_SSR_FEATURES as ENABLED_SSR_FEATURES, ɵInitialRenderPendingTasks as InitialRenderPendingTasks} from '@angular/core';
9+
import {APP_BOOTSTRAP_LISTENER, ApplicationRef, inject, InjectionToken, makeStateKey, Provider, StateKey, TransferState, ɵENABLED_SSR_FEATURES as ENABLED_SSR_FEATURES, ɵInitialRenderPendingTasks as InitialRenderPendingTasks} from '@angular/core';
1010
import {Observable, of} from 'rxjs';
1111
import {first, tap} from 'rxjs/operators';
1212

@@ -160,7 +160,7 @@ export function withHttpTransferCache(): Provider[] {
160160
deps: [TransferState, CACHE_STATE]
161161
},
162162
{
163-
provide: APP_INITIALIZER,
163+
provide: APP_BOOTSTRAP_LISTENER,
164164
multi: true,
165165
useFactory: () => {
166166
const appRef = inject(ApplicationRef);

0 commit comments

Comments
 (0)