Skip to content

Commit 762d487

Browse files
authored
Add an isr route to the playground (#367)
1 parent 2a3e896 commit 762d487

File tree

6 files changed

+103
-2
lines changed

6 files changed

+103
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// This test relies on using `.dev.vars` to set the environment to `development`
2+
// However `next build` is not passed an environment, so we do not want to cache
3+
// the output.
4+
export const dynamic = "force-dynamic";
5+
16
export async function GET() {
27
return new Response(JSON.stringify(process.env));
38
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Imported from https://nextjs.org/docs/app/building-your-application/data-fetching/incremental-static-regeneration
2+
interface Post {
3+
id: string;
4+
title: string;
5+
content: string;
6+
}
7+
8+
// Next.js will invalidate the cache when a
9+
// request comes in, at most once every 1 hour.
10+
export const revalidate = 3600;
11+
12+
// We'll prerender only the params from `generateStaticParams` at build time.
13+
// If a request comes in for a path that hasn't been generated,
14+
// Next.js will server-render the page on-demand.
15+
export const dynamicParams = true;
16+
17+
export async function generateStaticParams() {
18+
return [{ id: "1" }, { id: "2" }, { id: "3" }];
19+
}
20+
21+
export default async function Page({ params }: { params: Promise<{ id: string }> }) {
22+
const id = (await params).id;
23+
const post: Post = await fetch(`https://api.vercel.app/blog/${id}`).then((res) => res.json());
24+
return (
25+
<main>
26+
<h1>{post.title}</h1>
27+
<p>{post.content}</p>
28+
</main>
29+
);
30+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Imported from https://nextjs.org/docs/app/building-your-application/data-fetching/incremental-static-regeneration
2+
interface Post {
3+
id: string;
4+
title: string;
5+
content: string;
6+
}
7+
8+
// Next.js will invalidate the cache when a
9+
// request comes in, at most once every 1 hour.
10+
export const revalidate = 3600;
11+
12+
// We'll prerender only the params from `generateStaticParams` at build time.
13+
// If a request comes in for a path that hasn't been generated, it will 404.
14+
export const dynamicParams = false;
15+
16+
export async function generateStaticParams() {
17+
return [{ id: "1" }, { id: "2" }, { id: "3" }];
18+
}
19+
20+
export default async function Page({ params }: { params: Promise<{ id: string }> }) {
21+
const id = (await params).id;
22+
const post: Post = await fetch(`https://api.vercel.app/blog/${id}`).then((res) => res.json());
23+
return (
24+
<main>
25+
<h1>{post.title}</h1>
26+
<p>{post.content}</p>
27+
</main>
28+
);
29+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { test, expect, type APIResponse } from "@playwright/test";
2+
import type { BinaryLike } from "node:crypto";
3+
import { createHash } from "node:crypto";
4+
5+
test("Generated pages exist", async ({ page }) => {
6+
const generatedIds = [1, 2, 3];
7+
let res: APIResponse;
8+
for (const id of generatedIds) {
9+
res = await page.request.get(`/isr/${id}/dynamic`);
10+
expect(res.status()).toBe(200);
11+
res = await page.request.get(`/isr/${id}/no-dynamic`);
12+
expect(res.status()).toBe(200);
13+
}
14+
});
15+
16+
test("Non generated pages 404 when dynamic is false", async ({ page }) => {
17+
const generatedIds = [4, 5, 6];
18+
for (const id of generatedIds) {
19+
const res = await page.request.get(`/isr/${id}/no-dynamic`);
20+
expect(res.status()).toBe(404);
21+
}
22+
});
23+
24+
test("Non generated pages are generated when dynamic is true", async ({ page }) => {
25+
const generatedIds = [4, 5, 6];
26+
for (const id of generatedIds) {
27+
const res = await page.request.get(`/isr/${id}/dynamic`);
28+
expect(res.status()).toBe(200);
29+
}
30+
});

examples/playground/open-next.config.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import type { OpenNextConfig } from "@opennextjs/aws/types/open-next.js";
2+
import cache from "@opennextjs/cloudflare/kv-cache";
23

34
const config: OpenNextConfig = {
45
default: {
56
override: {
67
wrapper: "cloudflare-node",
78
converter: "edge",
9+
incrementalCache: async () => cache,
10+
queue: "direct",
811
// Unused implementation
9-
incrementalCache: "dummy",
1012
tagCache: "dummy",
11-
queue: "dummy",
1213
},
1314
},
1415

examples/playground/wrangler.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
"directory": ".open-next/assets",
99
"binding": "ASSETS"
1010
},
11+
"kv_namespaces": [
12+
{
13+
"binding": "NEXT_CACHE_WORKERS_KV",
14+
"id": "<BINDING_ID>"
15+
}
16+
],
1117
"vars": {
1218
"hello": "Hello World from the cloudflare context!"
1319
}

0 commit comments

Comments
 (0)