Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop using protocol.registerFileProtocol that is deprecated #1165

Merged
merged 3 commits into from
Oct 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions packages/desktop/electron/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { app, BrowserWindow, ipcMain, protocol } from "electron";
import { app, BrowserWindow, ipcMain, protocol, net } from "electron";
import * as path from "node:path";
import * as fsPromises from "node:fs/promises";
import workerThreads from "node:worker_threads";
Expand Down Expand Up @@ -223,14 +223,22 @@ app.whenReady().then(() => {
// which is configured at `package.json` with the `"homepage"` field.
// Ref: https://github.com/electron/electron/issues/4612#issuecomment-189116655
const bundleBasePath = path.resolve(__dirname, "..");
protocol.interceptFileProtocol("file", function (req, callback) {
protocol.handle("file", (req) => {
const filePath = new URL(req.url).pathname; // `file://<absolute_path>?<query>#<hash>` -> `<absolute_path>`
if (path.isAbsolute(filePath)) {
const resolvedFilePath = path.join(bundleBasePath, filePath);
callback(path.normalize(resolvedFilePath));
} else {
callback(filePath);

if (!path.isAbsolute(filePath)) {
return net.fetch(req, {
bypassCustomProtocolHandlers: true,
});
}

const resolvedFilePath = path.normalize(
path.join(bundleBasePath, filePath),
);
const modifiedReq = new Request("file://" + resolvedFilePath, req);
return net.fetch(modifiedReq, {
bypassCustomProtocolHandlers: true,
});
});

createWindow();
Expand Down
Loading