single-file-core 1.6.36 → 1.6.37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core/util.js +19 -14
- package/package.json +1 -1
package/core/util.js
CHANGED
|
@@ -82,6 +82,7 @@ const MANIFEST_OUTCOME_OVERSIZED = "oversized";
|
|
|
82
82
|
const MANIFEST_FETCHED_BY_PAGE = "page";
|
|
83
83
|
const MANIFEST_FETCHED_BY_FRAME = "frame";
|
|
84
84
|
const MANIFEST_HEADER_NAMES = ["date", "last-modified", "etag", "content-length", "content-encoding", "cache-control", "age", "expires", "server"];
|
|
85
|
+
const MIN_SERVER_ERROR_STATUS = 500;
|
|
85
86
|
|
|
86
87
|
const URL = globalThis.URL;
|
|
87
88
|
const DOMParser = globalThis.DOMParser;
|
|
@@ -95,7 +96,7 @@ export {
|
|
|
95
96
|
|
|
96
97
|
function getInstance(utilOptions) {
|
|
97
98
|
utilOptions = utilOptions || {};
|
|
98
|
-
utilOptions.fetch = utilOptions.fetch || ((url, options) => globalThis.fetch(url, { ...options, cache: "force-cache", referrerPolicy: "strict-origin-when-cross-origin" }));
|
|
99
|
+
utilOptions.fetch = utilOptions.fetch || ((url, options) => globalThis.fetch(url, { ...options, cache: options.cache || "force-cache", referrerPolicy: "strict-origin-when-cross-origin" }));
|
|
99
100
|
utilOptions.frameFetch = utilOptions.frameFetch || utilOptions.fetch;
|
|
100
101
|
return {
|
|
101
102
|
getDoctypeString,
|
|
@@ -287,28 +288,32 @@ function getInstance(utilOptions) {
|
|
|
287
288
|
resolveNetworkTimeoutPromise = resolve;
|
|
288
289
|
});
|
|
289
290
|
}
|
|
291
|
+
const fetchWithRetry = async (fetchFunction, fetchOptions) => {
|
|
292
|
+
let response = await Promise.race([fetchFunction(resourceURL, fetchOptions), networkTimeoutPromise]);
|
|
293
|
+
if (response.status >= MIN_SERVER_ERROR_STATUS) {
|
|
294
|
+
try {
|
|
295
|
+
response = await Promise.race([fetchFunction(resourceURL, { ...fetchOptions, cache: "reload" }), networkTimeoutPromise]);
|
|
296
|
+
// eslint-disable-next-line no-unused-vars
|
|
297
|
+
} catch (error) {
|
|
298
|
+
// the stored error stands when the network cannot do better
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
return response;
|
|
302
|
+
};
|
|
290
303
|
try {
|
|
291
|
-
const accept =
|
|
304
|
+
const accept =(options.acceptHeaders && options.acceptHeaders[options.expectedType]) || "*/*";
|
|
305
|
+
const fetchOptions = { referrer: options.resourceReferrer, referrerPolicy: options.referrerPolicy || undefined, stylesheetURL: options.stylesheetURL || undefined, stylesheetReferrerPolicy: options.stylesheetReferrerPolicy || undefined, headers: { accept } };
|
|
292
306
|
if (options.frameId) {
|
|
293
307
|
try {
|
|
294
|
-
response = await
|
|
295
|
-
fetchFrameResource(resourceURL, { frameId: options.frameId, referrer: options.resourceReferrer, referrerPolicy: options.referrerPolicy || undefined, stylesheetURL: options.stylesheetURL || undefined, stylesheetReferrerPolicy: options.stylesheetReferrerPolicy || undefined, headers: { accept } }),
|
|
296
|
-
networkTimeoutPromise
|
|
297
|
-
]);
|
|
308
|
+
response = await fetchWithRetry(fetchFrameResource, { frameId: options.frameId, ...fetchOptions });
|
|
298
309
|
setManifestResponse(entry, response, MANIFEST_FETCHED_BY_FRAME);
|
|
299
310
|
// eslint-disable-next-line no-unused-vars
|
|
300
311
|
} catch (error) {
|
|
301
|
-
response = await
|
|
302
|
-
fetchResource(resourceURL, { referrer: options.resourceReferrer, referrerPolicy: options.referrerPolicy || undefined, stylesheetURL: options.stylesheetURL || undefined, stylesheetReferrerPolicy: options.stylesheetReferrerPolicy || undefined, headers: { accept } }),
|
|
303
|
-
networkTimeoutPromise
|
|
304
|
-
]);
|
|
312
|
+
response = await fetchWithRetry(fetchResource, fetchOptions);
|
|
305
313
|
setManifestResponse(entry, response, MANIFEST_FETCHED_BY_PAGE);
|
|
306
314
|
}
|
|
307
315
|
} else {
|
|
308
|
-
response = await
|
|
309
|
-
fetchResource(resourceURL, { referrer: options.resourceReferrer, referrerPolicy: options.referrerPolicy || undefined, stylesheetURL: options.stylesheetURL || undefined, stylesheetReferrerPolicy: options.stylesheetReferrerPolicy || undefined, headers: { accept } }),
|
|
310
|
-
networkTimeoutPromise
|
|
311
|
-
]);
|
|
316
|
+
response = await fetchWithRetry(fetchResource, fetchOptions);
|
|
312
317
|
setManifestResponse(entry, response, MANIFEST_FETCHED_BY_PAGE);
|
|
313
318
|
}
|
|
314
319
|
} catch (error) {
|