Path: blob/trunk/javascript/grid-ui/src/serviceWorker.ts
2884 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617// This optional code is used to register a service worker.18// register() is not called by default.1920// This lets the app load faster on subsequent visits in production, and gives21// it offline capabilities. However, it also means that developers (and users)22// will only see deployed updates on subsequent visits to a page, after all the23// existing tabs open on the page have been closed, since previously cached24// resources are updated in the background.2526// To learn more about the benefits of this model and instructions on how to27// opt-in, read https://bit.ly/CRA-PWA2829/* eslint-disable */3031const isLocalhost = Boolean(32window.location.hostname === 'localhost' ||33// [::1] is the IPv6 localhost address.34window.location.hostname === '[::1]' ||35// 127.0.0.0/8 are considered localhost for IPv4.36window.location.hostname.match(37/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/38)39)4041export function register (config) {42if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {43// The URL constructor is available in all browsers that support SW.44// @ts-ignore45const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href)46if (publicUrl.origin !== window.location.origin) {47// Our service worker won't work if PUBLIC_URL is on a different origin48// from what our page is served on. This might happen if a CDN is used to49// serve assets; see https://github.com/facebook/create-react-app/issues/237450return51}5253window.addEventListener('load', () => {54const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`5556if (isLocalhost) {57// This is running on localhost. Let's check if a service worker still exists or not.58checkValidServiceWorker(swUrl, config)5960// Add some additional logging to localhost, pointing developers to the61// service worker/PWA documentation.62navigator.serviceWorker.ready.then(() => {63console.log(64'This web app is being served cache-first by a service ' +65'worker. To learn more, visit https://bit.ly/CRA-PWA'66)67})68} else {69// Is not localhost. Just register service worker70registerValidSW(swUrl, config)71}72})73}74}7576function registerValidSW (swUrl, config) {77navigator.serviceWorker78.register(swUrl)79.then(registration => {80registration.onupdatefound = () => {81const installingWorker = registration.installing82if (installingWorker == null) {83return84}85installingWorker.onstatechange = () => {86if (installingWorker.state === 'installed') {87if (navigator.serviceWorker.controller != null) {88// At this point, the updated precached content has been fetched,89// but the previous service worker will still serve the older90// content until all client tabs are closed.91console.log(92'New content is available and will be used when all ' +93'tabs for this page are closed. See https://bit.ly/CRA-PWA.'94)9596// Execute callback97if (config && config.onUpdate) {98config.onUpdate(registration)99}100} else {101// At this point, everything has been precached.102// It's the perfect time to display a103// "Content is cached for offline use." message.104console.log('Content is cached for offline use.')105106// Execute callback107if (config && config.onSuccess) {108config.onSuccess(registration)109}110}111}112}113}114})115.catch(error => {116console.error('Error during service worker registration:', error)117})118}119120function checkValidServiceWorker (swUrl, config) {121// Check if the service worker can be found. If it can't reload the page.122window.fetch(swUrl, {123headers: { 'Service-Worker': 'script' }124})125.then(response => {126// Ensure service worker exists, and that we really are getting a JS file.127const contentType = response.headers.get('content-type')128if (129response.status === 404 ||130(contentType != null && !contentType.includes('javascript'))131) {132// No service worker found. Probably a different app. Reload the page.133navigator.serviceWorker.ready.then(registration => {134registration.unregister().then(() => {135window.location.reload()136})137})138} else {139// Service worker found. Proceed as normal.140registerValidSW(swUrl, config)141}142})143.catch(() => {144console.log(145'No internet connection found. App is running in offline mode.'146)147})148}149150export function unregister () {151if ('serviceWorker' in navigator) {152navigator.serviceWorker.ready153.then(registration => {154registration.unregister()155})156.catch(error => {157console.error(error.message)158})159}160}161162163