Thursday, February 21, 2019

How to prevent Browser cache on Angular

angular-cli resolves this brilliantly by providing an --output-hashing flag for the build command. Example usage:

ng build --aot --output-hashing=all
Bundling & Tree-Shaking provides some details and context.

Running ng help build, documents the flag:

--output-hashing=none|all|media|bundles (String) Define the output filename cache-busting hashing mode.
aliases: -oh , --outputHashing

--output-hashing all — hash contents of the generated files and append hash to the file name to facilitate browser cache busting (any change to file content will result in different hash and hence browser is forced to load a new version of the file)

Other Way

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

Or

 just add the following meta tags at the top:



No comments:

Followers

Link