Skip to content

Instantly share code, notes, and snippets.

@2J
Last active November 25, 2016 20:34
Show Gist options
  • Save 2J/05407da0ad47a8d1a360486ac15d6272 to your computer and use it in GitHub Desktop.
Save 2J/05407da0ad47a8d1a360486ac15d6272 to your computer and use it in GitHub Desktop.
Cachebuster for Angular2 html/css files

Cachebuster for html/css files in Angular2

Use a settings file to get the environment/version # in cachebuster loader

import { Injectable } from '@angular/core';
import { __platform_browser_dynamic_private__ } from '@angular/platform-browser-dynamic';
@Injectable()
export class CachebusterResourceLoader extends __platform_browser_dynamic_private__.ResourceLoaderImpl {
constructor() {
super();
}
get(url: string): Promise<string> {
let ENV = "DEV"; //Get this value from settings file
let VERSION = "1.0.0"; //Get this value from settings file
let cacheBusterString = (ENV == "DEV") ? new Date().getTime().toString() : VERSION;
return super.get(`${url}?v=${cacheBusterString}`);
}
}
[...]
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
import { ResourceLoader } from '@angular/compiler';
import { CachebusterResourceLoader } from './cachebuster-resource-loader';
platformBrowserDynamic().bootstrapModule(AppModule, {
providers: [
{
provide: ResourceLoader,
useClass: CachebusterResourceLoader
}
]
});
[...]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment