Skip to content
This repository was archived by the owner on Apr 8, 2020. It is now read-only.

Commit a8d3eed

Browse files
Detect legacy aspnet-prerendering mode earlier to fix #470
1 parent 5d14f11 commit a8d3eed

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

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

+23-4
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@
7171
// a certain flag is attached to the function instance.
7272
function renderToStringImpl(callback, applicationBasePath, bootModule, absoluteRequestUrl, requestPathAndQuery, customDataParameter, overrideTimeoutMilliseconds) {
7373
try {
74-
var renderToStringFunc = findRenderToStringFunc(applicationBasePath, bootModule);
74+
var forceLegacy = isLegacyAspNetPrerendering();
75+
var renderToStringFunc = !forceLegacy && findRenderToStringFunc(applicationBasePath, bootModule);
7576
var isNotLegacyMode = renderToStringFunc && renderToStringFunc['isServerRenderer'];
7677
if (isNotLegacyMode) {
7778
// Current (non-legacy) mode - we invoke the exported function directly (instead of going through aspnet-prerendering)
@@ -81,9 +82,9 @@
8182
}
8283
else {
8384
// Legacy mode - just hand off execution to 'aspnet-prerendering' v1.x, which must exist in node_modules at runtime
84-
renderToStringFunc = __webpack_require__(3).renderToString;
85-
if (renderToStringFunc) {
86-
renderToStringFunc(callback, applicationBasePath, bootModule, absoluteRequestUrl, requestPathAndQuery, customDataParameter, overrideTimeoutMilliseconds);
85+
var aspNetPrerenderingV1RenderToString = __webpack_require__(3).renderToString;
86+
if (aspNetPrerenderingV1RenderToString) {
87+
aspNetPrerenderingV1RenderToString(callback, applicationBasePath, bootModule, absoluteRequestUrl, requestPathAndQuery, customDataParameter, overrideTimeoutMilliseconds);
8788
}
8889
else {
8990
callback('If you use aspnet-prerendering >= 2.0.0, you must update your server-side boot module to call createServerRenderer. '
@@ -141,6 +142,24 @@
141142
}
142143
return renderToStringFunc;
143144
}
145+
function isLegacyAspNetPrerendering() {
146+
var version = getAspNetPrerenderingPackageVersion();
147+
return version && /^1\./.test(version);
148+
}
149+
function getAspNetPrerenderingPackageVersion() {
150+
try {
151+
var packageEntryPoint = require.resolve('aspnet-prerendering');
152+
var packageDir = path.dirname(packageEntryPoint);
153+
var packageJsonPath = path.join(packageDir, 'package.json');
154+
var packageJson = require(packageJsonPath);
155+
return packageJson.version.toString();
156+
}
157+
catch (ex) {
158+
// Implies aspnet-prerendering isn't in node_modules at all (or node_modules itself doesn't exist,
159+
// which will be the case in production based on latest templates).
160+
return null;
161+
}
162+
}
144163

145164

146165
/***/ },

src/Microsoft.AspNetCore.SpaServices/TypeScript/Prerenderer.ts

+25-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/// <reference path="../npm/aspnet-prerendering/src/PrerenderingInterfaces.d.ts" />
22
import * as url from 'url';
33
import * as path from 'path';
4+
import * as fs from 'fs';
45
declare var __non_webpack_require__;
56

67
// Separate declaration and export just to add type checking on function signature
@@ -22,7 +23,8 @@ export const renderToString: RenderToStringFunc = renderToStringImpl;
2223
// a certain flag is attached to the function instance.
2324
function renderToStringImpl(callback: RenderToStringCallback, applicationBasePath: string, bootModule: BootModuleInfo, absoluteRequestUrl: string, requestPathAndQuery: string, customDataParameter: any, overrideTimeoutMilliseconds: number) {
2425
try {
25-
let renderToStringFunc = findRenderToStringFunc(applicationBasePath, bootModule);
26+
const forceLegacy = isLegacyAspNetPrerendering();
27+
const renderToStringFunc = !forceLegacy && findRenderToStringFunc(applicationBasePath, bootModule);
2628
const isNotLegacyMode = renderToStringFunc && renderToStringFunc['isServerRenderer'];
2729

2830
if (isNotLegacyMode) {
@@ -32,9 +34,9 @@ function renderToStringImpl(callback: RenderToStringCallback, applicationBasePat
3234
renderToStringFunc.apply(null, arguments);
3335
} else {
3436
// Legacy mode - just hand off execution to 'aspnet-prerendering' v1.x, which must exist in node_modules at runtime
35-
renderToStringFunc = require('aspnet-prerendering').renderToString;
36-
if (renderToStringFunc) {
37-
renderToStringFunc(callback, applicationBasePath, bootModule, absoluteRequestUrl, requestPathAndQuery, customDataParameter, overrideTimeoutMilliseconds);
37+
const aspNetPrerenderingV1RenderToString = require('aspnet-prerendering').renderToString;
38+
if (aspNetPrerenderingV1RenderToString) {
39+
aspNetPrerenderingV1RenderToString(callback, applicationBasePath, bootModule, absoluteRequestUrl, requestPathAndQuery, customDataParameter, overrideTimeoutMilliseconds);
3840
} else {
3941
callback('If you use aspnet-prerendering >= 2.0.0, you must update your server-side boot module to call createServerRenderer. '
4042
+ 'Either update your boot module code, or revert to aspnet-prerendering version 1.x');
@@ -92,3 +94,22 @@ function findRenderToStringFunc(applicationBasePath: string, bootModule: BootMod
9294

9395
return renderToStringFunc;
9496
}
97+
98+
function isLegacyAspNetPrerendering() {
99+
const version = getAspNetPrerenderingPackageVersion();
100+
return version && /^1\./.test(version);
101+
}
102+
103+
function getAspNetPrerenderingPackageVersion() {
104+
try {
105+
const packageEntryPoint = __non_webpack_require__.resolve('aspnet-prerendering');
106+
const packageDir = path.dirname(packageEntryPoint);
107+
const packageJsonPath = path.join(packageDir, 'package.json');
108+
const packageJson = __non_webpack_require__(packageJsonPath);
109+
return packageJson.version.toString();
110+
} catch(ex) {
111+
// Implies aspnet-prerendering isn't in node_modules at all (or node_modules itself doesn't exist,
112+
// which will be the case in production based on latest templates).
113+
return null;
114+
}
115+
}

0 commit comments

Comments
 (0)