Closed vbonne closed 4 years ago
i have been 10h on this... I am not good at debugging node... the error was not in the server.ts but in the interceptor. This interceptor, i orginally designed it for ngx-translate and I thought it could be shared just like that to retrieve the data from ./assets/locales.json as it was working fine in SSR for retrieving the ./assets/i18n/en.json, but no luck. ngx-translate and ngx-translate-router cannot share the same interceptor, I don't have the exact reason but that's it. So I had to create a second interceptor (code below) and this solved my issue.
I know I did not inspire a lot of responses but maby one day this post will help someone.
import { REQUEST } from '@nguniversal/express-engine/tokens';
import * as express from 'express';
import {Inject, Injectable, PLATFORM_ID} from '@angular/core';
import {HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http';
import {isPlatformServer} from '@angular/common';
@Injectable()
export class LocalizeInterceptor implements HttpInterceptor {
constructor(@Inject(REQUEST) private request: express.Request, @Inject(PLATFORM_ID) private platformId: any) {}
intercept(request: HttpRequest<any>, next: HttpHandler) {
if (request.url.startsWith('assets') && isPlatformServer(this.platformId)) {
const req = this.request;
const url = req.protocol + '://' + req.get('host') + '/' + request.url;
request = request.clone({
url
});
}
return next.handle(request);
}
}
I hope I did not polute your repo, you can close the issue, sorry about this, your tutorial is clear enough, my fault for being lazy and trying to share interceptors between modules, where i should not have...
Hello @vbonne,
Relative paths doesn't work on SSR so you have the choice between three things:
This providers can be added to app.server.module.ts
to use it only on SSR part.
I am implementing ngx-translate-router in an Angular Universal app already working fine with SRR. After adding ngx-translate-router with http loader it works correctly with
ng serve
so it means that the integration without SSR is working fine.But when running in SSR mode :
npm run serve:ssr
i get this error :I implemented the SSR part based your instructions so what i did additional for the SSR part are the following :
1 - implemented an interceptor in the app.server.module.ts to be able to access the translations within the server part. Here is the interceptor :
2 - I modified the server.ts to access the different locales and added the routes for them, but I think the issues lies there. I think I incorrectly added the routes listening in the server.ts but i did not find help about this anywhere and the instructions on github are a little short for my level with angular SSR...
here is the server.ts
when I start the SSR server and then request the page http://localhost I can see the redirect working to the default language and the console logs the
"domain requested with language /en/"
before the error reported above.I think the issue is that the server.ts does not manage to map the requested url to something within the routes declared in the app-routing.module.ts but i don't know how to do that. In the GitHub repository your say :
but you don't describe what "ngApp" is so i just extrapolated it base on how the server.ts was before integrating this plugin:
So my question is double. Do you think I am right to keep searching on the direction that I dentified ? (server.ts implementation is wrong). If yes do you have an idea how to correct it ? If no, any other direction to look for ?