From 83b2ee673cf30734060708b1b0efd94cbd16898e Mon Sep 17 00:00:00 2001 From: Aaron Hogue Date: Thu, 3 Dec 2026 11:49:57 -0500 Subject: [PATCH 1/2] fix: release previous exports after a hot update Each module version kept the previous version's exports on `hot.data`, so every version of a refresh boundary stayed reachable for the life of the page. Co-Authored-By: Claude Fable 5.2 --- client/refreshUtils.js | 3 +++ test/refreshUtils.spec.ts | 52 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/client/refreshUtils.js b/client/refreshUtils.js index fdbbfee..18e32d5 100644 --- a/client/refreshUtils.js +++ b/client/refreshUtils.js @@ -216,6 +216,9 @@ function executeRuntime(moduleExports, moduleId, hot, isTest) { let prevExports; if (isHotUpdate) { prevExports = hot.data.prevExports; + // `hot.data` lives as long as this module version does. Release the previous + // exports so that module versions do not keep each other alive. + delete hot.data.prevExports; } if (isReactRefreshBoundary(moduleExports)) { diff --git a/test/refreshUtils.spec.ts b/test/refreshUtils.spec.ts index a04c5ec..6745a08 100644 --- a/test/refreshUtils.spec.ts +++ b/test/refreshUtils.spec.ts @@ -1,9 +1,13 @@ import { performReactRefresh } from 'react-refresh/runtime'; import { rs } from 'rstack/test'; -import { enqueueUpdate } from '../client/refreshUtils.js'; +import { enqueueUpdate, executeRuntime } from '../client/refreshUtils.js'; rs.mock('react-refresh/runtime', () => ({ + getFamilyByType: (type: unknown) => + typeof type === 'function' ? type : undefined, + isLikelyComponentType: (type: unknown) => typeof type === 'function', performReactRefresh: rs.fn(), + register: () => {}, })); afterEach(() => { @@ -36,3 +40,49 @@ it.each([true, false])( expect(calls).toEqual([...expected, 'refresh']); }, ); + +it('releases the previous exports once a hot update has compared them', () => { + rs.useFakeTimers(); + const runVersion = ( + moduleExports: Record, + data?: Record, + ) => { + const disposeHandlers: Array<(data: Record) => void> = []; + const hot = { + data, + accept: () => {}, + dispose: (handler: (data: Record) => void) => { + disposeHandlers.push(handler); + }, + invalidate: rs.fn(), + }; + executeRuntime(moduleExports, 'module-id', hot); + const dispose = () => { + const nextData = {}; + for (const handler of disposeHandlers) { + handler(nextData); + } + return nextData; + }; + return { hot, dispose }; + }; + + const Component = () => null; + const firstExports = { default: Component }; + const first = runVersion(firstExports); + + // Same boundary signature: the update is accepted and the old exports are released. + const second = runVersion({ default: Component }, first.dispose()); + expect(second.hot.invalidate).not.toHaveBeenCalled(); + expect(Object.values(second.hot.data ?? {})).not.toContain(firstExports); + + // The previous exports are still read before they are released. + const third = runVersion( + { default: Component, Other: () => null }, + second.dispose(), + ); + expect(third.hot.invalidate).toHaveBeenCalledTimes(1); + + rs.advanceTimersByTime(30); + expect(performReactRefresh).toHaveBeenCalledTimes(1); +}); From d9b8717d0610798297792e6079ce6b781b01d7c2 Mon Sep 17 00:00:00 2001 From: Aaron Hogue Date: Thu, 3 Dec 2026 12:08:42 -0500 Subject: [PATCH 2/2] test: cover release on every hot-update path Assert that `hot.data` is empty after an accepted update, an invalidated boundary and a module that stops being a boundary, and flush the refresh timer before the assertions. Reword the comment on the release. Co-Authored-By: Claude Fable 5.2 --- client/refreshUtils.js | 4 ++-- test/refreshUtils.spec.ts | 13 +++++++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/client/refreshUtils.js b/client/refreshUtils.js index 18e32d5..6c1db7e 100644 --- a/client/refreshUtils.js +++ b/client/refreshUtils.js @@ -216,8 +216,8 @@ function executeRuntime(moduleExports, moduleId, hot, isTest) { let prevExports; if (isHotUpdate) { prevExports = hot.data.prevExports; - // `hot.data` lives as long as this module version does. Release the previous - // exports so that module versions do not keep each other alive. + // `hot.data` stays reachable until this module is next disposed. Release the + // previous exports so that each version does not retain the one before it. delete hot.data.prevExports; } diff --git a/test/refreshUtils.spec.ts b/test/refreshUtils.spec.ts index 6745a08..fb094ce 100644 --- a/test/refreshUtils.spec.ts +++ b/test/refreshUtils.spec.ts @@ -73,16 +73,21 @@ it('releases the previous exports once a hot update has compared them', () => { // Same boundary signature: the update is accepted and the old exports are released. const second = runVersion({ default: Component }, first.dispose()); + rs.advanceTimersByTime(30); expect(second.hot.invalidate).not.toHaveBeenCalled(); - expect(Object.values(second.hot.data ?? {})).not.toContain(firstExports); + expect(performReactRefresh).toHaveBeenCalledTimes(1); + expect(second.hot.data).toEqual({}); - // The previous exports are still read before they are released. + // Changed boundary signature: the previous exports are compared, then released. const third = runVersion( { default: Component, Other: () => null }, second.dispose(), ); expect(third.hot.invalidate).toHaveBeenCalledTimes(1); + expect(third.hot.data).toEqual({}); - rs.advanceTimersByTime(30); - expect(performReactRefresh).toHaveBeenCalledTimes(1); + // No longer a boundary: the previous exports still invalidate, then are released. + const fourth = runVersion({ value: 1 }, third.dispose()); + expect(fourth.hot.invalidate).toHaveBeenCalledTimes(1); + expect(fourth.hot.data).toEqual({}); });