Property 'serviceWorker' does not exist on type 'WorkerNavigator'

864 views Asked by At

I started to upgrade npm packages in my project, but after removing only 1 package (fontawesome pro icons) I start getting this errors in my webpack ts loader.

ERROR in ./src/serviceWorkerRegistration.ts (\src\serviceWorkerRegistration.ts) 46:18-31

[tsl] ERROR in \src\serviceWorkerRegistration.ts(46,19)
      TS2339: Property 'serviceWorker' does not exist on type 'WorkerNavigator'.


ERROR in ./src/serviceWorkerRegistration.ts (R:\git\vsb\vsb_fe\src\serviceWorkerRegistration.ts) 61:12-25

[tsl] ERROR in \src\serviceWorkerRegistration.ts(61,13)
      TS2339: Property 'serviceWorker' does not exist on type 'WorkerNavigator'.


ERROR in ./src/serviceWorkerRegistration.ts (\src\serviceWorkerRegistration.ts) 71:26-39

[tsl] ERROR in \src\serviceWorkerRegistration.ts(71,27)
      TS2339: Property 'serviceWorker' does not exist on type 'WorkerNavigator'.


ERROR in ./src/serviceWorkerRegistration.ts (src\serviceWorkerRegistration.ts) 117:18-31

[tsl] ERROR in \src\serviceWorkerRegistration.ts(117,19)
      TS2339: Property 'serviceWorker' does not exist on type 'WorkerNavigator'.

And I can't get rid of it. On my laptop project works fine, I have the same versions of node, ts, ts-loader, webpack etc.

There is literally simillar commit on both pc's but on my computer I get this errors.

Also I have simple serviceworker setup picked from default create-react-app configuration

// This optional code is used to register a service worker.
// register() is not called by default.

// This lets the app load faster on subsequent visits in production, and gives
// it offline capabilities. However, it also means that developers (and users)
// will only see deployed updates on subsequent visits to a page, after all the
// existing tabs open on the page have been closed, since previously cached
// resources are updated in the background.

// To learn more about the benefits of this model and instructions on how to
// opt-in, read https://cra.link/PWA

const isLocalhost = Boolean(
  window.location.hostname === 'localhost' ||
    // [::1] is the IPv6 localhost address.
    window.location.hostname === '[::1]' ||
    // 127.0.0.0/8 are considered localhost for IPv4.
    window.location.hostname.match(/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/)
);

type Config = {
  onSuccess?: (registration: ServiceWorkerRegistration) => void;
  onUpdate?: (registration: ServiceWorkerRegistration) => void;
};

export function register(config?: Config) {
  if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
    // The URL constructor is available in all browsers that support SW.
    const publicUrl = new URL(process.env.PUBLIC_URL, document.location.href);
    if (publicUrl.origin !== window.location.origin) {
      // Our service worker won't work if PUBLIC_URL is on a different origin
      // from what our page is served on. This might happen if a CDN is used to
      // serve assets; see https://github.com/facebook/create-react-app/issues/2374
      return;
    }

    window.addEventListener('load', () => {
      const swUrl = `${window.location.origin}/service-worker.js`;

      if (isLocalhost) {
        // This is running on localhost. Let's check if a service worker still exists or not.
        checkValidServiceWorker(swUrl, config);

        // Add some additional logging to localhost, pointing developers to the
        // service worker/PWA documentation.
        navigator.serviceWorker.ready.then(() => {
          console.log(
            'This web app is being served cache-first by a service ' +
              'worker. To learn more, visit https://cra.link/PWA'
          );
        });
      } else {
        // Is not localhost. Just register service worker
        registerValidSW(swUrl, config);
      }
    });
  }
}

function registerValidSW(swUrl: string, config?: Config) {
  navigator.serviceWorker
    .register(swUrl)
    .then((registration) => {
      registration.onupdatefound = () => {
        const installingWorker = registration.installing;
        if (installingWorker == null) {
          return;
        }
        installingWorker.onstatechange = () => {
          if (installingWorker.state === 'installed') {
            if (navigator.serviceWorker.controller) {
              // At this point, the updated precached content has been fetched,
              // but the previous service worker will still serve the older
              // content until all client tabs are closed.
              console.log(
                'New content is available and will be used when all ' +
                  'tabs for this page are closed. See https://cra.link/PWA.'
              );

              // Execute callback
              if (config && config.onUpdate) {
                config.onUpdate(registration);
              }
            } 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 cached for offline use.');

              // Execute callback
              if (config && config.onSuccess) {
                config.onSuccess(registration);
              }
            }
          }
        };
      };
    })
    .catch((error) => {
      console.error('Error during service worker registration:', error);
    });
}

