From 9694e7593ff642eb1b45e6d31dc0dbdb325c0244 Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Mon, 13 May 2024 16:59:34 +0200 Subject: [PATCH] docs: improve protocol.handle file examples (#42142) * docs: improve protocol.handle file examples Co-authored-by: David Sanders * chore: fix lint Co-authored-by: David Sanders --------- Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: David Sanders --- docs/api/protocol.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/docs/api/protocol.md b/docs/api/protocol.md index 6d2de18751de..3f4ce799a137 100644 --- a/docs/api/protocol.md +++ b/docs/api/protocol.md @@ -9,10 +9,14 @@ An example of implementing a protocol that has the same effect as the ```js const { app, protocol, net } = require('electron') +const path = require('node:path') +const url = require('node:url') app.whenReady().then(() => { - protocol.handle('atom', (request) => - net.fetch('file://' + request.url.slice('atom://'.length))) + protocol.handle('atom', (request) => { + const filePath = request.url.slice('atom://'.length) + return net.fetch(url.pathToFileURL(path.join(__dirname, filePath)).toString()) + }) }) ``` @@ -42,7 +46,7 @@ app.whenReady().then(() => { ses.protocol.handle('atom', (request) => { const filePath = request.url.slice('atom://'.length) - return net.fetch(url.pathToFileURL(path.join(__dirname, filePath)).toString()) + return net.fetch(url.pathToFileURL(path.resolve(__dirname, filePath)).toString()) }) const mainWindow = new BrowserWindow({ webPreferences: { partition } })