This repository is being used to experiment with Angular 2 (e.g., server side rendering with Edge.js in ASP.NET MVC).
Open a terminal and change directory to the AngularLab folder (the one that contains the package.json).
Before compiling, run npm i
. This will install the Node modules.
To compile, run node run tsc
. This will run the TypeScript compiler.
https://github.com/angular/universal/blob/master/examples/playground/src/server.ts
This file is interesting, as it seems to have some input markup and seems to output some markup.
https://github.com/angular/universal/blob/master/examples/playground/src/app.node.module.ts#L74
This seems to be key. platformDynamicNode comes from Universal, and it has a serializeModule function. That serializeModule function is what I suspect does the server side rendering.
http://www.syntaxsuccess.com/viewarticle/server-side-rendering-in-angular-2.0
Shows a basic example of rendering a sitemap on the server.
https://github.com/MarkPieszak/aspnetcore-angular2-universal/blob/master/Client/bootstrap-server.ts
This actually has some comments and looks relatively simple. Seems to do the serialization using serializeModule as seen elsewhere.
One question I still have is where the tag helpers are defined (i.e., asp-prerender-module, asp-prerender-webpack-config, asp-prerender-data). These could be useful in figuring out how this repo has managed to call Node.js from C#.
This is calling something that is performing a prerender.
I'll want to dig into how that's working in ASP.NET Core so I can possibly port it to .NET 4.5.2.
I'm guessing it's in Microsoft.AspNetCore.SpaServices.Prerendering
.
This references a JavaScript file at /Content/Node/prerenderer.js
.
See if there is a function in that file called renderToString
.
This calls aspNetPrerendering.renderToString
.
See what that does.
https://www.npmjs.com/package/aspnet-prerendering
Find the code for this.
http://blog.stevensanderson.com/2016/05/02/angular2-react-knockout-apps-on-aspnet-core/
Has some high level information on how all this works on ASP.NET Core.
This seems like a rabbit hole I could go down for a while.
Mentions that NodeServices should be usable in ASP.NET MVC 5. Perhaps I could use NodeServices to do the prerendering.
Seems like the code for NodeServices lives in this repo.
This shows how to instantiate NodeServices in a context other than ASP.NET Core. Just above this is a code snippet that shows how to use it. Taken together, I suspect it'd look something like this:
var services = new ServiceCollection();
services.AddNodeServices(options => {
// Set any properties that you want on 'options' here
});
var serviceProvider = services.BuildServiceProvider();
var nodeServices = serviceProvider.GetRequiredService<INodeServices>();
var task = nodeServices.InvokeAsync<int>("./addNumbers", 1, 2);
task.Wait();
var result = task.Result;
Given this C# example, there would need to be a addNumbers.js
files like this:
module.exports = function (callback, first, second) {
var result = first + second;
callback(/* error */ null, result);
};
Unsure of advantages of using this over Edge.js. They seem similar.
https://github.com/aspnet/JavaScriptServices/tree/dev/samples/angular/MusicStore
Server side rendering example.