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

Currently also stuck in same place as @mikkeldamm. Since I'm currently hosting in S3 with a fairly static site, also looking to see if anyone else is just using S3 static site and not EC2/ heroku/ GCP/ Azure/ etc to run a server.

Looking to understand the prerender on build with an S3 upload of post-processed static files (then pushed to Cloudfront). Has anyone seen a good resource/ example on this? Rob Wormald mentioned it as the easier end of the spectrum at ng-conf, but had no luck with angular-ssr and not seeing much documentation on this.

goelinsights avatar Jul 06 '17 09:07 goelinsights

@mikkeldamm yeah, that's a very basic example. Plus lazy loaded modules aren't rendered server side. I'm eagerly waiting for the CLI implementation... And some documentation and a style guide / best practices.

@wnabil regarding your comment below: that is so much overhead... Having 2 separate processes for server/client build, one with cli/webpack and another with gulp. If you absolutely must have SSR today, maybe. But I'd wait for the CLI.

Sent from my Samsung SM-G950F using FastHub

NgxDev avatar Jul 06 '17 09:07 NgxDev

@mikkeldamm i am still new to angular universal but i think we can make the integration like here https://github.com/pquarme/cli-universal-demo and using gulp we can compile the scss and also the third party libraries with webpack external

wnabil avatar Jul 06 '17 09:07 wnabil

this one looks promising: https://www.npmjs.com/package/angular-ssr haven't had the time to try it yet tho

jofrly avatar Jul 06 '17 09:07 jofrly

So, what the plan about cli ssr support? I'm waiting it so much :(

glebmachine avatar Jul 06 '17 09:07 glebmachine

@goelinsights I have also S3 and CloudFront serving my ng app. I am planning to use nodejs in a docker container to serve it server side rendered.

@MrCroft

But I'd wait for the CLI.

Waiting from 27th of March (my first comment here) :(

dimitriy-k avatar Jul 06 '17 10:07 dimitriy-k

Hey guys, I know it might not feel like it, but the cli team does have intentions on getting us a stable integration of universal into the cli as soon as possible.

I can't remember which of the team members I spoke to during the ngConf angular cli Q&A, but I do remember this being my #1 question for the team and I remember them saying something to the effect of them really wanting to get it added, but that when it does get implemented into a release of the cli, they wanted to make sure that it followed their standard, which is "it just works" - meaning, they don't want to push the feature to an update until they are absolutely confident that it is working and people don't have to worry about repercussions of its usage.

I'm eagerly awaiting this capability as well. Waiting sucks, but sometimes with open source we have to.

offroadkev avatar Jul 07 '17 05:07 offroadkev

I can second what @vegaskev said. I talked to the team at Google I/O this year, and they said very similar things. They are keenly aware of how much people want this. It really does suck having to wait, but it will happen.

thePunderWoman avatar Jul 07 '17 11:07 thePunderWoman

Angular CLI team. Don't hurry. We all understand that while we are lying under the sun at the sea side you work hard to make a great product for us. Thank you for that. Better It'l be good, we can wait. We believe in you! And love you of course :heart: :kissing_heart:

alexbyk avatar Jul 07 '17 12:07 alexbyk

The best way is to give an option in cli like $ ng build --prod --ssr or $ ng build --ssr which will build a simple express server server.js in the dist directory like in https://www.npmjs.com/package/angular-ssr

gopumon avatar Jul 07 '17 17:07 gopumon

In the meantime I think a lot of us would be happy with just documentation and guidelines on setting things up. Particularly to do with:

  1. Lazy loading and SSR

  2. Meta tags

  3. Sending http status codes like 301, 404, etc from within the module back up through zone/express

  4. preboot and state transition related stuff

  5. prerendering static htmls for components/pages that don't rely on dynamic data, if at all possible

  6. optional caching of http/API calls in a server side cache for pages/components with dynamic data

Not really relevant to the cli, but given that the feature itself has nearly no documentation, many of us don't even know where to begin. I'll probably give angular-ssr a try till then. It looks quite promising.

I have almost everything working fine with ng2 but I'm eager to upgrade and take advantage of the improvements in angular 4. At this rate I'm likely better off moving to vue or react over this one issue.

On Fri 7 Jul, 2017, 11:27 PM Gopakumar, [email protected] wrote:

The best way is to gave an option in cli like $ ng build --prod --ssr or $ ng buil --prod --ssr which will build a simple express server server.js in the dist directory like in https://www.npmjs.com/package/angular-ssr

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/angular/angular-cli/issues/1050#issuecomment-313751047, or mute the thread https://github.com/notifications/unsubscribe-auth/AAeZ8Z02TLXe_KYfqEuCrC5j_wuxYHpLks5sLnFugaJpZM4Ixuiz .

fareesh avatar Jul 07 '17 18:07 fareesh

@kblestarge i've been using https://github.com/clbond/angular-ssr and works pretty good (even with angular material and other modules)

@stratio84 i dont know how would be possible to have build time rendering as we have assync calls almost everywhere on web..

to be honest.. very happy with @clbond project and as soon as the oficial support for universal comes to angular for SSR, will be quite easy to port this project on top of that!

thanks angular team for the awesome work!

mariohmol avatar Jul 12 '17 12:07 mariohmol

1.3.0-rc.0 contains the Universal/server stuff we were waiting for from FrozenPandaz: https://github.com/angular/angular-cli/releases/tag/v1.3.0-rc.0 Here's an example repository referenced from the PR which implements it: https://github.com/FrozenPandaz/u-cli-app

Will have a play around with it now maybe

intellix avatar Jul 22 '17 02:07 intellix

Seems documentation has just been added

https://github.com/angular/angular-cli/wiki/stories-universal-rendering

EricBichara avatar Jul 26 '17 06:07 EricBichara

Yeah, a day ago)))

