Added serviceWorker

This commit is contained in:
Timon Ostertun
2020-09-23 19:58:35 +02:00
parent b06e2bf6ce
commit 8855128600
3 changed files with 95 additions and 4 deletions

View File

@@ -8,7 +8,7 @@
//Loading the Service Worker //Loading the Service Worker
if ('serviceWorker' in navigator) { if ('serviceWorker' in navigator) {
window.addEventListener('load', function() { window.addEventListener('load', function() {
// TODO navigator.serviceWorker.register('_service-worker.js', {scope: ''}); navigator.serviceWorker.register('<?php echo SERVER_ADDR; ?>/service-worker.js.php');
}); });
} }

View File

@@ -5,6 +5,8 @@
require_once(__DIR__ . '/server/templates.php'); require_once(__DIR__ . '/server/templates.php');
require_once(__DIR__ . '/server/scripts.php'); require_once(__DIR__ . '/server/scripts.php');
define('LINK_PRE', SERVER_ADDR . '/');
$request = false; $request = false;
if (isset($_GET['request'])) { if (isset($_GET['request'])) {
$request = explode('/', $_GET['request']); $request = explode('/', $_GET['request']);
@@ -18,11 +20,10 @@
$site = ''; $site = '';
} }
if ($site == '') { if ($site == '') {
$site = 'index'; header('Location: ' . LINK_PRE . 'index');
exit;
} }
define('LINK_PRE', SERVER_ADDR . '/');
if (!file_exists(__DIR__ . '/content/' . $site . '.php')) { if (!file_exists(__DIR__ . '/content/' . $site . '.php')) {
$site = '404'; $site = '404';
} }

90
service-worker.js.php Normal file
View File

@@ -0,0 +1,90 @@
<?php
header('Content-Type: text/javascript');
require_once(__DIR__ . '/server/config.php');
?>
importScripts('https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js');
//Workbox Config
workbox.setConfig({
debug: false //set to true if you want to see SW in action.
});
workbox.core.setCacheNameDetails({
prefix: 'regatten-<?php echo BOATCLASS; ?>',
suffix: 'v1',
precache: 'regatten-<?php echo BOATCLASS; ?>-precache',
runtime: 'regatten-<?php echo BOATCLASS; ?>-runtime'
});
workbox.precaching.precacheAndRoute([
<?php
// CONTENT
$path = __DIR__ . '/content/';
$dir = opendir($path);
while ($file = readdir($dir)) {
if (($file == '.') or ($file == '..') or (pathinfo($file, PATHINFO_EXTENSION) != 'php')) continue;
$revision = filemtime($path . $file);
$file = SERVER_ADDR . '/' . pathinfo($file, PATHINFO_FILENAME);
echo "\t{url: '$file', revision: '$revision'},\n";
}
closedir($dir);
// ASSETS
$filesToCache = [
'/manifest.json.php',
];
$dirsToCache = [
'/client',
];
function addDir($path) {
global $filesToCache;
if ($dir = opendir(__DIR__ . $path)) {
while (($file = readdir($dir)) !== false) {
if ($file == '.') continue;
if ($file == '..') continue;
if (is_dir(__DIR__ . $path . '/' . $file)) {
addDir($path . '/' . $file);
} else {
$filesToCache[] = $path . '/' . $file;
}
}
closedir($dir);
}
}
foreach ($dirsToCache as $path) {
addDir($path);
}
foreach ($filesToCache as $file) {
$revision = filemtime(__DIR__ . $file);
$file = SERVER_ADDR . $file;
echo "\t{url: '$file', revision: '$revision'},\n";
}
?>
], {
ignoreURLParametersMatching: [/.*/]
});
workbox.routing.registerRoute(
({request}) => {
if (request.destination === 'style') return true;
if (request.destination === 'script') return true;
if (request.destination === 'image') return true;
if (request.destination === 'font') return true;
return false;
},
new workbox.strategies.StaleWhileRevalidate({
})
);
//Learn more about Service Workers and Configurations
//https://developers.google.com/web/tools/workbox/