mirror of
https://github.com/home-assistant/frontend.git
synced 2025-12-24 12:49:19 +00:00
Use sw-precache to generate service worker
This commit is contained in:
52
script/sw-precache.js
Normal file
52
script/sw-precache.js
Normal file
@@ -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',
|
||||
});
|
||||
Reference in New Issue
Block a user