From 8398ae14e0df7adf8ac3feec7f838f8653abccde Mon Sep 17 00:00:00 2001 From: Pixel Date: Thu, 1 Oct 2026 03:01:56 +0300 Subject: [PATCH] fix: load state when the URL hash changes --- e2e-tests/persistence.test.ts | 29 +++++++++++++++++++++++++++++ src/hooks/use-explorer.ts | 4 ++++ 2 files changed, 33 insertions(+) diff --git a/e2e-tests/persistence.test.ts b/e2e-tests/persistence.test.ts index d763851c..fc41af44 100644 --- a/e2e-tests/persistence.test.ts +++ b/e2e-tests/persistence.test.ts @@ -183,3 +183,32 @@ test("should patch legacy Markdown options without math", async ({ page }) => { page.getByRole("switch", { exact: true, name: "Math" }), ).toHaveAttribute("aria-checked", "false"); }); + +test("should load state when the URL hash changes without a page reload", async ({ + page, +}) => { + await page.goto("/"); + + const codeEditor = page.getByRole("textbox", { + name: "Code Editor", + exact: true, + }); + + const firstCode = "const first = 1;"; + await codeEditor.fill(firstCode); + await expect.poll(() => getPersistedJavaScriptCode(page)).toBe(firstCode); + const firstHash = new URL(page.url()).hash; + + const secondCode = "const second = 2;"; + await codeEditor.fill(secondCode); + await expect.poll(() => getPersistedJavaScriptCode(page)).toBe(secondCode); + + await page.goto(`/${firstHash}`); + await expect(codeEditor).toContainText(firstCode); + + await page.goBack(); + await expect(codeEditor).toContainText(secondCode); + + await page.goForward(); + await expect(codeEditor).toContainText(firstCode); +}); diff --git a/src/hooks/use-explorer.ts b/src/hooks/use-explorer.ts index e34621a3..f540487c 100644 --- a/src/hooks/use-explorer.ts +++ b/src/hooks/use-explorer.ts @@ -300,3 +300,7 @@ export const useExplorer = create()( ), ), ); + +window.addEventListener("hashchange", () => { + useExplorer.persist.rehydrate(); +});