Skip to content

Commit c076448

Browse files
authoredFeb 7, 2024
fix: unstable_cache not working (#237)
* chore: add repro test * fix: use revalidate value from blob
1 parent f728c4a commit c076448

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed
 

‎src/run/handlers/cache.cts

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export class NetlifyCacheHandler implements CacheHandler {
6969
value: {
7070
kind: blob.value.kind,
7171
data: blob.value.data,
72-
revalidate: ctx.revalidate || 1,
72+
revalidate: blob.value.revalidate,
7373
},
7474
}
7575

‎tests/e2e/page-router.test.ts

+72
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,75 @@ test('requesting a page with a very long name works', async ({ page }) => {
9696
)
9797
expect(response?.status()).toBe(200)
9898
})
99+
100+
export function waitFor(millis: number) {
101+
return new Promise((resolve) => setTimeout(resolve, millis))
102+
}
103+
104+
/**
105+
* Check for content in 1 second intervals timing out after 30 seconds.
106+
*
107+
* @param {() => Promise<unknown> | unknown} contentFn
108+
* @param {RegExp | string | number} regex
109+
* @param {boolean} hardError
110+
* @param {number} maxRetries
111+
* @returns {Promise<boolean>}
112+
*/
113+
export async function check(
114+
contentFn: () => any | Promise<any>,
115+
regex: any,
116+
hardError = true,
117+
maxRetries = 30,
118+
) {
119+
let content
120+
let lastErr
121+
122+
for (let tries = 0; tries < maxRetries; tries++) {
123+
try {
124+
content = await contentFn()
125+
if (typeof regex !== typeof /regex/) {
126+
if (regex === content) {
127+
return true
128+
}
129+
} else if (regex.test(content)) {
130+
// found the content
131+
return true
132+
}
133+
await waitFor(1000)
134+
} catch (err) {
135+
await waitFor(1000)
136+
lastErr = err
137+
}
138+
}
139+
console.error('TIMED OUT CHECK: ', { regex, content, lastErr })
140+
141+
if (hardError) {
142+
throw new Error('TIMED OUT: ' + regex + '\n\n' + content + '\n\n' + lastErr)
143+
}
144+
return false
145+
}
146+
147+
// adapted from https://github.com/vercel/next.js/blob/89fcf68c6acd62caf91a8cf0bfd3fdc566e75d9d/test/e2e/app-dir/app-static/app-static.test.ts#L108
148+
149+
test('unstable-cache should work', async () => {
150+
const pathname = `${ctx.url}/api/unstable-cache-node`
151+
let res = await fetch(`${ctx.url}/api/unstable-cache-node`)
152+
expect(res.status).toBe(200)
153+
let prevData = await res.json()
154+
155+
expect(prevData.data.random).toBeTruthy()
156+
157+
await check(async () => {
158+
res = await fetch(pathname)
159+
expect(res.status).toBe(200)
160+
const curData = await res.json()
161+
162+
try {
163+
expect(curData.data.random).toBeTruthy()
164+
expect(curData.data.random).toBe(prevData.data.random)
165+
} finally {
166+
prevData = curData
167+
}
168+
return 'success'
169+
}, 'success')
170+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { unstable_cache } from 'next/cache'
2+
3+
export default async function handler(req, res) {
4+
const data = await unstable_cache(async () => {
5+
return {
6+
random: Math.random(),
7+
}
8+
})()
9+
10+
res.json({
11+
now: Date.now(),
12+
data,
13+
})
14+
}

0 commit comments

Comments
 (0)
Please sign in to comment.