diff --git a/packages/hydrogen/src/storefront.ts b/packages/hydrogen/src/storefront.ts index a390ed769e..a8111a9795 100644 --- a/packages/hydrogen/src/storefront.ts +++ b/packages/hydrogen/src/storefront.ts @@ -173,6 +173,8 @@ type HydrogenClientProps = { i18n?: TI18n; /** Whether it should print GraphQL errors automatically. Defaults to true */ logErrors?: boolean | ((error?: Error) => boolean); + /** Allows for the cache to be disabled globally. Defaults to false */ + disableCache?: boolean; }; export type CreateStorefrontClientOptions = @@ -218,6 +220,7 @@ export function createStorefrontClient( i18n, storefrontId, logErrors = true, + disableCache = false, ...clientOptions } = options; const H2_PREFIX_WARN = '[h2:warn:createStorefrontClient] '; @@ -324,7 +327,7 @@ export function createStorefrontClient( const [body, response] = await fetchWithServerCache(url, requestInit, { cacheInstance: mutation ? undefined : cache, - cache: cacheOptions || CacheDefault(), + cache: disableCache ? CacheNone() : cacheOptions || CacheDefault(), cacheKey, shouldCacheResponse: checkGraphQLErrors, waitUntil, diff --git a/templates/skeleton/server.ts b/templates/skeleton/server.ts index ff1ce2ba87..8604560609 100644 --- a/templates/skeleton/server.ts +++ b/templates/skeleton/server.ts @@ -40,6 +40,12 @@ export default { AppSession.init(request, [env.SESSION_SECRET]), ]); + /** + * Example implemention of cache supression. + */ + const { searchParams } = new URL(request.url); + const disableCache = searchParams.get('disable-cache') === 'true'; + /** * Create Hydrogen's Storefront client. */ @@ -52,6 +58,7 @@ export default { storeDomain: env.PUBLIC_STORE_DOMAIN, storefrontId: env.PUBLIC_STOREFRONT_ID, storefrontHeaders: getStorefrontHeaders(request), + disableCache, }); /**