Skip to content

Commit 1dee5ab

Browse files
authored
Allow cacheLife to error (#890)
Next.js currently bundles proxy in a way that is indistinguishable from page code but in this context "use cache" isn't turned into a Cache Function so calls to `cacheLife` error. This will be fixed in Next.js but as an interrim fix we can just try/catch and suppress the error. This is safe for our context becasue we know we only call this from within "use cache" and we know that in any environment without "use cache" it should just fall back to normal behavior.
1 parent b5cc954 commit 1dee5ab

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

.changeset/thirty-dogs-play.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@vercel/edge-config': patch
3+
---
4+
5+
Suppress errors thrown by cacheLife for contexts where the next-js condition is set but "use cache" functions are not turned into Cache Functions

packages/edge-config/src/index.next-js.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,22 @@ export {
2929
function setCacheLifeFromFetchCache(
3030
fetchCache: undefined | 'force-cache' | 'no-store',
3131
): void {
32-
if (fetchCache === 'force-cache') {
33-
cacheLife('default');
34-
} else {
35-
// Working around a limitation of cacheLife in older Next.js versions
36-
// where stale was required to be greater than expire if set concurrently.
37-
// Instead we do this over two calls.
38-
cacheLife({ revalidate: 0, expire: 0 });
39-
cacheLife({ stale: 60 });
32+
try {
33+
if (fetchCache === 'force-cache') {
34+
cacheLife('default');
35+
} else {
36+
// Working around a limitation of cacheLife in older Next.js versions
37+
// where stale was required to be greater than expire if set concurrently.
38+
// Instead we do this over two calls.
39+
cacheLife({ revalidate: 0, expire: 0 });
40+
cacheLife({ stale: 60 });
41+
}
42+
} catch {
43+
// if we error setting cache life it means we are not in a cache scope
44+
// The only time that might happen is if next-js entrypoint is used
45+
// in a context that doesn't process the "use cache" directive.
46+
// In these contexts we don't really need the cache life to be set because there
47+
// is no Cache Component semantics
4048
}
4149
}
4250

0 commit comments

Comments
 (0)