Skip to content

Commit

Permalink
fix(embedded): third party cookies (apache#20019)
Browse files Browse the repository at this point in the history
* debugging

* logging

* add comment

* remove logging

* Update superset-frontend/packages/superset-ui-core/src/connection/callApi/callApi.ts

Co-authored-by: David Aaron Suddjian <1858430+suddjian@users.noreply.github.com>

Co-authored-by: David Aaron Suddjian <aasuddjian@gmail.com>
Co-authored-by: David Aaron Suddjian <1858430+suddjian@users.noreply.github.com>
  • Loading branch information
3 people authored and philipher29 committed Jun 9, 2022
1 parent bfa8109 commit 553db25
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>): Result;
}) {
>(
options: SupersetApiFactoryOptions & {
/**
* How to parse response, choose from: 'json' | 'text' | 'raw'.
*/
responseType?: T;
/**
* Further process parsed response
*/
processResponse?(result: ParsedResponseType<T>): 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');
Expand Down

0 comments on commit 553db25

Please sign in to comment.