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
6 changes: 3 additions & 3 deletions packages/cache/src/bootstrap/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ describe('Cache API', () => {
})

test('logs a message when the response is not added to the cache', async () => {
const consoleWarn = vi.spyOn(globalThis.console, 'warn')
const logger = vi.fn()
const mockFetch = getMockFetch({
responses: {
'https://example.netlify/.netlify/cache/https%3A%2F%2Fnetlify.com%2F': [
Expand All @@ -274,6 +274,7 @@ describe('Cache API', () => {
const cache = new NetlifyCache({
base64Encode,
getContext: () => ({ host, token, url }),
logger,
name: 'my-cache',
userAgent,
})
Expand All @@ -288,8 +289,7 @@ describe('Cache API', () => {

mockFetch.restore()

expect(consoleWarn).toHaveBeenCalledWith(`Failed to write to the cache: ${ERROR_CODES.no_ttl}`)
consoleWarn.mockRestore()
expect(logger).toHaveBeenCalledWith(`Failed to write to the cache: ${ERROR_CODES.no_ttl}`)

expect(mockFetch.requests.length).toBe(1)
})
Expand Down
15 changes: 12 additions & 3 deletions packages/cache/src/bootstrap/cache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { Base64Encoder, EnvironmentOptions, RequestContext, Operation, RequestContextFactory } from './environment.js'
import {
Base64Encoder,
EnvironmentOptions,
Logger,
RequestContext,
Operation,
RequestContextFactory,
} from './environment.js'

import { ERROR_CODES, GENERIC_ERROR } from './errors.js'
import * as HEADERS from '../headers.js'
Expand All @@ -19,12 +26,14 @@ const serializeResourceHeaders = Symbol('serializeResourceHeaders')
export class NetlifyCache implements Cache {
#base64Encode: Base64Encoder
#getContext: RequestContextFactory
#logger?: Logger
#name: string
#userAgent?: string

constructor({ base64Encode, getContext, name, userAgent }: NetlifyCacheOptions) {
constructor({ base64Encode, getContext, logger, name, userAgent }: NetlifyCacheOptions) {
this.#base64Encode = base64Encode
this.#getContext = getContext
this.#logger = logger
this.#name = name
this.#userAgent = userAgent
}
Expand Down Expand Up @@ -176,7 +185,7 @@ export class NetlifyCache implements Cache {
const errorDetail = cacheResponse.headers.get(HEADERS.ErrorDetail) ?? ''
const errorMessage = ERROR_CODES[errorDetail as keyof typeof ERROR_CODES] || GENERIC_ERROR

console.warn(`Failed to write to the cache: ${errorMessage}`)
this.#logger?.(`Failed to write to the cache: ${errorMessage}`)
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions packages/cache/src/bootstrap/environment.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
export type Base64Encoder = (input: string) => string

export type Logger = (...args: any[]) => void

export interface EnvironmentOptions {
base64Encode: Base64Encoder
getContext: RequestContextFactory
logger?: Logger
userAgent?: string
}

Expand Down
Loading