refactor: apply default vs transform to xproj

refactor(spa-services): clean code

refactor(node-services): clean code, extract classes nto separate files

refactor(angular-services): prime cache cleanup
This commit is contained in:
Andrei Tserakhau
2016-05-23 11:28:42 +03:00
parent 2c35945562
commit 95cba7f5dd
32 changed files with 621 additions and 448 deletions

View File

@@ -0,0 +1,14 @@
namespace Microsoft.AspNetCore.SpaServices.Prerendering
{
public class JavaScriptModuleExport
{
public JavaScriptModuleExport(string moduleName)
{
this.ModuleName = moduleName;
}
public string ModuleName { get; private set; }
public string ExportName { get; set; }
public string WebpackConfig { get; set; }
}
}

View File

@@ -16,11 +16,31 @@ namespace Microsoft.AspNetCore.SpaServices.Prerendering
[HtmlTargetElement(Attributes = PrerenderModuleAttributeName)]
public class PrerenderTagHelper : TagHelper
{
static INodeServices fallbackNodeServices; // Used only if no INodeServices was registered with DI
private const string PrerenderModuleAttributeName = "asp-prerender-module";
private const string PrerenderExportAttributeName = "asp-prerender-export";
private const string PrerenderWebpackConfigAttributeName = "asp-prerender-webpack-config";
private static INodeServices _fallbackNodeServices; // Used only if no INodeServices was registered with DI
const string PrerenderModuleAttributeName = "asp-prerender-module";
const string PrerenderExportAttributeName = "asp-prerender-export";
const string PrerenderWebpackConfigAttributeName = "asp-prerender-webpack-config";
private readonly string _applicationBasePath;
private readonly INodeServices _nodeServices;
public PrerenderTagHelper(IServiceProvider serviceProvider)
{
var hostEnv = (IHostingEnvironment) serviceProvider.GetService(typeof(IHostingEnvironment));
_nodeServices = (INodeServices) serviceProvider.GetService(typeof(INodeServices)) ?? _fallbackNodeServices;
_applicationBasePath = hostEnv.ContentRootPath;
// 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 (_nodeServices == null)
{
_nodeServices = _fallbackNodeServices = Configuration.CreateNodeServices(new NodeServicesOptions
{
HostingModel = NodeHostingModel.Http,
ProjectPath = _applicationBasePath
});
}
}
[HtmlAttributeName(PrerenderModuleAttributeName)]
public string ModuleName { get; set; }
@@ -35,52 +55,37 @@ namespace Microsoft.AspNetCore.SpaServices.Prerendering
[ViewContext]
public ViewContext ViewContext { get; set; }
private string applicationBasePath;
private INodeServices nodeServices;
public PrerenderTagHelper(IServiceProvider serviceProvider)
{
var hostEnv = (IHostingEnvironment)serviceProvider.GetService(typeof (IHostingEnvironment));
this.nodeServices = (INodeServices)serviceProvider.GetService(typeof (INodeServices)) ?? fallbackNodeServices;
this.applicationBasePath = hostEnv.ContentRootPath;
// 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) {
this.nodeServices = fallbackNodeServices = Configuration.CreateNodeServices(new NodeServicesOptions {
HostingModel = NodeHostingModel.Http,
ProjectPath = this.applicationBasePath
});
}
}
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
var request = this.ViewContext.HttpContext.Request;
var request = ViewContext.HttpContext.Request;
var result = await Prerenderer.RenderToString(
applicationBasePath: this.applicationBasePath,
nodeServices: this.nodeServices,
bootModule: new JavaScriptModuleExport(this.ModuleName) {
exportName = this.ExportName,
webpackConfig = this.WebpackConfigPath
_applicationBasePath,
_nodeServices,
new JavaScriptModuleExport(ModuleName)
{
ExportName = ExportName,
WebpackConfig = WebpackConfigPath
},
requestAbsoluteUrl: UriHelper.GetEncodedUrl(request),
requestPathAndQuery: request.Path + request.QueryString.Value);
request.GetEncodedUrl(),
request.Path + request.QueryString.Value);
output.Content.SetHtmlContent(result.Html);
// Also attach any specified globals to the 'window' object. This is useful for transferring
// general state between server and client.
if (result.Globals != null) {
if (result.Globals != null)
{
var stringBuilder = new StringBuilder();
foreach (var property in result.Globals.Properties()) {
foreach (var property in result.Globals.Properties())
{
stringBuilder.AppendFormat("window.{0} = {1};",
property.Name,
property.Value.ToString(Formatting.None));
}
if (stringBuilder.Length > 0) {
output.PostElement.SetHtmlContent($"<script>{ stringBuilder.ToString() }</script>");
if (stringBuilder.Length > 0)
{
output.PostElement.SetHtmlContent($"<script>{stringBuilder}</script>");
}
}
}
}
}
}

View File

@@ -1,42 +1,34 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.NodeServices;
using Newtonsoft.Json.Linq;
namespace Microsoft.AspNetCore.SpaServices.Prerendering
{
public static class Prerenderer
{
private static Lazy<StringAsTempFile> nodeScript;
private static readonly Lazy<StringAsTempFile> NodeScript;
static Prerenderer() {
nodeScript = new Lazy<StringAsTempFile>(() => {
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(string applicationBasePath, INodeServices nodeServices, JavaScriptModuleExport bootModule, string requestAbsoluteUrl, string requestPathAndQuery) {
return await nodeServices.InvokeExport<RenderToStringResult>(nodeScript.Value.FileName, "renderToString",
public static async Task<RenderToStringResult> RenderToString(
string applicationBasePath,
INodeServices nodeServices,
JavaScriptModuleExport bootModule,
string requestAbsoluteUrl,
string requestPathAndQuery)
=> await nodeServices.InvokeExport<RenderToStringResult>(
NodeScript.Value.FileName,
"renderToString",
applicationBasePath,
bootModule,
requestAbsoluteUrl,
requestPathAndQuery);
}
}
public class JavaScriptModuleExport {
public string moduleName { get; private set; }
public string exportName { get; set; }
public string webpackConfig { get; set; }
public JavaScriptModuleExport(string moduleName) {
this.moduleName = moduleName;
}
}
public class RenderToStringResult {
public string Html;
public JObject Globals;
}
}
}

View File

@@ -0,0 +1,10 @@
using Newtonsoft.Json.Linq;
namespace Microsoft.AspNetCore.SpaServices.Prerendering
{
public class RenderToStringResult
{
public JObject Globals { get; set; }
public string Html { get; set; }
}
}