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.
Tested that repo but not satisfied. Performance are worse than universal-cli, and universal-cli even don't have AOT.
@janiukjf Did you manage to have your app working with cli and SSR even with prerendered AJAX HTTP requests? I checked on https://github.com/pquarme/cli-universal-demo but it's not implementing any prerendered AJAX HTTP requests. Is there any repo showcasing cli with ssr and prerendered AJAX reqs?
Thanks,
G.
@stratio84 I haven't gotten that far yet. I'm using Angular Material, and that repo is breaking most SSR. Until they get a few PRs merged, I can't test all of it without removing a ton of code. That said, I'm using route resolvers for most of my calls. So I can't imagine why it wouldn't pre-render that. If it required calls on the page post init, I doubt it would pre-render them.
@janiukjf The problem is that I need a full prerender so that I can upload my app on an AWS S3 bucket. I'm trying to find an example that uses angular cli but I found none so far. The one at https://github.com/clbond/angular-ssr has an example but it's not using angular-cli. Which repo do you advice me to start from in order to develop a working skeleton?
Thanks,
G.
Found this one: https://github.com/stevermeister/angular-ssr But not tested yet...
@stratio84. With https://github.com/evertonrobertoauler/cli-universal-demo you're at least able to fully aot-render a working application that can be uploaded to an S3 bucket. The only actual static file is the index.html and everything else is compiled angular code that gets loaded in. I've yet to find any solution that renders multiple static pages. @vforv with this setup I'm getting satisfactory performance, not better than the regular aot in angular-cli though.
I can't imagine the performance of each individual technique ever being any better as all of these SSR approaches use essentially the same process. All the code always has to go through the same compiler and use platform-server / browser. The only thing that might actually make a difference is what server it's running on and the complexity of what your app has to do on first load.
@AntonScotte I checked that repo but the code is not implementing any AJAX HTTP call. Are you saying that if i clone that repo and I myself add one call it will be prerendered just by compiling with ng build --aot?
Thanks everybody for the help and sorry for the rookie questions but I have been discovering angular 4 and ssr only since a couple weeks. I'm coming from AngularJS and everything seems quite different :)
@stratio84 Take a look at https://github.com/FrozenPandaz/ng-universal-demo. There's a transferState module that I think is what you're looking for. You'll find the app server file here: https://github.com/FrozenPandaz/ng-universal-demo/blob/master/src/app/server-app.module.ts#L38. It's referencing the TransferState module.
@stratio84 I'm not sure, but in general my application works as normal when I ported over to SSR and I'm using a bunch of observables throughout the app. I'm not keen on AJAX calls, so I don't want to give false hope out if ignorance but, a few quirks had to be delt with to tell angular to execute chunks on the client instead of server side as for example window could not be accessed server side. My guess is that you might be able to do a similar workaround to prevent angular from directly executing the call on the server and wait until the client is ready to go.
@janiukjf client bundle can significantly increase performance... Current size is too big 350KB+... It is wird universal-cli with Angular 2 have better performance that that repo with Angular 4.
On reguler 3G network universal-cli "Hello world" app open for 2 sec but for that repo it taks 5s
@janiukjf I just cloned and build it with aot. Inside the home page it contains an http call to a json whose content is then shown inside the page. However even with aot build the request doesn't get prerendered at build time, It is still issued at runtime. My main goal is to have an app that can be fully prerendered at build time so that when the S3 bucket serve it it is completely static and no elaboration is doing even client side.
Thanks
G.
@vforv universal-cli is also 4 months or more out of date and doesn't seem to be maintained anymore. I wonder how the performance would be if it was brought up to date with the latest code.
@stratio84 If you do get things working the way you want them to, please post back here about it. It'd be interesting to see. Good luck!
@janiukjf the universal-cli project is no longer maintained. I moved further to other projects and @FrozenPandaz wanted to implement universal in the angular-cli. But I don't know the status.
They said angular universal will be maintained by Angular official team when ng4 released. Is there still a problem with angular-cli and universal?
@altugank I think it is not really clear how to use it together. The original plan for the integration was to have some kind of flag that gives you all the power of the cli and make it easy to use angular-universal with different use cases (pre-rendering/re-rendering).
Right now it seems to be a whole bunch of manual steps to get these things to work (not a good DX in my opinion).
@FrozenPandaz: are you still working on the universal/cli integration?
@devCrossNet On usage together, I think if ultimately, it could be like angular-ssr would be great!
About @FrozenPandaz ... I understood https://github.com/angular/angular-cli/issues/1050#issuecomment-289225562 as that he is basically waiting on you to merge his PRs.. (Almost all of the open PRs on universal/cli are from him to you).
Am I mistaken?
Hello, I checked https://github.com/clbond/angular-ssr and tested the example with express for on demand rendering. The problem is that it does not render at build time but only at request time on server side.
It would be really helpful to look at an example that does a full prerender of routes and AJAX requests completely at build time (better if with angular-cli but anything else working would be good too).
Anybody can provide a working repo showcasing these features?
Thanks,
G.
@somombo the universal-cli is no longer maintained. That's the reason why the PRs are not merged. @FrozenPandaz and I decided to leave it like it is in favor of a real angular-cli/angular-universal integration that he is working on. (I already saw a proposal to the team some month ago but I don't know the status)