diff --git a/superset-frontend/packages/superset-ui-core/src/connection/callApi/callApi.ts b/superset-frontend/packages/superset-ui-core/src/connection/callApi/callApi.ts index f50b24127e7a6..7c3fe21fdb8a4 100644 --- a/superset-frontend/packages/superset-ui-core/src/connection/callApi/callApi.ts +++ b/superset-frontend/packages/superset-ui-core/src/connection/callApi/callApi.ts @@ -96,25 +96,34 @@ export default async function callApi({ CACHE_AVAILABLE && (window.location && window.location.protocol) === 'https:' ) { - const supersetCache = await caches.open(CACHE_KEY); - const cachedResponse = await supersetCache.match(url); - if (cachedResponse) { - // if we have a cached response, send its ETag in the - // `If-None-Match` header in a conditional request - const etag = cachedResponse.headers.get('Etag') as string; - request.headers = { ...request.headers, 'If-None-Match': etag }; + let supersetCache: Cache | null = null; + try { + supersetCache = await caches.open(CACHE_KEY); + const cachedResponse = await supersetCache.match(url); + if (cachedResponse) { + // if we have a cached response, send its ETag in the + // `If-None-Match` header in a conditional request + const etag = cachedResponse.headers.get('Etag') as string; + request.headers = { ...request.headers, 'If-None-Match': etag }; + } + } catch { + // If superset is in an iframe and third-party cookies are disabled, caches.open throws } const response = await fetchWithRetry(url, request); - if (response.status === HTTP_STATUS_NOT_MODIFIED) { + if (supersetCache && response.status === HTTP_STATUS_NOT_MODIFIED) { const cachedFullResponse = await supersetCache.match(url); if (cachedFullResponse) { return cachedFullResponse.clone(); } throw new Error('Received 304 but no content is cached!'); } - if (response.status === HTTP_STATUS_OK && response.headers.get('Etag')) { + if ( + supersetCache && + response.status === HTTP_STATUS_OK && + response.headers.get('Etag') + ) { supersetCache.delete(url); supersetCache.put(url, response.clone()); } diff --git a/superset-frontend/packages/superset-ui-core/src/query/api/v1/makeApi.ts b/superset-frontend/packages/superset-ui-core/src/query/api/v1/makeApi.ts index f286d93f4ea81..b7229cc98006b 100644 --- a/superset-frontend/packages/superset-ui-core/src/query/api/v1/makeApi.ts +++ b/superset-frontend/packages/superset-ui-core/src/query/api/v1/makeApi.ts @@ -68,23 +68,26 @@ export default function makeApi< Payload = SupersetPayload, Result = JsonObject, T extends ParseMethod = ParseMethod, ->({ - endpoint, - method, - requestType: requestType_, - responseType, - processResponse, - ...requestOptions -}: SupersetApiFactoryOptions & { - /** - * How to parse response, choose from: 'json' | 'text' | 'raw'. - */ - responseType?: T; - /** - * Further process parsed response - */ - processResponse?(result: ParsedResponseType): Result; -}) { +>( + options: SupersetApiFactoryOptions & { + /** + * How to parse response, choose from: 'json' | 'text' | 'raw'. + */ + responseType?: T; + /** + * Further process parsed response + */ + processResponse?(result: ParsedResponseType): Result; + }, +) { + const { + endpoint, + method, + requestType: requestType_, + responseType, + processResponse, + ...requestOptions + } = options; // use `search` payload (searchParams) when it's a GET request const requestType = requestType_ || (isPayloadless(method) ? 'search' : 'json');