Use sw-precache to generate service worker
parent
ead2ba36ac
commit
0be98873d7
|
@ -34,6 +34,7 @@
|
|||
"eslint-config-airbnb-base": "^2.0.0",
|
||||
"eslint-plugin-import": "^1.7.0",
|
||||
"html-minifier": "^2.1.2",
|
||||
"sw-precache": "^3.2.0",
|
||||
"vulcanize": "^1.14.8",
|
||||
"webpack": "^2.1.0-beta.7"
|
||||
}
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
Generate a caching service worker for HA
|
||||
|
||||
Will be called as part of build_frontend.
|
||||
|
||||
Expects home-assistant-polymer repo as submodule of HA repo.
|
||||
Creates a caching service worker based on the CURRENT content of HA repo.
|
||||
Output service worker to build/service_worker.js
|
||||
*/
|
||||
var crypto = require('crypto');
|
||||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
var swPrecache = require('sw-precache');
|
||||
|
||||
var rootDir = '..';
|
||||
|
||||
function md5(filename) {
|
||||
return crypto.createHash('md5')
|
||||
.update(fs.readFileSync(filename)).digest('hex');
|
||||
}
|
||||
|
||||
var appPaths = ['/', '/states', '/logbook', '/history', '/map',
|
||||
'/devService', '/devState', '/devEvent', '/devInfo', '/devTemplate'];
|
||||
var fingerprinted = ['frontend', 'mdi'];
|
||||
|
||||
var dynamicUrlToDependencies = {};
|
||||
|
||||
appPaths.forEach(ap => {
|
||||
dynamicUrlToDependencies[ap] = [rootDir + '/frontend.html'];
|
||||
});
|
||||
|
||||
fingerprinted.forEach(fn => {
|
||||
var hash = md5(rootDir + '/' + fn + '.html');
|
||||
var url = '/static/' + fn + '-' + hash + '.html';
|
||||
var fpath = rootDir + '/' + fn + '.html';
|
||||
dynamicUrlToDependencies[url] = [fpath];
|
||||
});
|
||||
|
||||
swPrecache.write(path.join('build', 'service_worker.js'), {
|
||||
dynamicUrlToDependencies: dynamicUrlToDependencies,
|
||||
staticFileGlobs: [
|
||||
rootDir + '/favicon-192x192.png',
|
||||
rootDir + '/webcomponents-lite.min.js',
|
||||
rootDir + '/fonts/roboto/Roboto-Light.ttf',
|
||||
rootDir + '/fonts/roboto/Roboto-Medium.ttf',
|
||||
rootDir + '/fonts/roboto/Roboto-Regular.ttf',
|
||||
rootDir + '/fonts/roboto/Roboto-Bold.ttf',
|
||||
rootDir + '/images/card_media_player_bg.png',
|
||||
],
|
||||
stripPrefix: '..',
|
||||
replacePrefix: 'static',
|
||||
});
|
|
@ -1,63 +0,0 @@
|
|||
/* eslint-disable no-console */
|
||||
const CACHE = '0.10';
|
||||
const INDEX_CACHE_URL = '/';
|
||||
const INDEX_ROUTES = ['/', '/logbook', '/history', '/map', '/devService', '/devState',
|
||||
'/devEvent', '/devInfo', '/states'];
|
||||
const CACHE_URLS = [
|
||||
'/static/favicon-192x192.png',
|
||||
];
|
||||
|
||||
if (__DEV__) {
|
||||
console.log('Service Worker initialized.');
|
||||
}
|
||||
|
||||
self.addEventListener('install', event => {
|
||||
if (__DEV__) {
|
||||
console.log('Service Worker installed.');
|
||||
}
|
||||
|
||||
event.waitUntil(
|
||||
caches.open(CACHE).then(cache => cache.addAll(CACHE_URLS.concat(INDEX_CACHE_URL)))
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener('activate', event => {
|
||||
if (__DEV__) {
|
||||
console.log('Service Worker activated.');
|
||||
// Force refresh service worker
|
||||
event.waitUntil(global.clients.claim());
|
||||
}
|
||||
});
|
||||
|
||||
self.addEventListener('message', event => {
|
||||
if (__DEV__) {
|
||||
console.log('Message received', event.data);
|
||||
}
|
||||
});
|
||||
|
||||
self.addEventListener('fetch', event => {
|
||||
const path = event.request.url.substr(event.request.url.indexOf('/', 8));
|
||||
|
||||
// TODO: do not cache requests to 3rd party hosts (or remove those calls)
|
||||
|
||||
if (CACHE_URLS.includes(path)) {
|
||||
event.respondWith(
|
||||
caches.open(CACHE).then(cache => cache.match(event.request))
|
||||
);
|
||||
}
|
||||
|
||||
if (!INDEX_ROUTES.includes(path)) {
|
||||
return;
|
||||
}
|
||||
|
||||
event.respondWith(
|
||||
caches.open(CACHE).then(cache =>
|
||||
cache.match(INDEX_CACHE_URL).then(cachedResponse =>
|
||||
cachedResponse || fetch(event.request).then(response => {
|
||||
cache.put(INDEX_CACHE_URL, response.clone());
|
||||
return response;
|
||||
})
|
||||
)
|
||||
)
|
||||
);
|
||||
});
|
|
@ -9,7 +9,6 @@ var definePlugin = new webpack.DefinePlugin({
|
|||
module.exports = {
|
||||
entry: {
|
||||
_app_compiled: './src/home-assistant.js',
|
||||
service_worker: './src/service-worker/index.js',
|
||||
},
|
||||
output: {
|
||||
path: 'build',
|
||||
|
|
Loading…
Reference in New Issue