Commit 1602e33c authored by Jimmy Lai's avatar Jimmy Lai

Update Turbopack runtime snapshots

parent 90232b56
;!function(){try { var e="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof global?global:"undefined"!=typeof window?window:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&((e._debugIds|| (e._debugIds={}))[n]="a43d47ff-a1c3-8414-d8a9-d8bdcc7d398b")}catch(e){}}(); ;!function(){try { var e="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof global?global:"undefined"!=typeof window?window:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&((e._debugIds|| (e._debugIds={}))[n]="f55a371b-3827-66c8-69f4-8822992d5a44")}catch(e){}}();
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push([ (globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push([
"output/ba425_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_0151fefb.js", "output/ba425_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_0151fefb.js",
{"otherChunks":["output/aaf3a_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_0b8736b3.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/debug-ids/browser/input/index.js [test] (ecmascript)"]} {"otherChunks":["output/aaf3a_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_0b8736b3.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/debug-ids/browser/input/index.js [test] (ecmascript)"]}
...@@ -1841,6 +1841,12 @@ let BACKEND; ...@@ -1841,6 +1841,12 @@ let BACKEND;
/** /**
* Maps chunk paths to the corresponding resolver. * Maps chunk paths to the corresponding resolver.
*/ const chunkResolvers = new Map(); */ const chunkResolvers = new Map();
// Keep in sync with Next's client chunk retry policy.
const MAX_CHUNK_LOAD_RETRY_ATTEMPTS = 1;
const BASE_CHUNK_LOAD_RETRY_DELAY_MS = 200;
const MAX_CHUNK_LOAD_RETRY_JITTER_MS = 400;
const MAX_CHUNK_LOAD_RETRY_RTT_MS = 500;
const MAX_CHUNK_LOAD_RETRY_TOTAL_DELAY_MS = 1500;
(()=>{ (()=>{
BACKEND = { BACKEND = {
async registerChunk (chunk, params) { async registerChunk (chunk, params) {
...@@ -1893,6 +1899,7 @@ let BACKEND; ...@@ -1893,6 +1899,7 @@ let BACKEND;
resolver = { resolver = {
resolved: false, resolved: false,
loadingStarted: false, loadingStarted: false,
retryAttempts: 0,
promise, promise,
resolve: ()=>{ resolve: ()=>{
resolver.resolved = true; resolver.resolved = true;
...@@ -1911,28 +1918,31 @@ let BACKEND; ...@@ -1911,28 +1918,31 @@ let BACKEND;
} }
resolver.reject(error); resolver.reject(error);
} }
/** function getChunkLoadRetryDelayMs() {
* Loads the given chunk, and returns a promise that resolves once the chunk const jitter = Math.floor(Math.random() * (MAX_CHUNK_LOAD_RETRY_JITTER_MS + 1));
* has been loaded. let rtt = 0;
*/ function doLoadChunk(sourceType, chunkUrl) { if ('connection' in navigator) {
const resolver = getOrCreateResolver(chunkUrl); const connection = navigator.connection;
if (resolver.loadingStarted) { if (connection?.rtt) {
return resolver.promise; rtt = Math.min(MAX_CHUNK_LOAD_RETRY_RTT_MS, connection.rtt);
} }
if (sourceType === SourceType.Runtime) {
// We don't need to load chunks references from runtime code, as they're already
// present in the DOM.
resolver.loadingStarted = true;
if (isCss(chunkUrl)) {
// CSS chunks do not register themselves, and as such must be marked as
// loaded instantly.
resolver.resolve();
} }
// We need to wait for JS chunks to register themselves within `registerChunk` return Math.min(MAX_CHUNK_LOAD_RETRY_TOTAL_DELAY_MS, BASE_CHUNK_LOAD_RETRY_DELAY_MS + jitter + rtt);
// before we can start instantiating runtime modules, hence the absence of
// `resolver.resolve()` in this branch.
return resolver.promise;
} }
function retryChunkLoad(sourceType, chunkUrl, resolver, error) {
if (resolver.retryAttempts >= MAX_CHUNK_LOAD_RETRY_ATTEMPTS || chunkResolvers.get(chunkUrl) !== resolver) {
rejectChunkResolver(chunkUrl, resolver, error);
return;
}
resolver.retryAttempts++;
setTimeout(()=>{
if (resolver.resolved || chunkResolvers.get(chunkUrl) !== resolver) {
return;
}
loadChunkImpl(sourceType, chunkUrl, resolver);
}, getChunkLoadRetryDelayMs());
}
function loadChunkImpl(sourceType, chunkUrl, resolver) {
if (typeof importScripts === 'function') { if (typeof importScripts === 'function') {
// We're in a web worker // We're in a web worker
if (isCss(chunkUrl)) { if (isCss(chunkUrl)) {
...@@ -1942,13 +1952,13 @@ let BACKEND; ...@@ -1942,13 +1952,13 @@ let BACKEND;
try { try {
importScripts(chunkUrl); importScripts(chunkUrl);
} catch (error) { } catch (error) {
rejectChunkResolver(chunkUrl, resolver, error); retryChunkLoad(sourceType, chunkUrl, resolver, error);
throw error;
} }
} else { } else {
throw new Error(`can't infer type of chunk from URL ${chunkUrl} in worker`); throw new Error(`can't infer type of chunk from URL ${chunkUrl} in worker`);
} }
} else { return;
}
// TODO(PACK-2140): remove this once all filenames are guaranteed to be escaped. // TODO(PACK-2140): remove this once all filenames are guaranteed to be escaped.
const decodedChunkUrl = decodeURI(chunkUrl); const decodedChunkUrl = decodeURI(chunkUrl);
// Escape URLs for safe use in CSS selectors // Escape URLs for safe use in CSS selectors
...@@ -1966,7 +1976,7 @@ let BACKEND; ...@@ -1966,7 +1976,7 @@ let BACKEND;
link.href = chunkUrl; link.href = chunkUrl;
link.onerror = ()=>{ link.onerror = ()=>{
link.remove(); link.remove();
rejectChunkResolver(chunkUrl, resolver); retryChunkLoad(sourceType, chunkUrl, resolver);
}; };
link.onload = ()=>{ link.onload = ()=>{
// CSS chunks do not register themselves, and as such must be marked as // CSS chunks do not register themselves, and as such must be marked as
...@@ -1984,7 +1994,7 @@ let BACKEND; ...@@ -1984,7 +1994,7 @@ let BACKEND;
for (const script of Array.from(previousScripts)){ for (const script of Array.from(previousScripts)){
script.addEventListener('error', ()=>{ script.addEventListener('error', ()=>{
script.remove(); script.remove();
rejectChunkResolver(chunkUrl, resolver); retryChunkLoad(sourceType, chunkUrl, resolver);
}, { }, {
once: true once: true
}); });
...@@ -1997,7 +2007,7 @@ let BACKEND; ...@@ -1997,7 +2007,7 @@ let BACKEND;
// this branch. // this branch.
script.onerror = ()=>{ script.onerror = ()=>{
script.remove(); script.remove();
rejectChunkResolver(chunkUrl, resolver); retryChunkLoad(sourceType, chunkUrl, resolver);
}; };
// Append to the `head` for webpack compatibility. // Append to the `head` for webpack compatibility.
document.head.appendChild(script); document.head.appendChild(script);
...@@ -2006,7 +2016,30 @@ let BACKEND; ...@@ -2006,7 +2016,30 @@ let BACKEND;
throw new Error(`can't infer type of chunk from URL ${chunkUrl}`); throw new Error(`can't infer type of chunk from URL ${chunkUrl}`);
} }
} }
/**
* Loads the given chunk, and returns a promise that resolves once the chunk
* has been loaded.
*/ function doLoadChunk(sourceType, chunkUrl) {
const resolver = getOrCreateResolver(chunkUrl);
if (resolver.loadingStarted) {
return resolver.promise;
}
if (sourceType === SourceType.Runtime) {
// We don't need to load chunks references from runtime code, as they're already
// present in the DOM.
resolver.loadingStarted = true;
if (isCss(chunkUrl)) {
// CSS chunks do not register themselves, and as such must be marked as
// loaded instantly.
resolver.resolve();
}
// We need to wait for JS chunks to register themselves within `registerChunk`
// before we can start instantiating runtime modules, hence the absence of
// `resolver.resolve()` in this branch.
return resolver.promise;
}
resolver.loadingStarted = true; resolver.loadingStarted = true;
loadChunkImpl(sourceType, chunkUrl, resolver);
return resolver.promise; return resolver.promise;
} }
function fetchWebAssembly(wasmChunkPath) { function fetchWebAssembly(wasmChunkPath) {
...@@ -2119,5 +2152,5 @@ chunkListsToRegister.forEach(registerChunkList); ...@@ -2119,5 +2152,5 @@ chunkListsToRegister.forEach(registerChunkList);
})(); })();
//# debugId=a43d47ff-a1c3-8414-d8a9-d8bdcc7d398b //# debugId=f55a371b-3827-66c8-69f4-8822992d5a44
//# sourceMappingURL=aaf3a_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_0151fefb.js.map //# sourceMappingURL=aaf3a_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_0151fefb.js.map
\ No newline at end of file
...@@ -1840,6 +1840,12 @@ let BACKEND; ...@@ -1840,6 +1840,12 @@ let BACKEND;
/** /**
* Maps chunk paths to the corresponding resolver. * Maps chunk paths to the corresponding resolver.
*/ const chunkResolvers = new Map(); */ const chunkResolvers = new Map();
// Keep in sync with Next's client chunk retry policy.
const MAX_CHUNK_LOAD_RETRY_ATTEMPTS = 1;
const BASE_CHUNK_LOAD_RETRY_DELAY_MS = 200;
const MAX_CHUNK_LOAD_RETRY_JITTER_MS = 400;
const MAX_CHUNK_LOAD_RETRY_RTT_MS = 500;
const MAX_CHUNK_LOAD_RETRY_TOTAL_DELAY_MS = 1500;
(()=>{ (()=>{
BACKEND = { BACKEND = {
async registerChunk (chunk, params) { async registerChunk (chunk, params) {
...@@ -1892,6 +1898,7 @@ let BACKEND; ...@@ -1892,6 +1898,7 @@ let BACKEND;
resolver = { resolver = {
resolved: false, resolved: false,
loadingStarted: false, loadingStarted: false,
retryAttempts: 0,
promise, promise,
resolve: ()=>{ resolve: ()=>{
resolver.resolved = true; resolver.resolved = true;
...@@ -1910,28 +1917,31 @@ let BACKEND; ...@@ -1910,28 +1917,31 @@ let BACKEND;
} }
resolver.reject(error); resolver.reject(error);
} }
/** function getChunkLoadRetryDelayMs() {
* Loads the given chunk, and returns a promise that resolves once the chunk const jitter = Math.floor(Math.random() * (MAX_CHUNK_LOAD_RETRY_JITTER_MS + 1));
* has been loaded. let rtt = 0;
*/ function doLoadChunk(sourceType, chunkUrl) { if ('connection' in navigator) {
const resolver = getOrCreateResolver(chunkUrl); const connection = navigator.connection;
if (resolver.loadingStarted) { if (connection?.rtt) {
return resolver.promise; rtt = Math.min(MAX_CHUNK_LOAD_RETRY_RTT_MS, connection.rtt);
} }
if (sourceType === SourceType.Runtime) {
// We don't need to load chunks references from runtime code, as they're already
// present in the DOM.
resolver.loadingStarted = true;
if (isCss(chunkUrl)) {
// CSS chunks do not register themselves, and as such must be marked as
// loaded instantly.
resolver.resolve();
} }
// We need to wait for JS chunks to register themselves within `registerChunk` return Math.min(MAX_CHUNK_LOAD_RETRY_TOTAL_DELAY_MS, BASE_CHUNK_LOAD_RETRY_DELAY_MS + jitter + rtt);
// before we can start instantiating runtime modules, hence the absence of
// `resolver.resolve()` in this branch.
return resolver.promise;
} }
function retryChunkLoad(sourceType, chunkUrl, resolver, error) {
if (resolver.retryAttempts >= MAX_CHUNK_LOAD_RETRY_ATTEMPTS || chunkResolvers.get(chunkUrl) !== resolver) {
rejectChunkResolver(chunkUrl, resolver, error);
return;
}
resolver.retryAttempts++;
setTimeout(()=>{
if (resolver.resolved || chunkResolvers.get(chunkUrl) !== resolver) {
return;
}
loadChunkImpl(sourceType, chunkUrl, resolver);
}, getChunkLoadRetryDelayMs());
}
function loadChunkImpl(sourceType, chunkUrl, resolver) {
if (typeof importScripts === 'function') { if (typeof importScripts === 'function') {
// We're in a web worker // We're in a web worker
if (isCss(chunkUrl)) { if (isCss(chunkUrl)) {
...@@ -1941,13 +1951,13 @@ let BACKEND; ...@@ -1941,13 +1951,13 @@ let BACKEND;
try { try {
importScripts(chunkUrl); importScripts(chunkUrl);
} catch (error) { } catch (error) {
rejectChunkResolver(chunkUrl, resolver, error); retryChunkLoad(sourceType, chunkUrl, resolver, error);
throw error;
} }
} else { } else {
throw new Error(`can't infer type of chunk from URL ${chunkUrl} in worker`); throw new Error(`can't infer type of chunk from URL ${chunkUrl} in worker`);
} }
} else { return;
}
// TODO(PACK-2140): remove this once all filenames are guaranteed to be escaped. // TODO(PACK-2140): remove this once all filenames are guaranteed to be escaped.
const decodedChunkUrl = decodeURI(chunkUrl); const decodedChunkUrl = decodeURI(chunkUrl);
// Escape URLs for safe use in CSS selectors // Escape URLs for safe use in CSS selectors
...@@ -1965,7 +1975,7 @@ let BACKEND; ...@@ -1965,7 +1975,7 @@ let BACKEND;
link.href = chunkUrl; link.href = chunkUrl;
link.onerror = ()=>{ link.onerror = ()=>{
link.remove(); link.remove();
rejectChunkResolver(chunkUrl, resolver); retryChunkLoad(sourceType, chunkUrl, resolver);
}; };
link.onload = ()=>{ link.onload = ()=>{
// CSS chunks do not register themselves, and as such must be marked as // CSS chunks do not register themselves, and as such must be marked as
...@@ -1983,7 +1993,7 @@ let BACKEND; ...@@ -1983,7 +1993,7 @@ let BACKEND;
for (const script of Array.from(previousScripts)){ for (const script of Array.from(previousScripts)){
script.addEventListener('error', ()=>{ script.addEventListener('error', ()=>{
script.remove(); script.remove();
rejectChunkResolver(chunkUrl, resolver); retryChunkLoad(sourceType, chunkUrl, resolver);
}, { }, {
once: true once: true
}); });
...@@ -1996,7 +2006,7 @@ let BACKEND; ...@@ -1996,7 +2006,7 @@ let BACKEND;
// this branch. // this branch.
script.onerror = ()=>{ script.onerror = ()=>{
script.remove(); script.remove();
rejectChunkResolver(chunkUrl, resolver); retryChunkLoad(sourceType, chunkUrl, resolver);
}; };
// Append to the `head` for webpack compatibility. // Append to the `head` for webpack compatibility.
document.head.appendChild(script); document.head.appendChild(script);
...@@ -2005,7 +2015,30 @@ let BACKEND; ...@@ -2005,7 +2015,30 @@ let BACKEND;
throw new Error(`can't infer type of chunk from URL ${chunkUrl}`); throw new Error(`can't infer type of chunk from URL ${chunkUrl}`);
} }
} }
/**
* Loads the given chunk, and returns a promise that resolves once the chunk
* has been loaded.
*/ function doLoadChunk(sourceType, chunkUrl) {
const resolver = getOrCreateResolver(chunkUrl);
if (resolver.loadingStarted) {
return resolver.promise;
}
if (sourceType === SourceType.Runtime) {
// We don't need to load chunks references from runtime code, as they're already
// present in the DOM.
resolver.loadingStarted = true;
if (isCss(chunkUrl)) {
// CSS chunks do not register themselves, and as such must be marked as
// loaded instantly.
resolver.resolve();
}
// We need to wait for JS chunks to register themselves within `registerChunk`
// before we can start instantiating runtime modules, hence the absence of
// `resolver.resolve()` in this branch.
return resolver.promise;
}
resolver.loadingStarted = true; resolver.loadingStarted = true;
loadChunkImpl(sourceType, chunkUrl, resolver);
return resolver.promise; return resolver.promise;
} }
function fetchWebAssembly(wasmChunkPath) { function fetchWebAssembly(wasmChunkPath) {
......
...@@ -1622,6 +1622,12 @@ var BACKEND; ...@@ -1622,6 +1622,12 @@ var BACKEND;
/** /**
* Maps chunk paths to the corresponding resolver. * Maps chunk paths to the corresponding resolver.
*/ var chunkResolvers = new Map(); */ var chunkResolvers = new Map();
// Keep in sync with Next's client chunk retry policy.
var MAX_CHUNK_LOAD_RETRY_ATTEMPTS = 1;
var BASE_CHUNK_LOAD_RETRY_DELAY_MS = 200;
var MAX_CHUNK_LOAD_RETRY_JITTER_MS = 400;
var MAX_CHUNK_LOAD_RETRY_RTT_MS = 500;
var MAX_CHUNK_LOAD_RETRY_TOTAL_DELAY_MS = 1500;
(function() { (function() {
BACKEND = { BACKEND = {
registerChunk: function registerChunk(chunk, params) { registerChunk: function registerChunk(chunk, params) {
...@@ -1760,6 +1766,7 @@ var BACKEND; ...@@ -1760,6 +1766,7 @@ var BACKEND;
resolver = { resolver = {
resolved: false, resolved: false,
loadingStarted: false, loadingStarted: false,
retryAttempts: 0,
promise: promise, promise: promise,
resolve: function resolve1() { resolve: function resolve1() {
resolver.resolved = true; resolver.resolved = true;
...@@ -1778,28 +1785,31 @@ var BACKEND; ...@@ -1778,28 +1785,31 @@ var BACKEND;
} }
resolver.reject(error); resolver.reject(error);
} }
/** function getChunkLoadRetryDelayMs() {
* Loads the given chunk, and returns a promise that resolves once the chunk var jitter = Math.floor(Math.random() * (MAX_CHUNK_LOAD_RETRY_JITTER_MS + 1));
* has been loaded. var rtt = 0;
*/ function doLoadChunk(sourceType, chunkUrl) { if ('connection' in navigator) {
var resolver = getOrCreateResolver(chunkUrl); var connection = navigator.connection;
if (resolver.loadingStarted) { if (connection === null || connection === void 0 ? void 0 : connection.rtt) {
return resolver.promise; rtt = Math.min(MAX_CHUNK_LOAD_RETRY_RTT_MS, connection.rtt);
} }
if (sourceType === SourceType.Runtime) {
// We don't need to load chunks references from runtime code, as they're already
// present in the DOM.
resolver.loadingStarted = true;
if (isCss(chunkUrl)) {
// CSS chunks do not register themselves, and as such must be marked as
// loaded instantly.
resolver.resolve();
} }
// We need to wait for JS chunks to register themselves within `registerChunk` return Math.min(MAX_CHUNK_LOAD_RETRY_TOTAL_DELAY_MS, BASE_CHUNK_LOAD_RETRY_DELAY_MS + jitter + rtt);
// before we can start instantiating runtime modules, hence the absence of
// `resolver.resolve()` in this branch.
return resolver.promise;
} }
function retryChunkLoad(sourceType, chunkUrl, resolver, error) {
if (resolver.retryAttempts >= MAX_CHUNK_LOAD_RETRY_ATTEMPTS || chunkResolvers.get(chunkUrl) !== resolver) {
rejectChunkResolver(chunkUrl, resolver, error);
return;
}
resolver.retryAttempts++;
setTimeout(function() {
if (resolver.resolved || chunkResolvers.get(chunkUrl) !== resolver) {
return;
}
loadChunkImpl(sourceType, chunkUrl, resolver);
}, getChunkLoadRetryDelayMs());
}
function loadChunkImpl(sourceType, chunkUrl, resolver) {
if (typeof importScripts === 'function') { if (typeof importScripts === 'function') {
// We're in a web worker // We're in a web worker
if (isCss(chunkUrl)) { if (isCss(chunkUrl)) {
...@@ -1809,13 +1819,13 @@ var BACKEND; ...@@ -1809,13 +1819,13 @@ var BACKEND;
try { try {
importScripts(chunkUrl); importScripts(chunkUrl);
} catch (error) { } catch (error) {
rejectChunkResolver(chunkUrl, resolver, error); retryChunkLoad(sourceType, chunkUrl, resolver, error);
throw error;
} }
} else { } else {
throw new Error(`can't infer type of chunk from URL ${chunkUrl} in worker`); throw new Error(`can't infer type of chunk from URL ${chunkUrl} in worker`);
} }
} else { return;
}
// TODO(PACK-2140): remove this once all filenames are guaranteed to be escaped. // TODO(PACK-2140): remove this once all filenames are guaranteed to be escaped.
var decodedChunkUrl = decodeURI(chunkUrl); var decodedChunkUrl = decodeURI(chunkUrl);
// Escape URLs for safe use in CSS selectors // Escape URLs for safe use in CSS selectors
...@@ -1833,7 +1843,7 @@ var BACKEND; ...@@ -1833,7 +1843,7 @@ var BACKEND;
link.href = chunkUrl; link.href = chunkUrl;
link.onerror = function() { link.onerror = function() {
link.remove(); link.remove();
rejectChunkResolver(chunkUrl, resolver); retryChunkLoad(sourceType, chunkUrl, resolver);
}; };
link.onload = function() { link.onload = function() {
// CSS chunks do not register themselves, and as such must be marked as // CSS chunks do not register themselves, and as such must be marked as
...@@ -1852,7 +1862,7 @@ var BACKEND; ...@@ -1852,7 +1862,7 @@ var BACKEND;
var script = _step.value; var script = _step.value;
script.addEventListener('error', function() { script.addEventListener('error', function() {
script.remove(); script.remove();
rejectChunkResolver(chunkUrl, resolver); retryChunkLoad(sourceType, chunkUrl, resolver);
}, { }, {
once: true once: true
}); });
...@@ -1882,7 +1892,7 @@ var BACKEND; ...@@ -1882,7 +1892,7 @@ var BACKEND;
// this branch. // this branch.
script.onerror = function() { script.onerror = function() {
script.remove(); script.remove();
rejectChunkResolver(chunkUrl, resolver); retryChunkLoad(sourceType, chunkUrl, resolver);
}; };
// Append to the `head` for webpack compatibility. // Append to the `head` for webpack compatibility.
document.head.appendChild(script); document.head.appendChild(script);
...@@ -1891,7 +1901,30 @@ var BACKEND; ...@@ -1891,7 +1901,30 @@ var BACKEND;
throw new Error(`can't infer type of chunk from URL ${chunkUrl}`); throw new Error(`can't infer type of chunk from URL ${chunkUrl}`);
} }
} }
/**
* Loads the given chunk, and returns a promise that resolves once the chunk
* has been loaded.
*/ function doLoadChunk(sourceType, chunkUrl) {
var resolver = getOrCreateResolver(chunkUrl);
if (resolver.loadingStarted) {
return resolver.promise;
}
if (sourceType === SourceType.Runtime) {
// We don't need to load chunks references from runtime code, as they're already
// present in the DOM.
resolver.loadingStarted = true;
if (isCss(chunkUrl)) {
// CSS chunks do not register themselves, and as such must be marked as
// loaded instantly.
resolver.resolve();
}
// We need to wait for JS chunks to register themselves within `registerChunk`
// before we can start instantiating runtime modules, hence the absence of
// `resolver.resolve()` in this branch.
return resolver.promise;
}
resolver.loadingStarted = true; resolver.loadingStarted = true;
loadChunkImpl(sourceType, chunkUrl, resolver);
return resolver.promise; return resolver.promise;
} }
function fetchWebAssembly(wasmChunkPath) { function fetchWebAssembly(wasmChunkPath) {
......
...@@ -1840,6 +1840,12 @@ let BACKEND; ...@@ -1840,6 +1840,12 @@ let BACKEND;
/** /**
* Maps chunk paths to the corresponding resolver. * Maps chunk paths to the corresponding resolver.
*/ const chunkResolvers = new Map(); */ const chunkResolvers = new Map();
// Keep in sync with Next's client chunk retry policy.
const MAX_CHUNK_LOAD_RETRY_ATTEMPTS = 1;
const BASE_CHUNK_LOAD_RETRY_DELAY_MS = 200;
const MAX_CHUNK_LOAD_RETRY_JITTER_MS = 400;
const MAX_CHUNK_LOAD_RETRY_RTT_MS = 500;
const MAX_CHUNK_LOAD_RETRY_TOTAL_DELAY_MS = 1500;
(()=>{ (()=>{
BACKEND = { BACKEND = {
async registerChunk (chunk, params) { async registerChunk (chunk, params) {
...@@ -1892,6 +1898,7 @@ let BACKEND; ...@@ -1892,6 +1898,7 @@ let BACKEND;
resolver = { resolver = {
resolved: false, resolved: false,
loadingStarted: false, loadingStarted: false,
retryAttempts: 0,
promise, promise,
resolve: ()=>{ resolve: ()=>{
resolver.resolved = true; resolver.resolved = true;
...@@ -1910,28 +1917,31 @@ let BACKEND; ...@@ -1910,28 +1917,31 @@ let BACKEND;
} }
resolver.reject(error); resolver.reject(error);
} }
/** function getChunkLoadRetryDelayMs() {
* Loads the given chunk, and returns a promise that resolves once the chunk const jitter = Math.floor(Math.random() * (MAX_CHUNK_LOAD_RETRY_JITTER_MS + 1));
* has been loaded. let rtt = 0;
*/ function doLoadChunk(sourceType, chunkUrl) { if ('connection' in navigator) {
const resolver = getOrCreateResolver(chunkUrl); const connection = navigator.connection;
if (resolver.loadingStarted) { if (connection?.rtt) {
return resolver.promise; rtt = Math.min(MAX_CHUNK_LOAD_RETRY_RTT_MS, connection.rtt);
} }
if (sourceType === SourceType.Runtime) {
// We don't need to load chunks references from runtime code, as they're already
// present in the DOM.
resolver.loadingStarted = true;
if (isCss(chunkUrl)) {
// CSS chunks do not register themselves, and as such must be marked as
// loaded instantly.
resolver.resolve();
} }
// We need to wait for JS chunks to register themselves within `registerChunk` return Math.min(MAX_CHUNK_LOAD_RETRY_TOTAL_DELAY_MS, BASE_CHUNK_LOAD_RETRY_DELAY_MS + jitter + rtt);
// before we can start instantiating runtime modules, hence the absence of
// `resolver.resolve()` in this branch.
return resolver.promise;
} }
function retryChunkLoad(sourceType, chunkUrl, resolver, error) {
if (resolver.retryAttempts >= MAX_CHUNK_LOAD_RETRY_ATTEMPTS || chunkResolvers.get(chunkUrl) !== resolver) {
rejectChunkResolver(chunkUrl, resolver, error);
return;
}
resolver.retryAttempts++;
setTimeout(()=>{
if (resolver.resolved || chunkResolvers.get(chunkUrl) !== resolver) {
return;
}
loadChunkImpl(sourceType, chunkUrl, resolver);
}, getChunkLoadRetryDelayMs());
}
function loadChunkImpl(sourceType, chunkUrl, resolver) {
if (typeof importScripts === 'function') { if (typeof importScripts === 'function') {
// We're in a web worker // We're in a web worker
if (isCss(chunkUrl)) { if (isCss(chunkUrl)) {
...@@ -1941,13 +1951,13 @@ let BACKEND; ...@@ -1941,13 +1951,13 @@ let BACKEND;
try { try {
importScripts(chunkUrl); importScripts(chunkUrl);
} catch (error) { } catch (error) {
rejectChunkResolver(chunkUrl, resolver, error); retryChunkLoad(sourceType, chunkUrl, resolver, error);
throw error;
} }
} else { } else {
throw new Error(`can't infer type of chunk from URL ${chunkUrl} in worker`); throw new Error(`can't infer type of chunk from URL ${chunkUrl} in worker`);
} }
} else { return;
}
// TODO(PACK-2140): remove this once all filenames are guaranteed to be escaped. // TODO(PACK-2140): remove this once all filenames are guaranteed to be escaped.
const decodedChunkUrl = decodeURI(chunkUrl); const decodedChunkUrl = decodeURI(chunkUrl);
// Escape URLs for safe use in CSS selectors // Escape URLs for safe use in CSS selectors
...@@ -1965,7 +1975,7 @@ let BACKEND; ...@@ -1965,7 +1975,7 @@ let BACKEND;
link.href = chunkUrl; link.href = chunkUrl;
link.onerror = ()=>{ link.onerror = ()=>{
link.remove(); link.remove();
rejectChunkResolver(chunkUrl, resolver); retryChunkLoad(sourceType, chunkUrl, resolver);
}; };
link.onload = ()=>{ link.onload = ()=>{
// CSS chunks do not register themselves, and as such must be marked as // CSS chunks do not register themselves, and as such must be marked as
...@@ -1983,7 +1993,7 @@ let BACKEND; ...@@ -1983,7 +1993,7 @@ let BACKEND;
for (const script of Array.from(previousScripts)){ for (const script of Array.from(previousScripts)){
script.addEventListener('error', ()=>{ script.addEventListener('error', ()=>{
script.remove(); script.remove();
rejectChunkResolver(chunkUrl, resolver); retryChunkLoad(sourceType, chunkUrl, resolver);
}, { }, {
once: true once: true
}); });
...@@ -1996,7 +2006,7 @@ let BACKEND; ...@@ -1996,7 +2006,7 @@ let BACKEND;
// this branch. // this branch.
script.onerror = ()=>{ script.onerror = ()=>{
script.remove(); script.remove();
rejectChunkResolver(chunkUrl, resolver); retryChunkLoad(sourceType, chunkUrl, resolver);
}; };
// Append to the `head` for webpack compatibility. // Append to the `head` for webpack compatibility.
document.head.appendChild(script); document.head.appendChild(script);
...@@ -2005,7 +2015,30 @@ let BACKEND; ...@@ -2005,7 +2015,30 @@ let BACKEND;
throw new Error(`can't infer type of chunk from URL ${chunkUrl}`); throw new Error(`can't infer type of chunk from URL ${chunkUrl}`);
} }
} }
/**
* Loads the given chunk, and returns a promise that resolves once the chunk
* has been loaded.
*/ function doLoadChunk(sourceType, chunkUrl) {
const resolver = getOrCreateResolver(chunkUrl);
if (resolver.loadingStarted) {
return resolver.promise;
}
if (sourceType === SourceType.Runtime) {
// We don't need to load chunks references from runtime code, as they're already
// present in the DOM.
resolver.loadingStarted = true;
if (isCss(chunkUrl)) {
// CSS chunks do not register themselves, and as such must be marked as
// loaded instantly.
resolver.resolve();
}
// We need to wait for JS chunks to register themselves within `registerChunk`
// before we can start instantiating runtime modules, hence the absence of
// `resolver.resolve()` in this branch.
return resolver.promise;
}
resolver.loadingStarted = true; resolver.loadingStarted = true;
loadChunkImpl(sourceType, chunkUrl, resolver);
return resolver.promise; return resolver.promise;
} }
function fetchWebAssembly(wasmChunkPath) { function fetchWebAssembly(wasmChunkPath) {
......
...@@ -1840,6 +1840,12 @@ let BACKEND; ...@@ -1840,6 +1840,12 @@ let BACKEND;
/** /**
* Maps chunk paths to the corresponding resolver. * Maps chunk paths to the corresponding resolver.
*/ const chunkResolvers = new Map(); */ const chunkResolvers = new Map();
// Keep in sync with Next's client chunk retry policy.
const MAX_CHUNK_LOAD_RETRY_ATTEMPTS = 1;
const BASE_CHUNK_LOAD_RETRY_DELAY_MS = 200;
const MAX_CHUNK_LOAD_RETRY_JITTER_MS = 400;
const MAX_CHUNK_LOAD_RETRY_RTT_MS = 500;
const MAX_CHUNK_LOAD_RETRY_TOTAL_DELAY_MS = 1500;
(()=>{ (()=>{
BACKEND = { BACKEND = {
async registerChunk (chunk, params) { async registerChunk (chunk, params) {
...@@ -1892,6 +1898,7 @@ let BACKEND; ...@@ -1892,6 +1898,7 @@ let BACKEND;
resolver = { resolver = {
resolved: false, resolved: false,
loadingStarted: false, loadingStarted: false,
retryAttempts: 0,
promise, promise,
resolve: ()=>{ resolve: ()=>{
resolver.resolved = true; resolver.resolved = true;
...@@ -1910,28 +1917,31 @@ let BACKEND; ...@@ -1910,28 +1917,31 @@ let BACKEND;
} }
resolver.reject(error); resolver.reject(error);
} }
/** function getChunkLoadRetryDelayMs() {
* Loads the given chunk, and returns a promise that resolves once the chunk const jitter = Math.floor(Math.random() * (MAX_CHUNK_LOAD_RETRY_JITTER_MS + 1));
* has been loaded. let rtt = 0;
*/ function doLoadChunk(sourceType, chunkUrl) { if ('connection' in navigator) {
const resolver = getOrCreateResolver(chunkUrl); const connection = navigator.connection;
if (resolver.loadingStarted) { if (connection?.rtt) {
return resolver.promise; rtt = Math.min(MAX_CHUNK_LOAD_RETRY_RTT_MS, connection.rtt);
} }
if (sourceType === SourceType.Runtime) {
// We don't need to load chunks references from runtime code, as they're already
// present in the DOM.
resolver.loadingStarted = true;
if (isCss(chunkUrl)) {
// CSS chunks do not register themselves, and as such must be marked as
// loaded instantly.
resolver.resolve();
} }
// We need to wait for JS chunks to register themselves within `registerChunk` return Math.min(MAX_CHUNK_LOAD_RETRY_TOTAL_DELAY_MS, BASE_CHUNK_LOAD_RETRY_DELAY_MS + jitter + rtt);
// before we can start instantiating runtime modules, hence the absence of
// `resolver.resolve()` in this branch.
return resolver.promise;
} }
function retryChunkLoad(sourceType, chunkUrl, resolver, error) {
if (resolver.retryAttempts >= MAX_CHUNK_LOAD_RETRY_ATTEMPTS || chunkResolvers.get(chunkUrl) !== resolver) {
rejectChunkResolver(chunkUrl, resolver, error);
return;
}
resolver.retryAttempts++;
setTimeout(()=>{
if (resolver.resolved || chunkResolvers.get(chunkUrl) !== resolver) {
return;
}
loadChunkImpl(sourceType, chunkUrl, resolver);
}, getChunkLoadRetryDelayMs());
}
function loadChunkImpl(sourceType, chunkUrl, resolver) {
if (typeof importScripts === 'function') { if (typeof importScripts === 'function') {
// We're in a web worker // We're in a web worker
if (isCss(chunkUrl)) { if (isCss(chunkUrl)) {
...@@ -1941,13 +1951,13 @@ let BACKEND; ...@@ -1941,13 +1951,13 @@ let BACKEND;
try { try {
importScripts(chunkUrl); importScripts(chunkUrl);
} catch (error) { } catch (error) {
rejectChunkResolver(chunkUrl, resolver, error); retryChunkLoad(sourceType, chunkUrl, resolver, error);
throw error;
} }
} else { } else {
throw new Error(`can't infer type of chunk from URL ${chunkUrl} in worker`); throw new Error(`can't infer type of chunk from URL ${chunkUrl} in worker`);
} }
} else { return;
}
// TODO(PACK-2140): remove this once all filenames are guaranteed to be escaped. // TODO(PACK-2140): remove this once all filenames are guaranteed to be escaped.
const decodedChunkUrl = decodeURI(chunkUrl); const decodedChunkUrl = decodeURI(chunkUrl);
// Escape URLs for safe use in CSS selectors // Escape URLs for safe use in CSS selectors
...@@ -1965,7 +1975,7 @@ let BACKEND; ...@@ -1965,7 +1975,7 @@ let BACKEND;
link.href = chunkUrl; link.href = chunkUrl;
link.onerror = ()=>{ link.onerror = ()=>{
link.remove(); link.remove();
rejectChunkResolver(chunkUrl, resolver); retryChunkLoad(sourceType, chunkUrl, resolver);
}; };
link.onload = ()=>{ link.onload = ()=>{
// CSS chunks do not register themselves, and as such must be marked as // CSS chunks do not register themselves, and as such must be marked as
...@@ -1983,7 +1993,7 @@ let BACKEND; ...@@ -1983,7 +1993,7 @@ let BACKEND;
for (const script of Array.from(previousScripts)){ for (const script of Array.from(previousScripts)){
script.addEventListener('error', ()=>{ script.addEventListener('error', ()=>{
script.remove(); script.remove();
rejectChunkResolver(chunkUrl, resolver); retryChunkLoad(sourceType, chunkUrl, resolver);
}, { }, {
once: true once: true
}); });
...@@ -1996,7 +2006,7 @@ let BACKEND; ...@@ -1996,7 +2006,7 @@ let BACKEND;
// this branch. // this branch.
script.onerror = ()=>{ script.onerror = ()=>{
script.remove(); script.remove();
rejectChunkResolver(chunkUrl, resolver); retryChunkLoad(sourceType, chunkUrl, resolver);
}; };
// Append to the `head` for webpack compatibility. // Append to the `head` for webpack compatibility.
document.head.appendChild(script); document.head.appendChild(script);
...@@ -2005,7 +2015,30 @@ let BACKEND; ...@@ -2005,7 +2015,30 @@ let BACKEND;
throw new Error(`can't infer type of chunk from URL ${chunkUrl}`); throw new Error(`can't infer type of chunk from URL ${chunkUrl}`);
} }
} }
/**
* Loads the given chunk, and returns a promise that resolves once the chunk
* has been loaded.
*/ function doLoadChunk(sourceType, chunkUrl) {
const resolver = getOrCreateResolver(chunkUrl);
if (resolver.loadingStarted) {
return resolver.promise;
}
if (sourceType === SourceType.Runtime) {
// We don't need to load chunks references from runtime code, as they're already
// present in the DOM.
resolver.loadingStarted = true;
if (isCss(chunkUrl)) {
// CSS chunks do not register themselves, and as such must be marked as
// loaded instantly.
resolver.resolve();
}
// We need to wait for JS chunks to register themselves within `registerChunk`
// before we can start instantiating runtime modules, hence the absence of
// `resolver.resolve()` in this branch.
return resolver.promise;
}
resolver.loadingStarted = true; resolver.loadingStarted = true;
loadChunkImpl(sourceType, chunkUrl, resolver);
return resolver.promise; return resolver.promise;
} }
function fetchWebAssembly(wasmChunkPath) { function fetchWebAssembly(wasmChunkPath) {
......
...@@ -1840,6 +1840,12 @@ let BACKEND; ...@@ -1840,6 +1840,12 @@ let BACKEND;
/** /**
* Maps chunk paths to the corresponding resolver. * Maps chunk paths to the corresponding resolver.
*/ const chunkResolvers = new Map(); */ const chunkResolvers = new Map();
// Keep in sync with Next's client chunk retry policy.
const MAX_CHUNK_LOAD_RETRY_ATTEMPTS = 1;
const BASE_CHUNK_LOAD_RETRY_DELAY_MS = 200;
const MAX_CHUNK_LOAD_RETRY_JITTER_MS = 400;
const MAX_CHUNK_LOAD_RETRY_RTT_MS = 500;
const MAX_CHUNK_LOAD_RETRY_TOTAL_DELAY_MS = 1500;
(()=>{ (()=>{
BACKEND = { BACKEND = {
async registerChunk (chunk, params) { async registerChunk (chunk, params) {
...@@ -1892,6 +1898,7 @@ let BACKEND; ...@@ -1892,6 +1898,7 @@ let BACKEND;
resolver = { resolver = {
resolved: false, resolved: false,
loadingStarted: false, loadingStarted: false,
retryAttempts: 0,
promise, promise,
resolve: ()=>{ resolve: ()=>{
resolver.resolved = true; resolver.resolved = true;
...@@ -1910,28 +1917,31 @@ let BACKEND; ...@@ -1910,28 +1917,31 @@ let BACKEND;
} }
resolver.reject(error); resolver.reject(error);
} }
/** function getChunkLoadRetryDelayMs() {
* Loads the given chunk, and returns a promise that resolves once the chunk const jitter = Math.floor(Math.random() * (MAX_CHUNK_LOAD_RETRY_JITTER_MS + 1));
* has been loaded. let rtt = 0;
*/ function doLoadChunk(sourceType, chunkUrl) { if ('connection' in navigator) {
const resolver = getOrCreateResolver(chunkUrl); const connection = navigator.connection;
if (resolver.loadingStarted) { if (connection?.rtt) {
return resolver.promise; rtt = Math.min(MAX_CHUNK_LOAD_RETRY_RTT_MS, connection.rtt);
} }
if (sourceType === SourceType.Runtime) {
// We don't need to load chunks references from runtime code, as they're already
// present in the DOM.
resolver.loadingStarted = true;
if (isCss(chunkUrl)) {
// CSS chunks do not register themselves, and as such must be marked as
// loaded instantly.
resolver.resolve();
} }
// We need to wait for JS chunks to register themselves within `registerChunk` return Math.min(MAX_CHUNK_LOAD_RETRY_TOTAL_DELAY_MS, BASE_CHUNK_LOAD_RETRY_DELAY_MS + jitter + rtt);
// before we can start instantiating runtime modules, hence the absence of
// `resolver.resolve()` in this branch.
return resolver.promise;
} }
function retryChunkLoad(sourceType, chunkUrl, resolver, error) {
if (resolver.retryAttempts >= MAX_CHUNK_LOAD_RETRY_ATTEMPTS || chunkResolvers.get(chunkUrl) !== resolver) {
rejectChunkResolver(chunkUrl, resolver, error);
return;
}
resolver.retryAttempts++;
setTimeout(()=>{
if (resolver.resolved || chunkResolvers.get(chunkUrl) !== resolver) {
return;
}
loadChunkImpl(sourceType, chunkUrl, resolver);
}, getChunkLoadRetryDelayMs());
}
function loadChunkImpl(sourceType, chunkUrl, resolver) {
if (typeof importScripts === 'function') { if (typeof importScripts === 'function') {
// We're in a web worker // We're in a web worker
if (isCss(chunkUrl)) { if (isCss(chunkUrl)) {
...@@ -1941,13 +1951,13 @@ let BACKEND; ...@@ -1941,13 +1951,13 @@ let BACKEND;
try { try {
importScripts(chunkUrl); importScripts(chunkUrl);
} catch (error) { } catch (error) {
rejectChunkResolver(chunkUrl, resolver, error); retryChunkLoad(sourceType, chunkUrl, resolver, error);
throw error;
} }
} else { } else {
throw new Error(`can't infer type of chunk from URL ${chunkUrl} in worker`); throw new Error(`can't infer type of chunk from URL ${chunkUrl} in worker`);
} }
} else { return;
}
// TODO(PACK-2140): remove this once all filenames are guaranteed to be escaped. // TODO(PACK-2140): remove this once all filenames are guaranteed to be escaped.
const decodedChunkUrl = decodeURI(chunkUrl); const decodedChunkUrl = decodeURI(chunkUrl);
// Escape URLs for safe use in CSS selectors // Escape URLs for safe use in CSS selectors
...@@ -1965,7 +1975,7 @@ let BACKEND; ...@@ -1965,7 +1975,7 @@ let BACKEND;
link.href = chunkUrl; link.href = chunkUrl;
link.onerror = ()=>{ link.onerror = ()=>{
link.remove(); link.remove();
rejectChunkResolver(chunkUrl, resolver); retryChunkLoad(sourceType, chunkUrl, resolver);
}; };
link.onload = ()=>{ link.onload = ()=>{
// CSS chunks do not register themselves, and as such must be marked as // CSS chunks do not register themselves, and as such must be marked as
...@@ -1983,7 +1993,7 @@ let BACKEND; ...@@ -1983,7 +1993,7 @@ let BACKEND;
for (const script of Array.from(previousScripts)){ for (const script of Array.from(previousScripts)){
script.addEventListener('error', ()=>{ script.addEventListener('error', ()=>{
script.remove(); script.remove();
rejectChunkResolver(chunkUrl, resolver); retryChunkLoad(sourceType, chunkUrl, resolver);
}, { }, {
once: true once: true
}); });
...@@ -1996,7 +2006,7 @@ let BACKEND; ...@@ -1996,7 +2006,7 @@ let BACKEND;
// this branch. // this branch.
script.onerror = ()=>{ script.onerror = ()=>{
script.remove(); script.remove();
rejectChunkResolver(chunkUrl, resolver); retryChunkLoad(sourceType, chunkUrl, resolver);
}; };
// Append to the `head` for webpack compatibility. // Append to the `head` for webpack compatibility.
document.head.appendChild(script); document.head.appendChild(script);
...@@ -2005,7 +2015,30 @@ let BACKEND; ...@@ -2005,7 +2015,30 @@ let BACKEND;
throw new Error(`can't infer type of chunk from URL ${chunkUrl}`); throw new Error(`can't infer type of chunk from URL ${chunkUrl}`);
} }
} }
/**
* Loads the given chunk, and returns a promise that resolves once the chunk
* has been loaded.
*/ function doLoadChunk(sourceType, chunkUrl) {
const resolver = getOrCreateResolver(chunkUrl);
if (resolver.loadingStarted) {
return resolver.promise;
}
if (sourceType === SourceType.Runtime) {
// We don't need to load chunks references from runtime code, as they're already
// present in the DOM.
resolver.loadingStarted = true;
if (isCss(chunkUrl)) {
// CSS chunks do not register themselves, and as such must be marked as
// loaded instantly.
resolver.resolve();
}
// We need to wait for JS chunks to register themselves within `registerChunk`
// before we can start instantiating runtime modules, hence the absence of
// `resolver.resolve()` in this branch.
return resolver.promise;
}
resolver.loadingStarted = true; resolver.loadingStarted = true;
loadChunkImpl(sourceType, chunkUrl, resolver);
return resolver.promise; return resolver.promise;
} }
function fetchWebAssembly(wasmChunkPath) { function fetchWebAssembly(wasmChunkPath) {
......
...@@ -1840,6 +1840,12 @@ let BACKEND; ...@@ -1840,6 +1840,12 @@ let BACKEND;
/** /**
* Maps chunk paths to the corresponding resolver. * Maps chunk paths to the corresponding resolver.
*/ const chunkResolvers = new Map(); */ const chunkResolvers = new Map();
// Keep in sync with Next's client chunk retry policy.
const MAX_CHUNK_LOAD_RETRY_ATTEMPTS = 1;
const BASE_CHUNK_LOAD_RETRY_DELAY_MS = 200;
const MAX_CHUNK_LOAD_RETRY_JITTER_MS = 400;
const MAX_CHUNK_LOAD_RETRY_RTT_MS = 500;
const MAX_CHUNK_LOAD_RETRY_TOTAL_DELAY_MS = 1500;
(()=>{ (()=>{
BACKEND = { BACKEND = {
async registerChunk (chunk, params) { async registerChunk (chunk, params) {
...@@ -1892,6 +1898,7 @@ let BACKEND; ...@@ -1892,6 +1898,7 @@ let BACKEND;
resolver = { resolver = {
resolved: false, resolved: false,
loadingStarted: false, loadingStarted: false,
retryAttempts: 0,
promise, promise,
resolve: ()=>{ resolve: ()=>{
resolver.resolved = true; resolver.resolved = true;
...@@ -1910,28 +1917,31 @@ let BACKEND; ...@@ -1910,28 +1917,31 @@ let BACKEND;
} }
resolver.reject(error); resolver.reject(error);
} }
/** function getChunkLoadRetryDelayMs() {
* Loads the given chunk, and returns a promise that resolves once the chunk const jitter = Math.floor(Math.random() * (MAX_CHUNK_LOAD_RETRY_JITTER_MS + 1));
* has been loaded. let rtt = 0;
*/ function doLoadChunk(sourceType, chunkUrl) { if ('connection' in navigator) {
const resolver = getOrCreateResolver(chunkUrl); const connection = navigator.connection;
if (resolver.loadingStarted) { if (connection?.rtt) {
return resolver.promise; rtt = Math.min(MAX_CHUNK_LOAD_RETRY_RTT_MS, connection.rtt);
} }
if (sourceType === SourceType.Runtime) {
// We don't need to load chunks references from runtime code, as they're already
// present in the DOM.
resolver.loadingStarted = true;
if (isCss(chunkUrl)) {
// CSS chunks do not register themselves, and as such must be marked as
// loaded instantly.
resolver.resolve();
} }
// We need to wait for JS chunks to register themselves within `registerChunk` return Math.min(MAX_CHUNK_LOAD_RETRY_TOTAL_DELAY_MS, BASE_CHUNK_LOAD_RETRY_DELAY_MS + jitter + rtt);
// before we can start instantiating runtime modules, hence the absence of
// `resolver.resolve()` in this branch.
return resolver.promise;
} }
function retryChunkLoad(sourceType, chunkUrl, resolver, error) {
if (resolver.retryAttempts >= MAX_CHUNK_LOAD_RETRY_ATTEMPTS || chunkResolvers.get(chunkUrl) !== resolver) {
rejectChunkResolver(chunkUrl, resolver, error);
return;
}
resolver.retryAttempts++;
setTimeout(()=>{
if (resolver.resolved || chunkResolvers.get(chunkUrl) !== resolver) {
return;
}
loadChunkImpl(sourceType, chunkUrl, resolver);
}, getChunkLoadRetryDelayMs());
}
function loadChunkImpl(sourceType, chunkUrl, resolver) {
if (typeof importScripts === 'function') { if (typeof importScripts === 'function') {
// We're in a web worker // We're in a web worker
if (isCss(chunkUrl)) { if (isCss(chunkUrl)) {
...@@ -1941,13 +1951,13 @@ let BACKEND; ...@@ -1941,13 +1951,13 @@ let BACKEND;
try { try {
importScripts(chunkUrl); importScripts(chunkUrl);
} catch (error) { } catch (error) {
rejectChunkResolver(chunkUrl, resolver, error); retryChunkLoad(sourceType, chunkUrl, resolver, error);
throw error;
} }
} else { } else {
throw new Error(`can't infer type of chunk from URL ${chunkUrl} in worker`); throw new Error(`can't infer type of chunk from URL ${chunkUrl} in worker`);
} }
} else { return;
}
// TODO(PACK-2140): remove this once all filenames are guaranteed to be escaped. // TODO(PACK-2140): remove this once all filenames are guaranteed to be escaped.
const decodedChunkUrl = decodeURI(chunkUrl); const decodedChunkUrl = decodeURI(chunkUrl);
// Escape URLs for safe use in CSS selectors // Escape URLs for safe use in CSS selectors
...@@ -1965,7 +1975,7 @@ let BACKEND; ...@@ -1965,7 +1975,7 @@ let BACKEND;
link.href = chunkUrl; link.href = chunkUrl;
link.onerror = ()=>{ link.onerror = ()=>{
link.remove(); link.remove();
rejectChunkResolver(chunkUrl, resolver); retryChunkLoad(sourceType, chunkUrl, resolver);
}; };
link.onload = ()=>{ link.onload = ()=>{
// CSS chunks do not register themselves, and as such must be marked as // CSS chunks do not register themselves, and as such must be marked as
...@@ -1983,7 +1993,7 @@ let BACKEND; ...@@ -1983,7 +1993,7 @@ let BACKEND;
for (const script of Array.from(previousScripts)){ for (const script of Array.from(previousScripts)){
script.addEventListener('error', ()=>{ script.addEventListener('error', ()=>{
script.remove(); script.remove();
rejectChunkResolver(chunkUrl, resolver); retryChunkLoad(sourceType, chunkUrl, resolver);
}, { }, {
once: true once: true
}); });
...@@ -1996,7 +2006,7 @@ let BACKEND; ...@@ -1996,7 +2006,7 @@ let BACKEND;
// this branch. // this branch.
script.onerror = ()=>{ script.onerror = ()=>{
script.remove(); script.remove();
rejectChunkResolver(chunkUrl, resolver); retryChunkLoad(sourceType, chunkUrl, resolver);
}; };
// Append to the `head` for webpack compatibility. // Append to the `head` for webpack compatibility.
document.head.appendChild(script); document.head.appendChild(script);
...@@ -2005,7 +2015,30 @@ let BACKEND; ...@@ -2005,7 +2015,30 @@ let BACKEND;
throw new Error(`can't infer type of chunk from URL ${chunkUrl}`); throw new Error(`can't infer type of chunk from URL ${chunkUrl}`);
} }
} }
/**
* Loads the given chunk, and returns a promise that resolves once the chunk
* has been loaded.
*/ function doLoadChunk(sourceType, chunkUrl) {
const resolver = getOrCreateResolver(chunkUrl);
if (resolver.loadingStarted) {
return resolver.promise;
}
if (sourceType === SourceType.Runtime) {
// We don't need to load chunks references from runtime code, as they're already
// present in the DOM.
resolver.loadingStarted = true;
if (isCss(chunkUrl)) {
// CSS chunks do not register themselves, and as such must be marked as
// loaded instantly.
resolver.resolve();
}
// We need to wait for JS chunks to register themselves within `registerChunk`
// before we can start instantiating runtime modules, hence the absence of
// `resolver.resolve()` in this branch.
return resolver.promise;
}
resolver.loadingStarted = true; resolver.loadingStarted = true;
loadChunkImpl(sourceType, chunkUrl, resolver);
return resolver.promise; return resolver.promise;
} }
function fetchWebAssembly(wasmChunkPath) { function fetchWebAssembly(wasmChunkPath) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment