问题描述
我在服务人员第1行中遇到此错误。
已附上错误图片: Error
还附有Index.html代码
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon2.ico" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Expense Tracker PWA by M. Nawfal Mehboob"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico","%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>Expense Tracker</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser,you will see an empty page.
You can add webfonts,meta tags,or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development,run `npm start` or `yarn start`.
To create a production bundle,use `npm run build` or `yarn build`.
-->
<script>
window.addEventListener('load',() => {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('./sw.js')
}
})
</script>
</body>
</html>
index.tsx代码附在下面
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
// import swDev from './swDev';
import { ThemeProvider } from '@material-ui/core';
import { theme } from './Theme.styles';
// import * as serviceWorker from './serviceWorker';
ReactDOM.render(
<React.StrictMode>
<ThemeProvider theme={theme}>
<App />
</ThemeProvider>
</React.StrictMode>,document.getElementById('root')
);
// serviceWorker.register();
// swDev();
我的自定义服务工作者代码:
let swDev = `${process.env.PUBLIC_URL}/sw.js`;
navigator.serviceWorker.register(swDev).then().catch((err) => console.log('err',err));
}
公用文件夹代码中的Service Worker文件:
var urlsToCache = [
'./static/js/bundle.js','./static/js/0.chunk.js','./static/js/main.chunk.js','./static/js/5.chunk.js','./static/js/2.chunk.js','./static/js/3.chunk.js','./static/js/1.chunk.js','./static/js/4.chunk.js','./manifest.json','./favicon2.ico','./','./index.html','./sw.js'
];
// Install
self.addEventListener('install',(event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
return cache.addAll(urlsToCache);
})
);
});
// Activate
self.addEventListener('activate',function(event) {
event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.filter(function(cacheName) {}).map(function(cacheName) {
return caches.delete(cacheName);
})
);
})
);
});
// Fetch
self.addEventListener('fetch',(event) => {
event.respondWith(
caches.match(event.request).then(function(response) {
if (response) {
console.log('Found In Cache');
console.log(event.request);
console.log(response);
return response;
}
console.log('Not found in cache');
console.log(event.request);
return fetch(event.request);
})
);
});
我尝试使用create-react-app附带的自定义服务工作程序和服务工作程序,但是很遗憾,我无法运行该服务工作程序。我什至尝试从“ /”、“./”更改,并且目录名称不带正斜杠。
如果有人想访问该网站,请访问www.expensetracker.nawfalmehboob.com
预先感谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)