如何防止Angular 2网站上的浏览器缓存?

问题描述:

我们目前正在开发一个具有定期更新的新项目,我们的一位客户每天都会使用该更新.该项目是使用angular 2开发的,我们面临着缓存问题,这就是说我们的客户没有看到他们机器上的最新变化.

We're currently working on a new project with regular updates that's being used daily by one of our clients. This project is being developed using angular 2 and we're facing cache issues, that is our clients are not seeing the latest changes on their machines.

主要是js文件的html/css文件似乎已正确更新,没有太多麻烦.

Mainly the html/css files for the js files seem to get updated properly without giving much trouble.

找到了一种方法,只需添加一个查询字符串即可加载您的组件,就像这样:

Found a way to do this, simply add a querystring to load your components, like so:

@Component({
  selector: 'some-component',
  templateUrl: `./app/component/stuff/component.html?v=${new Date().getTime()}`,
  styleUrls: [`./app/component/stuff/component.css?v=${new Date().getTime()}`]
})

这将强制客户端加载服务器的模板副本,而不是浏览器的模板副本. 如果您希望仅在一定时间后刷新,可以使用此ISOString代替:

This should force the client to load the server's copy of the template instead of the browser's. If you would like it to refresh only after a certain period of time you could use this ISOString instead:

new Date().toISOString() //2016-09-24T00:43:21.584Z

然后对一些字符进行字符串处理,这样一小时后它才会更改:

And substring some characters so that it will only change after an hour for example:

new Date().toISOString().substr(0,13) //2016-09-24T00

希望这会有所帮助