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
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
2 changed files with 23 additions and 7 deletions

View File

@ -10,11 +10,11 @@ actual frontend bundle and build the app:
```bash
wget https://dl.vikunja.io/frontend/vikunja-frontend-master.zip
unzip vikunja-frontend-master.zip -d frontend
sed -i 's/\/fonts/\.\/fonts/g' frontend/index.html
sed -i 's/\/js/\.\/js/g' frontend/index.html
sed -i 's/\/css/\.\/css/g' frontend/index.html
sed -i 's/\/images/\.\/images/g' frontend/index.html
sed -i 's/\/images/\.\/images/g' frontend/js/*
sed -i 's/\/fonts/file\:\/\/fonts/g' frontend/index.html
sed -i 's/\/js/file\:\/\/js/g' frontend/index.html
sed -i 's/\/css/file\:\/\/css/g' frontend/index.html
sed -i 's/\/images/file\:\/\/images/g' frontend/index.html
sed -i "s/\/'images/'file\:\/\/images/g" frontend/js/*
```
## Building for release

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.