代码之家  ›  专栏  ›  技术社区  ›  Htet Ah Yan

如何在NEXT js 13应用程序路由器中通过标记重新验证缓存

  •  0
  • Htet Ah Yan  · 技术社区  · 2 年前

    我想通过标记重新验证缓存 export default async function getData() 。当我调用我想要的删除函数时 getdata 重新验证。如何在next.js中实现这一点?

    {
    
      const res = await fetch('/api/admin')
    
     
      if (!res.ok) {
        // This will activate the closest `error.js` Error Boundary
        throw new Error('Failed to fetch data')
      }
     
      return res.json()
    }
    
    0 回复  |  直到 2 年前
        1
  •  0
  •   maximus383    2 年前

    您可以使用应用程序路由器的重新验证功能或缓存选项来完成此操作。以下是您可以尝试的两种方法:

    使用重新验证:

    {
    
      const res = await fetch('/api/admin', 
          { next: { revalidate: 10 }}  //here you can change the time, I put 10 seconds
      )
    
     
      if (!res.ok) {
        // This will activate the closest `error.js` Error Boundary
        throw new Error('Failed to fetch data')
      }
     
      return res.json()
    }
    

    使用缓存选项:

    {
    
      const res = await fetch('/api/admin', 
          { cache: 'no-store' }  //this way you will receive new data at every reload of your page
      )
    
     
      if (!res.ok) {
        // This will activate the closest `error.js` Error Boundary
        throw new Error('Failed to fetch data')
      }
     
      return res.json()
    }