Skip to content
Permalink
Browse files

fix(sys): node sys prerender applyPrerenderGlobalPatch

  • Loading branch information
adamdbradley committed Jul 28, 2020
1 parent 9875752 commit 517891d3aabcddf456321a52b9a7d477663da47e

This file was deleted.

@@ -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[]>,
@@ -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);
@@ -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>;
@@ -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 => {

0 comments on commit 517891d

Please sign in to comment.
You can’t perform that action at this time.