Skip to content

Instantly share code, notes, and snippets.

@doup
Last active November 6, 2020 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doup/30b9435f8128b338695fafe885ddf764 to your computer and use it in GitHub Desktop.
Save doup/30b9435f8128b338695fafe885ddf764 to your computer and use it in GitHub Desktop.
`ngx-translate` cache busting
+ // This value will be injected by Webpack
+ declare const I18N_HASH: string;

export function createTranslateLoader(http: HttpClient) {
- return new TranslateHttpLoader(http, './assets/i18n/', '.json');
+ return new TranslateHttpLoader(http, './assets/i18n/', '.json?v=' + I18N_HASH);
}

package.json:

{
  "scripts": {
    …
-   "start": "ng serve",
+   "start": "ng serve --extra-webpack-config webpack.partial.js",
-   "build": "ng build",
+   "build": "ng build --extra-webpack-config webpack.partial.js",
    …
  }
}
const crypto = require('crypto');
const fs = require('fs');
const glob = require('glob');
const webpack = require('webpack');
function generateChecksum(str, algorithm, encoding) {
return crypto
.createHash(algorithm || 'md5')
.update(str, 'utf8')
.digest(encoding || 'hex');
}
const content = glob.sync(`src/assets/i18n/**/*.json`)
.map(path => fs.readFileSync(path, { encoding: 'utf-8' }))
.reduce((content, file) => content + file, '')
module.exports = {
plugins: [
new webpack.DefinePlugin({
I18N_HASH: JSON.stringify(generateChecksum(content))
}),
],
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment