diff --git a/.changeset/cached-environment-before-client.md b/.changeset/cached-environment-before-client.md new file mode 100644 index 00000000000..2b97b682656 --- /dev/null +++ b/.changeset/cached-environment-before-client.md @@ -0,0 +1,5 @@ +--- +'@clerk/clerk-js': patch +--- + +Fix React Native apps that use `__experimental_resourceCache` overwriting the cached environment with an empty one when startup falls back to the cached resources. Listeners notified of the cached client now see the cached environment instead of an unloaded one, so `@clerk/expo` no longer persists an environment without instance settings, which the next offline launch would load as if it were valid. diff --git a/packages/clerk-js/src/core/__tests__/clerk.test.ts b/packages/clerk-js/src/core/__tests__/clerk.test.ts index 6042860db24..12e0e883f7b 100644 --- a/packages/clerk-js/src/core/__tests__/clerk.test.ts +++ b/packages/clerk-js/src/core/__tests__/clerk.test.ts @@ -1130,6 +1130,29 @@ describe('Clerk singleton', () => { expect(sut.status).toBe('degraded'); }); + it('applies the cached environment before notifying listeners of the cached client', async () => { + const clientInstance = { fetch: mockClientFetch, signedInSessions: [] }; + vi.mocked(Client.getOrCreateInstance) + .mockReturnValueOnce(clientInstance as any) + .mockReturnValueOnce(clientInstance as any); + mockClientFetch.mockRejectedValue( + new ClerkRuntimeError('Network request failed.', { code: 'network_error' }), + ); + + const sut = new Clerk(productionPublishableKey); + const unloadedEnvironment = sut.__internal_environment; + sut.__internal_getCachedResources = vi.fn(() => + Promise.resolve({ client: null, environment: { display_config: { id: 'display_config_cached' } } as any }), + ); + const environmentsSeenByListeners: unknown[] = []; + sut.addListener(() => environmentsSeenByListeners.push(sut.__internal_environment)); + await sut.load({ standardBrowser: false }); + + expect(sut.status).toBe('degraded'); + expect(environmentsSeenByListeners.length).toBeGreaterThan(0); + expect(environmentsSeenByListeners).not.toContain(unloadedEnvironment); + }); + describe('when the initial requests stall', () => { beforeEach(() => { vi.useFakeTimers(); diff --git a/packages/clerk-js/src/core/clerk.ts b/packages/clerk-js/src/core/clerk.ts index 83b41df7f59..8fad0e7ee23 100644 --- a/packages/clerk-js/src/core/clerk.ts +++ b/packages/clerk-js/src/core/clerk.ts @@ -3750,8 +3750,8 @@ export class Clerk implements ClerkInterface { } } - this.updateClient(client); this.updateEnvironment(environment); + this.updateClient(client); // TODO: Add an auth service also for non standard browsers that will poll for the __session JWT but won't use cookies this.#publicEventBus.emit(clerkEvents.Status, initializationDegradedCounter > 0 ? 'degraded' : 'ready'); }; @@ -3785,8 +3785,8 @@ export class Clerk implements ClerkInterface { __internal_reloadInitialResources = async (): Promise => { const [environment, client] = await this.#fetchInitialResources({ fetchMaxTries: 1, timeLimited: true }); - this.updateClient(client); this.updateEnvironment(environment); + this.updateClient(client); // A load that is still running finishes with these resources if it fails. An overlapping reload may have finished // loading first.