即使缓存数据存在,swr useSWR 钩子是否必须在每次使用数据时发送 HTTP 请求以进行重新验证?

发布于 2025-01-14 20:21:05 字数 1126 浏览 3 评论 0原文

当我阅读 SWR React hook 文档和 Stale-While-Revalidate 方法时,似乎 swr 仅使用缓存数据作为短时间占位符,以快速向用户显示结果。 (不要误会我的意思,我仍然认为 swr 有很多好处)

我想将 SWR 与 HTTP 静态内容缓存进行比较以使事情变得清楚。

就 HTTP 静态内容缓存(又称 HTTP 缓存)而言,

  1. 客户端获取带有 Cache-ControlExpires 标头的静态内容。
  2. 下次需要获取相同内容时,只要基于Cache-ControlExpires,缓存文件可用且有效。它使用缓存的数据并且不向服务器发送HTTP 请求

然而,当涉及到swr useSWR时,

  1. 它将HTTP响应数据保存到本地缓存中。
  2. 下次需要获取相同的数据。它使用缓存中的数据(如果存在)并向服务器发送 HTTP 请求(重新验证)来检查数据是否已更改。

我知道 HTTP 缓存和 swr React hook 有很多功能,但这只是 HTTP 缓存和 swr React hook 的简要总结。

我的问题是,

如果每次使用缓存数据时 swr 都必须重新验证,那么除了来自多个组件的同一 API URL 的并发请求之外,它不会减少请求数量,对吗?

如果是的话,这种缓存机制是否可以更快地向用户显示数据,并且可能会阻止来自多个组件对同一 API URL 的多个并发请求?

我对 swr 很陌生,如果我误解了,请告诉我。

谢谢!

swr 文档:https://swr.vercel.app/

Mozilla 上的 HTTP 缓存:https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching

While I read SWR react hook documentation and Stale-While-Revalidate methodology, It seems that swr uses cached data just for a short time placeholder to show result to users quickly. (Don't get me wrong I still think swr has many benefits)

I'd like to compare SWR with HTTP static content cache to make things clear.

In terms of HTTP static content cache aka HTTP cache,

  1. Client fetches static content that comes with the Cache-Control or Expires header.
  2. Next time the same content needs to be fetched, As long as the cached file is available and valid based on Cache-Control or Expires. It uses the cached data and doesn't send HTTP request to the server.

However, when it comes to swr useSWR,

  1. it saves HTTP response data to local cache.
  2. Next time the same data needs to be fetched. It uses data from cache(if exists) and sends HTTP requests to server(revalidate) to check if the data has changed.

I know HTTP cache and swr react hook has tons of more features but this is just a brief summary of HTTP cache and swr react hook.

My question is

if swr has to revalidates every time the cached data is used, it doesn't reduce the number of requests other than the concurrent requests of the same API URL from multiple components, right?

If then, is this cache mechanism for showing data quicker to users and may be preventing multiple concurrent requests of the same API URL from multiple components?

I am quite new to swr and please let me know if I misunderstood.

Thanks!

swr docs: https://swr.vercel.app/

HTTP cache on mozilla: https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching

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

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

发布评论

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

评论(2

雨的味道风的声音 2025-01-21 20:21:06

有一个名为 dedupingInterval 的选项,默认设置为 2000ms。如果多个组件在此时间范围内请求相同的缓存键/url,则不会发送重新验证请求。它可以使用 SWRConfig 上下文进行全局设置,也可以针对选项内的每个 useSWR(key, fetcher, options) 挂钩进行设置。

链接:SWR 文档

还有更多功能,例如使用useSWRImmutable(key, fetcher)< /code> 而不是 useSWR(key, fetcher) 来完全禁用重新验证。

There is an option called dedupingInterval which is set to 2000ms by default. If multiple components request the same cache key/url within this time span no request to revalidate will be send. It can be set globally with SWRConfig context or for each useSWR(key, fetcher, options) hook inside options.

Link: SWR docs

There are more features like using useSWRImmutable(key, fetcher) instead of the useSWR(key, fetcher) to disable revalidation at all.

羅雙樹 2025-01-21 20:21:06

您还可以在配置选项中将 revalidateIfStale 设置为 false:

const { data, isLoading } = useSWR('/albumTypes', { revalidateIfStale: false });

You can also also set revalidateIfStale to false in your config options:

const { data, isLoading } = useSWR('/albumTypes', { revalidateIfStale: false });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文