using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.NodeServices;
namespace Microsoft.AspNetCore.SpaServices.Prerendering
{
///
/// Performs server-side prerendering by invoking code in Node.js.
///
public static class Prerenderer
{
private static readonly Lazy NodeScript;
static Prerenderer()
{
NodeScript = new Lazy(() =>
{
var script = EmbeddedResourceReader.Read(typeof(Prerenderer), "/Content/Node/prerenderer.js");
return new StringAsTempFile(script); // Will be cleaned up on process exit
});
}
///
/// Performs server-side prerendering by invoking code in Node.js.
///
/// The root path to your application. This is used when resolving project-relative paths.
/// The instance of that will be used to invoke JavaScript code.
/// The path to the JavaScript file containing the prerendering logic.
/// The URL of the currently-executing HTTP request. This is supplied to the prerendering code.
/// The path and query part of the URL of the currently-executing HTTP request. This is supplied to the prerendering code.
/// An optional JSON-serializable parameter to be supplied to the prerendering code.
/// The maximum duration to wait for prerendering to complete.
///
public static Task RenderToString(
string applicationBasePath,
INodeServices nodeServices,
JavaScriptModuleExport bootModule,
string requestAbsoluteUrl,
string requestPathAndQuery,
object customDataParameter,
int timeoutMilliseconds)
{
return nodeServices.InvokeExportAsync(
NodeScript.Value.FileName,
"renderToString",
applicationBasePath,
bootModule,
requestAbsoluteUrl,
requestPathAndQuery,
customDataParameter,
timeoutMilliseconds);
}
}
}