Skip to content

Commit 1964a9f

Browse files
committed
feat: update fetchWithCache
1 parent b61b923 commit 1964a9f

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

packages/cache/src/fetchwithcache.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { ReadableStream } from 'node:stream/web'
55
import { describe, test, expect, beforeAll, afterAll } from 'vitest'
66

77
import { NetlifyCacheStorage } from './bootstrap/cachestorage.js'
8-
import { fetchWithCache } from './fetchwithcache.js'
8+
import type { FetchWithCache } from './fetchwithcache.js'
99
import { getMockFetch } from './test/fetch.js'
1010
import { readAsString, sleep } from './test/util.js'
1111
import { decodeHeaders } from './test/headers.js'
@@ -17,11 +17,17 @@ const token = 'mock-token'
1717

1818
let originalCaches = globalThis.caches
1919

20-
beforeAll(() => {
20+
let fetchWithCache: FetchWithCache
21+
22+
beforeAll(async () => {
2123
globalThis.caches = new NetlifyCacheStorage({
2224
base64Encode,
2325
getContext: () => ({ host, token, url }),
2426
})
27+
28+
// Using a dynamic import so that `globalThis.caches` is populated by the
29+
// time the polyfill is loaded.
30+
fetchWithCache = (await import('./fetchwithcache.js')).fetchWithCache
2531
})
2632

2733
afterAll(() => {

packages/cache/src/fetchwithcache.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { NetlifyCache } from './bootstrap/cache.js'
22
import { setCacheHeaders } from './cache-headers/cache-headers.js'
33
import type { CacheSettings } from './cache-headers/options.js'
4+
import { caches } from './polyfill.js'
45

56
const requestInitOptions = [
67
'method',
@@ -51,7 +52,7 @@ const isRequestInit = (input: any): input is RequestInit => {
5152
return false
5253
}
5354

54-
type FetchWithCache = {
55+
export type FetchWithCache = {
5556
(request: string | URL | Request, init?: RequestInit): Promise<Response>
5657
(request: string | URL | Request, cacheSettings?: CacheOptions): Promise<Response>
5758
(request: string | URL | Request, init: RequestInit, cacheSettings?: CacheOptions): Promise<Response>

0 commit comments

Comments
 (0)