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

On desktop apps, the resource URLs became absolute paths and they are resolved to absolute file paths through a handler registered via protocol.interceptFileProtocol #414

Merged
merged 2 commits into from
Dec 4, 2022
Merged
Show file tree
Hide file tree
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
23 changes: 21 additions & 2 deletions packages/desktop/electron/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { app, BrowserWindow, ipcMain } from "electron";
import { app, BrowserWindow, ipcMain, protocol } from "electron";
import * as path from "path";
import * as fsPromises from "fs/promises";
import { walkRead } from "./file";
Expand Down Expand Up @@ -41,7 +41,11 @@ const createWindow = () => {
);

if (app.isPackaged || process.env.NODE_ENV === "production") {
mainWindow.loadFile(path.resolve(__dirname, "../index.html"));
// Use .loadURL() with an absolute URL based on "/" instead of .loadFile()
// because absolute URLs with the file:// scheme will be resolved
// to absolute file paths based on the special handler
// registered through `interceptFileProtocol` below.
mainWindow.loadURL("file:///index.html");
} else {
mainWindow.loadURL("http://localhost:3000");
}
Expand All @@ -55,6 +59,21 @@ const createWindow = () => {
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
// Resolve absolute paths based on the bundled directory.
// It is assumed that the resource paths are absolute paths starting with "/",
// 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) {
const urlWithoutScheme = req.url.slice(7); // 7 = "file://".length
if (path.isAbsolute(urlWithoutScheme)) {
const resolvedFilePath = path.join(bundleBasePath, urlWithoutScheme);
callback(path.normalize(resolvedFilePath));
} else {
callback(urlWithoutScheme);
}
});

createWindow();

app.on("activate", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@stlite/desktop",
"version": "0.16.1",
"license": "Apache-2.0",
"homepage": "./",
"homepage": "/",
"main": "./build/electron/main.js",
"files": [
"build",
Expand Down