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

@@ -0,0 +1,32 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNet.NodeServices;
using Newtonsoft.Json.Linq;
namespace Microsoft.AspNet.SpaServices.Prerendering
{
public static class Prerenderer
{
private static Lazy<StringAsTempFile> nodeScript;
static Prerenderer() {
nodeScript = new Lazy<StringAsTempFile>(() => {
var script = EmbeddedResourceReader.Read(typeof(Prerenderer), "/Content/Node/prerenderer.js");
return new StringAsTempFile(script); // Will be cleaned up on process exit
});
}
public static async Task<RenderToStringResult> RenderToString(INodeServices nodeServices, string componentModuleName, string componentExportName, string requestAbsoluteUrl, string requestPathAndQuery) {
return await nodeServices.InvokeExport<RenderToStringResult>(nodeScript.Value.FileName, "renderToString",
/* bootModulePath */ componentModuleName,
/* bootModuleExport */ componentExportName,
/* absoluteRequestUrl */ requestAbsoluteUrl,
/* requestPathAndQuery */ requestPathAndQuery);
}
}
public class RenderToStringResult {
public string Html;
public JObject Globals;
}
}