Skip to content

Commit

Permalink
feat(kv): Switch to default for fetch cache option (#464)
Browse files Browse the repository at this point in the history
* feat(kv): Switch to `default` for fetch `cache` option

BREAKING CHANGE: When using Next.js and `vercel/kv`, you may have `kv` requests
and/or Next.js resources using kv being cached when you don't want them to.

If that's the case, then opt-out of caching with
https://nextjs.org/docs/app/api-reference/functions/unstable_noStore.

On the contrary, if you want to enforce caching of resources you can use
https://nextjs.org/docs/app/api-reference/functions/unstable_cache.

Explanation: Before this commit, every `kv` fetch request was made with
the `cache: no-store` option of fetch
(https://developer.mozilla.org/en-US/docs/Web/API/Request/cache).

Next.js uses the fetch cache behavior as part of data caching
(https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#caching-data).

`no-store` was added by `upstash/redis` in a way to enforce `kv` queries to
never be cached. But in some situations you do want them to be cached/and or the
underlying Next.js resources to be cached.

This change made it hard to opt-out of no-store
(#213). The Next.js team
recommended we switch to `default` and let users opt-in/out from Next.js cache.

If you're not using Next.js this should have no impact on you.

fixes #213

* Create stale-stingrays-hunt.md
  • Loading branch information
vvo authored Nov 8, 2023
1 parent d57df99 commit d85bb76
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
12 changes: 12 additions & 0 deletions .changeset/stale-stingrays-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
"@vercel/kv": major
---

feat(kv): Switch to `default` for fetch `cache` option

BREAKING CHANGE: When using Next.js and vercel/kv, you may have kv requests and/or Next.js resources using kv being cached when you don't want them to.

If that's the case, then opt-out of caching with
https://nextjs.org/docs/app/api-reference/functions/unstable_noStore.

On the contrary, if you want to enforce caching of resources you can use https://nextjs.org/docs/app/api-reference/functions/unstable_cache.
7 changes: 6 additions & 1 deletion packages/kv/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ export class VercelKV extends Redis {
}

export function createClient(config: RedisConfigNodejs): VercelKV {
return new VercelKV(config);
return new VercelKV({
// The Next.js team recommends no value or `default` for fetch requests's `cache` option
// upstash/redis defaults to `no-store`, so we enforce `default`
cache: 'default',
...config,
});
}

// eslint-disable-next-line import/no-default-export -- [@vercel/style-guide@5 migration]
Expand Down

1 comment on commit d85bb76

@vercel
Copy link

@vercel vercel bot commented on d85bb76 Nov 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.