Automatically update service-worker.js assets during update

This commit is contained in:
grandeljay 2022-11-28 16:05:16 +01:00
parent 37799a64b8
commit 837ee3fb9f
3 changed files with 117 additions and 21 deletions

View file

@ -1,48 +1,56 @@
const staticDevCoffee = 'wishthis'
const assets = [ const service_worker = 'wishthis';
'/src/assets/css/default/dark.css', const assets = [
'/src/assets/css/blog.css', '/src/assets/css/blog.css',
'/src/assets/css/default.css', '/src/assets/css/default.css',
'/src/assets/css/home.css', '/src/assets/css/home.css',
'/src/assets/css/install.css',
'/src/assets/css/post.css', '/src/assets/css/post.css',
'/src/assets/css/wish.css',
'/src/assets/css/wishlists-saved.css', '/src/assets/css/wishlists-saved.css',
'/src/assets/css/wishlists.css', '/src/assets/css/wishlists.css',
'/src/assets/css/default/dark.css',
'/src/assets/img/favicon/android-chrome-192x192.png',
'/src/assets/img/favicon/android-chrome-512x512.png',
'/src/assets/img/favicon/apple-touch-icon.png',
'/src/assets/img/favicon/favicon-16x16.png',
'/src/assets/img/favicon/favicon-32x32.png',
'/src/assets/img/favicon/favicon.ico',
'/src/assets/img/favicon/mstile-70x70.png',
'/src/assets/img/favicon/mstile-150x150.png',
'/src/assets/img/favicon/mstile-310x150.png',
'/src/assets/img/favicon.png', '/src/assets/img/favicon.png',
'/src/assets/img/favicon.svg', '/src/assets/img/favicon.svg',
'/src/assets/img/logo-animation.svg', '/src/assets/img/logo-animation.svg',
'/src/assets/img/logo-readme.svg',
'/src/assets/img/logo-square-white-background.png', '/src/assets/img/logo-square-white-background.png',
'/src/assets/img/logo-square-white-background.svg', '/src/assets/img/logo-square-white-background.svg',
'/src/assets/img/logo-square.png', '/src/assets/img/logo-square.png',
'/src/assets/img/logo-square.svg', '/src/assets/img/logo-square.svg',
'/src/assets/img/logo.svg', '/src/assets/img/logo.svg',
'/src/assets/img/no-image.svg', '/src/assets/img/no-image.svg',
'/src/assets/img/favicon/android-chrome-192x192.png',
'/src/assets/js/parts/wishlist-filter.js', '/src/assets/img/favicon/android-chrome-512x512.png',
'/src/assets/img/favicon/apple-touch-icon.png',
'/src/assets/img/favicon/browserconfig.xml',
'/src/assets/img/favicon/favicon-16x16.png',
'/src/assets/img/favicon/favicon-32x32.png',
'/src/assets/img/favicon/favicon.ico',
'/src/assets/img/favicon/mstile-144x144.png',
'/src/assets/img/favicon/mstile-150x150.png',
'/src/assets/img/favicon/mstile-310x150.png',
'/src/assets/img/favicon/mstile-310x310.png',
'/src/assets/img/favicon/mstile-70x70.png',
'/src/assets/img/favicon/safari-pinned-tab.svg',
'/src/assets/js/changelog.js', '/src/assets/js/changelog.js',
'/src/assets/js/default.js', '/src/assets/js/default.js',
'/src/assets/js/home.js', '/src/assets/js/home.js',
'/src/assets/js/html2canvas.js',
'/src/assets/js/install.js',
'/src/assets/js/login-as.js', '/src/assets/js/login-as.js',
'/src/assets/js/login.js',
'/src/assets/js/profile.js', '/src/assets/js/profile.js',
'/src/assets/js/register.js', '/src/assets/js/register.js',
'/src/assets/js/service-worker.js',
'/src/assets/js/wishlist.js', '/src/assets/js/wishlist.js',
'/src/assets/js/wishlists.js', '/src/assets/js/parts/wishlist-filter.js',
] '/src/assets/js/parts/wishlists.js',
];
self.addEventListener('install', installEvent => { self.addEventListener('install', installEvent => {
installEvent.waitUntil( installEvent.waitUntil(
caches.open(staticDevCoffee).then(cache => { caches.open(service_worker).then(cache => {
cache.addAll(assets) cache.addAll(assets);
}) })
) )
}) })
@ -50,7 +58,8 @@ self.addEventListener('install', installEvent => {
self.addEventListener('fetch', fetchEvent => { self.addEventListener('fetch', fetchEvent => {
fetchEvent.respondWith( fetchEvent.respondWith(
caches.match(fetchEvent.request).then(res => { caches.match(fetchEvent.request).then(res => {
return res || fetch(fetchEvent.request) return res || fetch(fetchEvent.request);
}) })
) )
}) })

View file

@ -0,0 +1,84 @@
<?php
/**
* Service Worker
*/
namespace wishthis;
$assets = array();
$css = array_map(
function ($stylesheet) {
return '/src/assets/css/' . $stylesheet;
},
scandir(ROOT . '/src/assets/css')
);
$css[] = '/src/assets/css/default/dark.css';
$img = array_merge(
array_map(
function ($image) {
return '/src/assets/img/' . $image;
},
scandir(ROOT . '/src/assets/img')
),
array_map(
function ($favicon) {
return '/src/assets/img/favicon/' . $favicon;
},
scandir(ROOT . '/src/assets/img/favicon')
)
);
$js = array_merge(
array_map(
function ($script) {
return '/src/assets/js/' . $script;
},
scandir(ROOT . '/src/assets/js')
),
array_map(
function ($script) {
return '/src/assets/js/parts/' . $script;
},
scandir(ROOT . '/src/assets/js/parts')
)
);
$assets = array_merge($css, $img, $js);
ob_start();
?>
const service_worker = 'wishthis';
const assets = [
<?php
foreach ($assets as $asset) {
$pathinfo = pathinfo($asset);
if (!in_array($pathinfo['basename'], array('.', '..'), true) && isset($pathinfo['extension']) && 'php' !== $pathinfo['extension']) {
echo '\'' . $asset . '\',' . "\n ";
}
}
?>
];
self.addEventListener('install', installEvent => {
installEvent.waitUntil(
caches.open(service_worker).then(cache => {
cache.addAll(assets);
})
)
})
self.addEventListener('fetch', fetchEvent => {
fetchEvent.respondWith(
caches.match(fetchEvent.request).then(res => {
return res || fetch(fetchEvent.request);
})
)
})
<?php
file_put_contents(ROOT . '/service-worker.js', ob_get_clean());

View file

@ -44,6 +44,9 @@ if ('POST' === $_SERVER['REQUEST_METHOD']) {
$options->setOption('version', VERSION); $options->setOption('version', VERSION);
$options->setOption('updateAvailable', false); $options->setOption('updateAvailable', false);
/** Update service-worker.js */
require ROOT . '/src/assets/js/service-worker.js.php';
$page->messages[] = Page::success( $page->messages[] = Page::success(
sprintf( sprintf(
__('Database successfully migrated to %s.'), __('Database successfully migrated to %s.'),