angular-cli: SSR with i18n with Angular 9 not working
š Bug report
What modules are related to this issue?
- aspnetcore-engine
- builders
- common
- express-engine
- hapi-engine
- module-map-ngfactory-loader
Is this a regression?
No, localize is new to Angular 9.
Description
The distFolder is hardcoded in ./server.ts. When the browser server assets are built with localize: true, the assets are placed in a subfolder with the locale name (eg: dist/{appName}/browser/{locale}/ and dist/{appName}/server/{locale}). Now the server can no longer find the correct directory for the browser assets and fails to render.
Is there any way server.ts can know location of the browser assets without hardcoding the path?
Thanks.
š¬ Minimal Reproduction
ng new ng-sample
ng add @angular/localize@next
ng add @nguniversal/express-engine@next
add localize: true to the options of the build and server architect
ng build --prod
ng run ng-sample:server:production
node dist/ng-sample/server/en-US/main.js
browser to http://localhost:4000
š„ Exception or Error
Error: Failed to lookup view "index" in views directory "/code/ng-sample/dist/ng-sample/browser"
at Function.render (/code/ng-sample/dist/ng-sample/server/en-US/main.js:1:1122933)
at ServerResponse.render (/code/ng-sample/dist/ng-sample/server/en-US/main.js:1:1398756)
at server.get (/code/ng-sample/dist/ng-sample/server/en-US/main.js:1:2259271)
at Layer.handle [as handle_request] (/code/ng-sample/dist/ng-sample/server/en-US/main.js:1:1144375)
at next (/code/ng-sample/dist/ng-sample/server/en-US/main.js:1:1131913)
at Route.dispatch (/code/ng-sample/dist/ng-sample/server/en-US/main.js:1:1131942)
at Layer.handle [as handle_request] (/code/ng-sample/dist/ng-sample/server/en-US/main.js:1:1144375)
at /code/ng-sample/dist/ng-sample/server/en-US/main.js:1:2473361
at param (/code/ng-sample/dist/ng-sample/server/en-US/main.js:1:2474870)
at param (/code/ng-sample/dist/ng-sample/server/en-US/main.js:1:2475277)
š Your Environment
Angular CLI: 9.0.0-rc.9
Node: 10.16.0
OS: darwin x64
Angular: 9.0.0-rc.9
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, localize, platform-browser
... platform-browser-dynamic, platform-server, router
Ivy Workspace: Yes
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.900.0-rc.9
@angular-devkit/build-angular 0.900.0-rc.9
@angular-devkit/build-optimizer 0.900.0-rc.9
@angular-devkit/build-webpack 0.900.0-rc.9
@angular-devkit/core 9.0.0-rc.9
@angular-devkit/schematics 9.0.0-rc.9
@ngtools/webpack 9.0.0-rc.9
@nguniversal/builders 9.0.0-rc.0
@nguniversal/common 9.0.0-rc.0
@nguniversal/express-engine 9.0.0-rc.0
@schematics/angular 9.0.0-rc.9
@schematics/update 0.900.0-rc.9
rxjs 6.5.4
typescript 3.6.4
webpack 4.41.2
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 21
- Comments: 53 (8 by maintainers)
Commits related to this issue
- Client: Bumped Angular Universal to 9.0.1, not yet fully functional It doesn't seem possible to render multiple languages from a single universal server anymore: https://github.com/angular/universal/... — committed to MarcusRiemer/esqulino by MarcusRiemer 4 years ago
- Add config, but wait solve https://github.com/angular/universal/issues/1454 — committed to Fafnur/medium-stories by Fafnur 4 years ago
- Add config, but wait solve https://github.com/angular/universal/issues/1454 — committed to Fafnur/medium-stories by Fafnur 4 years ago
- refactor: remove deprecated engines BREAKING CHANGE: Deprecated `@nguniversal/aspnetcore-engine`, `@nguniversal/hapi-engine` and `@nguniversal/socket-engine` has been removed in favor of `@ngunivers... — committed to angular/universal by alan-agius4 2 years ago
@piotrbrzuska solution worked for me.
Basically, I did the following:
server.ts:
Then I created a separate server.run.js with this:
Any update about this ?
I see this issue has been open since 2020, are there any plans to fix it?
any solution to run application (with multiple language) on single express port???
I am in the same boat: Previously I could import multiple bundles and load them dynamically based on the URL that was requested. Now I need to run one server for each language, this is quite tedious.
I am sharing ready working solution for Angular 10 on one port based on your answers šš
Repositorium
Angular documentation is so deprecated, maybe this gonna helps someone š
@PowerKiKi and @schippie - thanks for the tips!
Indeed I managed to get
npm run prerender
to work without any hacks and with a single change inangular.json
- only had to add"localize": ["en", "ab", "cd"]
under āserverā -> āconfigurationsā -> āproductionā` (to match the regular ābuildā āproductionā configuration).It seems that the
ng add @nguniversal/express-engine
schematics donāt copy over the localize value when generating the āserverā configuration. I believe this can be fixed in the schematics to improve developer experience.(Clarification: For now Iām only doing prerendering, which worked well without hacks - just by adding one localize line to angular.json like I mentioned. I havenāt fully tried SSR yet, but
npm run dev:ssr
seems to work too.)Any update of this, at incoming Angular 10 ?
Hi, Is there any progress about this issue?
Same here. I adopted same setup for my production site as mentioned by @keserwan in angular/universal#1497, which is now broken.
Besides, look like this block of code is wrong. āreq.baseUrlā causes app routing on server side to fail:
I changed it as follows, and the routing works again:
To add onto @marcmarcet 's workaround:
angular.json
, make sure to end thebaseHref
with a/
, otherwise view-source will have the prerendered texts in the correct language, but as soon as themain.js
bundle is loaded from/
, the texts are replaced with the default language again./
:I know this was a little bit off-topic, but I canāt be the only one struggling with this, perhaps it helps someone.
Ok, I spend about a one work day, but Iām do it.
a idea: to have files like ~/dist/app-name/server/{locale}/main.js and one main ~/server.js, which start all locales servers as modules:
other things i must change is pass Locale_ID to APP_BASE_HREF in my (Browser) AppModule.
I made it work with 2 node apps working on the server. If anybody can come up with 1 node app serving all locales that would be perfect as we were doing it with webpack.server.config previously.
You can probably use the simpler
"localize": true
, instead of repeating every locale. Thatās what we do.Here we never prerender, we only render live. Glad it could also work for prerendering then š
My solution requires no hacks and it still in use in production today with Angular 15. You might want to try that if the rest is not working for you.
@alan-agius4 from my understanding, the recommended approach includes a hack from https://github.com/angular/universal/issues/1689 inside node_modules. That sounds very problematic, as the hack will get erased on npm install (unless we add more hacks).
Angular Universal and Angular i18n are two major Angular features, and I was expecting them to work together seamlessly by v15. For instance, when you run
ng add @nguniversal/express-engine
, the schematics should check that youāre using i18n and update the server.ts code accordingly.npm run prerender
isnāt working for me either.Could you guys please revisit this issue or recommend a workaround for Angular 15 prerendering with i18n?
A bit more context on my use-case:
ng add @nguniversal/express-engine
npm run prerender
=> error innode_modules/@nguniversal/builders/src/prerender/index.js
:An unhandled exception occurred: Could not find the main bundle: dist/my-project/server/en/main.js
/en/
folder underserver/
:Update:
npm run prerender
worked after adding"localize": ...
toangular.json
under"server" -> "configurations" -> "production"
(see comment below).Here is the entire commit (minus my app very specific changes) that I used to add SSR on an Angular 10 app that already used i18n: https://gist.github.com/PowerKiKi/b8ecd4bdfb3f4d694a1370e3c57a5062
It is based on the
server.run.js
solution. But it automatically gets locales fromangular.json
(so no duplicated config). And it automatically use the proxy config that you might need for your local API.server.ts
still has itsrun()
function in order to runyarn dev-ssr
, although the app still fails because of incorrect baseHref. And it has a full configuration for pm2 where you can see thatserver.run.js
is the main entry point (and notserver.ts
anymore).And to be extra complete here is the relevant nginx configuration to proxy bots, but no humans, to the SSR.
It turns out that the problem comes from angularFire (firebase/firestore). Any data query using a rxjs pipe with take(1) in the application, cause angular universal to get stuck in a infinite loop until the server timeoutā¦ https://github.com/angular/angularfire/issues/2420