diff --git a/src/components/include/request.ts b/src/components/include/request.ts index efcb818c..8985b6a9 100644 --- a/src/components/include/request.ts +++ b/src/components/include/request.ts @@ -4,20 +4,26 @@ interface IncludeFile { html: string; } -const includeFiles = new Map>(); +const includeFiles = new Map>(); /** Fetches an include file from a remote source. Caching is enabled so the origin is only pinged once. */ export function requestInclude(src: string, mode: 'cors' | 'no-cors' | 'same-origin' = 'cors'): Promise { - if (includeFiles.has(src)) { - return includeFiles.get(src)!; + const prev = includeFiles.get(src); + if (prev !== undefined) { + // Promise.resolve() transparently unboxes prev if it was a promise. + return Promise.resolve(prev); } const fileDataPromise = fetch(src, { mode: mode }).then(async response => { - return { + const res = { ok: response.ok, status: response.status, html: await response.text() }; + // Replace the cached promise with its result to avoid having buggy browser Promises retain memory as mentioned in #1284 and #1249 + includeFiles.set(src, res); + return res; }); + // Cache the promise to only fetch() once per src includeFiles.set(src, fileDataPromise); return fileDataPromise; }