protocol
Register a custom protocol and intercept existing protocol requests.
Process: Main
An example of implementing a protocol that has the same effect as the
file:// protocol:
const { app, protocol, net } = require('electron')
const path = require('node:path')
const url = require('node:url')
app.whenReady().then(() => {
protocol.handle('atom', (request) => {
const filePath = request.url.slice('atom://'.length)
return net.fetch(url.pathToFileURL(path.join(__dirname, filePath)).toString())
})
})
All methods unless specified can only be used after the ready event
of the app module gets emitted.
Using protocol with a custom partition or session
A protocol is registered to a specific Electron session
object. If you don't specify a session, then your protocol will be applied to
the default session that Electron uses. However, if you define a partition or
session on your browserWindow's webPreferences, then that window will use
a different session and your custom protocol will not work if you just use
electron.protocol.XXX.
To have your custom protocol work in combination with a custom session, you need to register it to that session explicitly.
const { app, BrowserWindow, net, protocol, session } = require('electron')
const path = require('node:path')
const url = require('node:url')
app.whenReady().then(() => {
const partition = 'persist:example'
const ses = session.fromPartition(partition)
ses.protocol.handle('atom', (request) => {
const filePath = request.url.slice('atom://'.length)
return net.fetch(url.pathToFileURL(path.resolve(__dirname, filePath)).toString())
})
const mainWindow = new BrowserWindow({ webPreferences: { partition } })
})
Protocol names
RFC 3986 defines what a valid protocol name is:
Scheme names consist of a sequence of characters beginning with a letter and followed by any combination of letters, digits, plus ("+"), period ("."), or hyphen ("-"). Although schemes are case-insensitive, the canonical form is lowercase […].
Methods
The protocol module has the following methods:
protocol.registerSchemesAsPrivileged(customSchemes)
customSchemesCustomScheme[]
This method can only be used before the ready event of the app
module gets emitted and can be called only once.
Registers the scheme as standard, secure, bypasses content security policy for
resources, allows registering ServiceWorker, supports fetch API, streaming
video/audio, and V8 code cache. Specify a privilege with the value of true to
enable the capability.
An example of registering a privileged scheme, that bypasses Content Security Policy:
const { protocol } = require('electron')
protocol.registerSchemesAsPrivileged([
{ scheme: 'foo', privileges: { bypassCSP: true } }
])
A standard scheme adheres to what RFC 3986 calls generic URI syntax.
For example http and https are standard schemes, while file is not.
Registering a scheme as standard allows relative and absolute resources to
be resolved correctly when served. Otherwise the scheme will behave like the
file protocol, but without the ability to resolve relative URLs.
For example when you load following page with custom protocol without registering it as standard scheme, the image will not be loaded because non-standard schemes can not recognize relative URLs:
<body>
<img src='test.png'>
</body>
Registering a scheme as standard will allow access to files through the FileSystem API. Otherwise the renderer will throw a security error for the scheme.
By default web storage apis (localStorage, sessionStorage, webSQL, indexedDB,
cookies) are disabled for non standard schemes. So in general if you want to
register a custom protocol to replace the http protocol, you have to register
it as a standard scheme.
Protocols that use streams (http and stream protocols) should set stream: true.
The <video> and <audio> HTML elements expect protocols to buffer their
responses by default. The stream flag configures those elements to correctly
expect streaming responses.
protocol.handle(scheme, handler)
schemestring - scheme to handle, for examplehttpsormy-app. This is the bit before the:in a URL.handlerFunction<GlobalResponse | Promise<GlobalResponse>>requestGlobalRequest
Register a protocol handler for scheme. Requests made to URLs with this
scheme will delegate to this handler to determine what response should be sent.
Either a Response or a Promise<Response> can be returned.
Example:
const { app, net, protocol } = require('electron')
const path = require('node:path')
const { pathToFileURL } = require('node:url')
protocol.registerSchemesAsPrivileged([
{
scheme: 'app',
privileges: {
standard: true,
secure: true,
supportFetchAPI: true