Skip to content
5 changes: 5 additions & 0 deletions .changeset/script-manager-resolve-failure-retry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@callstack/repack": patch
---

Fix `ScriptManager.loadScript` and `ScriptManager.prefetchScript` caching a failed resolution forever. `resolveScript` was awaited outside the `try`/`finally` that clears `scriptsPromises`, so when a resolver threw (e.g. the device was offline while fetching the script URL), the rejected promise stayed cached and every later call for the same script returned that same error without calling the resolvers again, until the app was restarted. The rejected promise is now removed when resolving fails, so the next call retries.
44 changes: 31 additions & 13 deletions packages/repack/src/modules/ScriptManager/ScriptManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,19 +576,29 @@ export class ScriptManager extends EventEmitter {

// prefetch is not execute the script so we need to run loadScript if promise is for prefetch
if (isPrefetch) {
await this.scriptsPromises[uniqueId];
// prefetch only warms up the native cache, so its failure must not
// fail this load - wait for it to settle and load the script anyway
await this.scriptsPromises[uniqueId].catch(() => undefined);
} else {
return this.scriptsPromises[uniqueId];
}
}
Comment thread
dannyhw marked this conversation as resolved.
Resolved

const loadProcess = async () => {
let script = await this.resolveScript(
options.scriptId,
options.caller,
options.webpackContext,
options.referenceUrl
);
let script: Script;
try {
script = await this.resolveScript(
options.scriptId,
options.caller,
options.webpackContext,
options.referenceUrl
);
} catch (error) {
// resolving failed - remove the rejected promise so that
// subsequent calls can retry instead of reusing this failure
delete this.scriptsPromises[uniqueId];
Comment thread
dannyhw marked this conversation as resolved.
Resolved
throw error;
}

try {
if (this.hookMap.beforeLoad.isUsed()) {
Expand Down Expand Up @@ -708,12 +718,20 @@ export class ScriptManager extends EventEmitter {
return this.scriptsPromises[uniqueId];
}
const loadProcess = async () => {
const script = await this.resolveScript(
scriptId,
caller,
webpackContext,
referenceUrl
);
let script: Script;
try {
script = await this.resolveScript(
scriptId,
caller,
webpackContext,
referenceUrl
);
} catch (error) {
// resolving failed - remove the rejected promise so that
// subsequent calls can retry instead of reusing this failure
delete this.scriptsPromises[uniqueId];
throw error;
}

try {
this.emit('prefetching', script.toObject());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,134 @@ describe('ScriptManagerAPI', () => {

spy.mockRestore();
});

it('should retry loadScript after resolver failure', async () => {
Comment thread
dannyhw marked this conversation as resolved.
Resolved
const spy = mockLoadScriptBasedOnFetch();

const resolver = jest
.fn()
.mockRejectedValueOnce(new Error('Network request failed'))
.mockImplementation(async (scriptId: string) => ({
url: Script.getRemoteURL(scriptId),
}));
ScriptManager.shared.addResolver(resolver);

await expect(ScriptManager.shared.loadScript('miniApp')).rejects.toThrow(
'Network request failed'
);

// should not reuse the rejected promise from the first call
await expect(
ScriptManager.shared.loadScript('miniApp')
).resolves.toBeUndefined();
expect(resolver).toHaveBeenCalledTimes(2);
expect(spy).toHaveBeenCalledTimes(1);

spy.mockRestore();
});

it('should retry prefetchScript after resolver failure', async () => {
const spy = jest.spyOn(NativeScriptManager, 'prefetchScript');
spy.mockResolvedValue(null);

const resolver = jest
.fn()
.mockRejectedValueOnce(new Error('Network request failed'))
.mockImplementation(async (scriptId: string) => ({
url: Script.getRemoteURL(scriptId),
}));
ScriptManager.shared.addResolver(resolver);

await expect(
ScriptManager.shared.prefetchScript('miniApp')
).rejects.toThrow('Network request failed');

// should not reuse the rejected promise from the first call
await expect(
ScriptManager.shared.prefetchScript('miniApp')
).resolves.toBeUndefined();
expect(resolver).toHaveBeenCalledTimes(2);
expect(spy).toHaveBeenCalledTimes(1);

spy.mockRestore();
});
it('should share a single resolution between concurrent loadScript calls and retry after failure', async () => {
const spy = mockLoadScriptBasedOnFetch();

const resolver = jest
.fn()
.mockRejectedValueOnce(new Error('Network request failed'))
.mockImplementation(async (scriptId: string) => ({
url: Script.getRemoteURL(scriptId),
}));
ScriptManager.shared.addResolver(resolver);

const results = await Promise.allSettled([
ScriptManager.shared.loadScript('miniApp'),
ScriptManager.shared.loadScript('miniApp'),
]);
expect(results.map((result) => result.status)).toEqual([
'rejected',
'rejected',
]);
// concurrent calls are still deduplicated
expect(resolver).toHaveBeenCalledTimes(1);

await expect(
ScriptManager.shared.loadScript('miniApp')
).resolves.toBeUndefined();
expect(resolver).toHaveBeenCalledTimes(2);
expect(spy).toHaveBeenCalledTimes(1);

spy.mockRestore();
});

it('should load script when a pending prefetch fails to resolve', async () => {
const spy = mockLoadScriptBasedOnFetch();
const prefetchSpy = jest.spyOn(NativeScriptManager, 'prefetchScript');

const resolver = jest
.fn()
.mockRejectedValueOnce(new Error('Network request failed'))
.mockImplementation(async (scriptId: string) => ({
url: Script.getRemoteURL(scriptId),
}));
ScriptManager.shared.addResolver(resolver);

const prefetch = ScriptManager.shared.prefetchScript('miniApp');
// loadScript is called while the prefetch is still resolving
const load = ScriptManager.shared.loadScript('miniApp');

await expect(prefetch).rejects.toThrow('Network request failed');
// prefetch failure should not fail the load - it resolves on its own
await expect(load).resolves.toBeUndefined();
expect(resolver).toHaveBeenCalledTimes(2);
expect(prefetchSpy).not.toHaveBeenCalled();
expect(spy).toHaveBeenCalledTimes(1);

prefetchSpy.mockRestore();
spy.mockRestore();
});

it('should load script when a pending prefetch fails to download', async () => {
const spy = mockLoadScriptBasedOnFetch();
const prefetchSpy = jest.spyOn(NativeScriptManager, 'prefetchScript');
prefetchSpy.mockRejectedValueOnce({ code: 'NetworkFailure' });

ScriptManager.shared.addResolver(async (scriptId) => ({
url: Script.getRemoteURL(scriptId),
}));

const prefetch = ScriptManager.shared.prefetchScript('miniApp');
const load = ScriptManager.shared.loadScript('miniApp');

await expect(prefetch).rejects.toBeDefined();
await expect(load).resolves.toBeUndefined();
expect(spy).toHaveBeenCalledTimes(1);

prefetchSpy.mockRestore();
spy.mockRestore();
});
});

function mockLoadScriptBasedOnFetch() {
Expand Down