Updates to the structure of SW cataloging
Update cache name based on filemtime
This commit is contained in:
Родитель
39aa9650d2
Коммит
ff29789fb1
|
@ -1,23 +1,26 @@
|
|||
(function(self){
|
||||
var CACHE_PREFIX = '__wp-sw-cache::';
|
||||
|
||||
var CACHE_NAME = CACHE_PREFIX + $name;
|
||||
|
||||
var CACHE_NAME = CACHE_PREFIX + '$name';
|
||||
var CACHE_FILES = $files;
|
||||
|
||||
var debug = $debug;
|
||||
|
||||
self.addEventListener('install', function(event) {
|
||||
// Perform install step: loading each required file into cache
|
||||
event.waitUntil(
|
||||
caches.open(CACHE_NAME)
|
||||
.then(function(cache) {
|
||||
// Add all offline dependencies to the cache
|
||||
console.log('[install] Caches opened, adding all core components ' +
|
||||
'to cache');
|
||||
if (debug) {
|
||||
console.log('[install] Caches opened, adding all core components to cache');
|
||||
}
|
||||
return cache.addAll(CACHE_FILES);
|
||||
})
|
||||
.then(function() {
|
||||
console.log('[install] All required resources have been cached, ' +
|
||||
'we\'re good!');
|
||||
if (debug) {
|
||||
console.log('[install] All required resources have been cached, we\'re good!');
|
||||
}
|
||||
return self.skipWaiting();
|
||||
})
|
||||
);
|
||||
|
@ -34,13 +37,14 @@
|
|||
caches.match(lookupRequest)
|
||||
.then(function(response) {
|
||||
if (response) {
|
||||
console.log(
|
||||
'[fetch] Returning from ServiceWorker cache: ',
|
||||
event.request.url
|
||||
);
|
||||
if (debug) {
|
||||
console.log('[fetch] Returning from ServiceWorker cache: ', event.request.url);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
console.error('[fetch] Cache miss! This should not happen. It implies problems caching.');
|
||||
if (debug) {
|
||||
console.error('[fetch] Cache miss! This should not happen. It implies problems caching.');
|
||||
}
|
||||
return fetch(event.request);
|
||||
}
|
||||
)
|
||||
|
@ -72,14 +76,18 @@
|
|||
}
|
||||
|
||||
self.addEventListener('activate', function(event) {
|
||||
console.log('[activate] Activating ServiceWorker!');
|
||||
if (debug) {
|
||||
console.log('[activate] Activating ServiceWorker!');
|
||||
}
|
||||
|
||||
// Clean up old cache in the background
|
||||
caches.keys().then(function(cacheNames) {
|
||||
return Promise.all(
|
||||
cacheNames.map(function(cacheName) {
|
||||
if(cacheName.startsWith(CACHE_PREFIX) && cacheName != CACHE_NAME) {
|
||||
console.log('[activate] Deleting out of date cache:', cacheName);
|
||||
if (debug) {
|
||||
console.log('[activate] Deleting out of date cache:', cacheName);
|
||||
}
|
||||
return caches.delete(cacheName);
|
||||
}
|
||||
})
|
||||
|
@ -87,7 +95,9 @@
|
|||
});
|
||||
|
||||
// Calling claim() to force a "controllerchange" event on navigator.serviceWorker
|
||||
console.log('[activate] Claiming this ServiceWorker!');
|
||||
if (debug) {
|
||||
console.log('[activate] Claiming this ServiceWorker!');
|
||||
}
|
||||
event.waitUntil(self.clients.claim());
|
||||
});
|
||||
})(self);
|
||||
|
|
|
@ -24,19 +24,14 @@ class SW_Cache_Admin {
|
|||
// Update "enabled" status
|
||||
update_option('wp_sw_cache_enabled', isset($_POST['wp_sw_cache_enabled']));
|
||||
|
||||
// Update "prefix" value
|
||||
if(isset($_POST['wp_sw_cache_name'])) {
|
||||
update_option('wp_sw_cache_name', $_POST['wp_sw_cache_name']);
|
||||
}
|
||||
else {
|
||||
update_option('wp_sw_cache_name', SW_Cache_DB::$cache_prefix.'-'.time());
|
||||
}
|
||||
// Update "debug" status
|
||||
update_option('wp_sw_cache_debug', isset($_POST['wp_sw_cache_debug']));
|
||||
|
||||
// Update files to cache
|
||||
$files = array();
|
||||
if(isset($_POST['wp_sw_cache_files'])) {
|
||||
foreach($_POST['wp_sw_cache_files'] as $file) {
|
||||
array_push($files, stripslashes(htmlspecialchars_decode($file)));
|
||||
array_push($files, stripslashes($file));
|
||||
}
|
||||
}
|
||||
update_option('wp_sw_cache_files', $files);
|
||||
|
@ -84,6 +79,25 @@ class SW_Cache_Admin {
|
|||
|
||||
?>
|
||||
|
||||
<style>
|
||||
.wp-sw-cache-suggested {
|
||||
background: lightgreen;
|
||||
}
|
||||
|
||||
.wp-sw-cache-suggest-file,
|
||||
.wp-sw-cache-toggle-all {
|
||||
float: right;
|
||||
margin-left: 10px !important;
|
||||
}
|
||||
|
||||
.wp-sw-cache-file-list {
|
||||
max-height: 300px;
|
||||
background: #fefefe;
|
||||
border: 1px solid #ccc;
|
||||
padding: 10px;
|
||||
overflow-y: auto;
|
||||
}
|
||||
</style>
|
||||
<div class="wrap">
|
||||
|
||||
<?php if($submitted) { ?>
|
||||
|
@ -92,7 +106,7 @@ class SW_Cache_Admin {
|
|||
</div>
|
||||
<?php } ?>
|
||||
|
||||
<h1><?php _e('WordPress Service Worker Cache', 'wpswcache'); ?></h1>
|
||||
<h1><?php _e('WordPress Service Worker Cache', 'wpswcache'); ?> (<?php echo SW_Cache_Main::$cache_prefix; ?>)</h1>
|
||||
|
||||
<p><?php _e('WordPress Service Worker Cache is a ultility that harnesses the power of the <a href="https://serviceworke.rs" target="_blank">ServiceWorker API</a> to cache frequently used assets for the purposes of performance and offline viewing.'); ?></p>
|
||||
|
||||
|
@ -108,10 +122,15 @@ class SW_Cache_Admin {
|
|||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row"><label for="wp_sw_cache_name"><?php _e('Cache Name', 'wpswcache'); ?></label></th>
|
||||
<th scope="row"><label for="wp_sw_cache_debug"><?php _e('Enable Debug Messages', 'wpswcache'); ?></label></th>
|
||||
<td>
|
||||
<input type="text" name="wp_sw_cache_name" id="wp_sw_cache_name" value="<?php echo esc_attr__(get_option('wp_sw_cache_name')); ?>" class="regular-text ltr" disabled />
|
||||
<?php _e('(Will update upon save for cache-busting purposes.)'); ?>
|
||||
<input type="checkbox" name="wp_sw_cache_debug" id="wp_sw_cache_debug" value="1" <?php if(get_option('wp_sw_cache_debug')) echo 'checked'; ?> />
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row"><label for="wp_sw_cache_name"><?php _e('Current Cache Name', 'wpswcache'); ?></label></th>
|
||||
<td>
|
||||
<em><?php echo get_option('wp_sw_cache_name'); ?></em>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
@ -122,6 +141,8 @@ class SW_Cache_Admin {
|
|||
<button type="button" class="button button-primary wp-sw-cache-toggle-all"><?php _e('Select All Files'); ?></button>
|
||||
<button type="button" class="button button-primary wp-sw-cache-suggest-file" data-suggested-text="<?php echo esc_attr__('Files Suggested: '); ?>"><?php _e('Suggest More Files'); ?></button>
|
||||
</p>
|
||||
|
||||
<?php /* <pre><?php print_r($selected_files); ?></pre> */ ?>
|
||||
<div class="wp-sw-cache-file-list">
|
||||
|
||||
<?php
|
||||
|
@ -158,10 +179,10 @@ class SW_Cache_Admin {
|
|||
<?php foreach($category['files'] as $file) { $file_id++; ?>
|
||||
<tr>
|
||||
<td style="width: 30px;">
|
||||
<input type="checkbox" name="wp_sw_cache_files[]" id="wp_sw_cache_files['file_<?php echo $file_id; ?>']" value="<?php echo htmlspecialchars($file); ?>" <?php if(in_array($file, $selected_files)) { echo 'checked'; } ?> />
|
||||
<input type="checkbox" name="wp_sw_cache_files[]" id="wp_sw_cache_files['file_<?php echo $file_id; ?>']" value="<?php echo esc_attr__($file); ?>" <?php if(in_array($file, $selected_files)) { echo 'checked'; } ?> />
|
||||
</td>
|
||||
<td>
|
||||
<label for="wp_sw_cache_files['file_<?php echo $file_id; ?>']"><?php echo htmlspecialchars($file); ?></label>
|
||||
<label for="wp_sw_cache_files['file_<?php echo $file_id; ?>']"><?php echo $file; ?></label>
|
||||
</td>
|
||||
</tr>
|
||||
<?php } ?>
|
||||
|
@ -175,7 +196,7 @@ class SW_Cache_Admin {
|
|||
|
||||
<h2>Clear Caches</h2>
|
||||
<p><?php _e('Click the button below to clear any caches created by this plugin.'); ?></p>
|
||||
<button type="button" class="button button-primary wp-sw-cache-clear-caches-button" data-cleared-text="<?php echo esc_attr__('Caches cleared: '); ?>"><?php _e('Clear Caches'); ?></button>
|
||||
<button type="button" class="button button-primary wp-sw-cache-clear-caches-button" data-cleared-text="<?php echo esc_attr('Caches cleared: '); ?>"><?php _e('Clear Caches'); ?></button>
|
||||
|
||||
</div>
|
||||
|
||||
|
@ -213,7 +234,7 @@ class SW_Cache_Admin {
|
|||
return Promise.all(
|
||||
cacheNames.map(function(cacheName) {
|
||||
|
||||
if(cacheName.indexOf('<?php echo SW_Cache_DB::$cache_prefix; ?>') != -1) {
|
||||
if(cacheName.indexOf('<?php echo SW_Cache_Main::$cache_prefix; ?>') != -1) {
|
||||
console.log('Clearing cache: ', cacheName);
|
||||
clearedCounter++;
|
||||
return caches.delete(cacheName);
|
||||
|
@ -231,26 +252,6 @@ class SW_Cache_Admin {
|
|||
});
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.wp-sw-cache-suggested {
|
||||
background: lightgreen;
|
||||
}
|
||||
|
||||
.wp-sw-cache-suggest-file,
|
||||
.wp-sw-cache-toggle-all {
|
||||
float: right;
|
||||
margin-left: 10px !important;
|
||||
}
|
||||
|
||||
.wp-sw-cache-file-list {
|
||||
max-height: 300px;
|
||||
background: #fefefe;
|
||||
border: 1px solid #ccc;
|
||||
padding: 10px;
|
||||
overflow-y: auto;
|
||||
}
|
||||
</style>
|
||||
|
||||
<?php
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
class SW_Cache_DB {
|
||||
|
||||
private static $instance;
|
||||
public static $cache_prefix = 'wp-sw-cache';
|
||||
|
||||
public function __construct() {
|
||||
}
|
||||
|
@ -19,8 +18,9 @@ class SW_Cache_DB {
|
|||
public static function on_activate() {
|
||||
// Set default options.
|
||||
update_option('wp_sw_cache_enabled', false);
|
||||
update_option('wp_sw_cache_name', self::$cache_prefix.'-'.time());
|
||||
update_option('wp_sw_cache_files', array());
|
||||
SW_Cache_Main::update_version();
|
||||
update_option('wp_sw_cache_debug', true);
|
||||
}
|
||||
|
||||
public static function on_deactivate() {
|
||||
|
@ -30,6 +30,7 @@ class SW_Cache_DB {
|
|||
delete_option('wp_sw_cache_enabled');
|
||||
delete_option('wp_sw_cache_name');
|
||||
delete_option('wp_sw_cache_files');
|
||||
delete_option('wp_sw_cache_debug');
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ load_plugin_textdomain('wpswcache', false, dirname(plugin_basename(__FILE__)) .
|
|||
|
||||
class SW_Cache_Main {
|
||||
private static $instance;
|
||||
public static $cache_prefix = 'wp-sw-cache';
|
||||
|
||||
public function __construct() {
|
||||
if (get_option('wp_sw_cache_enabled')) {
|
||||
|
@ -21,18 +22,36 @@ class SW_Cache_Main {
|
|||
}
|
||||
}
|
||||
|
||||
public function update_version($name = '') {
|
||||
if(!$name) {
|
||||
$name = time();
|
||||
}
|
||||
update_option('wp_sw_cache_name', self::$cache_prefix.'-'.$name);
|
||||
}
|
||||
|
||||
public function write_sw() {
|
||||
|
||||
$files = get_option('wp_sw_cache_files');
|
||||
$file_keys = array();
|
||||
if(!$files) {
|
||||
$files = array();
|
||||
$files = array();
|
||||
}
|
||||
foreach($files as $index=>$file) {
|
||||
$files[$index] = get_template_directory_uri().'/'.$file;
|
||||
$tfile = get_template_directory().'/'.$file;
|
||||
|
||||
if(file_exists($tfile)) {
|
||||
$file_keys[get_template_directory_uri().'/'.$file] = filemtime($tfile);
|
||||
}
|
||||
}
|
||||
|
||||
$file_keys = array_keys($file_keys);
|
||||
$name = md5(serialize($file_keys));
|
||||
self::update_version($name);
|
||||
|
||||
$contents = file_get_contents(dirname(__FILE__).'/lib/service-worker.js');
|
||||
$contents = str_replace('$name', json_encode(get_option('wp_sw_cache_name')), $contents);
|
||||
$contents = str_replace('$files', json_encode($files), $contents);
|
||||
$contents = str_replace('$name', $name, $contents);
|
||||
$contents = str_replace('$files', json_encode($file_keys), $contents);
|
||||
$contents = str_replace('$debug', get_option('wp_sw_cache_debug') ? 'true' : 'false', $contents);
|
||||
echo $contents;
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче