反应编译错误“尝试导入错误:'./ registerServiceWorker'不包含默认导出(导入为'registerServiceWorker')。”

时间:2018-12-30 18:11:06

标签: node.js reactjs mern

作为新手,我可以从不同的教程示例中轻松使用MERN CRUD应用程序。

在“ npm run dev”期间出现错误。

尝试导入错误:“ ./ registerServiceWorker”不包含默认导出(导入为“ registerServiceWorker”)。

任何帮助都会很有帮助。

// registerServiceWorker.js

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

export default function register() {
  if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
    const publicUrl = new URL(process.env.PUBLIC_URL, window.location);
    if (publicUrl.origin !== window.location.origin) {
      return;
    }

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

      if (isLocalhost) {
        navigator.serviceWorker.ready.then(() => {
          console.log(
            'This web app is being served cache-first by a service'
          );
        });
      } else {
        registerValidSW(swUrl);
      }
    });
  }
}

function registerValidSW(swUrl) {
  navigator.serviceWorker
    .register(swUrl)
    .then(registration => {
      registration.onupdatefound = () => {
        const installingWorker = registration.installing;
        installingWorker.onstatechange = () => {
          if (installingWorker.state === 'installed') {
            if (navigator.serviceWorker.controller) {

              console.log('New content is available; please refresh.');
            } else {

              console.log('Content is cached for offline use.');
            }
          }
        };
      };
    })
    .catch(error => {
      console.error('Error during service worker registration:', error);
    });
}

function checkValidServiceWorker(swUrl) {

  fetch(swUrl)
    .then(response => {

      if (
        response.status === 404 ||
        response.headers.get('content-type').indexOf('javascript') === -1
      ) {

        navigator.serviceWorker.ready.then(registration => {
          registration.unregister().then(() => {
            window.location.reload();
          });
        });
      } else {

        registerValidSW(swUrl);
      }
    })
    .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();
    });
  }
}

0 个答案:

没有答案
相关问题