[英]Failed to register a ServiceWorker: The script has an unsupported MIME type ('text/html') Vue js?
I want to include service worker in my vue js project and i did this before with my simple HTML projects in which it works really great but when i include the same file to my vue js code it keeps throwing the error 我想在我的vue js项目中包括服务工作者,而我之前是通过简单的HTML项目做到这一点的,在该项目中它确实很棒,但是当我在vue js代码中包含相同的文件时,它会不断抛出错误
The script has an unsupported MIME type ('text/html').
该脚本具有不受支持的MIME类型('text / html')。 index.js?3609:11 Service wroker: error: SecurityError: Failed to register a ServiceWorker: The script has an unsupported MIME type ('text/html').
index.js?3609:11服务wroker:错误:SecurityError:无法注册ServiceWorker:脚本具有不受支持的MIME类型(“ text / html”)。
here is the code which i used for ragister my service worker 这是我用来激怒我的服务人员的代码
if (navigator.serviceWorker) {
console.log("Service wroker: available");
window.addEventListener("load", () => {
navigator.serviceWorker
.register("/service-worker.js")
.then(res => console.log("Service worker: ragister succefully"))
.catch(err => console.log("Service wroker: error: " + err))
})
} else {
console.log("Servicde wroker: not found");
}
my service worker file 我的服务人员档案
// this service worker cache all the response of the sites beside caching files and assets individually
const cacheName = "v1";
// install service worker
self.addEventListener("install", (e) => {
console.log("Service wroker: installed")
})
// Call activate event
self.addEventListener("activate", (e) => {
console.log("Service wroker: Activated")
// Deleting old and unwanted cache
e.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cache => {
if (cache !== cacheName) {
console.log('Service worker: Clearing Old cache');
return caches.delete(cache);
}
})
)
})
)
})
// Call fetch event
self.addEventListener('fetch', (e) => {
console.log('Service worker: Fetching Data');
e.respondWith(
fetch(e.request)
.then(res => {
// Make copy/clone of response
const resClone = res.clone();
caches
.open(cacheName)
.then(cache => {
console.log("Service worker: Caching files")
cache.put(e.request, resClone)
})
return res
})
.catch(err => caches.match(e.request).then(res => res))
)
})
i succefully found service worker on my browser but when i try to register my service worker file it throw the error i search alot and still searching for solution. 我在浏览器中成功找到服务人员,但是当我尝试注册服务人员文件时,抛出错误,我进行了大量搜索并仍在寻找解决方案。 so please help me out with this.
所以请帮我
thanks in advance.... 提前致谢....
I am not sure, but can you try to register the worker like this, so we can have additional information about the progress and set the path correctly: 我不确定,但是您可以尝试像这样注册工作人员,以便我们可以获取有关进度的其他信息并正确设置路径:
... ...
navigator.serviceWorker
.register("service-worker.js", {
scope: './' }).then(function (registration) {
var serviceWorker;
if (registration.installing) {
serviceWorker = registration.installing;
console.log('installing');
} else if (registration.waiting) {
serviceWorker = registration.waiting;
console.log('waiting');
} else if (registration.active) {
serviceWorker = registration.active;
console.log('active');
}
if (serviceWorker) {
// logState(serviceWorker.state);
serviceWorker.addEventListener('statechange', function (e) {
// logState(e.target.state);
});
}
}).catch({…})
Can you as well show the 您还能显示
service-worker.js
service-worker.js
file? 文件?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.