From c0aea90a3f608108d10d0306ecc165c27a78ad22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Bou=C3=A7as?= Date: Wed, 15 Oct 2025 09:27:25 +0100 Subject: [PATCH 1/2] feat: add `fetch` parameter to `fetchWithCache` --- packages/cache/src/fetchwithcache.test.ts | 21 +++++++++++++++++++++ packages/cache/src/fetchwithcache.ts | 7 +++++++ 2 files changed, 28 insertions(+) diff --git a/packages/cache/src/fetchwithcache.test.ts b/packages/cache/src/fetchwithcache.test.ts index 555c712c..165abbd3 100644 --- a/packages/cache/src/fetchwithcache.test.ts +++ b/packages/cache/src/fetchwithcache.test.ts @@ -307,4 +307,25 @@ describe('`fetchWithCache`', () => { mockFetch.restore() expect(mockFetch.fulfilled).toBe(true) }) + + test('Accepts a custom `fetch` implementation', async () => { + const mockFetch = new MockFetch() + .get({ + url: 'https://example.netlify/.netlify/cache/https%3A%2F%2Fnetlify.com%2F', + response: new Response(null, { status: 404 }), + }) + .post({ + url: 'https://example.netlify/.netlify/cache/https%3A%2F%2Fnetlify.com%2F', + response: new Response(null, { status: 201 }), + }) + .inject() + const body = 'Hello from custom fetch' + const customFetch: typeof globalThis.fetch = async () => new Response(body) + + const response = await fetchWithCache('https://netlify.com', { fetch: customFetch }) + expect(await response.text()).toBe(body) + + mockFetch.restore() + expect(mockFetch.fulfilled).toBe(true) + }) }) diff --git a/packages/cache/src/fetchwithcache.ts b/packages/cache/src/fetchwithcache.ts index 77f7b3f4..ae50c6fa 100644 --- a/packages/cache/src/fetchwithcache.ts +++ b/packages/cache/src/fetchwithcache.ts @@ -31,6 +31,11 @@ type CacheOptions = CacheSettings & { */ cache?: NetlifyCache | string + /** + * A custom `fetch` implementation to be used instead of the native one. + */ + fetch?: typeof globalThis.fetch + /** * When `fetchWithCache` fetches a new response and adds it to the cache, the * `Promise` it returns waits for both the network call to finish and for the @@ -115,6 +120,8 @@ export const fetchWithCache: FetchWithCache = async ( return cached } + const { fetch = globalThis.fetch } = cacheOptions + const fresh = await fetch(request) if (!fresh.body) { return fresh From d836a04b514a269d136c7e45b6df23ec7a59524d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Bou=C3=A7as?= Date: Wed, 15 Oct 2025 09:33:37 +0100 Subject: [PATCH 2/2] chore: update test --- packages/cache/src/fetchwithcache.test.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/cache/src/fetchwithcache.test.ts b/packages/cache/src/fetchwithcache.test.ts index 165abbd3..4062fe48 100644 --- a/packages/cache/src/fetchwithcache.test.ts +++ b/packages/cache/src/fetchwithcache.test.ts @@ -319,11 +319,10 @@ describe('`fetchWithCache`', () => { response: new Response(null, { status: 201 }), }) .inject() - const body = 'Hello from custom fetch' - const customFetch: typeof globalThis.fetch = async () => new Response(body) + const customFetch: typeof globalThis.fetch = async () => new Response('rijwiel') const response = await fetchWithCache('https://netlify.com', { fetch: customFetch }) - expect(await response.text()).toBe(body) + expect(await response.text()).toBe('rijwiel') mockFetch.restore() expect(mockFetch.fulfilled).toBe(true)