angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Integrating angular2 Universal with angular CLI project

Open vinodbhargava opened this issue 10 years ago • 214 comments

  1. OS - Windows 8

  2. Versions - angular-cli: 1.0.0-beta.5 node: 4.3.1 os: win32 x64

  3. Yes, the app was created using CLI

  4. Steps - A. Created the app using following command - ng new testApp

    B. Installed angular2-universal dependencies as mentioned in -
    Angular 2 Universal quickstart

    C. 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:3000 it 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.

vinodbhargava avatar Jun 09 '16 08:06 vinodbhargava

Tested that repo but not satisfied. Performance are worse than universal-cli, and universal-cli even don't have AOT.

vforv avatar May 07 '17 11:05 vforv

@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 avatar May 08 '17 15:05 stratio84

@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.

thePunderWoman avatar May 08 '17 15:05 thePunderWoman

@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.

stratio84 avatar May 08 '17 16:05 stratio84

Found this one: https://github.com/stevermeister/angular-ssr But not tested yet...

vforv avatar May 08 '17 16:05 vforv

@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.

AntonScotte avatar May 08 '17 16:05 AntonScotte

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.

thePunderWoman avatar May 08 '17 16:05 thePunderWoman

@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 avatar May 08 '17 16:05 stratio84

@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.

thePunderWoman avatar May 08 '17 16:05 thePunderWoman

@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.

AntonScotte avatar May 08 '17 17:05 AntonScotte

@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

vforv avatar May 08 '17 18:05 vforv

@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.

stratio84 avatar May 08 '17 18:05 stratio84

@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.

thePunderWoman avatar May 08 '17 19:05 thePunderWoman

@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!

thePunderWoman avatar May 08 '17 20:05 thePunderWoman

@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.

devCrossNet avatar May 09 '17 07:05 devCrossNet

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 avatar May 09 '17 07:05 altugank

@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 avatar May 09 '17 09:05 devCrossNet

@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?

somombo avatar May 09 '17 10:05 somombo

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.

stratio84 avatar May 09 '17 10:05 stratio84

@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)

devCrossNet avatar May 09 '17 10:05 devCrossNet