How to prevent Browser cache on Angular 2 site?

后端 未结 6 1604
慢半拍i
慢半拍i 2020-12-04 05:37

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 fac

6条回答
  •  眼角桃花
    2020-12-04 06:11

    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()}`]
    })
    

    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
    

    Hope this helps

提交回复
热议问题