Skip to content

Commit

Permalink
Reload page if new version detected going to error page
Browse files Browse the repository at this point in the history
  • Loading branch information
myieye committed Oct 27, 2023
1 parent bd93f15 commit eb1d8a7
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion frontend/src/hooks.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { handleFetch } from '$lib/util/fetch-proxy';
import {invalidate} from '$app/navigation';
import {USER_LOAD_KEY} from '$lib/user';
import { updated } from '$app/stores';
import { APP_VERSION } from '$lib/util/version';

await loadI18n();

Expand All @@ -19,11 +20,19 @@ export const handleError: HandleClientError = async ({ error, event }) => {
// eslint-disable-next-line @typescript-eslint/no-empty-function
updated.subscribe(() => { })();
const updateDetected = await updated.check();
const autoReload = shouldTryAutoReload(updateDetected);

const traceId = ensureErrorIsTraced(error, { event }, {
['app.error.source']: handler,
...(updateDetected ? { ['app.update-detected']: true } : {}),
['app.update-detected']: updateDetected,
['app.auto-reload']: autoReload,
});

if (autoReload) {
location.reload();
return;
}

const message = getErrorMessage(error);
return {
traceId,
Expand All @@ -33,6 +42,22 @@ export const handleError: HandleClientError = async ({ error, event }) => {
};
};

function shouldTryAutoReload(updateDetected: boolean): boolean {
if (!updateDetected) {
return false;
}

const lastReloadVersion = sessionStorage.getItem('last-reload-version');
const currVersion = APP_VERSION;
if (!lastReloadVersion || lastReloadVersion !== currVersion) {
sessionStorage.setItem('last-reload-version', currVersion);
return true;
}

// we already tried a reload on the current version
return false;
}

/**
* This is obviously NOT a SvelteKit handler/feature. It just mimics the `handleFetch` in hooks.server.ts.
*/
Expand Down

0 comments on commit eb1d8a7

Please sign in to comment.