-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathworker.php
95 lines (72 loc) · 2.14 KB
/
worker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
// A quick and dirty way to create a working Offline Application worker :)
header('Content-Type: text/javascript');
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
?>
var CACHE = 'rewtro-cache-v1.2';
var precacheFiles =
<?php
function getDirContents($dir, &$results = array()){
$files = scandir($dir);
foreach($files as $key => $value){
$path = realpath($dir.DIRECTORY_SEPARATOR.$value);
if(!is_dir($path)) {
$results[] = $path;
} else if($value != "." && $value != "..") {
getDirContents($path, $results);
}
}
return $results;
}
$path=realpath("libs");
$files=getDirContents($path);
$out=[];
for ($i=0;$i<count($files);$i++) {
$filename=substr($files[$i],strlen($path));
if ($filename[0]==DIRECTORY_SEPARATOR) $filename=substr($filename, 1);
$filename="libs/".str_replace("\\","/",$filename);
array_push($out,$filename);
}
echo json_encode($out);
?>
;
self.addEventListener('install', function(evt) {
evt.waitUntil(precache().then(function() {
return self.skipWaiting();
})
);
});
self.addEventListener('activate', function(event) {
return self.clients.claim();
});
self.addEventListener('fetch', function(evt) {
evt.respondWith(fromCache(evt).catch(fromServer(evt.request)));
evt.waitUntil(update(evt.request));
});
function precache() {
return caches.open(CACHE).then(function (cache) {
return cache.addAll(precacheFiles);
});
}
function fromCache(evt) {
return caches.open(CACHE).then(function (cache) {
return cache.match(evt.request).then(function (matching) {
return matching || fetch(evt.request).then(function(response) {
cache.put(evt.request, response.clone());
return response;
});
});
});
}
function update(request) {
return caches.open(CACHE).then(function (cache) {
return fetch(request).then(function (response) {
return cache.put(request, response);
});
});
}
function fromServer(request){
return fetch(request).then(function(response){ return response})
}