This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
forked from SuperPWA/super-progressive-web-apps
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsw.php
228 lines (197 loc) · 6.17 KB
/
sw.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/**
* Service worker related functions of SuperPWA
*
* @since 1.0
*
* @function superpwa_sw() Service worker filename, absolute path and link
* @function superpwa_generate_sw() Generate and write service worker into sw.js
* @function superpwa_sw_template() Service worker tempalte
* @function superpwa_register_sw() Register service worker
* @function superpwa_delete_sw() Delete service worker
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
/**
* Service worker filename, absolute path and link
*
* For Multisite compatibility. Used to be constants defined in superpwa.php
* On a multisite, each sub-site needs a different service worker.
*
* @param $arg filename for service worker filename (replaces SUPERPWA_SW_FILENAME)
* abs for absolute path to service worker (replaces SUPERPWA_SW_ABS)
* src for relative link to service worker (replaces SUPERPWA_SW_SRC). Default value
*
* @return (string) filename, absolute path or link to manifest.
*
* @since 1.6
* @since 1.7 src to service worker is made relative to accomodate for domain mapped multisites.
* @since 1.8 Added filter superpwa_sw_filename.
*/
function superpwa_sw( $arg = 'src' ) {
$sw_filename = apply_filters( 'superpwa_sw_filename', 'superpwa-sw' . superpwa_multisite_filename_postfix() . '.js' );
switch( $arg ) {
// Name of service worker file
case 'filename':
return $sw_filename;
break;
// Absolute path to service worker. SW must be in the root folder
case 'abs':
return trailingslashit( ABSPATH ) . $sw_filename;
break;
// Link to service worker
case 'src':
default:
return parse_url( trailingslashit( network_site_url() ) . $sw_filename, PHP_URL_PATH );
break;
}
}
/**
* Generate and write service worker into superpwa-sw.js
*
* @return (boolean) true on success, false on failure.
*
* @since 1.0
*/
function superpwa_generate_sw() {
// Get Settings
$settings = superpwa_get_settings();
// Get the service worker tempalte
$sw = superpwa_sw_template();
// Delete service worker if it exists
superpwa_delete_sw();
if ( ! superpwa_put_contents( superpwa_sw( 'abs' ), $sw ) ) {
return false;
}
return true;
}
/**
* Service Worker Tempalte
*
* @return (string) Contents to be written to superpwa-sw.js
*
* @since 1.0
* @since 1.7 added filter superpwa_sw_template
*/
function superpwa_sw_template() {
// Get Settings
$settings = superpwa_get_settings();
// Start output buffer. Everything from here till ob_get_clean() is returned
ob_start(); ?>
'use strict';
/**
* Service Worker of SuperPWA
* To learn more and add one to your website, visit - https://superpwa.com
*/
const cacheName = '<?php echo parse_url( get_bloginfo( 'wpurl' ), PHP_URL_HOST ) . '-superpwa-' . SUPERPWA_VERSION; ?>';
const startPage = '<?php echo superpwa_get_start_url(); ?>';
const offlinePage = '<?php echo get_permalink( $settings['offline_page'] ) ? superpwa_httpsify( get_permalink( $settings['offline_page'] ) ) : superpwa_httpsify( get_bloginfo( 'wpurl' ) ); ?>';
const fallbackImage = '<?php echo $settings['icon']; ?>';
const filesToCache = [startPage, offlinePage, fallbackImage];
const neverCacheUrls = [<?php echo apply_filters( 'superpwa_sw_never_cache_urls', '/\/wp-admin/,/\/wp-login/,/preview=true/' ); ?>];
// Install
self.addEventListener('install', function(e) {
console.log('SuperPWA service worker installation');
e.waitUntil(
caches.open(cacheName).then(function(cache) {
console.log('SuperPWA service worker caching dependencies');
return cache.addAll(filesToCache);
})
);
});
// Activate
self.addEventListener('activate', function(e) {
console.log('SuperPWA service worker activation');
e.waitUntil(
caches.keys().then(function(keyList) {
return Promise.all(keyList.map(function(key) {
if ( key !== cacheName ) {
console.log('SuperPWA old cache removed', key);
return caches.delete(key);
}
}));
})
);
return self.clients.claim();
});
// Fetch
self.addEventListener('fetch', function(e) {
// Return if the current request url is in the never cache list
if ( ! neverCacheUrls.every(checkNeverCacheList, e.request.url) ) {
console.log( 'SuperPWA: Current request is excluded from cache.' );
return;
}
// Return if request url protocal isn't http or https
if ( ! e.request.url.match(/^(http|https):\/\//i) )
return;
// Return if request url is from an external domain.
if ( new URL(e.request.url).origin !== location.origin )
return;
// For POST requests, do not use the cache. Serve offline page if offline.
if ( e.request.method !== 'GET' ) {
e.respondWith(
fetch(e.request).catch( function() {
return caches.match(offlinePage);
})
);
return;
}
// Revving strategy
if ( e.request.mode === 'navigate' && navigator.onLine ) {
e.respondWith(
fetch(e.request).then(function(response) {
return caches.open(cacheName).then(function(cache) {
cache.put(e.request, response.clone());
return response;
});
})
);
return;
}
e.respondWith(
caches.match(e.request).then(function(response) {
return response || fetch(e.request).then(function(response) {
return caches.open(cacheName).then(function(cache) {
cache.put(e.request, response.clone());
return response;
});
});
}).catch(function() {
return caches.match(offlinePage);
})
);
});
// Check if current url is in the neverCacheUrls list
function checkNeverCacheList(url) {
if ( this.match(url) ) {
return false;
}
return true;
}
<?php return apply_filters( 'superpwa_sw_template', ob_get_clean() );
}
/**
* Register service worker
*
* @refer https://developers.google.com/web/fundamentals/primers/service-workers/registration#conclusion
*
* @since 1.0
*/
function superpwa_register_sw() {
wp_enqueue_script( 'superpwa-register-sw', SUPERPWA_PATH_SRC . 'public/js/register-sw.js', array(), null, true );
wp_localize_script( 'superpwa-register-sw', 'superpwa_sw', array(
'url' => superpwa_sw( 'src' ),
)
);
}
add_action( 'wp_enqueue_scripts', 'superpwa_register_sw' );
/**
* Delete Service Worker
*
* @return true on success, false on failure
*
* @since 1.0
*/
function superpwa_delete_sw() {
return superpwa_delete( superpwa_sw( 'abs' ) );
}