Add url rewrite to serve files locally without requiring a server
This commit is contained in:
parent
e5d4dd9197
commit
b0e81e1945
10
readme.md
10
readme.md
@ -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
|
||||
|
20
src/index.js
20
src/index.js
@ -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.
|
||||
|
Loading…
x
Reference in New Issue
Block a user