Skip to content

Commit 8707d32

Browse files
committed
test: add 'use cache' test with SWR revalidateTag
1 parent 0e951a2 commit 8707d32

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

tests/fixtures/use-cache/app/api/revalidate/[...slug]/route.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ export async function GET(request: NextRequest, { params }) {
99
const { slug } = await params
1010

1111
const tagToInvalidate = slug.join('/')
12+
let profile = undefined
13+
if (request.nextUrl.searchParams.has('expire')) {
14+
profile = { expire: parseInt(request.nextUrl.searchParams.get('expire')) }
15+
}
1216

13-
revalidateTag(tagToInvalidate)
17+
revalidateTag(tagToInvalidate, profile)
1418

1519
return Response.json({ tagToInvalidate })
1620
}

tests/integration/use-cache.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,54 @@ describe.skipIf(!nextVersionSatisfies('>=15.3.0-canary.13'))('use cache', () =>
290290
pageComponentTimeShouldBeEqual: false,
291291
})
292292
})
293+
294+
test.skipIf(!nextVersionSatisfies('>=16.0.0-alpha.0'))<FixtureTestContext>(
295+
'invalidating tag with SWR on one lambda result in invalidating them on all lambdas',
296+
async () => {
297+
const url = `${routeRoot}/different-lambdas-tag-invalidation`
298+
299+
const { invokeFunction: invokeFunctionLambda1 } = await loadSandboxedFunction(ctx)
300+
const { invokeFunction: invokeFunctionLambda2 } = await loadSandboxedFunction(ctx)
301+
302+
const call1 = await invokeFunctionLambda1({ url })
303+
expect(call1).not.toBeCacheableResponse()
304+
305+
await invokeFunctionLambda2({
306+
url: `/api/revalidate/${useCacheTagPrefix}/${url}?expire=5`,
307+
})
308+
309+
const call2 = await invokeFunctionLambda1({ url })
310+
expect(call2).toHaveExpectedCachingBehavior(
311+
call1,
312+
// if we hit before 5 second expire timeframe passes, we expect to get stale response
313+
expectedCachingBehaviorWhenUseCacheRegenerates,
314+
)
315+
316+
const call3 = await invokeFunctionLambda1({ url })
317+
expect(call3).toHaveExpectedCachingBehavior(call2, {
318+
// previous request should trigger revalidation in background and cause everything to be fresh on this request
319+
getDataTimeShouldBeEqual: false,
320+
resultWrapperComponentTimeShouldBeEqual: false,
321+
pageComponentTimeShouldBeEqual: false,
322+
})
323+
324+
await invokeFunctionLambda2({
325+
url: `/api/revalidate/${useCacheTagPrefix}/${url}?expire=5`,
326+
})
327+
328+
// let's sleep for expire period to test that expire period is honored
329+
await new Promise((resolve) => setTimeout(resolve, 6000))
330+
331+
const call4 = await invokeFunctionLambda1({ url })
332+
expect(call4).toHaveExpectedCachingBehavior(call3, {
333+
// we are not getting stale response here because we issued request after expire period
334+
// so everything should be fresh
335+
getDataTimeShouldBeEqual: false,
336+
resultWrapperComponentTimeShouldBeEqual: false,
337+
pageComponentTimeShouldBeEqual: false,
338+
})
339+
},
340+
)
293341
})
294342

295343
describe('TTL=5 seconds', () => {

0 commit comments

Comments
 (0)