Make prerenderer code not rely on a specific process.cwd()

This commit is contained in:
SteveSandersonMS
2016-03-01 15:28:06 +00:00
parent 4dcf63dab4
commit 444475ef16
3 changed files with 14 additions and 10 deletions

View File

@@ -69,19 +69,19 @@ var domain = require('domain');
var domainTask = require('domain-task'); var domainTask = require('domain-task');
var baseUrl = require('domain-task/fetch').baseUrl; var baseUrl = require('domain-task/fetch').baseUrl;
function findBootModule(bootModule, callback) { function findBootModule(applicationBasePath, bootModule, callback) {
var bootModuleNameFullPath = path.resolve(process.cwd(), bootModule.moduleName); var bootModuleNameFullPath = path.resolve(applicationBasePath, bootModule.moduleName);
if (bootModule.webpackConfig) { if (bootModule.webpackConfig) {
var webpackConfigFullPath = path.resolve(process.cwd(), bootModule.webpackConfig); var webpackConfigFullPath = path.resolve(applicationBasePath, bootModule.webpackConfig);
loadViaWebpack(webpackConfigFullPath, bootModuleNameFullPath, callback); loadViaWebpack(webpackConfigFullPath, bootModuleNameFullPath, callback);
} else { } else {
callback(null, require(bootModuleNameFullPath)); callback(null, require(bootModuleNameFullPath));
} }
} }
function findBootFunc(bootModule, callback) { function findBootFunc(applicationBasePath, bootModule, callback) {
// First try to load the module (possibly via Webpack) // First try to load the module (possibly via Webpack)
findBootModule(bootModule, function(findBootModuleError, foundBootModule) { findBootModule(applicationBasePath, bootModule, function(findBootModuleError, foundBootModule) {
if (findBootModuleError) { if (findBootModuleError) {
callback(findBootModuleError); callback(findBootModuleError);
return; return;
@@ -113,8 +113,8 @@ function findBootFunc(bootModule, callback) {
}); });
} }
function renderToString(callback, bootModule, absoluteRequestUrl, requestPathAndQuery) { function renderToString(callback, applicationBasePath, bootModule, absoluteRequestUrl, requestPathAndQuery) {
findBootFunc(bootModule, function (findBootFuncError, bootFunc) { findBootFunc(applicationBasePath, bootModule, function (findBootFuncError, bootFunc) {
if (findBootFuncError) { if (findBootFuncError) {
callback(findBootFuncError); callback(findBootFuncError);
return; return;

View File

@@ -28,21 +28,23 @@ namespace Microsoft.AspNet.SpaServices.Prerendering
[HtmlAttributeName(PrerenderWebpackConfigAttributeName)] [HtmlAttributeName(PrerenderWebpackConfigAttributeName)]
public string WebpackConfigPath { get; set; } public string WebpackConfigPath { get; set; }
private string applicationBasePath;
private IHttpContextAccessor contextAccessor; private IHttpContextAccessor contextAccessor;
private INodeServices nodeServices; private INodeServices nodeServices;
public PrerenderTagHelper(IServiceProvider serviceProvider, IHttpContextAccessor contextAccessor) public PrerenderTagHelper(IServiceProvider serviceProvider, IHttpContextAccessor contextAccessor)
{ {
var appEnv = (IApplicationEnvironment)serviceProvider.GetService(typeof(IApplicationEnvironment));
this.contextAccessor = contextAccessor; this.contextAccessor = contextAccessor;
this.nodeServices = (INodeServices)serviceProvider.GetService(typeof (INodeServices)) ?? fallbackNodeServices; this.nodeServices = (INodeServices)serviceProvider.GetService(typeof (INodeServices)) ?? fallbackNodeServices;
this.applicationBasePath = appEnv.ApplicationBasePath;
// Consider removing the following. Having it means you can get away with not putting app.AddNodeServices() // 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. // in your startup file, but then again it might be confusing that you don't need to.
if (this.nodeServices == null) { if (this.nodeServices == null) {
var appEnv = (IApplicationEnvironment)serviceProvider.GetService(typeof(IApplicationEnvironment));
this.nodeServices = fallbackNodeServices = Configuration.CreateNodeServices(new NodeServicesOptions { this.nodeServices = fallbackNodeServices = Configuration.CreateNodeServices(new NodeServicesOptions {
HostingModel = NodeHostingModel.Http, HostingModel = NodeHostingModel.Http,
ProjectPath = appEnv.ApplicationBasePath ProjectPath = this.applicationBasePath
}); });
} }
} }
@@ -51,6 +53,7 @@ namespace Microsoft.AspNet.SpaServices.Prerendering
{ {
var request = this.contextAccessor.HttpContext.Request; var request = this.contextAccessor.HttpContext.Request;
var result = await Prerenderer.RenderToString( var result = await Prerenderer.RenderToString(
applicationBasePath: this.applicationBasePath,
nodeServices: this.nodeServices, nodeServices: this.nodeServices,
bootModule: new JavaScriptModuleExport(this.ModuleName) { bootModule: new JavaScriptModuleExport(this.ModuleName) {
exportName = this.ExportName, exportName = this.ExportName,

View File

@@ -16,8 +16,9 @@ namespace Microsoft.AspNet.SpaServices.Prerendering
}); });
} }
public static async Task<RenderToStringResult> RenderToString(INodeServices nodeServices, JavaScriptModuleExport bootModule, string requestAbsoluteUrl, string requestPathAndQuery) { 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", return await nodeServices.InvokeExport<RenderToStringResult>(nodeScript.Value.FileName, "renderToString",
applicationBasePath,
bootModule, bootModule,
requestAbsoluteUrl, requestAbsoluteUrl,
requestPathAndQuery); requestPathAndQuery);