如何避免缓存特定的NextJS API路线?

发布于 2025-02-01 16:56:17 字数 229 浏览 2 评论 0原文

我有NextJS API Route /api/auth/emigout,它基本上只是清除了特定的cookie并将JSON发送回。

问题是,当我在Vercel中部署项目时,该特定的API首次正常工作(Cookie已清除并给出200个响应),但后来它不起作用(Cookie不清除并给出304响应)。

我想知道,有什么方法可以避免使用此路线的缓存?

解决此问题的最佳方法是什么?

I have nextjs api route /api/auth/signout, it basically just clears the specific cookie and send JSON back.

The problem is, when I deploy project in Vercel, this particular API working first time properly (cookie cleared and giving 200 response) but later it's not working (cookie not cleared and giving 304 response).

I want to know, is there any way to avoid cache only for this route?

What's the best possible way to fix this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

書生途 2025-02-08 16:56:17

玉米API路线有一个非常相似的问题,并将其固定

export const revalidate = 0;

在路由中。

Had a very similar problem with a corn API route and fixed it with

export const revalidate = 0;

in the route.js file

惯饮孤独 2025-02-08 16:56:17

next.config.js上添加了此配置

async headers() {
    return [
      {
        source: '/api/<route-name>',
        headers: [
          {
            key: 'Cache-Control',
            value: 'no-store, max-age=0',
          },
        ],
      },
    ];
  },

Added this config on next.config.js

async headers() {
    return [
      {
        source: '/api/<route-name>',
        headers: [
          {
            key: 'Cache-Control',
            value: 'no-store, max-age=0',
          },
        ],
      },
    ];
  },
染火枫林 2025-02-08 16:56:17

您可以配置cache-control每个API端点标题

https://nextjs.org/docs/api-reference/next.config.js/headers#cache-control

在您的情况下,类似这样的事情可能会遇到问题:

res.setHeader('Cache-Control', 'no-store')

You can configure Cache-Control header per API endpoint

https://nextjs.org/docs/api-reference/next.config.js/headers#cache-control

In your case, something like this might do the trick:

res.setHeader('Cache-Control', 'no-store')

无妨# 2025-02-08 16:56:17

这更多是对您的用例的答案,而不是问题的标题:

我认为您应该将签名HTTP端点视为具有副作用的IE的签名,即销毁用户会话服务器端)。对于具有副作用的HTTP端点,建议使用适当的HTTP方法调用它们,这意味着副作用将发生,例如在这种情况下会删除。通过HTTP规范,不应缓存对HTTP删除请求的响应。

This is more of an answer to your use-case rather than the title of the question:

I think you should view the signout http endpoint as one that has a side-effect i.e. the destruction of a user session (even though nothing may be happening server-side). For http endpoints that have side-effects, it’s recommended to call them with the appropriate http method that implies that a side-effect will occur e.g. DELETE would be good in this case. By way of the http spec, responses to http DELETE requests shouldn’t be cached.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文