Skip to content

Commit

Permalink
fix(sys): node sys prerender applyPrerenderGlobalPatch
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Jul 28, 2020
1 parent 9875752 commit 517891d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 43 deletions.
39 changes: 0 additions & 39 deletions src/compiler/prerender/prerender-global-patch.ts

This file was deleted.

11 changes: 7 additions & 4 deletions src/compiler/prerender/prerender-worker.ts
@@ -1,10 +1,9 @@
import * as d from '../../declarations';
import { addModulePreloads, excludeStaticComponents, minifyScriptElements, minifyStyleElements, removeModulePreloads, removeStencilScripts } from './prerender-optimize';
import { catchError, isPromise, isRootPath, normalizePath, requireFunc } from '@utils';
import { catchError, isPromise, isRootPath, normalizePath, requireFunc, isFunction } from '@utils';
import { crawlAnchorsForNextUrls } from './crawl-urls';
import { getHydrateOptions } from './prerender-hydrate-options';
import { getPrerenderConfig } from './prerender-config';
import { patchNodeGlobal, patchWindowGlobal } from './prerender-global-patch';

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

// patch this new window
patchNodeGlobal(globalThis, prerenderRequest.devServerHostUrl);
patchWindowGlobal(globalThis, win);
if (isFunction(sys.applyPrerenderGlobalPatch)) {
sys.applyPrerenderGlobalPatch({
devServerHostUrl: prerenderRequest.devServerHostUrl,
window: win,
});
}

if (prerenderCtx.prerenderConfig == null) {
prerenderCtx.prerenderConfig = getPrerenderConfig(results.diagnostics, prerenderRequest.prerenderConfigPath);
Expand Down
1 change: 1 addition & 0 deletions src/declarations/stencil-public-compiler.ts
Expand Up @@ -823,6 +823,7 @@ export interface CompilerSystem {
*/
accessSync(p: string): boolean;
applyGlobalPatch?(fromDir: string): Promise<void>;
applyPrerenderGlobalPatch?(opts: { devServerHostUrl: string; window: any }): void;
cacheStorage?: CacheStorage;
checkVersion?: (logger: Logger, currentVersion: string) => Promise<() => void>;
copy?(copyTasks: Required<CopyTask>[], srcDir: string): Promise<CopyResults>;
Expand Down
28 changes: 28 additions & 0 deletions src/sys/node/node-sys.ts
Expand Up @@ -118,6 +118,34 @@ export function createNodeSysNoWatch(c: { process?: any } = {}) {
removeDestory(cb) {
destroys.delete(cb);
},
applyPrerenderGlobalPatch(opts) {
if (typeof global.fetch !== 'function') {
const nodeFetch = require(path.join(__dirname, 'node-fetch.js'));

global.fetch = (input: any, init: any) => {
if (typeof input === 'string') {
// fetch(url) w/ url string
const urlStr = new URL(input, opts.devServerHostUrl).href;
return nodeFetch.fetch(urlStr, init);
} else {
// fetch(Request) w/ request object
input.url = new URL(input.url, opts.devServerHostUrl).href;
return nodeFetch.fetch(input, init);
}
};

global.Headers = nodeFetch.Headers;
global.Request = nodeFetch.Request;
global.Response = nodeFetch.Response;
(global as any).FetchError = nodeFetch.FetchError;
}

opts.window.fetch = global.fetch;
opts.window.Headers = global.Headers;
opts.window.Request = global.Request;
opts.window.Response = global.Response;
opts.window.FetchError = (global as any).FetchError;
},
checkVersion,
copyFile(src, dst) {
return new Promise(resolve => {
Expand Down

0 comments on commit 517891d

Please sign in to comment.