Repository navigation
Expand file tree
/
Copy pathPrerenderer.ts
More file actions
115 lines (105 loc) · 5.89 KB
/
Copy pathPrerenderer.ts
File metadata and controls
115 lines (105 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/// <reference path="../npm/aspnet-prerendering/src/PrerenderingInterfaces.d.ts" />
import * as url from 'url';
import * as path from 'path';
import * as fs from 'fs';
declare var __non_webpack_require__;
// Separate declaration and export just to add type checking on function signature
export const renderToString: RenderToStringFunc = renderToStringImpl;
// This function is invoked by .NET code (via NodeServices). Its job is to hand off execution to the application's
// prerendering boot function. It can operate in two modes:
// [1] Legacy mode
// This is for backward compatibility with projects created with templates older than the generator version 0.6.0.
// In this mode, we don't really do anything here - we just load the 'aspnet-prerendering' NPM module (which must
// exist in node_modules, and must be v1.x (not v2+)), and pass through all the parameters to it. Code in
// 'aspnet-prerendering' v1.x will locate the boot function and invoke it.
// The drawback to this mode is that, for it to work, you have to deploy node_modules to production.
// [2] Current mode
// This is for projects created with the Yeoman generator 0.6.0+ (or projects manually updated). In this mode,
// we don't invoke 'require' at runtime at all. All our dependencies are bundled into the NuGet package, so you
// don't have to deploy node_modules to production.
// To determine whether we're in mode [1] or [2], the code locates your prerendering boot function, and checks whether
// a certain flag is attached to the function instance.
function renderToStringImpl(callback: RenderToStringCallback, applicationBasePath: string, bootModule: BootModuleInfo, absoluteRequestUrl: string, requestPathAndQuery: string, customDataParameter: any, overrideTimeoutMilliseconds: number) {
try {
const forceLegacy = isLegacyAspNetPrerendering();
const renderToStringFunc = !forceLegacy && findRenderToStringFunc(applicationBasePath, bootModule);
const isNotLegacyMode = renderToStringFunc && renderToStringFunc['isServerRenderer'];
if (isNotLegacyMode) {
// Current (non-legacy) mode - we invoke the exported function directly (instead of going through aspnet-prerendering)
// It's type-safe to just apply the incoming args to this function, because we already type-checked that it's a RenderToStringFunc,
// just like renderToStringImpl itself is.
renderToStringFunc.apply(null, arguments);
} else {
// Legacy mode - just hand off execution to 'aspnet-prerendering' v1.x, which must exist in node_modules at runtime
const aspNetPrerenderingV1RenderToString = require('aspnet-prerendering').renderToString;
if (aspNetPrerenderingV1RenderToString) {
aspNetPrerenderingV1RenderToString(callback, applicationBasePath, bootModule, absoluteRequestUrl, requestPathAndQuery, customDataParameter, overrideTimeoutMilliseconds);
} else {
callback('If you use aspnet-prerendering >= 2.0.0, you must update your server-side boot module to call createServerRenderer. '
+ 'Either update your boot module code, or revert to aspnet-prerendering version 1.x');
}
}
} catch (ex) {
// Make sure loading errors are reported back to the .NET part of the app
callback(
'Prerendering failed because of error: '
+ ex.stack
+ '\nCurrent directory is: '
+ process.cwd()
);
}
};
function findBootModule(applicationBasePath: string, bootModule: BootModuleInfo): any {
const bootModuleNameFullPath = path.resolve(applicationBasePath, bootModule.moduleName);
if (bootModule.webpackConfig) {
// If you're using asp-prerender-webpack-config, you're definitely in legacy mode
return null;
} else {
return __non_webpack_require__(bootModuleNameFullPath);
}
}
function findRenderToStringFunc(applicationBasePath: string, bootModule: BootModuleInfo): RenderToStringFunc {
// First try to load the module
const foundBootModule = findBootModule(applicationBasePath, bootModule);
if (foundBootModule === null) {
return null; // Must be legacy mode
}
// Now try to pick out the function they want us to invoke
let renderToStringFunc: RenderToStringFunc;
if (bootModule.exportName) {
// Explicitly-named export
renderToStringFunc = foundBootModule[bootModule.exportName];
} else if (typeof foundBootModule !== 'function') {
// TypeScript-style default export
renderToStringFunc = foundBootModule.default;
} else {
// Native default export
renderToStringFunc = foundBootModule;
}
// Validate the result
if (typeof renderToStringFunc !== 'function') {
if (bootModule.exportName) {
throw new Error(`The module at ${ bootModule.moduleName } has no function export named ${ bootModule.exportName }.`);
} else {
throw new Error(`The module at ${ bootModule.moduleName } does not export a default function, and you have not specified which export to invoke.`);
}
}
return renderToStringFunc;
}
function isLegacyAspNetPrerendering() {
const version = getAspNetPrerenderingPackageVersion();
return version && /^1\./.test(version);
}
function getAspNetPrerenderingPackageVersion() {
try {
const packageEntryPoint = __non_webpack_require__.resolve('aspnet-prerendering');
const packageDir = path.dirname(packageEntryPoint);
const packageJsonPath = path.join(packageDir, 'package.json');
const packageJson = __non_webpack_require__(packageJsonPath);
return packageJson.version.toString();
} catch(ex) {
// Implies aspnet-prerendering isn't in node_modules at all (or node_modules itself doesn't exist,
// which will be the case in production based on latest templates).
return null;
}
}