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

Great to hear we can now use --universal. Is there anywhere we can find out about deploying the resulting project to a remote server (CentOS in my case)? I thought it should be just a matter of running "node server.bundle.js" on the server, but after few hours of trying I'm not so sure anymore.

code-div avatar Nov 11 '16 13:11 code-div

@code-div do you get an error or something?

devCrossNet avatar Nov 11 '16 14:11 devCrossNet

@devCrossNet sorry, yes, should've mentioned: "Error: Cannot find module 'angular2-universal-polyfills'".

code-div avatar Nov 11 '16 14:11 code-div

Would be great if you can create an issue in the universal-cli repo

devCrossNet avatar Nov 11 '16 17:11 devCrossNet

So is the idea that with universal, the first hit to the app will be fully rendered on the server and sent to the client, which if your needs are for proper SEO meta tags for crawling, these will be present in the payload sent to the client. Then, when you continue to interact with the app, you will be in client mode?

stusquared avatar Nov 17 '16 19:11 stusquared

@photostu Yes, and it also helps speed up the first rendering of the page since you get a very small pre-rendered page first, and then you load the js. Making the app snappier.

HonnKing avatar Dec 01 '16 07:12 HonnKing

Hi Guys, I have an angular-cli app which is compatible for AOT compilation. and I want it to have a angular universal support as well. so I just run a command "ng serve --universal" and it working perfectly

My Question is - "Does it mean that my app is working fine for angular universal?"

Any help will be appreciated in advance.

shriunyscape avatar Mar 02 '17 08:03 shriunyscape

If you're using the Angular-CLI here it doesn't have any aspects of Universal in it just yet, Universal aspects are within the Fork here (https://github.com/devCrossNet/universal-cli). Hopefully to be integrated with this CLI in the future. It is in Alpha though, so you may experience some issues. @shriunyscape

MarkPieszak avatar Mar 02 '17 15:03 MarkPieszak

Does anyone have any idea when Universal will be integrated with Angular-cli? I'm looking for a realistic timeline.

kblestarge avatar Mar 21 '17 16:03 kblestarge

Please note that the universal cli fork is going to be abandoned soon ( https://github.com/devCrossNet/universal-cli/issues/101#issuecomment-288464913 ) so it may be not a good idea to start stuff using it now.

esistgut avatar Mar 24 '17 15:03 esistgut

@angular/platform-server related PRs: https://github.com/angular/angular-cli/pull/5194 https://github.com/angular/angular-cli/pull/5547

Universal demo from robwormald: https://github.com/robwormald/ng-universal-demo My attempt to get Universal working in CLI: https://github.com/intellix/angular-cli-server/commits/master

Attempt 1

Commit: https://github.com/intellix/angular-cli-server/commit/c2ea1eb21bc88f530cd2fafa57fb1768698eded3

Errors I get when serving via: ng serve --app=server

ERROR Error: The selector "app-root" did not match any elements
    at DefaultServerRenderer2.selectRootElement

Attempt 2

Commit: https://github.com/intellix/angular-cli-server/commit/0159223ce2e873c8653b07d72d595c455537b213

So I found this commit to universal-cli by FrozenPandaz: https://github.com/devCrossNet/universal-cli/pull/85 which is essentially ng4 and platform-server. I copied some stuff into my angular-cli-server project and this time when I try to serve I get the error:

Tried to find bootstrap code, but could not. Specify either statically analyzable bootstrap code or pass in an entryModule to the plugins options.

Which is because the bootstrap code is inside ng-express-engine.ts, but a check is made for it inside your main file here: https://github.com/angular/angular-cli/blob/master/packages/@ngtools/webpack/src/entry_resolver.ts#L133

--

Judging by the PRs in here and universal-cli, I'd say most of the Universal work is coming from @FrozenPandaz so maybe he knows a little more about what's left to get Universal into CLI :)

intellix avatar Mar 25 '17 14:03 intellix

Please be patient, it'll take some time. At this time.. the best option is to probably eject from CLI and then use it in place of my client config here: https://github.com/FrozenPandaz/ng-universal-demo/tree/master/webpack

Those 2 PR's are the main bulk of what I think is required to run universal BUILDS through the CLI. A considerable amount still needs to be done to run a dev environment which I have lined up but am waiting for approval of the initial PR's before submitting.

FrozenPandaz avatar Mar 25 '17 17:03 FrozenPandaz

should be highest prio right now

dimitriy-k avatar Mar 27 '17 11:03 dimitriy-k

should be highest prio right now

indeed!!!!!!!!!!!!!!!!!

guzuomuse avatar Mar 28 '17 19:03 guzuomuse

All the available examples, including the awesome work by @FrozenPandaz show how to achieve SSR via some sort of a server (node.js express),

However, I think it's important not to lose track of the case when you'd want to pre-render selected pages and have them available for static web hosting (example: AWS S3).

This is especially important since it's the recommended way of doing SSR:

In fact, it is our recommendation that your first option should be using Angular Universal pre-rendering to generate static HTML files so you don't need any dynamic rendering at run time and everything can be cached on a CDN

I can imagine the build command would look something like this:

ng build --platform=server --pre-render-urls=ssrUrls.json

ssrUrls.json

{
  "urls": [
    "/",
    "/about",
    "/contact"
  ]
}

DmitryEfimenko avatar Mar 28 '17 22:03 DmitryEfimenko

Based on the reply from https://github.com/angular/angular/issues/13822#issuecomment-285554330 . It sounds like very simple to do the pre-rendering. It would be great if someone can show some example or point out how to do pre-rendering for a cli generated project. Or just do it after call the eject cmd and modify the webpack configure at this stage?

It should be pretty simple to call renderModuleFactory to get your HTML. 4.0 examples should be up at angular/universal repo next week.

shawnsz avatar Mar 29 '17 04:03 shawnsz

I completely agree with @DmitryEfimenko. Some static pages into /dist would perfectly fit into the current way how the Angular-CLI works. No custom-server/express.js to mess around with, instead super easy deployments and great scalability.

JohannesHoppe avatar Mar 29 '17 13:03 JohannesHoppe

Can anyone share a timeline on when universal would be available with cli , or at least a current working instructions to enable sever side rendering on cli project ? Looks like https://github.com/devCrossNet/universal-cli is not stable (or building) currently, So what are the options available for a cli project at this stage ?

kaleemxii avatar Apr 11 '17 19:04 kaleemxii

	It's integrating into core.  Take a look at platform-server.  Rob mentions it in the latest ng-conf keynote 
	

	Get Outlook for iOS

On Tue, Apr 11, 2017 at 12:29 PM -0700, "kaleemxii" [email protected] wrote:

Can anyone share a timeline on when universal would be available with cli , or at least a current working instructions to enable sever side rendering on cli project ? Looks like https://github.com/devCrossNet/universal-cli is not stable (or building) currently, So what are the options available for a cli project at this stage ?

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

goelinsights avatar Apr 11 '17 21:04 goelinsights

Thanks Vijay.

Tried to use the platform-server as per the keynote , but couldn't get the main.server.ts configured in angular-cli.json without ejecting cli and going back to webpack.

Tried following this https://www.softwarearchitekt.at/post/2017/03/07/server-side-rendering-with-angular-4.aspx , but after ejecting cli , there are multiple issues popping up with webpack (around scss and aot compilation).

Waiting on platform-server support with cli ..

kaleemxii avatar Apr 12 '17 17:04 kaleemxii