Add a service worker generator and registration code user/dbs/pwa_myopac_caching
authorDan Scott <dscott@laurentian.ca>
Tue, 4 Apr 2017 04:11:32 +0000 (00:11 -0400)
committerDan Scott <dscott@laurentian.ca>
Tue, 4 Apr 2017 04:11:32 +0000 (00:11 -0400)
Signed-off-by: Dan Scott <dscott@laurentian.ca>
Open-ILS/src/templates/opac/parts/base.tt2
Open-ILS/src/templates/opac/parts/sw-register.tt2 [new file with mode: 0644]
Open-ILS/web/service-worker/package.json [new file with mode: 0644]
Open-ILS/web/service-worker/sw-precache.js [new file with mode: 0644]

index 2b0cb2c..3c79fd8 100644 (file)
@@ -51,5 +51,6 @@
         [%- IF ENV.OILS_CHILIFRESH_ACCOUNT %]
             [%- INCLUDE 'opac/parts/chilifresh.tt2' %]
         [%- END %]
+        [% INCLUDE 'opac/parts/sw-register.tt2' %]
     </body>
 </html>
diff --git a/Open-ILS/src/templates/opac/parts/sw-register.tt2 b/Open-ILS/src/templates/opac/parts/sw-register.tt2
new file mode 100644 (file)
index 0000000..2c7304e
--- /dev/null
@@ -0,0 +1,61 @@
+</script>
+/**
+ * Copyright 2015 Google Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+// Source: https://github.com/GoogleChrome/sw-precache/blob/master/demo/app/js/service-worker-registration.js
+/* eslint-env browser */
+'use strict';
+if ('serviceWorker' in navigator) {
+  // Delay registration until after the page has loaded, to ensure that our
+  // precaching requests don't degrade the first visit experience.
+  // See https://developers.google.com/web/fundamentals/instant-and-offline/service-worker/registration
+  window.addEventListener('load', function() {
+    // Your service-worker.js *must* be located at the top-level directory relative to your site.
+    // It won't be able to control pages unless it's located at the same level or higher than them.
+    // *Don't* register service worker file in, e.g., a scripts/ sub-directory!
+    // See https://github.com/slightlyoff/ServiceWorker/issues/468
+    navigator.serviceWorker.register('/sw.js').then(function(reg) {
+      // updatefound is fired if service-worker.js changes.
+      reg.onupdatefound = function() {
+        // The updatefound event implies that reg.installing is set; see
+        // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#service-worker-container-updatefound-event
+        var installingWorker = reg.installing;
+        installingWorker.onstatechange = function() {
+          switch (installingWorker.state) {
+            case 'installed':
+              if (navigator.serviceWorker.controller) {
+                // At this point, the old content will have been purged and the fresh content will
+                // have been added to the cache.
+                // It's the perfect time to display a "New content is available; please refresh."
+                // message in the page's interface.
+                console.log('New or updated content is available.');
+              } else {
+                // At this point, everything has been precached.
+                // It's the perfect time to display a "Content is cached for offline use." message.
+                console.log('Content is now available offline!');
+              }
+              break;
+            case 'redundant':
+              console.error('The installing service worker became redundant.');
+              break;
+          }
+        };
+      };
+    }).catch(function(e) {
+      console.error('Error during service worker registration:', e);
+    });
+  });
+}
+</script>
diff --git a/Open-ILS/web/service-worker/package.json b/Open-ILS/web/service-worker/package.json
new file mode 100644 (file)
index 0000000..eff64f5
--- /dev/null
@@ -0,0 +1,10 @@
+{
+  "name": "evergreen-service-worker",
+  "version": "1.0.0",
+  "description": "Generates a service worker for the Evergreen library system",
+  "author": "Dan Scott",
+  "license": "GPL-2.0+",
+  "dependencies": {
+    "sw-precache": ">4.3.0"
+  }
+}
diff --git a/Open-ILS/web/service-worker/sw-precache.js b/Open-ILS/web/service-worker/sw-precache.js
new file mode 100644 (file)
index 0000000..69be34f
--- /dev/null
@@ -0,0 +1,37 @@
+module.exports = {
+  staticFileGlobs: [
+    '/openils/var/web/favicon.ico',
+    '/openils/var/web/offline.html',
+    '/openils/var/web/js/sw-register.js',
+    '/openils/var/web/css/skin/default/opac/semiauto.css',
+    '/openils/var/web/js/ui/default/opac/simple.js',
+    '/openils/var/web/opac/images/small_logo.png',
+    '/openils/var/web/opac/images/progressbar_green.png',
+    '/openils/var/web/opac/images/main_logo.png',
+    '/openils/var/web/opac/images/eg_tiny_logo.png'
+  ],
+  stripPrefix: '/openils/var/web/',
+  runtimeCaching: [
+    // aggressively cache images
+    {
+      urlPattern: /\.(gif|jpg|jpeg|png)$/,
+      handler: 'cacheFirst'
+    },
+    // aggressively cache stylesheets and JavaScript
+    {
+      urlPattern: /\.(css|js)$/,
+      handler: 'cacheFirst'
+    },
+    // cache added content too
+    {
+      urlPattern: /\/opac\/extras\/ac\//,
+      handler: 'cacheFirst'
+    },
+    // prefer the network, but fall back to the cache
+    // for safety, add https:\/\/hostname to the start of the urlPattern
+    {
+      urlPattern: /\/(eg|js|opac|staff)\//,
+      handler: 'networkFirst'
+    }
+  ]
+};