Skip to content

Commit

Permalink
Wdk service cache fix (#1111)
Browse files Browse the repository at this point in the history
* Convert to explicit return to make debugging easier

* Use window.fetch directly, to avoid infinite loop

* Ensure version is always included in header for _fetchJson calls

* Update packages/libs/wdk-client/src/Service/ServiceBase.ts

Co-authored-by: bobular <[email protected]>

* Use fetchWithRetry

---------

Co-authored-by: bobular <[email protected]>
  • Loading branch information
dmfalke and bobular authored Jun 13, 2024
1 parent dd14494 commit b5d5d56
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions packages/libs/wdk-client/src/Service/ServiceBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ export const ServiceBase = (serviceUrl: string) => {
return submitErrorIfNot500(error, extra);
}

function _fetchJson<T>(
async function _fetchJson<T>(
method: string,
url: string,
body?: string,
Expand All @@ -244,7 +244,7 @@ export const ServiceBase = (serviceUrl: string) => {
'Content-Type': 'application/json',
traceid: makeTraceid(),
});
if (_version) headers.append(CLIENT_WDK_VERSION_HEADER, String(_version));
headers.append(CLIENT_WDK_VERSION_HEADER, String(await _initializeStore()));
return fetchWithRetry(1, isBaseUrl ? url : serviceUrl + url, {
headers,
method: method.toUpperCase(),
Expand Down Expand Up @@ -285,14 +285,14 @@ export const ServiceBase = (serviceUrl: string) => {
_isInvalidating = true;
_store
.clear()
.then(() =>
hasRequestBeenMade
? alert(
'Reload page',
'This page is no longer valid and will be reloaded.'
)
: undefined
)
.then(() => {
if (hasRequestBeenMade) {
return alert(
'Reload page',
'This page is no longer valid and will be reloaded.'
);
}
})
.then(() => {
window.location.reload();
});
Expand Down Expand Up @@ -350,14 +350,27 @@ export const ServiceBase = (serviceUrl: string) => {
.getItem<ServiceConfig>('/__config')
.then((storeConfig) => {
if (storeConfig == null) {
return _fetchJson<ServiceConfig>('GET', '/').then((serviceConfig) => {
return _store.setItem('/__config', serviceConfig);
});
return fetchWithRetry(1, serviceUrl)
.then((response) => {
if (!response.ok) {
console.error(
`Fetching ${serviceUrl} failed for _initializeStore: ${response.statusText}`
);
throw new Error('Failed to initialize service');
}
return response.json();
})
.then((serviceConfig: ServiceConfig) => {
return _store
.setItem('/__config', serviceConfig)
.then(() => serviceConfig);
});
}
return storeConfig;
})
.then((config) => {
_version = config.startupTime;
return _version;
});
});

Expand Down

0 comments on commit b5d5d56

Please sign in to comment.