Skip to content

Lesson 2 of 9 · 20m

Windows, Navigation, and Lifecycle Discipline

Window creation, navigation policy, process cleanup, and platform lifecycle quirks are where desktop apps stop feeling like web pages.

A browser tab dies when the user closes it. A desktop app has a lifetime: windows can be hidden rather than closed, the app can keep running in a tray, and closing the last window means different things on macOS and Windows. Teams that ignore this ship apps that leak windows, reopen zombie state, or terminate background work mid-write.

Create windows with intent

const win = new BrowserWindow({
  width: 1280, height: 800, show: false, autoHideMenuBar: true,
  webPreferences: {
    preload: path.join(__dirname, "preload.js"),
    contextIsolation: true, sandbox: true, nodeIntegration: false,
  },
});
win.once("ready-to-show", () => win.show());

show: false plus ready-to-show avoids the white flash and half-rendered UI users mistake for a crash. More importantly, the security posture is declared at creation time. You do not get to retrofit it after content has already executed.

A renderer should not wander wherever a link points. Use setWindowOpenHandler and will-navigate deliberately:

win.webContents.setWindowOpenHandler(({ url }) => {
  if (url.startsWith("https://docs.sudosecurity.org/")) shell.openExternal(url);
  return { action: "deny" };
});
win.webContents.on("will-navigate", (event, url) => {
  if (new URL(url).origin !== "https://app.sudosecurity.org") event.preventDefault();
});

The rule of thumb: app-controlled UI may navigate inside the app; everything external goes to the user's real browser or is denied. Loading arbitrary third-party pages inside your privileged application shell turns every victim page into part of your attack surface.

macOS is allowed to outlive its windows

On macOS, users expect the app to remain active after windows close; Windows and Linux users usually expect quit. Implement the platform distinction explicitly:

app.on("window-all-closed", () => {
  if (process.platform !== "darwin") app.quit();
});

app.on("activate", () => {
  if (BrowserWindow.getAllWindows().length === 0) createMainWindow();
});

Also decide what close means for long-running work. A sync client often hides to tray; a document editor warns on unsaved changes; a terminal asks before killing active sessions. There is no universal correct answer — only dangerous accidental answers.

Cleanup belongs to the owner

Every window listener, tray instance, power monitor hook, and protocol registration needs an owner and teardown path. win.on("closed", ...) is where you drop references so garbage collection can work. Main-process leaks are worse than renderer leaks because there is exactly one main process and users keep desktop apps open for weeks.

Field notes

  • ready-to-show can be delayed by renderer work; add a timeout if first paint is occasionally pathological.
  • Restore window bounds only after validating them against current displays — users unplug monitors.
  • shell.openExternal is for http(s) URLs you intend to open, never raw user input.
  • Test sleep/resume and display disconnect. Desktop lifecycle bugs live there, not in the happy path.

Was this lesson helpful?

Sign in to rate