Skip to content

Commit 444475e

Browse files
Make prerenderer code not rely on a specific process.cwd()
1 parent 4dcf63d commit 444475e

3 files changed

Lines changed: 14 additions & 10 deletions

File tree

src/Microsoft.AspNet.SpaServices/Content/Node/prerenderer.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,19 @@ var domain = require('domain');
6969
var domainTask = require('domain-task');
7070
var baseUrl = require('domain-task/fetch').baseUrl;
7171

72-
function findBootModule(bootModule, callback) {
73-
var bootModuleNameFullPath = path.resolve(process.cwd(), bootModule.moduleName);
72+
function findBootModule(applicationBasePath, bootModule, callback) {
73+
var bootModuleNameFullPath = path.resolve(applicationBasePath, bootModule.moduleName);
7474
if (bootModule.webpackConfig) {
75-
var webpackConfigFullPath = path.resolve(process.cwd(), bootModule.webpackConfig);
75+
var webpackConfigFullPath = path.resolve(applicationBasePath, bootModule.webpackConfig);
7676
loadViaWebpack(webpackConfigFullPath, bootModuleNameFullPath, callback);
7777
} else {
7878
callback(null, require(bootModuleNameFullPath));
7979
}
8080
}
8181

82-
function findBootFunc(bootModule, callback) {
82+
function findBootFunc(applicationBasePath, bootModule, callback) {
8383
// First try to load the module (possibly via Webpack)
84-
findBootModule(bootModule, function(findBootModuleError, foundBootModule) {
84+
findBootModule(applicationBasePath, bootModule, function(findBootModuleError, foundBootModule) {
8585
if (findBootModuleError) {
8686
callback(findBootModuleError);
8787
return;
@@ -113,8 +113,8 @@ function findBootFunc(bootModule, callback) {
113113
});
114114
}
115115

116-
function renderToString(callback, bootModule, absoluteRequestUrl, requestPathAndQuery) {
117-
findBootFunc(bootModule, function (findBootFuncError, bootFunc) {
116+
function renderToString(callback, applicationBasePath, bootModule, absoluteRequestUrl, requestPathAndQuery) {
117+
findBootFunc(applicationBasePath, bootModule, function (findBootFuncError, bootFunc) {
118118
if (findBootFuncError) {
119119
callback(findBootFuncError);
120120
return;

src/Microsoft.AspNet.SpaServices/Prerendering/PrerenderTagHelper.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,23 @@ public class PrerenderTagHelper : TagHelper
2828
[HtmlAttributeName(PrerenderWebpackConfigAttributeName)]
2929
public string WebpackConfigPath { get; set; }
3030

31+
private string applicationBasePath;
3132
private IHttpContextAccessor contextAccessor;
3233
private INodeServices nodeServices;
3334

3435
public PrerenderTagHelper(IServiceProvider serviceProvider, IHttpContextAccessor contextAccessor)
3536
{
37+
var appEnv = (IApplicationEnvironment)serviceProvider.GetService(typeof(IApplicationEnvironment));
3638
this.contextAccessor = contextAccessor;
3739
this.nodeServices = (INodeServices)serviceProvider.GetService(typeof (INodeServices)) ?? fallbackNodeServices;
40+
this.applicationBasePath = appEnv.ApplicationBasePath;
3841

3942
// Consider removing the following. Having it means you can get away with not putting app.AddNodeServices()
4043
// in your startup file, but then again it might be confusing that you don't need to.
4144
if (this.nodeServices == null) {
42-
var appEnv = (IApplicationEnvironment)serviceProvider.GetService(typeof(IApplicationEnvironment));
4345
this.nodeServices = fallbackNodeServices = Configuration.CreateNodeServices(new NodeServicesOptions {
4446
HostingModel = NodeHostingModel.Http,
45-
ProjectPath = appEnv.ApplicationBasePath
47+
ProjectPath = this.applicationBasePath
4648
});
4749
}
4850
}
@@ -51,6 +53,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
5153
{
5254
var request = this.contextAccessor.HttpContext.Request;
5355
var result = await Prerenderer.RenderToString(
56+
applicationBasePath: this.applicationBasePath,
5457
nodeServices: this.nodeServices,
5558
bootModule: new JavaScriptModuleExport(this.ModuleName) {
5659
exportName = this.ExportName,

src/Microsoft.AspNet.SpaServices/Prerendering/Prerenderer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ static Prerenderer() {
1616
});
1717
}
1818

19-
public static async Task<RenderToStringResult> RenderToString(INodeServices nodeServices, JavaScriptModuleExport bootModule, string requestAbsoluteUrl, string requestPathAndQuery) {
19+
public static async Task<RenderToStringResult> RenderToString(string applicationBasePath, INodeServices nodeServices, JavaScriptModuleExport bootModule, string requestAbsoluteUrl, string requestPathAndQuery) {
2020
return await nodeServices.InvokeExport<RenderToStringResult>(nodeScript.Value.FileName, "renderToString",
21+
applicationBasePath,
2122
bootModule,
2223
requestAbsoluteUrl,
2324
requestPathAndQuery);

0 commit comments

Comments
 (0)