Skip to content

Commit 517891d

Browse files
committed
fix(sys): node sys prerender applyPrerenderGlobalPatch
1 parent 9875752 commit 517891d

4 files changed

Lines changed: 36 additions & 43 deletions

File tree

src/compiler/prerender/prerender-global-patch.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/compiler/prerender/prerender-worker.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import * as d from '../../declarations';
22
import { addModulePreloads, excludeStaticComponents, minifyScriptElements, minifyStyleElements, removeModulePreloads, removeStencilScripts } from './prerender-optimize';
3-
import { catchError, isPromise, isRootPath, normalizePath, requireFunc } from '@utils';
3+
import { catchError, isPromise, isRootPath, normalizePath, requireFunc, isFunction } from '@utils';
44
import { crawlAnchorsForNextUrls } from './crawl-urls';
55
import { getHydrateOptions } from './prerender-hydrate-options';
66
import { getPrerenderConfig } from './prerender-config';
7-
import { patchNodeGlobal, patchWindowGlobal } from './prerender-global-patch';
87

98
const prerenderCtx = {
109
componentGraph: null as Map<string, string[]>,
@@ -39,8 +38,12 @@ export const prerenderWorker = async (sys: d.CompilerSystem, prerenderRequest: d
3938
const doc = win.document;
4039

4140
// patch this new window
42-
patchNodeGlobal(globalThis, prerenderRequest.devServerHostUrl);
43-
patchWindowGlobal(globalThis, win);
41+
if (isFunction(sys.applyPrerenderGlobalPatch)) {
42+
sys.applyPrerenderGlobalPatch({
43+
devServerHostUrl: prerenderRequest.devServerHostUrl,
44+
window: win,
45+
});
46+
}
4447

4548
if (prerenderCtx.prerenderConfig == null) {
4649
prerenderCtx.prerenderConfig = getPrerenderConfig(results.diagnostics, prerenderRequest.prerenderConfigPath);

src/declarations/stencil-public-compiler.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,7 @@ export interface CompilerSystem {
823823
*/
824824
accessSync(p: string): boolean;
825825
applyGlobalPatch?(fromDir: string): Promise<void>;
826+
applyPrerenderGlobalPatch?(opts: { devServerHostUrl: string; window: any }): void;
826827
cacheStorage?: CacheStorage;
827828
checkVersion?: (logger: Logger, currentVersion: string) => Promise<() => void>;
828829
copy?(copyTasks: Required<CopyTask>[], srcDir: string): Promise<CopyResults>;

src/sys/node/node-sys.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,34 @@ export function createNodeSysNoWatch(c: { process?: any } = {}) {
118118
removeDestory(cb) {
119119
destroys.delete(cb);
120120
},
121+
applyPrerenderGlobalPatch(opts) {
122+
if (typeof global.fetch !== 'function') {
123+
const nodeFetch = require(path.join(__dirname, 'node-fetch.js'));
124+
125+
global.fetch = (input: any, init: any) => {
126+
if (typeof input === 'string') {
127+
// fetch(url) w/ url string
128+
const urlStr = new URL(input, opts.devServerHostUrl).href;
129+
return nodeFetch.fetch(urlStr, init);
130+
} else {
131+
// fetch(Request) w/ request object
132+
input.url = new URL(input.url, opts.devServerHostUrl).href;
133+
return nodeFetch.fetch(input, init);
134+
}
135+
};
136+
137+
global.Headers = nodeFetch.Headers;
138+
global.Request = nodeFetch.Request;
139+
global.Response = nodeFetch.Response;
140+
(global as any).FetchError = nodeFetch.FetchError;
141+
}
142+
143+
opts.window.fetch = global.fetch;
144+
opts.window.Headers = global.Headers;
145+
opts.window.Request = global.Request;
146+
opts.window.Response = global.Response;
147+
opts.window.FetchError = (global as any).FetchError;
148+
},
121149
checkVersion,
122150
copyFile(src, dst) {
123151
return new Promise(resolve => {

0 commit comments

Comments
 (0)