Electron Custom Protocols, Resource Loading, and Deep Links
🎯 Introduction
During development, pages usually load from http://localhost; after packaging, they load from local files. These path changes often cause problems for developers new to Electron. This article first explains how to choose a loading method, then introduces custom protocols.
🧱 Loading in development and production
const isDevelopment = Boolean(process.env.VITE_DEV_SERVER_URL);
if (isDevelopment) {
win.loadURL(process.env.VITE_DEV_SERVER_URL);
} else {
win.loadFile(path.join(__dirname, '../dist/index.html'));
}
Development needs a server for hot updates. Production needs the build directory included in the application package. Do not hard-code a development server address into your production configuration.
🧰 Why use a custom protocol?
Most Electron projects do not need a custom protocol. Loading index.html with loadFile() is sufficient.
A custom protocol becomes useful when an app has many internal pages, images, and scripts, or when you want a consistent addressing scheme for all internal resources. Its role is straightforward: give internal resources a consistent address and let the main process decide which file that address can open.
For example, instead of referring to a specific local file path, the application uses:
app://local/index.html
The main process maps that address to dist/index.html in the application package:
const { app, BrowserWindow, net, protocol } = require('electron/main');
const path = require('node:path');
const { pathToFileURL } = require('node:url');
app.whenReady().then(async () => {
protocol.handle('app', async request => {
const url = new URL(request.url);
if (url.pathname !== '/index.html') {
return new Response('Not found', { status: 404 });
}
const filePath = path.join(__dirname, '../dist/index.html');
return net.fetch(pathToFileURL(filePath).toString());
});
const win = new BrowserWindow({
width: 960,
height: 640,
});
await win.loadURL('app://local/index.html');
});
Here is how the code works:
- The window requests
app://local/index.html. protocol.handle('app', ...)receives the request.- The main process allows access only to
index.html, mapping it todist/index.html. - Unknown paths return a 404 response instead of allowing arbitrary local file access.
Start with loadFile() in a beginner project. Consider a custom protocol when you actually need centralized management of internal resources; it is optional.
🔗 What are deep links for?
Regular links within your app, such as Vue Router’s /project/123, can navigate directly in the current window. They do not require deep linking.
Deep links address a different need: opening the desktop app at specific content when a user clicks a link outside the app.
The link might come from a browser address bar, an email, a system notification, or another app. For example:
electron-course://project/123
When the user clicks this link, the operating system finds the Electron app registered for the electron-course protocol, launches it, and passes project/123 to it. The app can then open project 123.
Deep links are useful in these situations:
- A project link in an email opens that project in the desktop app.
- A browser page provides an “Open in desktop app” button.
- A task link in a system notification takes the user directly to that task.
If your app only navigates between pages within its own windows and does not need to open specific content from browsers, emails, or notifications, you do not need deep linking.
When the main process receives a deep link, it typically does three things: parse the link, validate its parameters, and tell the current window to display the corresponding content.
function handleDeepLink(rawUrl) {
const url = new URL(rawUrl);
if (url.protocol !== 'electron-course:') {
return;
}
const projectId = url.pathname.slice(1);
if (!/^\d+$/.test(projectId)) {
return;
}
mainWindow.webContents.send('project:open', projectId);
}
electron-course: is the protocol registered by the application, and project/123 identifies the content to open. The regular expression prevents arbitrary text from being treated as a project ID.
A complete implementation also needs to register the protocol on Windows, macOS, or Linux and integrate with single-instance handling. This is part of release configuration. For now, focus on how a link tells the application which content to open.
🧾 Summary
- Development usually loads a Vite URL; production usually loads local build files.
- After packaging, check that resources were copied into the application package.
- Custom protocols help manage internal application resources consistently.
- Deep links can map external addresses to pages within the app.
- Restrict and validate both local paths and URL parameters.
❓ Questions and answers
Q1: Does every Electron project need a custom protocol?
No. loadFile is enough for simple projects. Custom protocols suit projects that need consistent resource loading or deep linking.
Q2: Why does the page load in development but show a blank screen after packaging?
Common causes include an incorrect production path or build resources missing from the application package.
🧪 Practice
Complete the code below with the production page path:
win
.loadFile
// Write your code here
();
🎉 You now understand the development and production differences that commonly cause confusion when loading resources in Electron.
Sessions, Networking, and Local Storage
Use session, cookies, and local configuration to preserve login state and application settings.
Long-running Tasks and Performance Troubleshooting
Identify main process blocking, choose an appropriate approach to background tasks, and learn the basics of resource cleanup.
