@trieb.work/nextjs-turbo-redis-cache 1.14.1 → 1.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,6 +11,10 @@ describe('Next.js 16 Cache Components Integration', () => {
11
11
  let redisClient: RedisClientType;
12
12
  let keyPrefix: string;
13
13
 
14
+ async function delay(ms: number) {
15
+ return new Promise((resolve) => setTimeout(resolve, ms));
16
+ }
17
+
14
18
  beforeAll(async () => {
15
19
  // Connect to Redis
16
20
  redisClient = createClient({
@@ -155,6 +159,41 @@ describe('Next.js 16 Cache Components Integration', () => {
155
159
  }, 20_000);
156
160
  });
157
161
 
162
+ describe('cacheLife functionality', () => {
163
+ it('should respect expire window and eventually return refreshed data', async () => {
164
+ const res1 = await fetch(`${BASE_URL}/api/cached-with-cachelife`);
165
+ const data1 = await res1.json();
166
+ expect(data1.counter).toBe(1);
167
+
168
+ const res2 = await fetch(`${BASE_URL}/api/cached-with-cachelife`);
169
+ const data2 = await res2.json();
170
+ expect(data2.counter).toBe(data1.counter);
171
+ expect(data2.timestamp).toBe(data1.timestamp);
172
+
173
+ await delay(6500);
174
+
175
+ let refreshedData: any;
176
+ for (let i = 0; i < 10; i++) {
177
+ const res = await fetch(`${BASE_URL}/api/cached-with-cachelife`);
178
+ const data = await res.json();
179
+
180
+ if (
181
+ data.counter !== data1.counter ||
182
+ data.timestamp !== data1.timestamp
183
+ ) {
184
+ refreshedData = data;
185
+ break;
186
+ }
187
+
188
+ await delay(500);
189
+ }
190
+
191
+ expect(refreshedData).toBeDefined();
192
+ expect(refreshedData.counter).toBeGreaterThan(data1.counter);
193
+ expect(refreshedData.timestamp).not.toBe(data1.timestamp);
194
+ }, 20_000);
195
+ });
196
+
158
197
  describe('Redis cache handler integration', () => {
159
198
  it('should call cache handler get and set methods', async () => {
160
199
  // Make request to trigger cache (don't clear first)
@@ -0,0 +1,24 @@
1
+ import { NextResponse } from 'next/server';
2
+ import { cacheLife, cacheTag } from 'next/cache';
3
+
4
+ let counter = 0;
5
+
6
+ export async function GET() {
7
+ const data = await getCachedDataWithLife();
8
+ return NextResponse.json(data);
9
+ }
10
+
11
+ async function getCachedDataWithLife() {
12
+ 'use cache';
13
+
14
+ cacheLife({ stale: 1, revalidate: 2, expire: 5 });
15
+ cacheTag('cache-life-test');
16
+
17
+ counter++;
18
+
19
+ return {
20
+ counter,
21
+ timestamp: Date.now(),
22
+ profile: { stale: 1, revalidate: 2, expire: 5 },
23
+ };
24
+ }
@@ -432,7 +432,7 @@ describe('Next.js Turbo Redis Cache Integration', () => {
432
432
  }
433
433
 
434
434
  // API route counter of revalidated sub-fetch-request should be the same (request deduplication of fetch requests)
435
- expect(data1.subFetchData.counter).toBe(data1.subFetchData.counter);
435
+ expect(data1.subFetchData.counter).toBe(data2.subFetchData.counter);
436
436
  subCounter = data1.subFetchData.counter;
437
437
  });
438
438
 
@@ -625,6 +625,7 @@ describe('Next.js Turbo Redis Cache Integration', () => {
625
625
  );
626
626
  // 14 days is default revalidate for pages -> expiration time is 2 * revalidate time -> -10 seconds for testing offset stability
627
627
  expect(ttl).toBeGreaterThan(2 * 14 * 24 * 60 * 60 - 30);
628
+ expect(ttl).toBeLessThanOrEqual(2 * 14 * 24 * 60 * 60);
628
629
  });
629
630
 
630
631
  it('The data in the redis key should match the expected format', async () => {