@trieb.work/nextjs-turbo-redis-cache 1.17.0 → 1.18.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.
- package/.github/workflows/release.yml +8 -157
- package/ARCHITECTURE.md +86 -47
- package/README.md +87 -24
- package/dist/index.d.mts +40 -1
- package/dist/index.d.ts +40 -1
- package/dist/index.js +264 -96
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +261 -95
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -6
- package/release.config.cjs +6 -25
- package/src/CacheComponentsHandler.ts +141 -111
- package/src/RedisStringsHandler.ts +37 -25
- package/src/SyncedMap.ts +16 -2
- package/src/index.ts +7 -0
- package/src/utils/cacheTtl.ts +71 -0
- package/src/utils/compareAndUnlink.ts +29 -0
- package/src/utils/redisConnection.ts +17 -0
- package/src/utils/tagRevalidation.ts +162 -0
- package/test/README.md +10 -10
- package/test/nextjs-test-projects/next-app-16-0-11-cache-components/next.config.ts +2 -0
- package/test/nextjs-test-projects/next-app-16-0-11-cache-components/src/app/api/expire-matrix/revalidate/route.ts +34 -0
- package/test/nextjs-test-projects/next-app-16-0-11-cache-components/src/app/api/expire-matrix/route.ts +23 -0
- package/test/nextjs-test-projects/next-app-16-0-11-cache-components/src/app/cache-lab/page.tsx +22 -0
- package/test/nextjs-test-projects/next-app-16-0-11-cache-components/src/app/cache-lab/revalidate-durations/page.tsx +106 -0
- package/test/nextjs-test-projects/next-app-16-0-11-cache-components/src/app/cache-lab/use-cache-remote/page.tsx +85 -0
- package/test/nextjs-test-projects/next-app-16-2-6-cache-components/next.config.ts +2 -0
- package/test/nextjs-test-projects/next-app-16-2-6-cache-components/src/app/api/expire-matrix/revalidate/route.ts +34 -0
- package/test/nextjs-test-projects/next-app-16-2-6-cache-components/src/app/api/expire-matrix/route.ts +23 -0
- package/test/nextjs-test-projects/next-app-16-2-6-cache-components/src/app/cache-lab/page.tsx +22 -0
- package/test/nextjs-test-projects/next-app-16-2-6-cache-components/src/app/cache-lab/revalidate-durations/page.tsx +106 -0
- package/test/nextjs-test-projects/next-app-16-2-6-cache-components/src/app/cache-lab/use-cache-remote/page.tsx +85 -0
- package/test/nextjs-test-projects/next-app-16-3-0-cache-components/next.config.ts +2 -0
- package/test/nextjs-test-projects/next-app-16-3-0-cache-components/src/app/api/expire-matrix/revalidate/route.ts +34 -0
- package/test/nextjs-test-projects/next-app-16-3-0-cache-components/src/app/api/expire-matrix/route.ts +23 -0
- package/test/nextjs-test-projects/next-app-16-3-0-cache-components/src/app/cache-lab/page.tsx +22 -0
- package/test/nextjs-test-projects/next-app-16-3-0-cache-components/src/app/cache-lab/revalidate-durations/page.tsx +106 -0
- package/test/nextjs-test-projects/next-app-16-3-0-cache-components/src/app/cache-lab/use-cache-remote/page.tsx +85 -0
- package/test/playwright/cache-lab.spec.ts +75 -0
- package/test/vitest/integration/cache-components/cache-components.integration.test.ts +194 -36
- package/test/vitest/integration/nextjs-cache-handler.integration.test.ts +16 -2
- package/test/vitest/integration/pages-router.integration.test.ts +16 -6
- package/test/vitest/unit/SyncedMap-orphan-cleanup.test.ts +111 -0
- package/test/vitest/unit/cache-components-update-tags.test.ts +350 -0
- package/test/vitest/unit/cache-ttl.test.ts +67 -0
- package/test/vitest/unit/compare-and-unlink.test.ts +22 -0
- package/test/vitest/unit/index.test.ts +17 -0
- package/test/vitest/unit/pages-router-kinds.test.ts +11 -0
- package/test/vitest/unit/redis-connection.test.ts +39 -0
- package/test/vitest/unit/tag-revalidation.test.ts +178 -0
- package/CHANGELOG.md +0 -411
package/test/nextjs-test-projects/next-app-16-2-6-cache-components/src/app/cache-lab/page.tsx
CHANGED
|
@@ -70,6 +70,28 @@ export default function CacheLabIndexPage() {
|
|
|
70
70
|
it into a cached component to show correct boundaries.
|
|
71
71
|
</p>
|
|
72
72
|
</Link>
|
|
73
|
+
|
|
74
|
+
<Link
|
|
75
|
+
className="rounded-lg border p-5 hover:bg-slate-50"
|
|
76
|
+
href="/cache-lab/use-cache-remote"
|
|
77
|
+
>
|
|
78
|
+
<h2 className="text-lg font-medium">use cache: remote</h2>
|
|
79
|
+
<p className="mt-1 text-sm text-slate-600">
|
|
80
|
+
Exercises <code>cacheHandlers.remote</code> with Redis-backed
|
|
81
|
+
streams and <code>updateTag</code>.
|
|
82
|
+
</p>
|
|
83
|
+
</Link>
|
|
84
|
+
|
|
85
|
+
<Link
|
|
86
|
+
className="rounded-lg border p-5 hover:bg-slate-50"
|
|
87
|
+
href="/cache-lab/revalidate-durations"
|
|
88
|
+
>
|
|
89
|
+
<h2 className="text-lg font-medium">revalidateTag durations</h2>
|
|
90
|
+
<p className="mt-1 text-sm text-slate-600">
|
|
91
|
+
Deferred tag invalidation via{' '}
|
|
92
|
+
<code>revalidateTag(tag, {'{ expire }'})</code> and SWR behavior.
|
|
93
|
+
</p>
|
|
94
|
+
</Link>
|
|
73
95
|
</section>
|
|
74
96
|
|
|
75
97
|
<section className="mt-12 rounded-lg border bg-slate-50 p-5">
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import Link from 'next/link';
|
|
2
|
+
import { Suspense } from 'react';
|
|
3
|
+
import { cacheLife, cacheTag, revalidateTag } from 'next/cache';
|
|
4
|
+
|
|
5
|
+
function sleep(ms: number): Promise<void> {
|
|
6
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
async function getSlowTaggedValue() {
|
|
10
|
+
'use cache';
|
|
11
|
+
|
|
12
|
+
cacheLife({ stale: 60, revalidate: 120, expire: 300 });
|
|
13
|
+
cacheTag('cache-lab:durations');
|
|
14
|
+
|
|
15
|
+
await sleep(2500);
|
|
16
|
+
|
|
17
|
+
return {
|
|
18
|
+
computedAt: Date.now(),
|
|
19
|
+
value: Math.random(),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async function triggerDeferredRevalidateTag() {
|
|
24
|
+
'use server';
|
|
25
|
+
revalidateTag('cache-lab:durations', { expire: 2 });
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async function CachedPanel() {
|
|
29
|
+
const data = await getSlowTaggedValue();
|
|
30
|
+
|
|
31
|
+
return (
|
|
32
|
+
<div className="rounded-lg border p-5">
|
|
33
|
+
<div className="text-sm text-slate-700">
|
|
34
|
+
<div>
|
|
35
|
+
<span className="font-medium">computedAt:</span>{' '}
|
|
36
|
+
<span data-testid="computedAt" className="font-mono">
|
|
37
|
+
{data.computedAt}
|
|
38
|
+
</span>
|
|
39
|
+
</div>
|
|
40
|
+
<div className="mt-1">
|
|
41
|
+
<span className="font-medium">value:</span>{' '}
|
|
42
|
+
<span data-testid="value" className="font-mono">
|
|
43
|
+
{data.value}
|
|
44
|
+
</span>
|
|
45
|
+
</div>
|
|
46
|
+
</div>
|
|
47
|
+
</div>
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export default function RevalidateDurationsPage() {
|
|
52
|
+
return (
|
|
53
|
+
<main className="mx-auto max-w-3xl p-10">
|
|
54
|
+
<div className="mb-6">
|
|
55
|
+
<Link
|
|
56
|
+
className="text-sm text-blue-600 hover:underline"
|
|
57
|
+
href="/cache-lab"
|
|
58
|
+
>
|
|
59
|
+
← Back to Cache Lab
|
|
60
|
+
</Link>
|
|
61
|
+
</div>
|
|
62
|
+
|
|
63
|
+
<header className="mb-6">
|
|
64
|
+
<h1 className="text-2xl font-semibold">
|
|
65
|
+
revalidateTag durations (SWR stale window)
|
|
66
|
+
</h1>
|
|
67
|
+
<p className="mt-2 text-sm text-slate-600">
|
|
68
|
+
Calls <code>revalidateTag(tag, {'{ expire: 2 }'})</code>. The handler
|
|
69
|
+
should mark the tag stale immediately while keeping the Redis entry so
|
|
70
|
+
the next request can serve stale content (SWR) during refresh.
|
|
71
|
+
</p>
|
|
72
|
+
</header>
|
|
73
|
+
|
|
74
|
+
<Suspense
|
|
75
|
+
fallback={
|
|
76
|
+
<div className="rounded-lg border bg-slate-50 p-5 text-sm text-slate-700">
|
|
77
|
+
Loading cached content… (first load can take ~2.5s)
|
|
78
|
+
</div>
|
|
79
|
+
}
|
|
80
|
+
>
|
|
81
|
+
<CachedPanel />
|
|
82
|
+
</Suspense>
|
|
83
|
+
|
|
84
|
+
<form action={triggerDeferredRevalidateTag} className="mt-6">
|
|
85
|
+
<button
|
|
86
|
+
className="rounded-md border border-blue-600 bg-white px-4 py-2 text-sm font-medium text-blue-700 hover:bg-blue-50"
|
|
87
|
+
type="submit"
|
|
88
|
+
>
|
|
89
|
+
revalidateTag('cache-lab:durations', {'{ expire: 2 }'})
|
|
90
|
+
</button>
|
|
91
|
+
</form>
|
|
92
|
+
|
|
93
|
+
<div className="mt-6 rounded-lg border bg-slate-50 p-5 text-sm text-slate-700">
|
|
94
|
+
<p className="font-medium">How to observe SWR invalidation</p>
|
|
95
|
+
<ol className="mt-2 list-decimal space-y-1 pl-5">
|
|
96
|
+
<li>Load once (expect ~2.5s).</li>
|
|
97
|
+
<li>Click the button, then reload immediately.</li>
|
|
98
|
+
<li>
|
|
99
|
+
The reload should still show the old values (fast SWR). After
|
|
100
|
+
background refresh (~2.5s), values should change.
|
|
101
|
+
</li>
|
|
102
|
+
</ol>
|
|
103
|
+
</div>
|
|
104
|
+
</main>
|
|
105
|
+
);
|
|
106
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import Link from 'next/link';
|
|
2
|
+
import { Suspense } from 'react';
|
|
3
|
+
import { cacheTag, updateTag } from 'next/cache';
|
|
4
|
+
|
|
5
|
+
async function getRemoteCachedValue() {
|
|
6
|
+
'use cache: remote';
|
|
7
|
+
|
|
8
|
+
cacheTag('cache-lab:remote');
|
|
9
|
+
|
|
10
|
+
return {
|
|
11
|
+
value: Math.random(),
|
|
12
|
+
computedAt: Date.now(),
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async function triggerUpdateTag() {
|
|
17
|
+
'use server';
|
|
18
|
+
updateTag('cache-lab:remote');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async function RemotePanel() {
|
|
22
|
+
const data = await getRemoteCachedValue();
|
|
23
|
+
|
|
24
|
+
return (
|
|
25
|
+
<div className="rounded-lg border p-5">
|
|
26
|
+
<div className="text-sm text-slate-700">
|
|
27
|
+
<div>
|
|
28
|
+
<span className="font-medium">computedAt:</span>{' '}
|
|
29
|
+
<span data-testid="computedAt" className="font-mono">
|
|
30
|
+
{data.computedAt}
|
|
31
|
+
</span>
|
|
32
|
+
</div>
|
|
33
|
+
<div className="mt-1">
|
|
34
|
+
<span className="font-medium">value:</span>{' '}
|
|
35
|
+
<span data-testid="value" className="font-mono">
|
|
36
|
+
{data.value}
|
|
37
|
+
</span>
|
|
38
|
+
</div>
|
|
39
|
+
</div>
|
|
40
|
+
</div>
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export default function UseCacheRemotePage() {
|
|
45
|
+
return (
|
|
46
|
+
<main className="mx-auto max-w-3xl p-10">
|
|
47
|
+
<div className="mb-6">
|
|
48
|
+
<Link
|
|
49
|
+
className="text-sm text-blue-600 hover:underline"
|
|
50
|
+
href="/cache-lab"
|
|
51
|
+
>
|
|
52
|
+
← Back to Cache Lab
|
|
53
|
+
</Link>
|
|
54
|
+
</div>
|
|
55
|
+
|
|
56
|
+
<header className="mb-6">
|
|
57
|
+
<h1 className="text-2xl font-semibold">use cache: remote</h1>
|
|
58
|
+
<p className="mt-2 text-sm text-slate-600">
|
|
59
|
+
Exercises the <code>cacheHandlers.remote</code> handler backed by
|
|
60
|
+
Redis. Values should stay stable across reloads until{' '}
|
|
61
|
+
<code>updateTag</code> runs.
|
|
62
|
+
</p>
|
|
63
|
+
</header>
|
|
64
|
+
|
|
65
|
+
<Suspense
|
|
66
|
+
fallback={
|
|
67
|
+
<div className="rounded-lg border bg-slate-50 p-5 text-sm text-slate-700">
|
|
68
|
+
Loading remote cached content…
|
|
69
|
+
</div>
|
|
70
|
+
}
|
|
71
|
+
>
|
|
72
|
+
<RemotePanel />
|
|
73
|
+
</Suspense>
|
|
74
|
+
|
|
75
|
+
<form action={triggerUpdateTag} className="mt-6">
|
|
76
|
+
<button
|
|
77
|
+
className="rounded-md border border-blue-600 bg-white px-4 py-2 text-sm font-medium text-blue-700 hover:bg-blue-50"
|
|
78
|
+
type="submit"
|
|
79
|
+
>
|
|
80
|
+
updateTag('cache-lab:remote')
|
|
81
|
+
</button>
|
|
82
|
+
</form>
|
|
83
|
+
</main>
|
|
84
|
+
);
|
|
85
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { NextResponse } from 'next/server';
|
|
2
|
+
import { revalidateTag } from 'next/cache';
|
|
3
|
+
|
|
4
|
+
type Profile =
|
|
5
|
+
| 'max'
|
|
6
|
+
| 'default'
|
|
7
|
+
| 'seconds'
|
|
8
|
+
| 'minutes'
|
|
9
|
+
| 'hours'
|
|
10
|
+
| 'days'
|
|
11
|
+
| 'weeks'
|
|
12
|
+
| { expire: number };
|
|
13
|
+
|
|
14
|
+
export async function POST(request: Request) {
|
|
15
|
+
const body = (await request.json()) as {
|
|
16
|
+
tag?: string;
|
|
17
|
+
profile?: Profile | null;
|
|
18
|
+
};
|
|
19
|
+
const tag = body.tag || 'expire-matrix-default';
|
|
20
|
+
|
|
21
|
+
if (body.profile === undefined || body.profile === null) {
|
|
22
|
+
// Deprecated single-arg form: Next.js calls updateTags(tags) with no durations.
|
|
23
|
+
(revalidateTag as (tag: string) => void)(tag);
|
|
24
|
+
} else {
|
|
25
|
+
revalidateTag(tag, body.profile);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return NextResponse.json({
|
|
29
|
+
revalidated: true,
|
|
30
|
+
tag,
|
|
31
|
+
profile: body.profile ?? null,
|
|
32
|
+
timestamp: Date.now(),
|
|
33
|
+
});
|
|
34
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { NextResponse } from 'next/server';
|
|
2
|
+
import { cacheLife, cacheTag } from 'next/cache';
|
|
3
|
+
|
|
4
|
+
let counter = 0;
|
|
5
|
+
|
|
6
|
+
export async function GET(request: Request) {
|
|
7
|
+
const id = new URL(request.url).searchParams.get('id') || 'default';
|
|
8
|
+
const data = await getExpireMatrixData(id);
|
|
9
|
+
return NextResponse.json({ ...data, id });
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
async function getExpireMatrixData(id: string) {
|
|
13
|
+
'use cache';
|
|
14
|
+
// Long time-based lifetime so only tag invalidation can change the value.
|
|
15
|
+
cacheLife({ stale: 300, revalidate: 3600, expire: 86_400 });
|
|
16
|
+
cacheTag(`expire-matrix-${id}`);
|
|
17
|
+
|
|
18
|
+
counter += 1;
|
|
19
|
+
return {
|
|
20
|
+
counter,
|
|
21
|
+
timestamp: Date.now(),
|
|
22
|
+
};
|
|
23
|
+
}
|
package/test/nextjs-test-projects/next-app-16-3-0-cache-components/src/app/cache-lab/page.tsx
CHANGED
|
@@ -70,6 +70,28 @@ export default function CacheLabIndexPage() {
|
|
|
70
70
|
it into a cached component to show correct boundaries.
|
|
71
71
|
</p>
|
|
72
72
|
</Link>
|
|
73
|
+
|
|
74
|
+
<Link
|
|
75
|
+
className="rounded-lg border p-5 hover:bg-slate-50"
|
|
76
|
+
href="/cache-lab/use-cache-remote"
|
|
77
|
+
>
|
|
78
|
+
<h2 className="text-lg font-medium">use cache: remote</h2>
|
|
79
|
+
<p className="mt-1 text-sm text-slate-600">
|
|
80
|
+
Exercises <code>cacheHandlers.remote</code> with Redis-backed
|
|
81
|
+
streams and <code>updateTag</code>.
|
|
82
|
+
</p>
|
|
83
|
+
</Link>
|
|
84
|
+
|
|
85
|
+
<Link
|
|
86
|
+
className="rounded-lg border p-5 hover:bg-slate-50"
|
|
87
|
+
href="/cache-lab/revalidate-durations"
|
|
88
|
+
>
|
|
89
|
+
<h2 className="text-lg font-medium">revalidateTag durations</h2>
|
|
90
|
+
<p className="mt-1 text-sm text-slate-600">
|
|
91
|
+
Deferred tag invalidation via{' '}
|
|
92
|
+
<code>revalidateTag(tag, {'{ expire }'})</code> and SWR behavior.
|
|
93
|
+
</p>
|
|
94
|
+
</Link>
|
|
73
95
|
</section>
|
|
74
96
|
|
|
75
97
|
<section className="mt-12 rounded-lg border bg-slate-50 p-5">
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import Link from 'next/link';
|
|
2
|
+
import { Suspense } from 'react';
|
|
3
|
+
import { cacheLife, cacheTag, revalidateTag } from 'next/cache';
|
|
4
|
+
|
|
5
|
+
function sleep(ms: number): Promise<void> {
|
|
6
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
async function getSlowTaggedValue() {
|
|
10
|
+
'use cache';
|
|
11
|
+
|
|
12
|
+
cacheLife({ stale: 60, revalidate: 120, expire: 300 });
|
|
13
|
+
cacheTag('cache-lab:durations');
|
|
14
|
+
|
|
15
|
+
await sleep(2500);
|
|
16
|
+
|
|
17
|
+
return {
|
|
18
|
+
computedAt: Date.now(),
|
|
19
|
+
value: Math.random(),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async function triggerDeferredRevalidateTag() {
|
|
24
|
+
'use server';
|
|
25
|
+
revalidateTag('cache-lab:durations', { expire: 2 });
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async function CachedPanel() {
|
|
29
|
+
const data = await getSlowTaggedValue();
|
|
30
|
+
|
|
31
|
+
return (
|
|
32
|
+
<div className="rounded-lg border p-5">
|
|
33
|
+
<div className="text-sm text-slate-700">
|
|
34
|
+
<div>
|
|
35
|
+
<span className="font-medium">computedAt:</span>{' '}
|
|
36
|
+
<span data-testid="computedAt" className="font-mono">
|
|
37
|
+
{data.computedAt}
|
|
38
|
+
</span>
|
|
39
|
+
</div>
|
|
40
|
+
<div className="mt-1">
|
|
41
|
+
<span className="font-medium">value:</span>{' '}
|
|
42
|
+
<span data-testid="value" className="font-mono">
|
|
43
|
+
{data.value}
|
|
44
|
+
</span>
|
|
45
|
+
</div>
|
|
46
|
+
</div>
|
|
47
|
+
</div>
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export default function RevalidateDurationsPage() {
|
|
52
|
+
return (
|
|
53
|
+
<main className="mx-auto max-w-3xl p-10">
|
|
54
|
+
<div className="mb-6">
|
|
55
|
+
<Link
|
|
56
|
+
className="text-sm text-blue-600 hover:underline"
|
|
57
|
+
href="/cache-lab"
|
|
58
|
+
>
|
|
59
|
+
← Back to Cache Lab
|
|
60
|
+
</Link>
|
|
61
|
+
</div>
|
|
62
|
+
|
|
63
|
+
<header className="mb-6">
|
|
64
|
+
<h1 className="text-2xl font-semibold">
|
|
65
|
+
revalidateTag durations (SWR stale window)
|
|
66
|
+
</h1>
|
|
67
|
+
<p className="mt-2 text-sm text-slate-600">
|
|
68
|
+
Calls <code>revalidateTag(tag, {'{ expire: 2 }'})</code>. The handler
|
|
69
|
+
should mark the tag stale immediately while keeping the Redis entry so
|
|
70
|
+
the next request can serve stale content (SWR) during refresh.
|
|
71
|
+
</p>
|
|
72
|
+
</header>
|
|
73
|
+
|
|
74
|
+
<Suspense
|
|
75
|
+
fallback={
|
|
76
|
+
<div className="rounded-lg border bg-slate-50 p-5 text-sm text-slate-700">
|
|
77
|
+
Loading cached content… (first load can take ~2.5s)
|
|
78
|
+
</div>
|
|
79
|
+
}
|
|
80
|
+
>
|
|
81
|
+
<CachedPanel />
|
|
82
|
+
</Suspense>
|
|
83
|
+
|
|
84
|
+
<form action={triggerDeferredRevalidateTag} className="mt-6">
|
|
85
|
+
<button
|
|
86
|
+
className="rounded-md border border-blue-600 bg-white px-4 py-2 text-sm font-medium text-blue-700 hover:bg-blue-50"
|
|
87
|
+
type="submit"
|
|
88
|
+
>
|
|
89
|
+
revalidateTag('cache-lab:durations', {'{ expire: 2 }'})
|
|
90
|
+
</button>
|
|
91
|
+
</form>
|
|
92
|
+
|
|
93
|
+
<div className="mt-6 rounded-lg border bg-slate-50 p-5 text-sm text-slate-700">
|
|
94
|
+
<p className="font-medium">How to observe SWR invalidation</p>
|
|
95
|
+
<ol className="mt-2 list-decimal space-y-1 pl-5">
|
|
96
|
+
<li>Load once (expect ~2.5s).</li>
|
|
97
|
+
<li>Click the button, then reload immediately.</li>
|
|
98
|
+
<li>
|
|
99
|
+
The reload should still show the old values (fast SWR). After
|
|
100
|
+
background refresh (~2.5s), values should change.
|
|
101
|
+
</li>
|
|
102
|
+
</ol>
|
|
103
|
+
</div>
|
|
104
|
+
</main>
|
|
105
|
+
);
|
|
106
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import Link from 'next/link';
|
|
2
|
+
import { Suspense } from 'react';
|
|
3
|
+
import { cacheTag, updateTag } from 'next/cache';
|
|
4
|
+
|
|
5
|
+
async function getRemoteCachedValue() {
|
|
6
|
+
'use cache: remote';
|
|
7
|
+
|
|
8
|
+
cacheTag('cache-lab:remote');
|
|
9
|
+
|
|
10
|
+
return {
|
|
11
|
+
value: Math.random(),
|
|
12
|
+
computedAt: Date.now(),
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async function triggerUpdateTag() {
|
|
17
|
+
'use server';
|
|
18
|
+
updateTag('cache-lab:remote');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async function RemotePanel() {
|
|
22
|
+
const data = await getRemoteCachedValue();
|
|
23
|
+
|
|
24
|
+
return (
|
|
25
|
+
<div className="rounded-lg border p-5">
|
|
26
|
+
<div className="text-sm text-slate-700">
|
|
27
|
+
<div>
|
|
28
|
+
<span className="font-medium">computedAt:</span>{' '}
|
|
29
|
+
<span data-testid="computedAt" className="font-mono">
|
|
30
|
+
{data.computedAt}
|
|
31
|
+
</span>
|
|
32
|
+
</div>
|
|
33
|
+
<div className="mt-1">
|
|
34
|
+
<span className="font-medium">value:</span>{' '}
|
|
35
|
+
<span data-testid="value" className="font-mono">
|
|
36
|
+
{data.value}
|
|
37
|
+
</span>
|
|
38
|
+
</div>
|
|
39
|
+
</div>
|
|
40
|
+
</div>
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export default function UseCacheRemotePage() {
|
|
45
|
+
return (
|
|
46
|
+
<main className="mx-auto max-w-3xl p-10">
|
|
47
|
+
<div className="mb-6">
|
|
48
|
+
<Link
|
|
49
|
+
className="text-sm text-blue-600 hover:underline"
|
|
50
|
+
href="/cache-lab"
|
|
51
|
+
>
|
|
52
|
+
← Back to Cache Lab
|
|
53
|
+
</Link>
|
|
54
|
+
</div>
|
|
55
|
+
|
|
56
|
+
<header className="mb-6">
|
|
57
|
+
<h1 className="text-2xl font-semibold">use cache: remote</h1>
|
|
58
|
+
<p className="mt-2 text-sm text-slate-600">
|
|
59
|
+
Exercises the <code>cacheHandlers.remote</code> handler backed by
|
|
60
|
+
Redis. Values should stay stable across reloads until{' '}
|
|
61
|
+
<code>updateTag</code> runs.
|
|
62
|
+
</p>
|
|
63
|
+
</header>
|
|
64
|
+
|
|
65
|
+
<Suspense
|
|
66
|
+
fallback={
|
|
67
|
+
<div className="rounded-lg border bg-slate-50 p-5 text-sm text-slate-700">
|
|
68
|
+
Loading remote cached content…
|
|
69
|
+
</div>
|
|
70
|
+
}
|
|
71
|
+
>
|
|
72
|
+
<RemotePanel />
|
|
73
|
+
</Suspense>
|
|
74
|
+
|
|
75
|
+
<form action={triggerUpdateTag} className="mt-6">
|
|
76
|
+
<button
|
|
77
|
+
className="rounded-md border border-blue-600 bg-white px-4 py-2 text-sm font-medium text-blue-700 hover:bg-blue-50"
|
|
78
|
+
type="submit"
|
|
79
|
+
>
|
|
80
|
+
updateTag('cache-lab:remote')
|
|
81
|
+
</button>
|
|
82
|
+
</form>
|
|
83
|
+
</main>
|
|
84
|
+
);
|
|
85
|
+
}
|
|
@@ -124,6 +124,81 @@ test.describe('Cache Lab (Cache Components)', () => {
|
|
|
124
124
|
expect(payloadB).not.toBe(payloadA1);
|
|
125
125
|
});
|
|
126
126
|
|
|
127
|
+
test('use cache: remote values are stable until updateTag', async ({
|
|
128
|
+
page,
|
|
129
|
+
}) => {
|
|
130
|
+
await page.goto('/cache-lab/use-cache-remote');
|
|
131
|
+
|
|
132
|
+
const computedAt = page.getByTestId('computedAt');
|
|
133
|
+
const value = page.getByTestId('value');
|
|
134
|
+
|
|
135
|
+
await expect(computedAt).toBeVisible();
|
|
136
|
+
|
|
137
|
+
const beforeComputedAt = await computedAt.textContent();
|
|
138
|
+
const beforeValue = await value.textContent();
|
|
139
|
+
|
|
140
|
+
await page.reload();
|
|
141
|
+
await expect(computedAt).toBeVisible();
|
|
142
|
+
expect(await computedAt.textContent()).toBe(beforeComputedAt);
|
|
143
|
+
expect(await value.textContent()).toBe(beforeValue);
|
|
144
|
+
|
|
145
|
+
await page
|
|
146
|
+
.getByRole('button', { name: "updateTag('cache-lab:remote')" })
|
|
147
|
+
.click();
|
|
148
|
+
await page.waitForLoadState('networkidle');
|
|
149
|
+
|
|
150
|
+
await expect(computedAt).toBeVisible();
|
|
151
|
+
expect(await computedAt.textContent()).not.toBe(beforeComputedAt);
|
|
152
|
+
expect(await value.textContent()).not.toBe(beforeValue);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
test('revalidateTag with durations can serve stale values before refresh completes', async ({
|
|
156
|
+
page,
|
|
157
|
+
}) => {
|
|
158
|
+
await page.goto('/cache-lab/revalidate-durations');
|
|
159
|
+
|
|
160
|
+
const computedAt = page.getByTestId('computedAt');
|
|
161
|
+
const value = page.getByTestId('value');
|
|
162
|
+
|
|
163
|
+
await expect(computedAt).toBeVisible();
|
|
164
|
+
|
|
165
|
+
const beforeComputedAt = await computedAt.textContent();
|
|
166
|
+
const beforeValue = await value.textContent();
|
|
167
|
+
|
|
168
|
+
const actionResponse = page.waitForResponse(
|
|
169
|
+
(res) =>
|
|
170
|
+
res.request().method() === 'POST' &&
|
|
171
|
+
res.url().includes('/cache-lab/revalidate-durations'),
|
|
172
|
+
);
|
|
173
|
+
await page
|
|
174
|
+
.getByRole('button', {
|
|
175
|
+
name: "revalidateTag('cache-lab:durations', { expire: 2 })",
|
|
176
|
+
})
|
|
177
|
+
.click();
|
|
178
|
+
await actionResponse;
|
|
179
|
+
|
|
180
|
+
// Reload as soon as the Server Action has landed. Do not wait for
|
|
181
|
+
// networkidle — that can let the ~2.5s background refresh finish first.
|
|
182
|
+
await page.reload();
|
|
183
|
+
await expect(computedAt).toBeVisible();
|
|
184
|
+
|
|
185
|
+
const afterRevalidateComputedAt = await computedAt.textContent();
|
|
186
|
+
const afterRevalidateValue = await value.textContent();
|
|
187
|
+
|
|
188
|
+
// SWR: ideally the first reload still serves previous values. If background
|
|
189
|
+
// refresh already completed, skip the stale assertion and verify eventual refresh.
|
|
190
|
+
if (afterRevalidateComputedAt === beforeComputedAt) {
|
|
191
|
+
expect(afterRevalidateValue).toBe(beforeValue);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
await page.waitForTimeout(5000);
|
|
195
|
+
await page.reload();
|
|
196
|
+
await expect(computedAt).toBeVisible();
|
|
197
|
+
|
|
198
|
+
expect(await computedAt.textContent()).not.toBe(beforeComputedAt);
|
|
199
|
+
expect(await value.textContent()).not.toBe(beforeValue);
|
|
200
|
+
});
|
|
201
|
+
|
|
127
202
|
test('stale-while-revalidate demo eventually produces a new value after revalidateTag', async ({
|
|
128
203
|
page,
|
|
129
204
|
}) => {
|