-
Notifications
You must be signed in to change notification settings - Fork 69
Cached JSON file #25
Comments
Is it maybe possible to provide a cachebusting query parameter as suffix? Something like: export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http, '/assets/i18n/', '.json?cacheBuster='
+ environment.cacheBusterHash);
} Then you only have to think about providing a caching hash on every production build. But sure, it would be nice if it is provided by the TranslateHttpLoader. |
Yes, would be great to have. We are experiencing the same thing. |
Yes please, another vote for this. Have just run into the same issue. |
We've gotten around this by having a separate pre-build gulp task that takes all the generated translation files and prepends them with a unique guid/hash. Is a hacky solution that we're not crazy about. |
If you are using webpack/angular-cli you can solve the problem by writing your own // webpack-translate-loader.ts
import { TranslateLoader } from '@ngx-translate/core';
import { Observable } from 'rxjs/Observable';
export class WebpackTranslateLoader implements TranslateLoader {
getTranslation(lang: string): Observable<any> {
return Observable.fromPromise(System.import(`../i18n/${lang}.json`));
}
} Cause declare var System: System;
interface System {
import(request: string): Promise<any>;
} Now we can import everything in our app module @NgModule({
bootstrap: [AppComponent],
imports: [
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useClass: WebpackTranslateLoader
}
})
]
})
export class AppModule { } When you are using |
@mlegenhausen thanks for your reply! export function createTranslateLoader(http: HttpClient, configService: ConfigService) {
return new TranslateHttpLoader(http, '/assets/translations/', `.${configService.config.translationsHash || ''}.json`);
} TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: createTranslateLoader,
deps: [HttpClient, ConfigService]
}
}) What exactly do you mean about build your cache busted files via ng build? |
Normally webpack is able to create this unique identifiers for you. This does not work when you use for example the copy plugin from webpack. Which can lead to caching problems when you use high ttls for your static content. I also prefer the pure webpack way so I don't have to use a build tool for my build tool 😉 |
In issue ngx-translate#25 @mlegenhausen pointed out a solution that shows a very common use-case for a custom TranslateLoader implementation. Maybe we can extend the readme to show this (and maybe other use-cases in the future)? Kind regards :)
I created http interceptor to set headers {'Cache-control': 'no-cache, must-revalidate'}. Add it to providers in module and it works fine. |
* Howto write a HashTranslateLoader In issue #25 @mlegenhausen pointed out a solution that shows a very common use-case for a custom TranslateLoader implementation. Maybe we can extend the readme to show this (and maybe other use-cases in the future)? Kind regards :) * Updated to review
Similar to @stetro solution above, I use a date timestamp for my caching hash. Saves me from having to manually set a cache hash for every build.
|
We are using https://github.com/manfredsteyer/ngx-build-plus and a similar approach to @errolgr solution. We want to update the timestamp only during a new deployment. |
To build on @karlhaas's comment, here's something I've come up with, which allows for cache-busting on a per-build basis, rather than @errolgr's solution - which will force the client to retrieve the JSON files on a per-request basis of your project.
const webpack = require('webpack');
// Using @angular-builder's custom-webpack
module.exports = {
plugins: [
new webpack.DefinePlugin({
VERSION: (+new Date()).toString(),
}),
],
};
declare const VERSION: string;
export function createTranslateLoader(http: HttpClient) {
return new TranslateHttpLoader(
http, './assets/i18n/', `.json?version=${VERSION}`,
);
} |
Now with angular 8 and esnext module loading this will also work. --prod build would add hashes to file names. import { TranslateLoader } from '@ngx-translate/core';
import { Observable, from } from 'rxjs';
export class LazyTranslateLoader implements TranslateLoader {
getTranslation(lang: string): Observable<any> {
return from(import(`../i18n/${lang}.json`));
}
} in tsconfig.json you also need ...
"module": "esnext",
... |
Hi @vixriihi , Is correct to add the loader like the code below? TranslateModule.forRoot({
defaultLanguage: 'en',
loader: {
provide: TranslateLoader,
useClass: LazyTranslateLoader,
}
}) |
Tried @vixriihi 's approach. But for some reason it is not working. For the record, I am building for Angular Universal. Would that make any change? |
Is there a way to clear the cache with a function on logout? We have an application with multiple locations but different translations on each location. For example location A may have different translations for the translation "de" than location B. |
For someone looking to have their translation files deployed on prod with a different hash, I solved it similar to @errolgr.
The regex above gets rid of dot(s), colon and hyphen as a result of the date object.
|
Thank you!!! // webpack-translate-loader.ts
import { TranslateLoader } from '@ngx-translate/core';
import { Observable } from 'rxjs/Observable';
import { from } from 'rxjs';
export class WebpackTranslateLoader implements TranslateLoader {
getTranslation(lang: string): Observable<any> {
return from(import(`../../../assets/i18n/${lang}.json`));
}
} |
This approach doesn't work for me. First, translation files aren't loaded due to Second, |
When new bundle is deployed to production server and new translation keys have been added, last JSON is cached.
It would be nice if, somehow, the loader could fetch the last version of the JSON file.
The way I provide the loader is like this:
The text was updated successfully, but these errors were encountered: