/
prerender-global-patch.ts
39 lines (34 loc) · 1.31 KB
/
prerender-global-patch.ts
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
import { join } from 'path';
import { requireFunc } from '@utils';
export const patchNodeGlobal = (nodeGlobal: any, devServerHostUrl: string) => {
if (typeof nodeGlobal.fetch !== 'function') {
// webpack work-around/hack
const nodeFetch = requireFunc(join(__dirname, '..', 'sys', 'node', 'node-fetch.js'));
nodeGlobal.fetch = (input: any, init: any) => {
if (typeof input === 'string') {
// fetch(url) w/ url string
const urlStr = normalizeUrl(input, devServerHostUrl);
return nodeFetch.fetch(urlStr, init);
} else {
// fetch(Request) w/ request object
input.url = normalizeUrl(input.url, devServerHostUrl);
return nodeFetch.fetch(input, init);
}
};
nodeGlobal.Headers = nodeFetch.Headers;
nodeGlobal.Request = nodeFetch.Request;
nodeGlobal.Response = nodeFetch.Response;
nodeGlobal.FetchError = nodeFetch.FetchError;
}
};
const normalizeUrl = (inputUrl: string, devServerHostUrl: string) => {
const requestUrl = new URL(inputUrl, devServerHostUrl);
return requestUrl.href;
};
export const patchWindowGlobal = (nodeGlobal: any, win: any) => {
win.fetch = nodeGlobal.fetch;
win.Headers = nodeGlobal.Headers;
win.Request = nodeGlobal.Request;
win.Response = nodeGlobal.Response;
win.FetchError = nodeGlobal.FetchError;
};