function checkValidServiceWorker(swUrl: string, config?: Config) {
  // Check if the service worker can be found. If it can't reload the page.
  fetch(swUrl, {
    headers: { 'Service-Worker': 'script' },
  })
    .then((response) => {
      // Ensure service worker exists, and that we really are getting a JS file.
      const contentType = response.headers.get('content-type');
      if (
        response.status === 404 ||
        (contentType != null && contentType.indexOf('javascript') === -1)
      ) {
        // No service worker found. Probably a different app. Reload the page.
        navigator.serviceWorker.ready.then((registration) => {
          registration.unregister().then(() => {
            window.location.reload();
          });
        });
      } else {
        // Service worker found. Proceed as normal.
        registerValidSW(swUrl, config);
      }
    })
    .catch(() => {
      console.log('No internet connection found. App is running in offline mode.');
    });
}

export function unregister() {
  if ('serviceWorker' in navigator) {
    navigator.serviceWorker.ready
      .then((registration) => {
        registration.unregister();
      })
      .catch((error) => {
        console.error(error.message);
      });
  }
}

UPD: my packages versions:

"devDependencies": {
    "@babel/core": "^7.12.3",
    "@babel/plugin-proposal-class-properties": "^7.12.1",
    "@babel/plugin-transform-destructuring": "^7.13.0",
    "@babel/plugin-transform-modules-commonjs": "^7.12.1",
    "@babel/plugin-transform-runtime": "^7.12.1",
    "@babel/preset-env": "^7.13.12",
    "@babel/preset-es2015": "^7.0.0-beta.53",
    "@babel/preset-react": "^7.12.13",
    "@types/chart.js": "^2.9.32",
    "@types/moment-duration-format": "^2.2.3",
    "@types/react-google-recaptcha": "^2.1.2",
    "@types/react-helmet": "^6.1.1",
    "@types/react-redux": "^7.1.16",
    "@types/react-router-dom": "^5.1.7",
    "@types/react-transition-group": "^4.4.1",
    "@types/reactstrap": "^8.7.2",
    "@types/workbox-core": "^4.3.1",
    "@types/workbox-expiration": "^4.3.1",
    "@types/workbox-precaching": "^4.3.1",
    "@types/workbox-strategies": "^4.3.1",
    "@webpack-cli/serve": "^1.6.0",
    "babel-core": "7.0.0-bridge.0",
    "babel-eslint": "^10.1.0",
    "babel-jest": "^24.9.0",
    "babel-loader": "^8.2.1",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "babel-polyfill": "^6.26.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1",
    "bootstrap": "^5.1.3",
    "bootstrap-sass": "^3.4.1",
    "compression-webpack-plugin": "^5.0.2",
    "copy-webpack-plugin": "^5.1.2",
    "css-loader": "^6.7.1",
    "enzyme": "^3.11.0",
    "enzyme-adapter-react-16": "^1.15.5",
    "eslint": "^6.8.0",
    "eslint-plugin-babel": "^5.3.1",
    "eslint-plugin-react": "^7.21.5",
    "extract-text-webpack-plugin": "^3.0.2",
    "fetch-mock": "^7.7.3",
    "hard-source-webpack-plugin": "^0.13.1",
    "html-loader": "^0.5.5",
    "html-webpack-plugin": "^5.5.0",
    "identity-obj-proxy": "^3.0.0",
    "jest-css-modules": "^2.1.0",
    "lodash": "^4.17.21",
    "mini-css-extract-plugin": "^2.4.4",
    "optimize-css-assets-webpack-plugin": "^5.0.4",
    "preload-webpack-plugin": "^3.0.0-beta.4",
    "prettier": "^2.1.2",
    "prop-types": "^15.7.2",
    "purgecss-webpack-plugin": "^2.3.0",
    "react-dnd": "^11.1.3",
    "react-dnd-touch-backend": "^14.0.0",
    "react-test-renderer": "^16.14.0",
    "redux-mock-store": "^1.5.4",
    "sass-loader": "^12.1.0",
    "style-loader": "^0.21.0",
    "sw-precache-webpack-plugin": "^1.0.0",
    "uglifyjs-webpack-plugin": "^2.2.0",
    "webpack": "^5.6.0",
    "webpack-bundle-analyzer": "^3.9.0",
    "webpack-cli": "^4.9.1",
    "webpack-dev-server": "^4.3.1",
    "workbox-webpack-plugin": "^6.3.0"
  },
  "dependencies": {
    "@babel/plugin-syntax-dynamic-import": "^7.8.3",
    "@babel/plugin-transform-arrow-functions": "^7.16.0",
    "@babel/plugin-transform-shorthand-properties": "^7.12.13",
    "@babel/polyfill": "^7.12.1",
    "@formatjs/intl-pluralrules": "^4.0.5",
    "@fortawesome/fontawesome-svg-core": "^1.2.32",
    "@fortawesome/free-brands-svg-icons": "^5.15.1",
    "@fortawesome/free-solid-svg-icons": "^5.15.1",
    "@fortawesome/react-fontawesome": "^0.1.12",
    "@microsoft/signalr": "^3.1.10",
    "@reduxjs/toolkit": "^1.5.1",
    "@types/react": "^17.0.34",
    "@types/react-bootstrap": "^0.32.29",
    "acorn": "^6.4.2",
    "acorn-dynamic-import": "^4.0.0",
    "animate.css": "^4.1.1",
    "axios": "^0.21.1",
    "babel-plugin-react-intl": "^3.5.1",
    "babel-plugin-transform-es2015-destructuring": "^6.23.0",
    "babel-plugin-transform-es2015-shorthand-properties": "^6.24.1",
    "cache-loader": "^4.1.0",
    "chart.js": "^2.9.4",
    "chartjs-plugin-zoom": "^0.7.7",
    "check-prop-types": "^1.1.2",
    "cookies": "^0.8.0",
    "copy-to-clipboard": "^3.3.1",
    "core-js": "^3.9.1",
    "date-fns": "^1.30.1",
    "faker": "^5.4.0",
    "file-loader": "^2.0.0",
    "fontawesome": "^4.7.2",
    "html-react-parser": "^1.2.1",
    "html2canvas": "^1.0.0-rc.7",
    "jest": "^24.9.0",
    "jest-enzyme": "^7.1.2",
    "jquery": "^3.5.1",
    "js-cookie": "^2.2.1",
    "js-file-download": "^0.4.12",
    "loader-utils": "^2.0.0",
    "moment": "^2.29.1",
    "moment-duration-format": "^2.3.2",
    "moment-timezone": "^0.5.34",
    "node-fetch": "^2.6.1",
    "npm": "^6.14.8",
    "performance-now": "^2.1.0",
    "quill": "^1.3.7",
    "raf": "^3.4.1",
    "react": "^16.14.0",
    "react-app-polyfill": "^2.0.0",
    "react-awesome-reveal": "^3.7.1",
    "react-beforeunload": "^2.4.0",
    "react-bootstrap": "^2.1.0",
    "react-calendly": "^1.1.4",
    "react-chartjs-2": "^2.11.1",
    "react-cookie": "^3.1.2",
    "react-device-detect": "^1.15.0",
    "react-dnd-html5-backend": "^11.0.0",
    "react-dnd-multi-backend": "^6.0.2",
    "react-dom": "^16.14.0",
    "react-dropdown": "^1.9.2",
    "react-facebook-pixel": "^1.0.4",
    "react-ga": "^2.7.0",
    "react-google-login": "^5.1.22",
    "react-google-recaptcha": "^1.1.0",
    "react-google-recaptcha-v3": "^1.9.5",
    "react-helmet": "^6.1.0",
    "react-indiana-drag-scroll": "^2.1.0",
    "react-inlinesvg": "^2.3.0",
    "react-intl": "^3.12.1",
    "react-joyride": "^2.3.0",
    "react-json-view": "^1.21.1",
    "react-lineto": "^3.2.0",
    "react-loadable": "^5.5.0",
    "react-markdown": "^6.0.1",
    "react-player": "^2.9.0",
    "react-quill": "^1.3.5",
    "react-redux": "^7.2.2",
    "react-resizable": "^1.11.0",
    "react-reveal": "^1.2.2",
    "react-router": "^5.2.0",
    "react-router-dom": "^5.2.0",
    "react-screen-orientation": "0.0.4",
    "react-script": "^2.0.5",
    "react-scripts": "^4.0.3",
    "react-share": "^4.4.0",
    "react-splitter-layout": "^4.0.0",
    "react-step-wizard": "^5.3.5",
    "react-svg": "^14.0.14",
    "react-toastify": "^4.5.2",
    "react-transition-group": "^4.4.1",
    "reactstrap": "^9.0.1",
    "read-excel-file": "^4.1.0",
    "redux": "^4.0.5",
    "redux-thunk": "^2.3.0",
    "rehype-raw": "^5.1.0",
    "remark": "^13.0.0",
    "remark-html": "^15.0.1",
    "remark-loader": "^4.0.0",
    "sass": "^1.49.0",
    "sweetalert2": "^8.19.0",
    "ts-loader": "^8.2.0",
    "typescript": "^4.2.4",
    "uuid": "^3.4.0",
    "web-vitals": "^2.1.2",
    "word-freq": "0.0.8",
    "workbox-expiration": "^6.3.0",
    "workbox-precaching": "^6.3.0",
    "workbox-routing": "^6.3.0"
  }
0

There are 0 answers