neilhem avatar Jul 26 '17 06:07 neilhem

	Looks like a great step forward! Are there any instructions on aot pre-rendering? - I remember Rob talking about this at ng-conf (which then leads to pwa => amp pages which should be great for content sites)

Very excited for the lazy loading additions (would be great to finally see cli, lazy loading, universal, Ng Rx store and angular material 2 all playing well together)

	Get Outlook for iOS

On Tue, Jul 25, 2017 at 11:10 PM -0700, "Eric" [email protected] wrote:

Seems documentation has just been added

https://github.com/angular/angular-cli/wiki/stories-universal-rendering

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

goelinsights avatar Jul 26 '17 06:07 goelinsights

Finally! Yeah)

But what about to support lazy loading?

glebmachine avatar Jul 26 '17 08:07 glebmachine

It seems to fail when the app imports libraries using ES2015 imports, for example @angular/flex-layout.

Steps to reproduce:

  • Create a new @angular/[email protected] project
  • Follow the steps in the universal rendering story (I write the final server test script in server-test.js, from step Testing the bundle)
  • npm i -S @angular/flex-layout
  • In app.module.ts, add FlexLayoutModule to AppModule imports
  • ng build --prod --app 1 and rename the main bundle to match the import in server-test.js
    • FYI a convenience npm script to rename after the server build: "rename-server-main": "renamer dist-server/* --regex --find \"main\\.[^\\.]*?\\.bundle.js\" --replace main.bundle.js" + npm i -D renamer, and update server-test.js to require('./dist-server/main.bundle').
  • Test with: node server-test.js

It logs the following error:

C:\nw\proto\ng-server\node_modules\@angular\flex-layout\media-query\breakpoints\break-points-token.js:8
import { InjectionToken } from '@angular/core';
^^^^^^

SyntaxError: Unexpected token import

After commenting the FlexLayoutModule module import, it works again.

antoineol avatar Jul 26 '17 17:07 antoineol

I dont think flexlayout its ready for SSR.. in the angular-ssr i could make even the angular-material to work but i couldnt make the the flex layout.. check it out

  • https://github.com/angular/flex-layout/issues/187

mariohmol avatar Jul 26 '17 18:07 mariohmol

@mariohmol It's just an example of library that we can find in the project and has an ES2015 import, thus blocking the SSR. I guess many other libraries will have the same module format (using import), and the issue above would be for any of them. I haven't tried, but we may be able to follow the same steps and replace FlexLayoutModule by another lib module with import. (But the MdButtonModule from material does not cause the issue.)

Good to know that flex-layout would have blocked SSR anyway after the import issue, thanks for the link. But I didn't even reach this error (document, window...), it is failing before.

EDIT: Angular material seems to also use ES2015 import, so I'm not clear about why flex-layout's import is not tolerated by Angular CLI SSR, but material's is.

antoineol avatar Jul 26 '17 21:07 antoineol