Skip to content

Commit dcc864d

Browse files
vicbconico974
andauthored
feat: add an experimental KV based tag cache (#906)
Co-authored-by: conico974 <[email protected]>
1 parent 2529c20 commit dcc864d

25 files changed

+804
-35
lines changed

.changeset/silver-walls-cover.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/cloudflare": minor
3+
---
4+
5+
feat: add an experimental KV based tag cache

examples/common/apps.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const apps = [
1616
"experimental",
1717
// overrides
1818
"d1-tag-next",
19+
"kv-tag-next",
1920
"memory-queue",
2021
"r2-incremental-cache",
2122
"static-assets-incremental-cache",
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.*
7+
.yarn/*
8+
!.yarn/patches
9+
!.yarn/plugins
10+
!.yarn/releases
11+
!.yarn/versions
12+
13+
# testing
14+
/coverage
15+
16+
# next.js
17+
/.next/
18+
/out/
19+
20+
# production
21+
/build
22+
23+
# misc
24+
.DS_Store
25+
*.pem
26+
27+
# debug
28+
npm-debug.log*
29+
yarn-debug.log*
30+
yarn-error.log*
31+
.pnpm-debug.log*
32+
33+
# env files (can opt-in for committing if needed)
34+
.env*
35+
36+
# vercel
37+
.vercel
38+
39+
# typescript
40+
*.tsbuildinfo
41+
next-env.d.ts
42+
43+
# playwright
44+
/test-results/
45+
/playwright-report/
46+
/blob-report/
47+
/playwright/.cache/
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"use server";
2+
3+
import { revalidatePath, revalidateTag } from "next/cache";
4+
5+
export async function revalidateTagAction() {
6+
revalidateTag("date");
7+
}
8+
9+
export async function revalidatePathAction() {
10+
revalidatePath("/");
11+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"use client";
2+
3+
import { revalidateTagAction, revalidatePathAction } from "../action";
4+
5+
export default function RevalidationButtons() {
6+
return (
7+
<div>
8+
<button
9+
data-testid="revalidate-tag"
10+
onClick={async () => {
11+
await revalidateTagAction();
12+
}}
13+
>
14+
Invalidate tag
15+
</button>
16+
17+
<button
18+
data-testid="revalidate-path"
19+
onClick={async () => {
20+
await revalidatePathAction();
21+
}}
22+
>
23+
Invalidate Path
24+
</button>
25+
</div>
26+
);
27+
}
25.3 KB
Binary file not shown.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
html,
2+
body {
3+
max-width: 100vw;
4+
overflow-x: hidden;
5+
height: 100vh;
6+
display: flex;
7+
flex-direction: column;
8+
}
9+
10+
footer {
11+
padding: 1rem;
12+
display: flex;
13+
justify-content: end;
14+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import type { Metadata } from "next";
2+
import "./globals.css";
3+
4+
import { getCloudflareContext } from "@opennextjs/cloudflare";
5+
6+
export const metadata: Metadata = {
7+
title: "SSG App",
8+
description: "An app in which all the routes are SSG'd",
9+
};
10+
11+
export default async function RootLayout({
12+
children,
13+
}: Readonly<{
14+
children: React.ReactNode;
15+
}>) {
16+
const cloudflareContext = await getCloudflareContext({
17+
async: true,
18+
});
19+
20+
return (
21+
<html lang="en">
22+
<body>
23+
{children}
24+
<footer data-testid="app-version">{cloudflareContext.env.APP_VERSION}</footer>
25+
</body>
26+
</html>
27+
);
28+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.page {
2+
display: grid;
3+
grid-template-rows: 20px 1fr 20px;
4+
align-items: center;
5+
justify-items: center;
6+
flex: 1;
7+
border: 3px solid gray;
8+
margin: 1rem;
9+
margin-block-end: 0;
10+
}
11+
12+
.main {
13+
display: flex;
14+
flex-direction: column;
15+
gap: 32px;
16+
grid-row-start: 2;
17+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { unstable_cache } from "next/cache";
2+
import styles from "./page.module.css";
3+
import RevalidationButtons from "./components/revalidationButtons";
4+
5+
const fetchedDateCb = unstable_cache(
6+
async () => {
7+
return Date.now();
8+
},
9+
["date"],
10+
{ tags: ["date"] }
11+
);
12+
13+
export default async function Home() {
14+
const fetchedDate = await fetchedDateCb();
15+
return (
16+
<div className={styles.page}>
17+
<main className={styles.main}>
18+
<h1>Hello from a Statically generated page</h1>
19+
<p data-testid="date-local">{Date.now()}</p>
20+
<p data-testid="date-fetched">{fetchedDate}</p>
21+
22+
<RevalidationButtons />
23+
</main>
24+
</div>
25+
);
26+
}

0 commit comments

Comments
 (0)