Add simpler prerendering API. Fixes #607

This commit is contained in:
Steve Sanderson 2017-07-03 14:07:28 +01:00
Родитель 9cce26ebd8
Коммит 94fc84a9b4
13 изменённых файлов: 225 добавлений и 33 удалений

Просмотреть файл

@ -1,6 +1,7 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.NodeServices;
using Microsoft.AspNetCore.SpaServices.Prerendering;
namespace NodeServicesExamples.Controllers
{
@ -34,6 +35,20 @@ namespace NodeServicesExamples.Controllers
return View();
}
public async Task<IActionResult> Prerendering([FromServices] ISpaPrerenderer prerenderer)
{
var result = await prerenderer.RenderToString("./Node/prerenderPage");
if (!string.IsNullOrEmpty(result.RedirectUrl))
{
return Redirect(result.RedirectUrl);
}
ViewData["PrerenderedHtml"] = result.Html;
ViewData["PrerenderedGlobals"] = result.CreateGlobalsAssignmentScript();
return View();
}
public IActionResult Error()
{
return View("~/Views/Shared/Error.cshtml");

Просмотреть файл

@ -0,0 +1,14 @@
var createServerRenderer = require('aspnet-prerendering').createServerRenderer;
module.exports = createServerRenderer(function(params) {
return new Promise(function (resolve, reject) {
var message = 'The HTML was returned by the prerendering boot function. '
+ 'The boot function received the following params:'
+ '<pre>' + JSON.stringify(params, null, 4) + '</pre>';
resolve({
html: '<h3>Hello, world!</h3>' + message,
globals: { sampleData: { nodeVersion: process.version } }
});
});
});

Просмотреть файл

@ -9,7 +9,7 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\src\Microsoft.AspNetCore.NodeServices\Microsoft.AspNetCore.NodeServices.csproj" />
<ProjectReference Include="..\..\..\src\Microsoft.AspNetCore.SpaServices\Microsoft.AspNetCore.SpaServices.csproj" />
</ItemGroup>
<ItemGroup>

Просмотреть файл

@ -17,6 +17,7 @@ namespace NodeServicesExamples
// Enable Node Services
services.AddNodeServices();
services.AddSpaPrerenderer();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

Просмотреть файл

@ -9,4 +9,5 @@
<ul>
<li><a asp-action="ES2015Transpilation">ES2015 transpilation</a></li>
<li><a asp-action="Chart">Server-side chart rendering</a></li>
<li><a asp-action="Prerendering">Server-side SPA prerendering</a></li>
</ul>

Просмотреть файл

@ -0,0 +1,21 @@
<h1>Server-side prerendering</h1>
<p>
This sample demonstrates how you can invoke a JavaScript module that contains
prerendering logic for a Single-Page Application framework.
</p>
</p>
Your prerendering boot function will receive parameters that describe the page
being rendered and any data supplied by the .NET code. The return value should be
a promise that resolves with data to be injected into the page, such as the
rendered HTML and any global data that should be made available to client-side code.
</p>
@Html.Raw(ViewData["PrerenderedHtml"])
<script>@Html.Raw(ViewData["PrerenderedGlobals"])</script>
<script>
// Demonstrates how client-side code can receive data from the prerendering process
console.log('Received Node version from prerendering logic: ' + sampleData.nodeVersion);
</script>

Просмотреть файл

@ -2,6 +2,7 @@
"name": "nodeservicesexamples",
"version": "0.0.0",
"dependencies": {
"aspnet-prerendering": "^2.0.6",
"babel-core": "^6.7.4",
"babel-preset-es2015": "^6.6.0",
"node-chartist": "^1.0.2"

Просмотреть файл

@ -0,0 +1,49 @@
using System.Threading;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.NodeServices;
using System.Threading.Tasks;
namespace Microsoft.AspNetCore.SpaServices.Prerendering
{
/// <summary>
/// Default implementation of a DI service that provides convenient access to
/// server-side prerendering APIs. This is an alternative to prerendering via
/// the asp-prerender-module tag helper.
/// </summary>
internal class DefaultSpaPrerenderer : ISpaPrerenderer
{
private readonly string _applicationBasePath;
private readonly CancellationToken _applicationStoppingToken;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly INodeServices _nodeServices;
public DefaultSpaPrerenderer(
INodeServices nodeServices,
IApplicationLifetime applicationLifetime,
IHostingEnvironment hostingEnvironment,
IHttpContextAccessor httpContextAccessor)
{
_applicationBasePath = hostingEnvironment.ContentRootPath;
_applicationStoppingToken = applicationLifetime.ApplicationStopping;
_httpContextAccessor = httpContextAccessor;
_nodeServices = nodeServices;
}
public Task<RenderToStringResult> RenderToString(
string moduleName,
string exportName = null,
object customDataParameter = null,
int timeoutMilliseconds = default(int))
{
return Prerenderer.RenderToString(
_applicationBasePath,
_nodeServices,
_applicationStoppingToken,
new JavaScriptModuleExport(moduleName) { ExportName = exportName },
_httpContextAccessor.HttpContext,
customDataParameter,
timeoutMilliseconds);
}
}
}

Просмотреть файл

@ -0,0 +1,28 @@
using System.Threading.Tasks;
namespace Microsoft.AspNetCore.SpaServices.Prerendering
{
/// <summary>
/// Represents a service that can perform server-side prerendering for
/// JavaScript-based Single Page Applications. This is an alternative
/// to using the 'asp-prerender-module' tag helper.
/// </summary>
public interface ISpaPrerenderer
{
/// <summary>
/// Invokes JavaScript code to perform server-side prerendering for a
/// Single-Page Application. This is an alternative to using the
/// 'asp-prerender-module' tag helper.
/// </summary>
/// <param name="moduleName">The JavaScript module that exports a prerendering function.</param>
/// <param name="exportName">The name of the export from the JavaScript module, if it is not the default export.</param>
/// <param name="customDataParameter">An optional JSON-serializable object to pass to the JavaScript prerendering function.</param>
/// <param name="timeoutMilliseconds">If specified, the prerendering task will time out after this duration if not already completed.</param>
/// <returns></returns>
Task<RenderToStringResult> RenderToString(
string moduleName,
string exportName = null,
object customDataParameter = null,
int timeoutMilliseconds = default(int));
}
}

Просмотреть файл

@ -1,14 +1,11 @@
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.NodeServices;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Newtonsoft.Json;
namespace Microsoft.AspNetCore.SpaServices.Prerendering
{
@ -90,19 +87,6 @@ namespace Microsoft.AspNetCore.SpaServices.Prerendering
/// <returns>A <see cref="Task"/> representing the operation.</returns>
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
// We want to pass the original, unencoded incoming URL data through to Node, so that
// server-side code has the same view of the URL as client-side code (on the client,
// location.pathname returns an unencoded string).
// The following logic handles special characters in URL paths in the same way that
// Node and client-side JS does. For example, the path "/a=b%20c" gets passed through
// unchanged (whereas other .NET APIs do change it - Path.Value will return it as
// "/a=b c" and Path.ToString() will return it as "/a%3db%20c")
var requestFeature = ViewContext.HttpContext.Features.Get<IHttpRequestFeature>();
var unencodedPathAndQuery = requestFeature.RawTarget;
var request = ViewContext.HttpContext.Request;
var unencodedAbsoluteUrl = $"{request.Scheme}://{request.Host}{unencodedPathAndQuery}";
var result = await Prerenderer.RenderToString(
_applicationBasePath,
_nodeServices,
@ -111,11 +95,9 @@ namespace Microsoft.AspNetCore.SpaServices.Prerendering
{
ExportName = ExportName
},
unencodedAbsoluteUrl,
unencodedPathAndQuery,
ViewContext.HttpContext,
CustomDataParameter,
TimeoutMillisecondsParameter,
request.PathBase.ToString());
TimeoutMillisecondsParameter);
if (!string.IsNullOrEmpty(result.RedirectUrl))
{
@ -134,19 +116,10 @@ namespace Microsoft.AspNetCore.SpaServices.Prerendering
// Also attach any specified globals to the 'window' object. This is useful for transferring
// general state between server and client.
if (result.Globals != null)
var globalsScript = result.CreateGlobalsAssignmentScript();
if (!string.IsNullOrEmpty(globalsScript))
{
var stringBuilder = new StringBuilder();
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}</script>");
}
output.PostElement.SetHtmlContent($"<script>{globalsScript}</script>");
}
}
}

Просмотреть файл

@ -2,6 +2,8 @@ using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.NodeServices;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
namespace Microsoft.AspNetCore.SpaServices.Prerendering
{
@ -14,6 +16,40 @@ namespace Microsoft.AspNetCore.SpaServices.Prerendering
private static StringAsTempFile NodeScript;
internal static Task<RenderToStringResult> RenderToString(
string applicationBasePath,
INodeServices nodeServices,
CancellationToken applicationStoppingToken,
JavaScriptModuleExport bootModule,
HttpContext httpContext,
object customDataParameter,
int timeoutMilliseconds)
{
// We want to pass the original, unencoded incoming URL data through to Node, so that
// server-side code has the same view of the URL as client-side code (on the client,
// location.pathname returns an unencoded string).
// The following logic handles special characters in URL paths in the same way that
// Node and client-side JS does. For example, the path "/a=b%20c" gets passed through
// unchanged (whereas other .NET APIs do change it - Path.Value will return it as
// "/a=b c" and Path.ToString() will return it as "/a%3db%20c")
var requestFeature = httpContext.Features.Get<IHttpRequestFeature>();
var unencodedPathAndQuery = requestFeature.RawTarget;
var request = httpContext.Request;
var unencodedAbsoluteUrl = $"{request.Scheme}://{request.Host}{unencodedPathAndQuery}";
return RenderToString(
applicationBasePath,
nodeServices,
applicationStoppingToken,
bootModule,
unencodedAbsoluteUrl,
unencodedPathAndQuery,
customDataParameter,
timeoutMilliseconds,
request.PathBase.ToString());
}
/// <summary>
/// Performs server-side prerendering by invoking code in Node.js.
/// </summary>

Просмотреть файл

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.NodeServices;
using Microsoft.AspNetCore.SpaServices.Prerendering;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// Extension methods for setting up prerendering features in an <see cref="IServiceCollection" />.
/// </summary>
public static class PrerenderingServiceCollectionExtensions
{
/// <summary>
/// Configures the dependency injection system to supply an implementation
/// of <see cref="ISpaPrerenderer"/>.
/// </summary>
/// <param name="serviceCollection">The <see cref="IServiceCollection"/>.</param>
public static void AddSpaPrerenderer(this IServiceCollection serviceCollection)
{
serviceCollection.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
serviceCollection.AddSingleton<ISpaPrerenderer, DefaultSpaPrerenderer>();
}
}
}

Просмотреть файл

@ -1,4 +1,6 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text;
namespace Microsoft.AspNetCore.SpaServices.Prerendering
{
@ -30,5 +32,29 @@ namespace Microsoft.AspNetCore.SpaServices.Prerendering
/// If set, specifies the HTTP status code that should be sent back with the server response.
/// </summary>
public int? StatusCode { get; set; }
/// <summary>
/// Constructs a block of JavaScript code that assigns data from the
/// <see cref="Globals"/> property to the global namespace.
/// </summary>
/// <returns>A block of JavaScript code.</returns>
public string CreateGlobalsAssignmentScript()
{
if (Globals == null)
{
return string.Empty;
}
var stringBuilder = new StringBuilder();
foreach (var property in Globals.Properties())
{
stringBuilder.AppendFormat("window.{0} = {1};",
property.Name,
property.Value.ToString(Formatting.None));
}
return stringBuilder.ToString();
}
}
}