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
22 changes: 22 additions & 0 deletions packages/cache/src/fetchwithcache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,4 +327,26 @@ describe('`fetchWithCache`', () => {
mockFetch.restore()
expect(mockFetch.fulfilled).toBe(true)
})

test('Does not throw an error when response returns a 5xx error', async () => {
const mockFetch = new MockFetch()
.get({
url: 'https://example.netlify/.netlify/cache/https%3A%2F%2Fnetlify.com%2F',
response: new Response(null, { status: 404 }),
})
.get({
url: 'https://netlify.com/',
response: new Response('Internal Server Error', { status: 500 }),
})
.inject()
const resourceURL = 'https://netlify.com'

const response = await fetchWithCache(resourceURL)

expect(response.status).toBe(500)
expect(await response.text()).toBe('Internal Server Error')

mockFetch.restore()
expect(mockFetch.fulfilled).toBe(true)
})
})
5 changes: 4 additions & 1 deletion packages/cache/src/fetchwithcache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ export const fetchWithCache: FetchWithCache = async (
const cacheResponse = new Response(cacheStream, fresh)
applyHeaders(cacheResponse.headers, cacheHeaders(cacheSettings))

const cachePut = cache.put(request, cacheResponse)
const cachePut = cache.put(request, cacheResponse).catch(() => {
// If we fail to cache the response, we want to swallow the error because
// we'll still return the result of `fetch`.
})

if (onCachePut) {
await onCachePut(cachePut)
Expand Down