Lesson 9 of 9 · 25m
Native Integration and Production Hardening
Tray, notifications, shell integration, file associations, power events, crash reporting, and the operational controls that make Electron feel professional.
Users judge desktop software by the edges: whether notifications respect focus mode, whether the tray icon quits cleanly, whether files open from Finder or Explorer, whether sleep/resume corrupts state, and whether support can diagnose a crash without asking for screenshots of a frozen window. Native integration is where Electron stops being a web page in a rectangle.
Integrate narrowly and own the side effects
const tray = new Tray(nativeImage.createFromPath(iconPath));
tray.setToolTip("SudoSecurity Desktop");
tray.setContextMenu(Menu.buildFromTemplate([
{ label: "Open", click: showMainWindow },
{ label: "Quit", click: () => app.exit(0) },
]));Tray menus, global shortcuts, protocol handlers, notification actions, and file associations all create input paths outside your window UI. Treat them as untrusted entry points. A global shortcut should trigger a validated application command, not mutate renderer state behind the user's back. File-open events need paths validated against actual file types and sizes before parsing.
Respect the operating system
macOS, Windows, and Linux differ in menu conventions, notification behavior, power management, and single-instance expectations. Use platform conventions rather than forcing one design everywhere. On Windows, test high-DPI scaling and long paths. On macOS, test full-screen spaces and open-file. On Linux, accept fragmentation and define supported targets honestly.
const gotLock = app.requestSingleInstanceLock();
if (!gotLock) {
app.quit();
} else {
app.on("second-instance", (_event, argv) => {
handleDeepLinkOrFile(argv);
focusMainWindow();
});
}Single-instance behavior is mandatory for many productivity apps. Otherwise users launch three copies, corrupt local SQLite state, and file bugs that sound supernatural.
Design for sleep, network loss, and brutal termination
Desktop apps are suspended, throttled, disconnected, and killed. Persist drafts incrementally. Use atomic writes for critical local files: write temp file, fsync where appropriate, rename. Reconnect network services with backoff and jitter. Do not assume timers fired while the machine slept. On resume, reconcile server state rather than replaying stale assumptions.
Crash reporting without surveillance
Native crashes produce minidumps; renderer crashes produce Chromium reports; JavaScript exceptions produce stack traces. Capture enough to diagnose, strip enough to protect users. Ask consent, document retention, redact paths and secrets, and never upload document content by default. Symbolize native crashes and keep release symbols indexed by exact artifact version — otherwise crash reports are expensive noise.
Production hardening checklist
- disable unused Electron features and inspect every
webPreferencesvalue in release builds - enforce single instance where required and audit deep links/file-open paths
- make critical writes atomic and recoverable after power loss
- version local schema migrations and test upgrades from real user versions
- monitor startup time, memory, crash-free sessions, and update failure rate
- ship support diagnostics users can inspect before sending
Field notes
- Endpoint protection may quarantine unsigned helpers or inspect localhost traffic; enterprise testing is its own environment.
- SQLite is excellent local storage if writes are transactional and one process owns the database.
- Notifications are not a message queue; they are user-facing interruptions and can be suppressed by the OS.
- A professional app has a written threat model, release runbook, rollback plan, and incident contact — not merely a clean build.
Was this lesson helpful?