Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: preload polyfill fetch cache #149

Merged
merged 1 commit into from
Aug 17, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 19 additions & 21 deletions src/es-module-shims.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,22 @@ const jsonContentType = /^application\/json(;|$)/;
const cssContentType = /^text\/css(;|$)/;
const wasmContentType = /^application\/wasm(;|$)/;

const fetchOptsMap = new Map();
async function doFetch (url, fetchOpts) {
const res = await fetchHook(url, fetchOpts);
if (!res.ok)
throw new Error(`${res.status} ${res.statusText} ${res.url}`);
const contentType = res.headers.get('content-type');
if (jsContentType.test(contentType))
return { r: res.url, s: await res.text() };
else if (jsonContentType.test(contentType))
return { r: res.url, s: `export default ${await res.text()}` };
else if (cssContentType.test(contentType))
throw new Error('CSS modules not yet supported');
else if (wasmContentType.test(contentType))
throw new Error('WASM modules not yet supported');
else
throw new Error(`Unknown Content-Type "${contentType}"`);
}

function getOrCreateLoad (url, fetchOpts, source) {
let load = registry[url];
Expand Down Expand Up @@ -252,21 +267,7 @@ function getOrCreateLoad (url, fetchOpts, source) {
load.f = (async () => {
if (!source) {
// preload fetch options override fetch options (race)
const res = await fetchHook(url, fetchOptsMap.get(url) || fetchOpts);
if (!res.ok)
throw new Error(`${res.status} ${res.statusText} ${res.url}`);
load.r = res.url;
const contentType = res.headers.get('content-type');
if (jsContentType.test(contentType))
source = await res.text();
else if (jsonContentType.test(contentType))
source = `export default ${await res.text()}`;
else if (cssContentType.test(contentType))
throw new Error('CSS modules not yet supported');
else if (wasmContentType.test(contentType))
throw new Error('WASM modules not yet supported');
else
throw new Error(`Unknown Content-Type "${contentType}"`);
({ r: load.r, s: source } = await (fetchCache[url] || doFetch(url, fetchOpts)));
}
try {
load.a = parse(source, load.u);
Expand Down Expand Up @@ -382,15 +383,12 @@ async function processScript (script, dynamic) {
}
}

const fetchCache = {};
function processPreload (link) {
if (link.ep) // ep marker = processed
return;
link.ep = true;
// prepopulate the load record
const fetchOpts = getFetchOpts(link);
// save preloaded fetch options for later load
fetchOptsMap.set(link.href, fetchOpts);
fetch(link.href, fetchOpts);
fetchCache[link.href] = doFetch(link.href, getFetchOpts(link));
}

async function defaultResolve (id, parentUrl) {
Expand Down