Files
sendou.ink/app/hooks/useReloadOnNewDeploy.ts
Kalle 0e3120bd2c
Some checks are pending
E2E Tests / e2e (push) Waiting to run
Tests and checks on push / run-checks-and-tests (push) Waiting to run
Updates translation progress / update-translation-progress-issue (push) Waiting to run
Reload page when there is an update
Closes #3185
2026-08-07 23:12:14 +03:00

34 lines
1.2 KiB
TypeScript

import * as React from "react";
import { z } from "zod";
import * as PersistedState from "~/modules/persisted-state/persisted-state";
import { GIT_COMMIT } from "~/utils/git-commit";
const reloadedForCommitPersisted = PersistedState.define({
key: "reloadedForCommit",
storage: "session",
schema: z.string(),
default: "",
});
/**
* Reloads the page when the server starts reporting a different build than the
* one this client's JavaScript was loaded from. Pages that stay open for hours
* while revalidating (SendouQ looking above all) otherwise keep running old
* code against new loader data, breaking whenever a deploy changes the shape of
* that data.
*/
export function useReloadOnNewDeploy(serverCommit: string) {
React.useEffect(() => {
if (!GIT_COMMIT || !serverCommit || serverCommit === GIT_COMMIT) return;
// the reload is expected to serve the new bundle, but should it not, the
// same build is never retried so the page can't end up in a reload loop
if (PersistedState.read(reloadedForCommitPersisted) === serverCommit) {
return;
}
PersistedState.write(reloadedForCommitPersisted, serverCommit);
window.location.reload();
}, [serverCommit]);
}