Skip to content

Commit a129b12

Browse files
committed
fix: skip URL sync in Electron to prevent page reload errors
In Electron with file:// protocol, updating the browser URL to /workspace/abc would cause page.reload() to fail with ERR_FILE_NOT_FOUND. The MemoryRouter state is sufficient for in-app navigation; only browser/server mode needs the URL bar to reflect current state.
1 parent 03528a7 commit a129b12

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/browser/hooks/useRouterUrlSync.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useRef } from "react";
1+
import { useEffect } from "react";
22
import { useLocation, useNavigate, type NavigateFunction } from "react-router-dom";
33
import { readPersistedState } from "./usePersistedState";
44
import { SELECTED_WORKSPACE_KEY } from "@/common/constants/storage";
@@ -49,19 +49,24 @@ export function parseInitialUrl(): string {
4949
* Syncs React Router's MemoryRouter state with the browser URL.
5050
* Uses replaceState to update URL without adding to history.
5151
*
52-
* This enables proper URLs while working with Electron's file:// protocol
53-
* in production, which doesn't support HTML5 pushState.
52+
* In browser/server mode, this enables proper URLs that survive refresh.
53+
* In Electron (file:// protocol), we skip URL sync since it would break page reload.
5454
*/
5555
export function useRouterUrlSync(): void {
5656
const location = useLocation();
57-
const initialSyncDone = useRef(false);
5857

5958
useEffect(() => {
6059
// Skip sync in Storybook iframe to avoid test runner issues
6160
if (typeof window !== "undefined" && window.location.pathname.endsWith("iframe.html")) {
6261
return;
6362
}
6463

64+
// Skip sync in Electron (file:// protocol) - updating URL would break page reload
65+
// since Electron would try to load /workspace/abc as a file instead of index.html
66+
if (window.location.protocol === "file:") {
67+
return;
68+
}
69+
6570
// Build the URL from router location
6671
const url = location.pathname + location.search;
6772

@@ -70,8 +75,6 @@ export function useRouterUrlSync(): void {
7075
if (url !== currentUrl) {
7176
window.history.replaceState(null, "", url);
7277
}
73-
74-
initialSyncDone.current = true;
7578
}, [location.pathname, location.search]);
7679
}
7780

0 commit comments

Comments
 (0)