Skip to content

Commit 3f5f7ff

Browse files
authored
Prevent window from being refreshed (#144)
1 parent 020f1e2 commit 3f5f7ff

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

main/main-window.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ class MainWindow {
140140
return { action: "deny" };
141141
});
142142

143+
// We're intentially preventing the browser from handling unload events (e.g. close, refresh)
144+
// so we have to handle closing the window manually.
145+
this.window.on("close", () => {
146+
this.window?.destroy();
147+
});
148+
143149
this.window.on("closed", () => {
144150
this.window = null;
145151
const [promise, resolve] = PromiseUtils.split<BrowserWindow>();

renderer/pages/_app.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ChakraProvider } from "@chakra-ui/react";
22
import { AppProps } from "next/app";
33
import Head from "next/head";
4+
import { useEffect } from "react";
45
import { useIsClient } from "usehooks-ts";
56

67
import { ErrorBoundary } from "@/components/ErrorBoundary/ErrorBoundary";
@@ -51,6 +52,17 @@ const systemColorModeManager = new SystemColorModeManager();
5152
export default function MyApp({ Component, pageProps }: AppProps) {
5253
const isClient = useIsClient();
5354

55+
useEffect(() => {
56+
// Prevent window from unloading (i.e. closing or being reloaded). This is due to dynamic routes not
57+
// being statically generated, which causes the app to 404 if the user reloads on a dynamic route.
58+
// Closing the window is handled by the main process, which listens for the "close" event and destroys
59+
// the window.
60+
window.addEventListener("beforeunload", (e) => {
61+
e.preventDefault();
62+
e.returnValue = true;
63+
});
64+
}, []);
65+
5466
if (!isClient) {
5567
return null;
5668
}

0 commit comments

Comments
 (0)