1- import { app , BrowserWindow , dialog , ipcMain } from 'electron' ;
1+ import { app , BrowserWindow , dialog , ipcMain , net , protocol } from 'electron' ;
22import fs from 'fs' ;
33import path from 'path' ;
4- import { fileURLToPath } from 'url' ;
4+ import { fileURLToPath , pathToFileURL } from 'url' ;
55
66const __filename = fileURLToPath ( import . meta. url ) ;
77const __dirname = path . dirname ( __filename ) ;
@@ -16,7 +16,6 @@ process.env.VITE_PUBLIC = process.env.VITE_DEV_SERVER_URL
1616let win : BrowserWindow | null = null ;
1717const preload = path . join ( __dirname , 'preload.mjs' ) ;
1818const url = process . env . VITE_DEV_SERVER_URL ;
19- const indexHtml = path . join ( process . env . DIST , 'index.html' ) ;
2019
2120// Simple state management for demo
2221let books : any [ ] = [ ] ;
@@ -32,14 +31,14 @@ let settings = {
3231
3332function 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
6274app . 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' , ( ) => {
0 commit comments