aboutsummaryrefslogtreecommitdiff
path: root/docs/_book/gitbook/gitbook-plugin-offline/service-worker-registration.js
diff options
context:
space:
mode:
authorJakob Stendahl <14180120+JakobST1n@users.noreply.github.com>2018-12-01 00:28:28 +0100
committerGitHub <noreply@github.com>2018-12-01 00:28:28 +0100
commit19df9946b438b38b9a4f3f57ad002981a1ae1eaf (patch)
treee8eb839a305502584e31e1b1a96c70406b9b6255 /docs/_book/gitbook/gitbook-plugin-offline/service-worker-registration.js
parentfd7a1b51126f8b8f889807cb7d56bb3626a0e0b5 (diff)
parent7ec685de6e441af1f614bb9d18e25c047d21466b (diff)
downloadLuxcena-Neo-19df9946b438b38b9a4f3f57ad002981a1ae1eaf.tar.gz
Luxcena-Neo-19df9946b438b38b9a4f3f57ad002981a1ae1eaf.zip
Merge pull request #6 from JakobST1n/dev
Written docs, finished CLI
Diffstat (limited to 'docs/_book/gitbook/gitbook-plugin-offline/service-worker-registration.js')
-rw-r--r--docs/_book/gitbook/gitbook-plugin-offline/service-worker-registration.js62
1 files changed, 62 insertions, 0 deletions
diff --git a/docs/_book/gitbook/gitbook-plugin-offline/service-worker-registration.js b/docs/_book/gitbook/gitbook-plugin-offline/service-worker-registration.js
new file mode 100644
index 0000000..e89448d
--- /dev/null
+++ b/docs/_book/gitbook/gitbook-plugin-offline/service-worker-registration.js
@@ -0,0 +1,62 @@
+/**
+ * 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.
+ */
+
+/* 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('service-worker.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);
+ });
+ });
+}