Open limes opened 9 months ago
Somebody know how to do prerendering with transloco ?
Heyo, I solved this by providing a special TranslocoLoader only for the server. I already use this in production with success.
This is the AppServerModule
I updated to provide the new loader
@NgModule({
imports: [AppModule, ServerModule, IonicServerModule],
providers: [
{ provide: TRANSLOCO_LOADER, useClass: TranslocoServerHttpLoaderService },
],
bootstrap: [AppComponent],
})
export class AppServerModule {}
The loader itself don't tries to get the JSON files by using a HTTP call and just uses the file system:
@Injectable({
providedIn: 'root',
})
export class TranslocoServerHttpLoaderService implements TranslocoLoader {
getTranslation(lang: string): Observable<Translation> {
const assetsFolder = join(
process.cwd(),
'dist',
'app',
'browser',
'assets',
'i18n',
);
return of(
JSON.parse(fs.readFileSync(`${assetsFolder}/${lang}.json`, 'utf8')),
);
}
}
With this solution I managed it to prerender the files for different languages.
Had a similar issue in an existing app, the new pre-rendering done in ng build
failed.
I fixed it by following the docs' recommendation.
i.e changing
return this.http.get<Translation>(`${environment["baseUrl"]}/assets/i18n/${lang}.json`);
to
return this.http.get<Translation>(`/assets/i18n/${lang}.json`);
Heyo, I solved this by providing a special TranslocoLoader only for the server. I already use this in production with success. This is the
AppServerModule
I updated to provide the new loader@NgModule({ imports: [AppModule, ServerModule, IonicServerModule], providers: [ { provide: TRANSLOCO_LOADER, useClass: TranslocoServerHttpLoaderService }, ], bootstrap: [AppComponent], }) export class AppServerModule {}
The loader itself don't tries to get the JSON files by using a HTTP call and just uses the file system:
@Injectable({ providedIn: 'root', }) export class TranslocoServerHttpLoaderService implements TranslocoLoader { getTranslation(lang: string): Observable<Translation> { const assetsFolder = join( process.cwd(), 'dist', 'app', 'browser', 'assets', 'i18n', ); return of( JSON.parse(fs.readFileSync(`${assetsFolder}/${lang}.json`, 'utf8')), ); } }
With this solution I managed it to prerender the files for different languages.
thanks. my project angular v17 with ssr without standalone. solved my problem
No updates on this issue?
@limes @mackelito Did you try the solution @NLueg suggested? Feels like something worth mentioning in the docs if someone wants to open a PR and add it 🙂
Is there an existing issue for this?
Which Transloco package(s) are the source of the bug?
Transloco
Is this a regression?
No
Current behavior
Expected behavior
Prerender process should work the same way as
ng build && ng run {app-name}:server
Please provide a link to a minimal reproduction of the bug, if you won't provide a link the issue won't be handled.
See current behavior (1-3)
Transloco Config
Please provide the environment you discovered this bug in
Browser
Additional context
No response
I would like to make a pull request for this bug
No