How does ASP.NET Core implement SPA server-side rendering internally?
I am trying to understand the internals of how ASP.NET Core implements server-side rendering with SPAs. To be clear, I'm not asking how to implement SPA server-side rendering, but rather what is occurring when SPA server-side rendering is being performed by .NET and Node.js.
What I understand so far is that .NET interacts with Node.js by calling a Node.js instance via the InvokeAsync<T> or InvokeExportAsync<T> methods. A js file path is passed in alongside any other parameters (serialized as JSON) that will be received by the module.exports function defined in the js module itself. Result data is then passed back to .NET by passing it into the callback that was also passed to the module.exports function. ASP.NET Core's NodeServices README has an example of it here as well as a few others further down the page. SpaServices also has a very similar example that specifically relates to server-side rendering.
Although I was able to understand the process a little bit better by digging into the source code, I still don't understand what is really happening behind the scenes here. My one clue is that INodeServices briefly mentions that its methods return a Task<TResult> object that represents the completion of something called an RPC call. I looked into this and I believe this is referring to what's called a remote procedure call. Unfortunately, I'm not familiar with RPC, how to use it, or what it is doing internally.
The other mystery is how data is being received in either direction. How is the module.exports function receiving the arguments passed to InvokeAsync<T> and/or InvokeExportAsync<T> on the .NET side? The callback parameter is also a mystery. Where is this callback coming from, and how does it know how to send the result data passed to it back to .NET?
My question is, what is going on here under the hood? RPC seems to be a factor here but on the surface it all appears to be just magic. There is also this Node.js instance being talked about, but what exactly is it? Is it its own separate server (and if so why not just use http?) or something else?
Please help me understand all of this. Thanks!
0 comments:
Post a Comment