清除缓存存储时会抛出错误

问题描述 投票:0回答:1
   useEffect(async () => {
    caches.open('my-cache')
  }, [token, user_id])

当我尝试注销我的应用程序时

const logoutFunc = () => {
    localStorage.clear()
    caches.keys().then((list) =>
       list.map((key) => {
       caches.delete(key)
       router.push('/')
    })
)

当它运行时,它会抛出类似的错误,

缓存已清除。但它会抛出错误

javascript reactjs next.js key browser-cache
1个回答
0
投票

您可以通过在代码中添加 catch 块来捕获错误。

caches.keys().then((list) =>
   list.map((key) => {
   caches.delete(key)
   router.push('/')
}).catch(e => ...)

解决方案可以去掉useEffect中的异步函数,阅读下面的文章。

useEffect(async () => {
 caches.open('my-cache')

}, [令牌, user_id])

https://devtrium.com/posts/async-functions-useeffect

© www.soinside.com 2019 - 2024. All rights reserved.