From 7ace102eac4d6ee134d1eb8c38d1b1d5f404edc6 Mon Sep 17 00:00:00 2001 From: Fauzan Date: Thu, 4 Dec 2025 15:29:15 +0700 Subject: [PATCH 1/2] feat: add a way to remove preload cache --- src/_internal/index.ts | 2 +- src/_internal/utils/preload.ts | 7 +++++++ src/index/index.ts | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/_internal/index.ts b/src/_internal/index.ts index dfe9a81485..2498c5344b 100644 --- a/src/_internal/index.ts +++ b/src/_internal/index.ts @@ -22,7 +22,7 @@ export { getTimestamp } from './utils/timestamp' export { useSWRConfig } from './utils/use-swr-config' export { preset, defaultConfigOptions } from './utils/web-preset' export { withMiddleware } from './utils/with-middleware' -export { preload } from './utils/preload' +export { preload, clearPreloadCache } from './utils/preload' export * from './types' diff --git a/src/_internal/utils/preload.ts b/src/_internal/utils/preload.ts index e5d47642a5..ae56c82dcd 100644 --- a/src/_internal/utils/preload.ts +++ b/src/_internal/utils/preload.ts @@ -39,6 +39,13 @@ export const preload = < return req } +export const clearPreloadCache = () => { + const globalState = SWRGlobalState.get(cache) as GlobalState + + // index 3 are PRELOAD + globalState[3] = Object.create(null) +} + export const middleware: Middleware = useSWRNext => (key_, fetcher_, config) => { // fetcher might be a sync function, so this should not be an async function diff --git a/src/index/index.ts b/src/index/index.ts index 56912273de..00f658bdb2 100644 --- a/src/index/index.ts +++ b/src/index/index.ts @@ -6,7 +6,7 @@ export { SWRConfig } from './use-swr' export { unstable_serialize } from './serialize' export { useSWRConfig } from '../_internal' export { mutate } from '../_internal' -export { preload } from '../_internal' +export { preload, clearPreloadCache } from '../_internal' // Config From 2b8319d40b3730f2f6c73f2978dd106f465d2340 Mon Sep 17 00:00:00 2001 From: Fauzan Date: Thu, 4 Dec 2025 15:31:14 +0700 Subject: [PATCH 2/2] test: add clearPreloadCache test --- test/use-swr-preload.test.tsx | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/test/use-swr-preload.test.tsx b/test/use-swr-preload.test.tsx index fb7e011cb5..2f74e7e680 100644 --- a/test/use-swr-preload.test.tsx +++ b/test/use-swr-preload.test.tsx @@ -1,6 +1,6 @@ import { act, fireEvent, screen } from '@testing-library/react' import { Suspense, useEffect, useState, Profiler } from 'react' -import useSWR, { preload, useSWRConfig } from 'swr' +import useSWR, { preload, clearPreloadCache, useSWRConfig } from 'swr' import { createKey, createResponse, @@ -224,4 +224,24 @@ describe('useSWR - preload', () => { preload(() => key, fetcher) expect(calledWith).toBe(key) }) + + it('re-call fetcher when preload cache are cleared', async () => { + const key = createKey() + + const fetcher = jest.fn(() => createResponse('foo')) + + function Page() { + const { data } = useSWR(key, fetcher) + return
data:{data}
+ } + + preload(key, fetcher) + expect(fetcher).toBeCalledTimes(1) + + clearPreloadCache() + + renderWithGlobalCache() + await screen.findByText('data:foo') + expect(fetcher).toBeCalledTimes(2) + }) })