diff --git a/.changeset/script-manager-resolve-failure-retry.md b/.changeset/script-manager-resolve-failure-retry.md new file mode 100644 index 000000000..a11298ff3 --- /dev/null +++ b/.changeset/script-manager-resolve-failure-retry.md @@ -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. diff --git a/packages/repack/src/modules/ScriptManager/ScriptManager.ts b/packages/repack/src/modules/ScriptManager/ScriptManager.ts index 5c4f22f24..db6dd35ee 100644 --- a/packages/repack/src/modules/ScriptManager/ScriptManager.ts +++ b/packages/repack/src/modules/ScriptManager/ScriptManager.ts @@ -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]; } } 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]; + throw error; + } try { if (this.hookMap.beforeLoad.isUsed()) { @@ -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()); diff --git a/packages/repack/src/modules/ScriptManager/__tests__/ScriptManager.test.ts b/packages/repack/src/modules/ScriptManager/__tests__/ScriptManager.test.ts index 3172a008e..2b512f29a 100644 --- a/packages/repack/src/modules/ScriptManager/__tests__/ScriptManager.test.ts +++ b/packages/repack/src/modules/ScriptManager/__tests__/ScriptManager.test.ts @@ -1050,6 +1050,134 @@ describe('ScriptManagerAPI', () => { spy.mockRestore(); }); + + it('should retry loadScript after resolver 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); + + 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() {