node.js - React 编译错误“尝试导入错误:'./registerServiceWorker' 不包含默认导出(导入为'registerServiceWorker')。”
问题描述
作为一个新手,我可以从不同的教程示例中免提 MERN CRUD 应用程序。
在“npm run dev”期间出现错误。
尝试导入错误:“./registerServiceWorker”不包含默认导出(导入为“registerServiceWorker”)。
任何帮助都会非常有帮助。
//注册ServiceWorker.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();
});
}
}
解决方案
推荐阅读
- javascript - 我的 cron 作业总是包含相同的标记值
- laravel - 在 laravel 的 mongodb 文档的对象列表中选择特定对象
- hibernate-envers - Hibernate级联所有和envers审计
- arrays - 如何分别显示列中的日期?
- c# - Xamarin 使用 MSAL Azure B2C 进行身份验证,在获取令牌请求和凭据中发送带有敏感详细信息的标头,而不对其进行加密
- opencart - 来自 ocmod 存档的 OpenCart 3 文件未加载
- python - 模块没有出现在 jupyter
- javascript - FullCalendar React 中的 React Bootstrap Popover
- python - pip 配置了需要 TLS/SSL 的位置,但是 Python 中的 ssl 模块不可用。Catalina MacOS 中的 Python 3.6.5
- scala - scala:如果实例的类型静态地是基类,则使用基于宏的扩展类的方法调用特征默认impl