1
0

Add url rewrite to serve files locally without requiring a server

This commit is contained in:
kolaente
2020-10-08 21:57:27 +02:00
parent e5d4dd9197
commit b0e81e1945
2 changed files with 23 additions and 7 deletions

View File

@ -1,4 +1,5 @@
const { app, BrowserWindow } = require('electron')
const { app, BrowserWindow, protocol, session } = require('electron')
const path = require('path')
function createWindow () {
// Create the browser window.
@ -17,8 +18,23 @@ function createWindow () {
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(createWindow)
app.whenReady().then(() => {
const root = path.normalize(`${__dirname}/..`)
// file:// interceptor to serve all assets without running a web server in the background
protocol.interceptFileProtocol('file', (request, callback) => {
let url = request.url.substr(7) /* all urls start with 'file://' */
if(!url.startsWith(root)) {
if(url.startsWith('css/fonts') || url.startsWith('css/img')) {
url = url.substr(4)
}
url = path.normalize(`${root}/frontend/${url}`)
}
callback({ path: url})
})
createWindow()
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.