我是服务人员的新手,我正在为我的 react 项目编写自定义 sw.js 以支持 pwa。它在后端有 graphql 实现。我已经缓存了 graphql 响应的响应,但如果用户离线,则无法提供服务。这是自定义 sw.js 代码段:
if(event.request.url=== 'https://my.custombackend.com/graphql'){
console.log('graphql request incoming!');
return event.respondWith(
caches.match('/graphql').then(graphqlRes=>{ //THIS LINE IS CAUSING ERROR AS sw.js is unable to retrieve such match . But in image you can see I have already cached the graphql response.
if(graphqlRes){
console.log(graphqlRes)
console.log('graphql request being served from cache');
return graphqlRes;
}
else return fetch(event.request).then(cacheToBe=>{
return caches.open(dynamicCache).then(newEntry=>{
console.log('graphql request served! Now caching...');
newEntry.put(event.request.url, cacheToBe.clone());
return cacheToBe;
})
})
}))
}
预期行为:sw.js 应获取所需的“/graphql”缓存数据。实际行为:它无法识别并回退到offline.html