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 | |
} | |
] | |
}); | |
[...] |