π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[desktop] Fix yarn dev flakiness when renderer is not ready
Manav Rathia10ca66e57c5
diff --git a/desktop/src/main.ts b/desktop/src/main.tsindex 20d91ffd9a..eed889c236 100644--- a/desktop/src/main.ts+++ b/desktop/src/main.ts@@ -11,7 +11,7 @@import { nativeImage, shell } from "electron/common";import type { WebContents } from "electron/main";-import { BrowserWindow, Menu, Tray, app, protocol } from "electron/main";+import { BrowserWindow, Menu, Tray, app, net, protocol } from "electron/main";import serveNextAt from "next-electron-server";import { existsSync } from "node:fs";import fs from "node:fs/promises";@@ -30,6 +30,7 @@ import { createWatcher } from "./main/services/watch";import { userPreferences } from "./main/stores/user-preferences";import { migrateLegacyWatchStoreIfNeeded } from "./main/stores/watch";import { registerStreamProtocol } from "./main/stream";+import { wait } from "./main/utils/common";import { isDev } from "./main/utils/electron";/**@@ -112,6 +113,8 @@ const main = () => {attachProcessHandlers();void (async () => {+ if (isDev) await waitForRendererDevServer();+// Create window and prepare for the renderer.mainWindow = createMainWindow();@@ -279,6 +282,42 @@ const attachProcessHandlers = () => {process.on("SIGINT", () => app.quit());};+/**+ * Wait for the renderer process' dev server to be ready.+ *+ * After creating the main window, we load the web app into it using `loadURL`.+ * In production, these are served directly from the SSR-ed static files bundled+ * with the app, and so can be served instantly. However, during development, we+ * start a dev server for serving the HMR-ed files.+ *+ * This Next.js HMR server takes time to startup and is sometimes not ready to+ * handle incoming requests when the main window tries to load it. In such+ * cases, Electron just hangs with this:+ *+ * > [main] Error: net::ERR_CONNECTION_REFUSED+ * [main] at SimpleURLLoaderWrapper.<anonymous> (node:electron/js2c/browser_init:2:114482)+ * [main] at SimpleURLLoaderWrapper.emit (node:events:519:28)+ *+ * As a workaround, poll the URL we'll subsequently be loading, and only proceed+ * if it is responsive.+ *+ * This workaround can likely be removed when we migrate to Vite.+ */+const waitForRendererDevServer = async () => {+ // TODO: eslint has fixed this spurious warning, but we're not on the latest+ // version yet, so add a disable.+ // https://github.com/eslint/eslint/pull/18286+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition+ while (true) {+ const res = await net.fetch(rendererURL, {+ method: "HEAD",+ });+ if (res.ok) break;+ log.info("waiting for renderer to be ready...");+ await wait(1000);+ }+};+/*** Create an return the {@link BrowserWindow} that will form our app's UI.*