Skip to content

Lesson 1 of 9 · 22m

The Process Model: Main, Renderer, Preload

Electron is Chromium plus Node with a hostile boundary in the middle; learn which process owns which power before writing app code.

Electron's most important fact is also the one new teams ignore: this is not one JavaScript process with windows. It is a small operating-system program — the main process — supervising one or more Chromium renderer processes, with tiny preload scripts acting as controlled bridges. If you blur those roles, the application becomes fragile and dangerous at the same time.

The power split

main process       one per app; owns windows, menus, lifecycle, files, shell, updater
renderer process   one per BrowserWindow/webContents; owns UI DOM, CSS, user interaction
preload script     runs before renderer JS; can see both worlds; should expose a narrow bridge

The main process is trusted. It can open sockets, read local files, spawn helpers, update the app, and talk to macOS or Windows APIs. The renderer is hostile territory: it displays HTML, parses images and fonts, loads dependencies, and often renders content influenced by a server. Treat any code executing there as one XSS away from attacker control.

The bridge should be deliberately boring

// preload.js
const { contextBridge, ipcRenderer } = require("electron");

contextBridge.exposeInMainWorld("desktop", {
  saveReport: (payload) => ipcRenderer.invoke("report:save", payload),
  onUpdateAvailable: (callback) => {
    const listener = (_event, version) => callback(version);
    ipcRenderer.on("update:available", listener);
    return () => ipcRenderer.removeListener("update:available", listener);
  },
});

The renderer then sees window.desktop.saveReport(...), not require, not ipcRenderer, not arbitrary send channels. That distinction matters. A bridge that forwards generic IPC is not a boundary; it is a tunnel through the boundary.

Main owns lifetime, not business logic soup

The main process should initialize once, create windows, own OS integrations, validate renderer requests, and shut down cleanly. It should not become a second backend full of unrelated controllers. Put durable business services behind small modules the main process composes. Keep each channel narrow enough that you can audit it.

Renderer crashes are normal engineering events

Renderers can crash because Chromium crashes, GPU drivers misbehave, or memory pressure wins. Listen for render-process-gone, preserve user work outside renderer-only memory, and offer recovery rather than a silent dead window. A desktop app with one unsaved document living only inside a renderer is an accident report waiting to happen.

Field notes

  • A second BrowserWindow usually means a second renderer process; don't assume shared memory.
  • The preload script is privileged by proximity, not by magic — keep it tiny and dependency-free.
  • Never send secrets, tokens, or filesystem handles into a renderer unless the user interface genuinely needs them.
  • webContents.executeJavaScript from main bypasses your preload API discipline; reserve it for diagnostics, not features.

Was this lesson helpful?

Sign in to rate