【发布时间】:2017-11-17 20:36:58
【问题描述】:
我已将 json 数据存储在 indexedDB 中,并希望在服务工作者中应用程序请求它时检索它,但我收到一条错误消息,提示为服务工作者获取脚本时出错。 我的 Service Worker 代码是这样的:
importScripts('idb.js');
const cache_v = 'a2',
cache_all = [
'shell_'+cache_v,
'data_'+cache_v
],
statics = [
'index.html',
'app.js',
'idb.js'
];
this.addEventListener('install',e=>{
console.log("Installing Service Worker");
console.log("Storing Static Data");
e.waitUntil(
caches.open(cache_all[0])
.then(cac=>cac.addAll(statics))
.catch(er=>console.error(er))
)
console.log("Installation Complete");
});
this.addEventListener('activate',e=>{
console.log("Service worker activated, now clearing obsolete data");
})
this.addEventListener('fetch',e=>{
if(e.request.url === 'https://api.github.com/users/abt10/repos'){
console.log("IndexDB Reqrd");
let db = idb.open('Projects_info')
.then(d=>{
let ob = d.transaction('proj_dat');
let data = ob.objectStore('proj_dat');
let fd = data.getAll()
.then(dt=>{
return dt;
})
})
}
else{
e.respondWith(caches.match(e.request)
.then((res)=>{
return res || fetch(e.request).then(res=>{if(res.status === 404)
return new Response("Not Found");});
})
)
}
console.log('fetched');
})
我找不到什么错误。
【问题讨论】:
-
需要看看 idb.js 正在做什么。当您发布使用包装 indexedDB 的库并且不包含库的相关部分或明确检查库的位置的代码时,很难回答有关 indexedDB 的问题。
-
Jake Archibald 的 idb 库。它在 GitHub 上。
-
您能发布您看到的确切错误消息吗?
-
"获取脚本时发生未知错误。service_worker.js:1"
标签: javascript caching indexeddb service-worker fetch-api