如何退出 Next.js v13 路由缓存?

huangapple go评论57阅读模式
英文:

How to opt out from Next.js v13 route cache?

问题

这是我的 route.ts 文件:

import { NextResponse } from 'next/server';
import { serializeError } from 'serialize-error';

export const GET = async () => {
  try {
    // ...
  } catch (error) {
    return NextResponse.json({
      error: serializeError(error),
    }, {
      status: 500,
    });
  }

  const response = NextResponse.json({
    status: 'OK',
  });

  response.headers.set('cache-control', 'no-store');
  response.headers.set('x-robots-tag', 'noindex');

  return response;
};

无论我添加哪些响应头,似乎响应始终被缓存。

以下是一个 cURL 请求的示例:

curl -v https://...
* 尝试连接到 104.196.232.237:443...
* 已连接到 ray.run (104.196.232.237) 端口 443 (#0)
* ALPN 提议了 h2
* ALPN 提议了 http/1.1
*  CAfile: /etc/ssl/cert.pem
*  CApath: none
* (304) (OUT), TLS 握手, 客户端 hello (1):
* (304) (IN), TLS 握手, 服务器 hello (2):
* (304) (IN), TLS 握手, 未知 (8):
* (304) (IN), TLS 握手, 证书 (11):
* (304) (IN), TLS 握手, CERT 验证 (15):
* (304) (IN), TLS 握手, 完成 (20):
* (304) (OUT), TLS 握手, 完成 (20):
* 使用 TLSv1.3 / AEAD-CHACHA20-POLY1305-SHA256 的 SSL 连接
* ALPN: 服务器接受了 h2
* 服务器证书:
*  主题: CN=ray.run
*  开始日期: May 27 01:05:38 2023 GMT
*  到期日期: Aug 25 01:05:37 2023 GMT
*  subjectAltName: host "ray.run" 匹配了证书的 "ray.run"
*  颁发者: C=US; O=Let's Encrypt; CN=R3
*  SSL 证书验证通过。
* 使用 HTTP2,服务器支持多路复用
* 在升级后将 HTTP/2 数据从流缓冲区复制到连接缓冲区:len=0
* h2h3 [:method: GET]
* h2h3 [:path: ...]
* h2h3 [:scheme: https]
* h2h3 [:authority: ray.run]
* h2h3 [user-agent: curl/7.86.0]
* h2h3 [accept: */*]
* 使用流 ID:1 (easy handle 0x11e811400)
> GET /... HTTP/2
> Host: ray.run
> user-agent: curl/7.86.0
> accept: */*
>
< HTTP/2 200
< x-dns-prefetch-control: on
< x-frame-options: DENY
< x-content-type-options: nosniff
< x-xss-protection: 1; mode=block
< referrer-policy: origin-when-cross-origin
< permissions-policy: camera=(), microphone=(), geolocation=(), interest-cohort=()
< strict-transport-security: max-age=63072000; includeSubDomains; preload
< vary: RSC, Next-Router-State-Tree, Next-Router-Prefetch, Accept-Encoding
< x-nextjs-cache: HIT
< cache-control: no-store
< content-type: application/json
< x-robots-tag: noindex
< date: Fri, 16 Jun 2023 00:56:58 GMT
< server: railway
<
* 连接到主机 ray.run 的连接 #0 保持不变

这只是 next.js 是否明显忽略规范的问题吗?

< x-nextjs-cache: HIT
< cache-control: no-store
英文:

This is my route.ts:

import { NextResponse } from &#39;next/server&#39;;
import { serializeError } from &#39;serialize-error&#39;;

export const GET = async () =&gt; {
  try {
    // ...
  } catch (error) {
    return NextResponse.json({
      error: serializeError(error),
    }, {
      status: 500,
    });
  }

  const response = NextResponse.json({
    status: &#39;OK&#39;,
  });

  response.headers.set(&#39;cache-control&#39;, &#39;no-store&#39;);
  response.headers.set(&#39;x-robots-tag&#39;, &#39;noindex&#39;);

  return response;
};

It seems no matter what headers I add to the response, the response is always cached.

Here is an example cURL request:

curl -v https://...
*   Trying 104.196.232.237:443...
* Connected to ray.run (104.196.232.237) port 443 (#0)
* ALPN: offers h2
* ALPN: offers http/1.1
*  CAfile: /etc/ssl/cert.pem
*  CApath: none
* (304) (OUT), TLS handshake, Client hello (1):
* (304) (IN), TLS handshake, Server hello (2):
* (304) (IN), TLS handshake, Unknown (8):
* (304) (IN), TLS handshake, Certificate (11):
* (304) (IN), TLS handshake, CERT verify (15):
* (304) (IN), TLS handshake, Finished (20):
* (304) (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / AEAD-CHACHA20-POLY1305-SHA256
* ALPN: server accepted h2
* Server certificate:
*  subject: CN=ray.run
*  start date: May 27 01:05:38 2023 GMT
*  expire date: Aug 25 01:05:37 2023 GMT
*  subjectAltName: host &quot;ray.run&quot; matched cert&#39;s &quot;ray.run&quot;
*  issuer: C=US; O=Let&#39;s Encrypt; CN=R3
*  SSL certificate verify ok.
* Using HTTP2, server supports multiplexing
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* h2h3 [:method: GET]
* h2h3 [:path: ...]
* h2h3 [:scheme: https]
* h2h3 [:authority: ray.run]
* h2h3 [user-agent: curl/7.86.0]
* h2h3 [accept: */*]
* Using Stream ID: 1 (easy handle 0x11e811400)
&gt; GET /... HTTP/2
&gt; Host: ray.run
&gt; user-agent: curl/7.86.0
&gt; accept: */*
&gt;
&lt; HTTP/2 200
&lt; x-dns-prefetch-control: on
&lt; x-frame-options: DENY
&lt; x-content-type-options: nosniff
&lt; x-xss-protection: 1; mode=block
&lt; referrer-policy: origin-when-cross-origin
&lt; permissions-policy: camera=(), microphone=(), geolocation=(), interest-cohort=()
&lt; strict-transport-security: max-age=63072000; includeSubDomains; preload
&lt; vary: RSC, Next-Router-State-Tree, Next-Router-Prefetch, Accept-Encoding
&lt; x-nextjs-cache: HIT
&lt; cache-control: no-store
&lt; content-type: application/json
&lt; x-robots-tag: noindex
&lt; date: Fri, 16 Jun 2023 00:56:58 GMT
&lt; server: railway
&lt;
* Connection #0 to host ray.run left intact

Is this just next.js blatantly ignoring the spec?

&lt; x-nextjs-cache: HIT
&lt; cache-control: no-store

答案1

得分: 1

GET路由处理程序默认情况下会被缓存,要退出缓存,您必须在您的GET路由处理程序中将request对象用作参数。

export const GET = async (request: Request) => {
  // 对request进行虚拟使用
  const headers = request.headers;
  // ...
};

您必须在函数体内使用它

以下是在文档中列出的退出缓存的所有方式:

  • 使用GET方法的Request对象。
  • 使用任何其他HTTP方法。
  • 使用类似于cookies和headers的Dynamic Functions。
  • Segment Config Options手动指定动态模式。
英文:

GET route handlers are cached by default, to opt out you have to use the request object in your GET route handler as a parameter

export const GET = async (request: Request) =&gt; {
  // dummy usage of request
  const headers = request.headers;
  // ...
};

You have to use it inside the body of your function

Here are all the ways you can opt out of caching as listed in the docs

  • Using the Request object with the GET method.
  • Using any of the other HTTP methods.
  • Using Dynamic Functions like cookies and headers.
  • The Segment Config Options manually specifies dynamic mode.

huangapple
  • 本文由 发表于 2023年6月16日 08:59:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486355.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定