我正在开发使用SSR的应用程序。
进入应用程序时,我正在发送获取一些产品的请求。
一切正常,因为我在浏览器中看不到该请求。
如果用户已登录,则应用程序API应该返回不同的值。
如果用户已登录,则该信息作为localStorage
保留在idToken
中。
如果在浏览器中发送了请求,则idToken
将添加到标题中。
API检查idToken
是否已记录。
现在,如果我以预渲染方式发送请求,则得到未登录用户的值,然后在浏览器中,我检查我是否拥有idToken
,如果是,我想再次执行HTTP请求以获取正确的值。
但是,由于HTTP请求已经订阅,因此不会在浏览器中再次执行该请求。
我该怎么做才能解决此问题?
我应该先退订吗?
getProductHome() {
return this.apiservice.get(this.COLLECTION_CONF_TOKEN.urlHomeProducts, {});
}
if (isPlatformServer(this.platformId)) {
this.collectionservice.getProductHome().subscribe(
res => {
this.newProducts = res;
}
);
} else if (isPlatformBrowser(this.platformId) && isLogged) {
this.collectionservice.getProductHome().subscribe(
res => {
this.newProducts = res;
}
);
}
答案 0 :(得分:0)
可能要考虑两件事:
getProductHome()
?如果不是,那么应该。 main.ts应该只被调用一次,所以我想它不会再次通过该代码。Subscription
。您可以在 http请求标头中添加标头以禁用缓存:
headers = new Headers({
'Cache-Control': 'no-cache, no-store, must-revalidate, post-
check=0, pre-check=0',
'Pragma': 'no-cache',
'Expires': '0'
});
或者您可以添加html标签,如下所示:
<meta http-equiv="cache-control" content="no-cache, must-revalidate, post-check=0, pre-check=0">
<meta http-equiv="expires" content="0">
<meta http-equiv="pragma" content="no-cache">