Move React server-side rendering into more general SpaServices package

This commit is contained in:
SteveSandersonMS
2016-02-09 16:42:42 -08:00
parent b35ac19485
commit 6c903f33ae
16 changed files with 225 additions and 159 deletions

View File

@@ -1,51 +0,0 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.NodeServices;
using Microsoft.AspNet.Razor.TagHelpers;
using Microsoft.Extensions.PlatformAbstractions;
namespace Microsoft.AspNet.ReactServices
{
[HtmlTargetElement(Attributes = PrerenderModuleAttributeName)]
public class ReactPrerenderTagHelper : TagHelper
{
static INodeServices fallbackNodeServices; // Used only if no INodeServices was registered with DI
const string PrerenderModuleAttributeName = "asp-react-prerender-module";
const string PrerenderExportAttributeName = "asp-react-prerender-export";
[HtmlAttributeName(PrerenderModuleAttributeName)]
public string ModuleName { get; set; }
[HtmlAttributeName(PrerenderExportAttributeName)]
public string ExportName { get; set; }
private IHttpContextAccessor contextAccessor;
private INodeServices nodeServices;
public ReactPrerenderTagHelper(IServiceProvider serviceProvider, IHttpContextAccessor contextAccessor)
{
this.contextAccessor = contextAccessor;
this.nodeServices = (INodeServices)serviceProvider.GetService(typeof (INodeServices)) ?? fallbackNodeServices;
// Consider removing the following. Having it means you can get away with not putting app.AddNodeServices()
// in your startup file, but then again it might be confusing that you don't need to.
if (this.nodeServices == null) {
var appEnv = (IApplicationEnvironment)serviceProvider.GetService(typeof(IApplicationEnvironment));
this.nodeServices = fallbackNodeServices = Configuration.CreateNodeServices(NodeHostingModel.Http, appEnv.ApplicationBasePath);
}
}
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
var request = this.contextAccessor.HttpContext.Request;
var result = await ReactRenderer.RenderToString(
nodeServices: this.nodeServices,
componentModuleName: this.ModuleName,
componentExportName: this.ExportName,
requestUrl: request.Path + request.QueryString.Value);
output.Content.SetHtmlContent(result);
}
}
}

View File

@@ -1,24 +0,0 @@
using System.Threading.Tasks;
using Microsoft.AspNet.NodeServices;
namespace Microsoft.AspNet.ReactServices
{
public static class ReactRenderer
{
private static StringAsTempFile nodeScript;
static ReactRenderer() {
// Consider populating this lazily
var script = EmbeddedResourceReader.Read(typeof (ReactRenderer), "/Content/Node/react-rendering.js");
nodeScript = new StringAsTempFile(script); // Will be cleaned up on process exit
}
public static async Task<string> RenderToString(INodeServices nodeServices, string componentModuleName, string componentExportName, string requestUrl) {
return await nodeServices.InvokeExport<string>(nodeScript.FileName, "renderToString", new {
moduleName = componentModuleName,
exportName = componentExportName,
requestUrl = requestUrl
});
}
}
}