Plugin Directory

Changeset 1354091


Ignore:
Timestamp:
02/19/2016 12:34:46 PM (10 years ago)
Author:
delapuente
Message:

Now cleaning older caches

Location:
offline-content/trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • offline-content/trunk/lib/js/sw.js

    r1347574 r1354091  
    11
    22(function (self) {
    3   self.addEventListener('install', event => {
    4     event.waitUntil(Promise.all([
    5       self.skipWaiting(),
    6       wpOfflineContent.precache()
    7     ]));
    8   });
    9 
    10   self.addEventListener('activate', event => {
    11     event.waitUntil(self.clients.claim());
    12   });
    13 
    14   self.addEventListener('fetch', event => {
    15     event.respondWith(wpOfflineContent.get(event.request));
    16   });
     3  var CACHE_PREFIX = '__wp-offline-content::';
    174
    185  var wpOfflineContent = self.wpOfflineContent = {
     
    2411    debug: $debug,
    2512
    26     cacheName: $cacheName,
     13    cacheName: CACHE_PREFIX + $cacheName,
    2714
    2815    networkTimeout: $networkTimeout,
     
    3623    origin: self.location.origin,
    3724
     25    onInstall: function (event) {
     26      event.waitUntil(Promise.all([
     27        self.skipWaiting(),
     28        wpOfflineContent.precache()
     29      ]));
     30    },
     31
     32    onActivate: function (event) {
     33      event.waitUntil(Promise.all([self.clients.claim(), this.deleteOutdatedCaches(CACHE_PREFIX)]));
     34    },
     35
     36    onFetch: function (event) {
     37      var request = event.request;
     38      if (this.shouldBeHandled(request)) {
     39        event.respondWith(wpOfflineContent.get(request));
     40      }
     41    },
     42
     43    shouldBeHandled: function (request) {
     44      return request.method === 'GET' && !this.isExcluded(request.url);
     45    },
     46
    3847    precache: function () {
    3948      return this.openCache().then(cache => cache.addAll(this.resources.map(entry => entry[0])));
     49    },
     50
     51    deleteOutdatedCaches: function (prefix) {
     52      return self.caches.keys().then(names => {
     53        return Promise.all(names.map(cacheName => {
     54          if (cacheName.startsWith(prefix) && cacheName !== this.cacheName) {
     55            return self.caches.delete(cacheName);
     56          }
     57          return Promise.resolve();
     58        }));
     59      });
    4060    },
    4161
     
    4969        throw error;
    5070      });
    51 
    52       if (request.method !== 'GET' || this.isExcluded(url)) {
    53         return fetchFromNetwork;
    54       }
    5571
    5672      var fetchAndCache = fetchFromNetwork.then(responseFromNetwork => {
     
    124140    }
    125141  };
     142
     143  self.addEventListener('install', wpOfflineContent.onInstall.bind(wpOfflineContent));
     144  self.addEventListener('activate', wpOfflineContent.onActivate.bind(wpOfflineContent));
     145  self.addEventListener('fetch', wpOfflineContent.onFetch.bind(wpOfflineContent));
     146
    126147})(self);
  • offline-content/trunk/readme.txt

    r1351108 r1354091  
    44Requires at least: 3.7
    55Tested up to: 4.4.1
    6 Stable tag: 0.2.0
     6Stable tag: 0.3.0
    77License: GPLv2 or later
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    4444== Change Log ==
    4545
     46= 0.3.0 =
     47Cleaning old caches when changing the name of the cache where offline content is stored.
     48
    4649= 0.2.0 =
    4750Now can be combined with other WP plugins using the [WordPress Service Worker Manager library](https://github.com/mozilla/wp-sw-manager/blob/master/README.md) such as [Web Push](https://wordpress.org/plugins/web-push/).
  • offline-content/trunk/vendor/mozilla/wp-sw-manager/README.md

    r1351108 r1354091  
    99
    1010```php
    11 WP_SW_Manager::get_manager()->sw()->add_contents(write_sw);
     11include_once(plugins_url(__FILE__) . /vendor/mozilla/wp-sw-manager);
     12
     13WP_SW_Manager::get_manager()->sw()->add_content(write_sw);
    1214
    1315function write_sw() {
     
    5456
    5557```php
    56 $swmgr->sw()->add_contents(write_sw);
     58$swmgr->sw()->add_content(write_sw);
    5759
    5860function write_sw() {
     
    6466
    6567```php
    66 $swmgr->sw()->add_contents(array($this, 'write_sw'));
     68$swmgr->sw()->add_content(array($this, 'write_sw'));
    6769
    6870public function write_sw() {
     
    7476
    7577```php
    76 $swmgr->sw()->add_contents(array($this, 'write_sw'));
     78$swmgr->sw()->add_content(array($this, 'write_sw'));
    7779
    7880public function write_sw() {
  • offline-content/trunk/vendor/mozilla/wp-sw-manager/class-wp-sw-manager.php

    r1351108 r1354091  
    2525     * ```
    2626     *
    27      * @version 0.1.0
     27     * @version 0.2.0
    2828     * @license GPL
    2929     * @author Salvador de la Puente González <salva@unoyunodiez.com>
     
    134134            $real_absolute_url = $this->router->route_url(self::SW_REGISTRAR_SCRIPT_URL);
    135135            $relative_to_root_url = ltrim($real_absolute_url, site_url('', 'relative'));
    136             wp_register_script(self::SW_REGISTRAR_SCRIPT, $relative_to_root_url);
     136            wp_enqueue_script(self::SW_REGISTRAR_SCRIPT, $relative_to_root_url);
    137137        }
    138138
  • offline-content/trunk/wp-offline-content.php

    r1351108 r1354091  
    44Description: Allow your users to read your content even while offline.
    55Plugin URI: https://github.com/delapuente/wp-offline-content
    6 Version: 0.2.0
     6Version: 0.3.0
    77Author: Mozilla
    88Author URI: https://www.mozilla.org/
Note: See TracChangeset for help on using the changeset viewer.