Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/cache/src/fetchwithcache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { ReadableStream } from 'node:stream/web'
import { describe, test, expect, beforeAll, afterAll } from 'vitest'

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

let originalCaches = globalThis.caches

beforeAll(() => {
let fetchWithCache: FetchWithCache

beforeAll(async () => {
globalThis.caches = new NetlifyCacheStorage({
base64Encode,
getContext: () => ({ host, token, url }),
})

// Using a dynamic import so that `globalThis.caches` is populated by the
// time the polyfill is loaded.
fetchWithCache = (await import('./fetchwithcache.js')).fetchWithCache
})

afterAll(() => {
Expand Down
3 changes: 2 additions & 1 deletion packages/cache/src/fetchwithcache.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { NetlifyCache } from './bootstrap/cache.js'
import { setCacheHeaders } from './cache-headers/cache-headers.js'
import type { CacheSettings } from './cache-headers/options.js'
import { caches } from './polyfill.js'

const requestInitOptions = [
'method',
Expand Down Expand Up @@ -51,7 +52,7 @@ const isRequestInit = (input: any): input is RequestInit => {
return false
}

type FetchWithCache = {
export type FetchWithCache = {
(request: string | URL | Request, init?: RequestInit): Promise<Response>
(request: string | URL | Request, cacheSettings?: CacheOptions): Promise<Response>
(request: string | URL | Request, init: RequestInit, cacheSettings?: CacheOptions): Promise<Response>
Expand Down
38 changes: 38 additions & 0 deletions packages/cache/src/main.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Buffer } from 'node:buffer'

import { describe, test, expect } from 'vitest'

import { NetlifyCacheStorage } from './bootstrap/cachestorage.js'
import { caches } from './main.js'
import { getMockFetch } from './test/fetch.js'

const base64Encode = (input: string) => Buffer.from(input, 'utf8').toString('base64')
const host = 'host.netlify'
const url = 'https://example.netlify/.netlify/cache'
const token = 'mock-token'

describe('`caches` export', () => {
test('Provides a no-op cache implementation', async () => {
const html = '<h1>Hello world</h1>'
const mockFetch = getMockFetch({
responses: {
'https://example.netlify/.netlify/cache/https%3A%2F%2Fnetlify.com%2F': [new Response(html)],
},
})
const cache1 = await caches.open('cache1')
const res1 = await cache1.match('https://netlify.com')
expect(res1).toBeUndefined()

globalThis.caches = new NetlifyCacheStorage({
base64Encode,
getContext: () => ({ host, token, url }),
})

const cache2 = await globalThis.caches.open('cache2')
const res2 = await cache2.match('https://netlify.com')
expect(res2?.status).toBe(200)
expect(await res2?.text()).toBe(html)

mockFetch.restore()
})
})
1 change: 1 addition & 0 deletions packages/cache/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { caches } from './polyfill.js'
export { setCacheHeaders } from './cache-headers/cache-headers.js'
export { getCacheStatus } from './cache-status/cache-status.js'
export { fetchWithCache } from './fetchwithcache.js'
Expand Down
13 changes: 13 additions & 0 deletions packages/cache/src/polyfill.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { NetlifyCacheStorage } from './bootstrap/cachestorage.js'

/**
* Polyfill for local development environments where `globalThis.caches` is not
* available. This is a no-op cache, which will automatically work with the
* real one in production.
*/
export const caches =
globalThis.caches ??
new NetlifyCacheStorage({
base64Encode: () => '',
getContext: () => null,
})
Loading