Integrating angular2 Universal with angular CLI project
-
OS - Windows 8
-
Versions - angular-cli: 1.0.0-beta.5 node: 4.3.1 os: win32 x64
-
Yes, the app was created using CLI
-
Steps - A. Created the app using following command -
ng new testAppB. Installed angular2-universal dependencies as mentioned in -
Angular 2 Universal quickstartC. Deployed client using command -
ng server(server started on port 4200) Started express server through -node server.js(server started on port 3000)D. Accessed the client and got following message in browser - test-app works! But when accessing
http://localhost:3000it gives below error message in console -
EXCEPTION: Error: Protocol "d:" not supported. Expected "http:".
STACKTRACE:
Error: Protocol "d:" not supported. Expected "http:".
at new ClientRequest (_http_client.js:52:11)
at Object.exports.request (http.js:31:10)
at Object.exports.get (http.js:35:21)
at D:\Development\Angular2\angular-cli\cli-angular\testApp\node_modules\angular2-universal\dist\node\platform\node_xhr_impl.js:53:22
at ZoneDelegate.invoke (D:\Development\Angular2\angular-cli\cli-angular\testApp\node_modules\zone.js\dist\zone-node.js:281:29)
at Object.NgZoneImpl.inner.inner.fork.onInvoke (D:\Development\Angular2\angular-cli\cli-angular\testApp\node_modules\@angular\core\src\zone\ng_zone_impl.js:45:41)
at ZoneDelegate.invoke (D:\Development\Angular2\angular-cli\cli-angular\testApp\node_modules\zone.js\dist\zone-node.js:280:35)
at Zone.run (D:\Development\Angular2\angular-cli\cli-angular\testApp\node_modules\zone.js\dist\zone-node.js:174:44)
at NgZoneImpl.runInner (D:\Development\Angular2\angular-cli\cli-angular\testApp\node_modules\@angular\core\src\zone\ng_zone_impl.js:76:71)
at NgZone.run (D:\Development\Angular2\angular-cli\cli-angular\testApp\node_modules\@angular\core\src\zone\ng_zone.js:223:66)
D:\Development\Angular2\angular-cli\cli-angular\testApp\node_modules\@angular\core\src\application_ref.js:302
throw e;
^
Kindly suggest if i had missed any step.
Any help will be appeciated.
I'm also do https://github.com/angular/angular-cli/wiki/stories-universal-rendering step by steps.
But for now i've got following error:
import { Directive, ElementRef, Input, Renderer, Renderer2, IterableDiffers, KeyValueDiffers } from '@angular/core';
^^^^^^
SyntaxError: Unexpected token import
at Object.exports.runInThisContext (vm.js:76:16)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.eO38 (dist-server/main.bundle.js:1:1140643)
at l (dist-server/main.bundle.js:1:149)
I'm also trying to using ts-node with --ignore false, but it isn't works for me.
what does you tsconfig.server.json look like?
This is my tsconfig.server.json. { "extends": "../tsconfig.json", "compilerOptions": { "outDir": "../out-tsc/app", "module": "commonjs", "baseUrl": "", "types": ["node"] }, "exclude": [ "test.ts", "**/*.spec.ts" ], "angularCompilerOptions": { "entryModule": "app/app.server.module#AppServerModule" } }
ok just wanted to check you had "module": "commonjs" as that error usually means something has compiled to es2015 on the server side which doesn't work in nodejs.
Is your tsconfig in angular-cli.json pointing to this file for the server settings?
yes, This is my angular-cli.json file.
{ "$schema": "./node_modules/@angular/cli/lib/config/schema.json", "project": { "name": "appName" }, "apps": [ { "root": "src", "outDir": "dist", "assets": [ "assets", "favicon.ico" ], "index": "index.html", "main": "main.ts", "polyfills": "polyfills.ts", "test": "test.ts", "tsconfig": "tsconfig.app.json", "testTsconfig": "tsconfig.spec.json", "prefix": "app", "styles": [ "../node_modules/bootstrap/dist/css/bootstrap.css", "../node_modules/font-awesome/css/font-awesome.css", "../node_modules/devextreme/dist/css/dx.common.css", "../node_modules/devextreme/dist/css/dx.light.css", "styles.css", "assets/theme/css/style.css", "assets/styles/step-application.css" ], "scripts": [ "../node_modules/jquery/dist/jquery.js", "../node_modules/bootstrap/dist/js/bootstrap.js" ], "environmentSource": "environments/environment.ts", "environments": { "dev": "environments/environment.ts", "prod": "environments/environment.prod.ts" } }, { "platform": "server", "root": "src", "outDir": "dist-server", "assets": [ "assets", "favicon.ico" ], "index": "index.html", "main": "main.server.ts", "test": "test.ts", "tsconfig": "tsconfig.server.json", "testTsconfig": "tsconfig.spec.json", "prefix": "app", "styles": [ "../node_modules/bootstrap/dist/css/bootstrap.css", "../node_modules/font-awesome/css/font-awesome.css", "../node_modules/devextreme/dist/css/dx.common.css", "../node_modules/devextreme/dist/css/dx.light.css", "styles.css", "assets/theme/css/style.css", "assets/styles/step-application.css" ], "scripts": [ "../node_modules/jquery/dist/jquery.js", "../node_modules/bootstrap/dist/js/bootstrap.js" ], "environmentSource": "environments/environment.ts", "environments": { "dev": "environments/environment.ts", "prod": "environments/environment.prod.ts" } } ], "e2e": { "protractor": { "config": "./protractor.conf.js" } }, "lint": [ { "project": "src/tsconfig.app.json" }, { "project": "src/tsconfig.spec.json" }, { "project": "e2e/tsconfig.e2e.json" } ], "test": { "karma": { "config": "./karma.conf.js" } }, "defaults": { "styleExt": "css", "component": {} } }
Try removing the scripts from your server settings
"module": "commonjs" helps, now i have to fight with 3rd party packages)
Do you have any ideas if angular cli universal rendering can be used with ASP.NET Core as implemented by @MarkPieszak in Angular universal with ASP.NET Core?
I could successfully get server bundles with CLI. But as soon as I consume main.bundle in Microsoft.AspNetCore.SpaServices.Prerendering.Prerenderer.RenderToString(...) it throws an error that webpackJsonp function is not available. It is defined in inline.bundle and it is used window object here, but window object is not available on the server side. I am not sure if/how it works with .NET Core. Any ideas?
@KhalipskiSiarhei you can try import it function from inline..bundle.js, that has been generated after ng build / ng build --prod. or try import 'dist/inline..bundle.js' I haven`t tried it, might be work
@PSTime I am not sure how it is possible to import inline.bundle in server ASP.NET Core environment: in Microsoft.AspNetCore.SpaServices.Prerendering.Prerenderer.RenderToString(...) it is possible to pass one module only, which in fact is main.bundle. Furthermore, inline.bundle is created for browser environment because it contains document.getElementsByTagName, document.createElement, window references which are not available in server environment.
If it helps i have a small demo showing Universal working in the CLI with both prerendering and express scenarios and Lazy Loading here https://github.com/Toxicable/universal-starter/tree/cli
Just run npm run build:prerender or npm run build:dynamic and inspect the /dist folder to see how my implementation works.
Feel free to ping me on the Angular Gitter with @Toxicable if you have any questions https://gitter.im/angular/angular
@Toxicable thanks, I will look at it. For now we still use CLI for client bundles and webpack for server bundles and it is not ideal...
https://github.com/angular/angular-cli/issues/1050#issuecomment-322974848 ... angular cli doesn't generate
LAZY_MODULE_MAP(I have added platform=server), do you have any suggestion ?
@PSTime I can confirm that I also have not been able to import LAZY_MODULE_MAP and I dont see it anywhere in the build output.
I suggest filing a separate bug on this @angular/cli repo..
Do you mind filing it? :bouquet: :relieved:
Be sure to reference the @nguniversal/module-map-ngfactory-loader instructions.
@somombo what version of the CLI? you need v1.3+ for LAZY_MODULE_MAP
In all of the testing i've done it's always worked like so
const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require(./dist-server/main.${hash}.bundle);
@Toxicable Thank you for this! Best implementation I've found so far that I could simply make work without banging my head against the wall or getting errors I couldn't even understand :) Also, first implementation that works with lazy loaded modules. I am curious though, what will the final official implementation be... I mean how will the CLI do it, by using only a command / argument when creating and when building the app...
Do you have any examples for things such as:
- reading cookies server-side (the content of some components might depend on that, let's say if a user is authenticated or not - verifiable through a cookie)
- not making an http request on the client side if we already have that data in the server rendered view
?
@MrCroft as far as I know I don't think it will change too much from how it looks now. Things like the prerender.js script I have and the server.js are very much things that the domain should provide rather than the CLI, therefore in that regard I don't think the CLI can provide much more.
As for cookies I havn't looked at how this will work yet, I'll put that on my list though, if you want to make an issue/proposal on https://github.com/angular/universal for it that would be appreciated.
Your second point is referring to a concept called StateTransfer, this is the process of storing your state on the server then transferring it to the client and reusing it. I am currently working on a proposal for this and subsequently a PR
Is universal now part of angular client or not yet? If not, is there any timeline when it could be?
@ennyta Yes, take a look at universal-starter to see it in action. I'll be updating the docs here in the CLI for it as well.
@mrcroft You'll still have to run and bundle the server things yourself, it won't be part of ng serve, that will still produce only the client bundle. The concept of universal implies the server aspect, so there needs to be that separation.
Also this gives freedom to any type of node server someone wishes to use, hapi koa express etc.
As for cookies, in the server you'd have to handle it yourself by passing in the req object to the providers[] and then use Dependency injection to have the server use those, while the client gets them from document.cookies.
Hope that helps a little bit!
Also, right now you can manually create or use one of the TransferHttp examples we have, but soon it will be part of Core that will (optionally) handle it, if you tell it to do it for a certain GET call.
Thanks @MarkPieszak Is there a way to test somehow universal build from dist-server locally?
@ennyta If you're using https://github.com/angular/universal-starter and in the cli project then you can use npm run serve:static for static rendering ( aka prerendering) or
npm run serve:dynamic for dynamic rendering (using express)
to see how it looks as if you were running on a prod server but locally