如何避免缓存特定的NextJS API路线?
我有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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
玉米API路线有一个非常相似的问题,并将其固定
在路由中。
Had a very similar problem with a corn API route and fixed it with
in the route.js file
在
next.config.js
上添加了此配置Added this config on
next.config.js
您可以配置
cache-control
每个API端点标题https://nextjs.org/docs/api-reference/next.config.js/headers#cache-control
在您的情况下,类似这样的事情可能会遇到问题:
You can configure
Cache-Control
header per API endpointhttps://nextjs.org/docs/api-reference/next.config.js/headers#cache-control
In your case, something like this might do the trick:
这更多是对您的用例的答案,而不是问题的标题:
我认为您应该将签名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.