My project was on angular 15 version and SSR and transfer state was working fine and APIs called from server side but after migrate to angular 17 transfer state not work and same APIs are called in client
export const HOME = makeStateKey('home');
getHomeSections(forMobile: string):Observable<HomeSection[]>{
return this.dataStateService.checkAndGetData(HOME,
this.http.get<ApiResponse<HomeSection[]>>(`core/Home`, {params: {forMobile}}).pipe(map(
res => {
return res.result;
}
))
);
}
checkAndGetData(key: string & { __not_a_string: never; __value_type?: void }, getDataObservable: Observable<any>, defaultValue: any = []) {
if (this.transferState.hasKey(key)) {
return of(this.transferState.get(key, defaultValue));
} else {
return getDataObservable.pipe(
tap((data) => {
if (this.isServer) {
this.transferState.set(key, data);
}
})
);
}
}
I use this operator in RxJS. It saves the data to the cache on the server and also runs the query on the Frontend to revalidate the fresh data. To disable this refresh use
of(transferState.get<T>(stateKey, {} as T))
if the key is found.The usage is like this: