Add simpler prerendering API. Fixes #607

This commit is contained in:
Steve Sanderson
2017-07-03 14:07:28 +01:00
parent 9cce26ebd8
commit 94fc84a9b4
13 changed files with 225 additions and 33 deletions

View File

@@ -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");

View File

@@ -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 } }
});
});
});

View File

@@ -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>

View File

@@ -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.

View File

@@ -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>

View File

@@ -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>

View File

@@ -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"