From 6186dbbe9c295f9dc04391fed8e057431972e96b Mon Sep 17 00:00:00 2001 From: harrismcc Date: Thu, 21 Nov 2024 22:38:33 -0800 Subject: [PATCH] Add new config flag to disable fetch `cache` option --- packages/js/src/fetchClient.ts | 6 ++++-- packages/js/src/httpClient.ts | 9 ++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/js/src/fetchClient.ts b/packages/js/src/fetchClient.ts index 8f3d0b3..674f2c2 100644 --- a/packages/js/src/fetchClient.ts +++ b/packages/js/src/fetchClient.ts @@ -2,7 +2,9 @@ import fetchRetry from 'fetch-retry'; import { parseLimitFromResponse, Limit, LimitType } from './limit.js'; export class FetchClient { - constructor(public config: { headers: HeadersInit; baseUrl: string; timeout: number }) {} + constructor( + public config: { headers: HeadersInit; baseUrl: string; timeout: number; disableFetchCashNoStore: boolean }, + ) {} async doReq( endpoint: string, @@ -29,7 +31,7 @@ export class FetchClient { method, body: init.body ? init.body : undefined, signal: AbortSignal.timeout(timeout), - cache: 'no-cache', + ...(this.config.disableFetchCacheNoStore ? {} : { cache: 'no-cache' }), }); if (resp.status === 204) { diff --git a/packages/js/src/httpClient.ts b/packages/js/src/httpClient.ts index 34ede08..6bfa422 100644 --- a/packages/js/src/httpClient.ts +++ b/packages/js/src/httpClient.ts @@ -31,13 +31,19 @@ export interface ClientOptions { * need to change this unless you are using a self-hosted version of Axiom. */ url?: string; + /** + * Disables the cache for the request, defaults to false. + * Fixes Cloudflare compatibility issue: + * https://developers.cloudflare.com/workers/configuration/compatibility-flags/#enable-cache-no-store-http-standard-api + */ + disableFetchCacheNoStore?: boolean; onError?: (error: Error) => void; } export default abstract class HTTPClient { protected readonly client: FetchClient; - constructor({ orgId = '', token, url }: ClientOptions) { + constructor({ orgId = '', token, url, disableFetchCacheNoStore }: ClientOptions) { if (!token) { console.warn('Missing Axiom token'); } @@ -60,6 +66,7 @@ export default abstract class HTTPClient { baseUrl, headers, timeout: 20_000, + disableFetchCacheNoStore, }); } }