Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 36 ยท Projects and Practice

Electron Practice Set and Interview Questions

Test what you learned with Electron practice exercises: security review, IPC design, packaging decisions, performance debugging and common interview questions with model answers.

You now have the full path: process model, IPC, windows, native APIs, security, packaging, updates, testing and performance. This final lesson is for practice. First, hands-on exercises that you can run and extend, then a review of the design questions that come up in code reviews and interviews, with short model answers.

Try each exercise before reading the hints. The runnable examples give you a starting point; change them until the behaviour is right.

Exercise 1: Review this window

A teammate sends this pull request. List every problem before reading on.

JavaScript
const win = new BrowserWindow({
  webPreferences: {
    nodeIntegration: true,
    contextIsolation: false,
    webSecurity: false,
  },
});
win.loadURL('http://my-dashboard.example.com');
win.webContents.on('did-create-window', (w) => w.loadURL('https://ads.example.net'));
ipcMain.handle('run', (_e, cmd) => require('child_process').execSync(cmd).toString());

Findings: nodeIntegration: true and contextIsolation: false give page scripts full Node; webSecurity: false removes the same-origin policy; the page is loaded over plain http, so anyone on the network can inject script; new windows are not controlled; and the run handler executes any string the renderer sends, which is remote code execution by design, blocking the main thread with execSync as well. Fix by following the checklist: safe flags, https and a CSP, setWindowOpenHandler, and replace run with specific, validated capabilities.

Exercise 2: Design the IPC contract

A settings screen needs to read and change the theme, and open the log folder. Write the channel names and what each handler checks.

A good answer has three named channels and nothing generic: settings:get-theme (no arguments), settings:set-theme (argument must be one of light, dark, system), app:open-logs (no arguments, opens a folder the main process computes). No channel accepts a path or a command from the renderer. Each handler checks the sender frame (Validating IPC).

Now implement the validating half. Complete the missing rules in this table-driven validator and watch the tests turn green.

Node.js Exercise: table-driven IPC validation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Exercise 3: Which process?

For each task decide where it runs and why.

Task Answer
Read a user-chosen file Main process (needs fs and dialog); renderer asks via IPC
Render Markdown preview Renderer (pure DOM work on text it already has)
Hash a 2 GB file Worker thread or utility process, never the main thread
Store an API token Main process with safeStorage; renderer never sees it
Show a notification Main process Notification, or the web API in the renderer
Resize handling and window bounds persistence Main process, debounced

Look back at the chapter on processes and IPC if any row surprised you. The rule behind all of them: the renderer is untrusted and presentational; the main process holds power and applies checks.

Exercise 4: Fix the freeze

A user reports that the whole app freezes for a second when they search notes. The search handler is below. Find the cause and fix it with a stream or worker.

Node.js Exercise: keep the event loop responsive

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Chunking with setImmediate is the cheapest fix; a worker thread or utility process is better for heavier work (Startup Performance). A third option is a real index built once, so each query is fast.

Exercise 5: Plan a release

Write the checklist for shipping version 2.0 of a notes app to Windows and macOS users: build, sign, verify, publish, update. Compare with your own:

  1. Run unit and E2E tests; smoke-test the packaged app.
  2. Bump the version (semantic: a major bump for breaking data changes) and write release notes.
  3. Flip fuses, build on a Windows runner and a macOS runner, sign both, notarize the Mac build.
  4. Verify signatures on clean machines; publish checksums.
  5. Upload to the release host; start a staged rollout at 10%.
  6. Watch crash reports and logs; raise to 100% or halt.
  7. Prepare a data migration for settings and notes created by version 1.

See Installers and Code Signing and Auto-update Flow.

Interview questions with model answers

What are the main and renderer processes? The main process is one Node.js process that owns the app lifecycle, native windows, menus and OS integration. Each window runs a renderer: a Chromium process that renders web content. They communicate through IPC.

Why is nodeIntegration disabled by default? Any script in the page, including injected ones, could use require and control the machine. With it off and context isolation plus sandbox on, the page can only use what the preload exposes.

What does contextBridge do? It safely exposes selected functions or values from the isolated preload world to the page, copying data across so the page cannot reach privileged objects such as ipcRenderer.

ipcRenderer.invoke versus send? invoke returns a promise resolved by an ipcMain.handle handler: request and response. send is fire and forget, handled by ipcMain.on. Prefer invoke; avoid sendSync because it blocks.

How do you store a secret? In the main process, encrypted with safeStorage (OS keychain-backed), never in the renderer, source code or plain JSON.

How do you update an Electron app? Sign the app, publish releases, use autoUpdater, update-electron-app or electron-updater; verify, download in the background, prompt to restart, and use staged rollouts.

How would you reduce startup time? Measure first; show the window early with ready-to-show; defer non-essential work; lazy-load heavy modules; bundle the renderer; keep the main thread free.

How do you test Electron code? Pure modules with unit tests, handlers with injected fakes, and a few Playwright end-to-end tests on the real and the packaged app.

Electron versus Tauri versus a PWA? Electron bundles Chromium and Node for full web-platform consistency and a huge ecosystem, at the cost of size and memory. Tauri uses the system webview with a Rust backend: smaller, but rendering differs between platforms. A PWA needs no install but has limited OS access.

Use a quick self-quiz below, then try building the next project of your own.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. An IPC channel accepts a shell command string from the renderer. What is the problem?

    Generic execution channels turn a renderer bug into full control of the machine. Offer specific, validated capabilities instead.
  2. 2. Which task belongs in a worker thread or utility process?

    CPU-heavy work on the main thread freezes every window. A worker or utility process keeps the app responsive.
  3. 3. What is the safest release strategy for a risky update?

    A percentage rollout limits the blast radius and lets you halt before most users are affected.
  4. 4. Which statement about preload and the renderer is correct?

    Named functions with fixed channels give the page only the capabilities you intend.

Summary

  • Practise reviewing code for the dangerous flags, generic IPC channels and blocking handlers.
  • Decide the process by capability: power and checks in the main process, presentation in the renderer, heavy work off the main thread.
  • A release is a pipeline: test, build, sign, verify, roll out gradually, monitor.
  • Be ready to explain the process model, contextBridge, safeStorage, updates and testing in plain words.
  • Keep building: extend the three projects, or start your own app with the checklist in hand.

Next: back to the Electron tutorial overview