1
1
/// <reference path="../npm/aspnet-prerendering/src/PrerenderingInterfaces.d.ts" />
2
2
import * as url from 'url' ;
3
3
import * as path from 'path' ;
4
+ import * as fs from 'fs' ;
4
5
declare var __non_webpack_require__ ;
5
6
6
7
// Separate declaration and export just to add type checking on function signature
@@ -22,7 +23,8 @@ export const renderToString: RenderToStringFunc = renderToStringImpl;
22
23
// a certain flag is attached to the function instance.
23
24
function renderToStringImpl ( callback : RenderToStringCallback , applicationBasePath : string , bootModule : BootModuleInfo , absoluteRequestUrl : string , requestPathAndQuery : string , customDataParameter : any , overrideTimeoutMilliseconds : number ) {
24
25
try {
25
- let renderToStringFunc = findRenderToStringFunc ( applicationBasePath , bootModule ) ;
26
+ const forceLegacy = isLegacyAspNetPrerendering ( ) ;
27
+ const renderToStringFunc = ! forceLegacy && findRenderToStringFunc ( applicationBasePath , bootModule ) ;
26
28
const isNotLegacyMode = renderToStringFunc && renderToStringFunc [ 'isServerRenderer' ] ;
27
29
28
30
if ( isNotLegacyMode ) {
@@ -32,9 +34,9 @@ function renderToStringImpl(callback: RenderToStringCallback, applicationBasePat
32
34
renderToStringFunc . apply ( null , arguments ) ;
33
35
} else {
34
36
// 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 ) ;
38
40
} else {
39
41
callback ( 'If you use aspnet-prerendering >= 2.0.0, you must update your server-side boot module to call createServerRenderer. '
40
42
+ 'Either update your boot module code, or revert to aspnet-prerendering version 1.x' ) ;
@@ -92,3 +94,22 @@ function findRenderToStringFunc(applicationBasePath: string, bootModule: BootMod
92
94
93
95
return renderToStringFunc ;
94
96
}
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