Skip to content

Commit 4592561

Browse files
authored
fix: 修复 desktop 白屏问题
fix: 修复 desktop 白屏问题
2 parents f0c42ac + 86ddb35 commit 4592561

4 files changed

Lines changed: 72 additions & 12 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ volumes:
5050
bookdock:
5151
```
5252
53+
> macOS 用户首次安装提示"已损坏"的解决方法:
54+
>
55+
> 1. 将 BookDock.app 拖到 Applications
56+
> 2. 打开终端执行:xattr -cr /Applications/BookDock.app
57+
> 3. 再次打开应用即可
58+
5359
## ⭐ Star History
5460
5561
[![Star History Chart](https://api.star-history.com/svg?repos=mmdctjj/BookDock&type=Date)](https://star-history.com/#mmdctjj/BookDock&Date)

apps/desktop/dist-electron/main.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { app, BrowserWindow, ipcMain, dialog } from "electron";
1+
import { protocol, app, net, BrowserWindow, ipcMain, dialog } from "electron";
22
import fs from "fs";
33
import path from "path";
4-
import { fileURLToPath } from "url";
4+
import { fileURLToPath, pathToFileURL } from "url";
55
const __filename$1 = fileURLToPath(import.meta.url);
66
const __dirname$1 = path.dirname(__filename$1);
77
process.env.DIST_ELECTRON = path.join(__dirname$1, "../dist-electron");
@@ -10,7 +10,6 @@ process.env.VITE_PUBLIC = process.env.VITE_DEV_SERVER_URL ? path.join(__dirname$
1010
let win = null;
1111
const preload = path.join(__dirname$1, "preload.mjs");
1212
const url = process.env.VITE_DEV_SERVER_URL;
13-
const indexHtml = path.join(process.env.DIST, "index.html");
1413
let books = [];
1514
let windowStates = {};
1615
let settings = {
@@ -42,11 +41,36 @@ function createWindow() {
4241
win.loadURL(url);
4342
win.webContents.openDevTools();
4443
} else {
45-
console.log("Main process loading file:", indexHtml);
46-
win.loadFile(indexHtml);
44+
console.log("Main process loading app://./index.html");
45+
win.loadURL("app://./index.html");
4746
}
4847
}
48+
protocol.registerSchemesAsPrivileged([
49+
{
50+
scheme: "app",
51+
privileges: {
52+
standard: true,
53+
secure: true,
54+
supportFetchAPI: true,
55+
bypassCSP: false,
56+
corsEnabled: true
57+
}
58+
}
59+
]);
4960
app.whenReady().then(() => {
61+
protocol.handle("app", (request) => {
62+
try {
63+
const url2 = new URL(request.url);
64+
let pathname = decodeURIComponent(url2.pathname);
65+
if (pathname.startsWith("/")) pathname = pathname.slice(1);
66+
const filePath = path.join(process.env.DIST, pathname || "index.html");
67+
console.log(`[Main] App Protocol: url=${request.url} -> filePath=${filePath}`);
68+
return net.fetch(pathToFileURL(filePath).href);
69+
} catch (e) {
70+
console.error("[Main] App protocol error:", e);
71+
return new Response("Internal error", { status: 500 });
72+
}
73+
});
5074
createWindow();
5175
app.on("activate", () => {
5276
if (BrowserWindow.getAllWindows().length === 0) {

apps/desktop/electron/main.ts

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { app, BrowserWindow, dialog, ipcMain } from 'electron';
1+
import { app, BrowserWindow, dialog, ipcMain, net, protocol } from 'electron';
22
import fs from 'fs';
33
import path from 'path';
4-
import { fileURLToPath } from 'url';
4+
import { fileURLToPath, pathToFileURL } from 'url';
55

66
const __filename = fileURLToPath(import.meta.url);
77
const __dirname = path.dirname(__filename);
@@ -16,7 +16,6 @@ process.env.VITE_PUBLIC = process.env.VITE_DEV_SERVER_URL
1616
let win: BrowserWindow | null = null;
1717
const preload = path.join(__dirname, 'preload.mjs');
1818
const url = process.env.VITE_DEV_SERVER_URL;
19-
const indexHtml = path.join(process.env.DIST, 'index.html');
2019

2120
// Simple state management for demo
2221
let books: any[] = [];
@@ -32,14 +31,14 @@ let settings = {
3231

3332
function createWindow() {
3433
console.log('Main process using preload script at:', preload);
35-
34+
3635
win = new BrowserWindow({
3736
width: 1200,
3837
height: 800,
3938
minWidth: 800,
4039
minHeight: 600,
4140
title: 'BookDock - 书仓',
42-
icon: path.join(process.env.VITE_PUBLIC, 'logo.png'),
41+
icon: path.join(process.env.VITE_PUBLIC!, 'logo.png'),
4342
webPreferences: {
4443
preload,
4544
nodeIntegration: false,
@@ -53,13 +52,43 @@ function createWindow() {
5352
win.loadURL(url);
5453
win.webContents.openDevTools();
5554
} else {
56-
console.log('Main process loading file:', indexHtml);
57-
win.loadFile(indexHtml);
55+
console.log('Main process loading app://./index.html');
56+
win.loadURL('app://./index.html');
5857
}
5958
}
6059

60+
// Register custom protocol before app is ready
61+
protocol.registerSchemesAsPrivileged([
62+
{
63+
scheme: 'app',
64+
privileges: {
65+
standard: true,
66+
secure: true,
67+
supportFetchAPI: true,
68+
bypassCSP: false,
69+
corsEnabled: true,
70+
},
71+
},
72+
]);
6173

6274
app.whenReady().then(() => {
75+
// Register app protocol handler
76+
protocol.handle('app', (request) => {
77+
try {
78+
const url = new URL(request.url);
79+
let pathname = decodeURIComponent(url.pathname);
80+
if (pathname.startsWith('/')) pathname = pathname.slice(1);
81+
82+
const filePath = path.join(process.env.DIST!, pathname || 'index.html');
83+
console.log(`[Main] App Protocol: url=${request.url} -> filePath=${filePath}`);
84+
85+
return net.fetch(pathToFileURL(filePath).href);
86+
} catch (e) {
87+
console.error('[Main] App protocol error:', e);
88+
return new Response('Internal error', { status: 500 });
89+
}
90+
});
91+
6392
createWindow();
6493

6594
app.on('activate', () => {

apps/desktop/vite.config.desktop.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export default defineConfig({
5959
port: 1424,
6060
strictPort: true,
6161
},
62+
base: './',
6263
build: {
6364
outDir: 'dist',
6465
emptyOutDir: true,

0 commit comments

Comments
 (0)