This repository has been archived by the owner on Apr 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 521
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example of full-page prerendering via a custom action result
- Loading branch information
1 parent
edf1f88
commit 749c7cb
Showing
8 changed files
with
115 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Http.Extensions; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.NodeServices; | ||
using Microsoft.AspNetCore.SpaServices.Prerendering; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Webpack.ActionResults | ||
{ | ||
// This is an example of how you could invoke the prerendering API from an ActionResult, so as to | ||
// prerender a SPA component as the entire response page (instead of injecting the SPA component | ||
// into a Razor view's output) | ||
public class PrerenderResult : ActionResult | ||
{ | ||
private JavaScriptModuleExport _moduleExport; | ||
private object _dataToSupply; | ||
|
||
public PrerenderResult(JavaScriptModuleExport moduleExport, object dataToSupply = null) | ||
{ | ||
_moduleExport = moduleExport; | ||
_dataToSupply = dataToSupply; | ||
} | ||
|
||
public override async Task ExecuteResultAsync(ActionContext context) | ||
{ | ||
var nodeServices = context.HttpContext.RequestServices.GetRequiredService<INodeServices>(); | ||
var hostEnv = context.HttpContext.RequestServices.GetRequiredService<IHostingEnvironment>(); | ||
var applicationBasePath = hostEnv.ContentRootPath; | ||
var request = context.HttpContext.Request; | ||
var response = context.HttpContext.Response; | ||
|
||
var prerenderedHtml = await Prerenderer.RenderToString( | ||
applicationBasePath, | ||
nodeServices, | ||
_moduleExport, | ||
request.GetEncodedUrl(), | ||
request.Path + request.QueryString.Value, | ||
_dataToSupply | ||
); | ||
|
||
response.ContentType = "text/html"; | ||
await response.WriteAsync(prerenderedHtml.Html); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
samples/misc/Webpack/ActionResults/PrerenderResultExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.SpaServices.Prerendering; | ||
|
||
namespace Webpack.ActionResults | ||
{ | ||
public static class PrerenderResultExtensions | ||
{ | ||
public static PrerenderResult Prerender(this ControllerBase controller, JavaScriptModuleExport exportToPrerender, object dataToSupply = null) | ||
{ | ||
return new PrerenderResult(exportToPrerender, dataToSupply); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export default function (params: any): Promise<{ html: string, globals?: any }> { | ||
return new Promise((resolve, reject) => { | ||
|
||
// Here, you could put any logic that synchronously or asynchronously prerenders | ||
// your SPA components. For example, see the boot-server.ts files in the Angular2Spa | ||
// and ReactReduxSpa templates for ways to prerender Angular 2 and React components. | ||
// | ||
// If you wanted, you could use a property on the 'params.data' object to specify | ||
// which SPA component or template to render. | ||
|
||
const html = ` | ||
<h1>Hello</h1> | ||
It works! You passed <b>${ JSON.stringify(params.data) }</b> | ||
and are currently requesting <b>${ params.location.path }</b>`; | ||
resolve({ html }); | ||
}); | ||
}; |
25 changes: 25 additions & 0 deletions
25
samples/misc/Webpack/Controllers/FullPagePrerenderingController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.SpaServices.Prerendering; | ||
using Webpack.ActionResults; | ||
|
||
namespace Webpack.Controllers | ||
{ | ||
// This sample shows how you could invoke the prerendering APIs directly from an MVC | ||
// action result. | ||
public class FullPagePrerenderingController : Controller | ||
{ | ||
private static JavaScriptModuleExport BootModule = new JavaScriptModuleExport("Clientside/PrerenderingSample") | ||
{ | ||
// Because the boot module is written in TypeScript, we need to specify a webpack | ||
// config so it can be built. If it was written in JavaScript, this would not be needed. | ||
WebpackConfig = "webpack.config.js" | ||
}; | ||
|
||
public IActionResult Index() | ||
{ | ||
var dataToSupply = new { nowTime = DateTime.Now.Ticks }; | ||
return this.Prerender(BootModule, dataToSupply); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters