fix #156: using ServiceWorker instead of AppCache

This commit is contained in:
antelle 2019-09-23 20:32:56 +02:00
parent b280c6f723
commit a01d85411c
11 changed files with 46 additions and 86 deletions

View File

@ -183,19 +183,9 @@ module.exports = function(grunt) {
},
files: { 'dist/manifest.appcache': 'app/manifest.appcache' }
},
'manifest-html': {
options: {
replacements: [
{ pattern: '<html', replacement: '<html manifest="manifest.appcache"' }
]
},
files: { 'dist/index.html': 'dist/index.html' }
},
'desktop-html': {
options: {
replacements: [{ pattern: ' manifest="manifest.appcache"', replacement: '' }]
},
files: { 'tmp/desktop/app/index.html': 'dist/index.html' }
'service-worker': {
options: { replacements: [{ pattern: '0.0.0', replacement: pkg.version }] },
files: { 'dist/service-worker.js': 'app/service-worker.js' }
},
'desktop-public-key': {
options: {
@ -481,14 +471,6 @@ module.exports = function(grunt) {
}
}
},
'sign-html': {
app: {
options: {
file: 'dist/index.html',
skip: grunt.option('skip-sign')
}
}
},
'sign-exe': {
options: {
spc: 'keys/keeweb.spc',

View File

@ -2,9 +2,3 @@ CACHE MANIFEST
# YYYY-MM-DD:v0.0.0
# updmin:v0.0.0
CACHE:
index.html
NETWORK:
*

View File

@ -5,6 +5,7 @@ const RuntimeInfo = {
beta: !!'@@BETA',
buildDate: '@@DATE',
commit: '@@COMMIT',
devMode: '@@DEVMODE',
userAgent: navigator.userAgent,
launcher: Launcher ? Launcher.name + ' v' + Launcher.version : ''
};

View File

@ -40,12 +40,21 @@ const Updater = {
init() {
this.scheduleNextCheck();
if (!Launcher && window.applicationCache) {
window.applicationCache.addEventListener(
'updateready',
this.checkAppCacheUpdateReady.bind(this)
);
this.checkAppCacheUpdateReady();
if (!Launcher && navigator.serviceWorker && !RuntimeInfo.beta && !RuntimeInfo.devMode) {
navigator.serviceWorker
.register('service-worker.js')
.then(reg => {
logger.info('Service worker registered');
reg.addEventListener('updatefound', () => {
if (reg.active) {
logger.info('Service worker found an update');
UpdateModel.set({ updateStatus: 'ready' });
}
});
})
.catch(e => {
logger.error('Failed to register a service worker', e);
});
}
},
@ -258,15 +267,6 @@ const Updater = {
return err.toString();
}
return null;
},
checkAppCacheUpdateReady() {
if (window.applicationCache.status === window.applicationCache.UPDATEREADY) {
try {
window.applicationCache.swapCache();
} catch (e) {}
UpdateModel.set({ updateStatus: 'ready' });
}
}
};

19
app/service-worker.js Normal file
View File

@ -0,0 +1,19 @@
const VERSION = '0.0.0';
self.addEventListener('install', event => {
event.waitUntil(
caches.open('v1').then(cache => {
return fetch('.?v=' + VERSION).then(response => {
if (response.ok) {
cache.put('.', response);
}
});
})
);
});
self.addEventListener('fetch', event => {
event.respondWith(
caches.match(event.request.url).then(response => response || fetch(event.request))
);
});

View File

@ -1,36 +0,0 @@
module.exports = function(grunt) {
grunt.registerMultiTask('sign-html', 'Signs html page with a private key', function() {
if (this.options().skip) {
grunt.log.writeln('Skipped app html signing');
return;
}
const done = this.async();
const fs = require('fs');
const sign = require('../util/sign');
const data = fs.readFileSync(this.options().file);
let fileStr = data.toString();
const marker = '<meta name="kw-signature" content="';
const ix = fileStr.indexOf(marker);
if (ix < 0) {
grunt.warn('Signature placeholder not found');
return;
}
sign(null, data)
.then(signature => {
signature = signature.toString('base64');
fileStr = fileStr.replace(marker, marker + signature);
fs.writeFileSync(this.options().file, fileStr, 'utf8');
done();
})
.catch(e => {
if (e === 'Cannot find PIN') {
grunt.warn(
'Error signing app html. To build without sign, please launch grunt with --skip-sign.'
);
} else {
grunt.warn('Sign error: ' + e);
}
done(false);
});
});
};

View File

@ -99,7 +99,8 @@ function config(grunt, mode = 'production') {
pattern: /@@COMMIT/g,
replacement: () =>
grunt.config.get('gitinfo.local.branch.current.shortSHA')
}
},
{ pattern: /@@DEVMODE/g, replacement: () => (devMode ? '1' : '') }
]
})
},

View File

@ -1,8 +1,7 @@
module.exports = function(grunt) {
// prettier-ignore
grunt.registerTask('default', 'Default: build web app', [
'build-web-app',
'sign-html'
'build-web-app'
]);
// prettier-ignore

View File

@ -11,7 +11,7 @@ module.exports = function(grunt) {
'webpack',
'inline',
'htmlmin',
'string-replace:manifest-html',
'string-replace:service-worker',
'string-replace:manifest',
'copy:dist-icons',
'copy:dist-manifest'
@ -19,8 +19,7 @@ module.exports = function(grunt) {
grunt.registerTask('build-desktop-app-content', [
'copy:desktop-app-content',
'string-replace:desktop-public-key',
'string-replace:desktop-html'
'string-replace:desktop-public-key'
]);
grunt.registerTask('build-desktop-update', [

View File

@ -90,9 +90,9 @@
"start": "grunt",
"test": "grunt test",
"postinstall": "cd desktop && npm install",
"build-beta": "grunt --skip-sign --beta && sed 's/<html manifest=\"manifest.appcache\">//' dist/index.html > ../keeweb-beta/index.html && cd ../keeweb-beta && git add index.html && git commit -a -m 'beta' && git push origin master",
"build-beta": "grunt --beta && sed 's/<html manifest=\"manifest.appcache\">//' dist/index.html > ../keeweb-beta/index.html && cd ../keeweb-beta && git add index.html && git commit -a -m 'beta' && git push origin master",
"electron": "ELECTRON_DISABLE_SECURITY_WARNINGS=1 electron desktop --htmlpath=http://localhost:8085",
"dev": "grunt dev --skip-sign",
"dev": "grunt dev",
"babel-helpers": "babel-external-helpers -l 'slicedToArray,toConsumableArray,defineProperty,typeof' -t global > app/lib/babel-helpers.js"
},
"author": {

View File

@ -10,6 +10,7 @@ Release notes
`+` #743: copying entry fields to clipboard
`+` #713: markdown notes
`+` #336: moving entries across files
`*` #156: using ServiceWorker instead of AppCache
`*` devtools are now opened with alt-cmd-I
`-` fix #764: multiple attachments display
`-` fix multi-line fields display in history