Skip to content

Commit 470a17d

Browse files
committed
added some e2e test for next/after
1 parent 14c5be3 commit 470a17d

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { revalidateTag } from "next/cache";
2+
import { NextResponse, unstable_after as after } from "next/server";
3+
4+
export function POST() {
5+
after(
6+
() =>
7+
new Promise<void>((resolve) =>
8+
setTimeout(() => {
9+
revalidateTag("date");
10+
resolve();
11+
}, 5000),
12+
),
13+
);
14+
15+
return NextResponse.json({ success: true });
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { unstable_cache } from "next/cache";
2+
import { NextResponse } from "next/server";
3+
4+
export const dynamic = "force-static";
5+
6+
export async function GET() {
7+
const dateFn = unstable_cache(() => new Date().toISOString(), ["date"], {
8+
tags: ["date"],
9+
});
10+
const date = await dateFn();
11+
console.log("date", date);
12+
return NextResponse.json({ date: date });
13+
}

examples/app-router/next.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ const nextConfig: NextConfig = {
1717
},
1818
],
1919
},
20+
experimental: {
21+
after: true,
22+
},
2023
redirects: async () => {
2124
return [
2225
{
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { test, expect } from "@playwright/test";
2+
3+
test("Next after", async ({ request }) => {
4+
const initialSSG = await request.get("/api/after/ssg");
5+
expect(initialSSG.status()).toEqual(200);
6+
const initialSSGJson = await initialSSG.json();
7+
8+
// We then fire a post request that will revalidate the SSG page 5 seconds after, but should respond immediately
9+
const dateNow = Date.now();
10+
const revalidateSSG = await request.post("/api/after/revalidate");
11+
expect(revalidateSSG.status()).toEqual(200);
12+
const revalidateSSGJson = await revalidateSSG.json();
13+
expect(revalidateSSGJson.success).toEqual(true);
14+
// This request should take less than 5 seconds to respond
15+
expect(Date.now() - dateNow).toBeLessThan(5000);
16+
17+
// We want to immediately check if the SSG page has been revalidated, it should not have been
18+
const notRevalidatedSSG = await request.get("/api/after/ssg");
19+
expect(notRevalidatedSSG.status()).toEqual(200);
20+
const notRevalidatedSSGJson = await notRevalidatedSSG.json();
21+
expect(notRevalidatedSSGJson.date).toEqual(initialSSGJson.date);
22+
23+
// We then wait for 5 seconds to ensure the SSG page has been revalidated
24+
await new Promise((resolve) => setTimeout(resolve, 5000));
25+
const revalidatedSSG = await request.get("/api/after/ssg");
26+
expect(revalidatedSSG.status()).toEqual(200);
27+
const revalidatedSSGJson = await revalidatedSSG.json();
28+
expect(revalidatedSSGJson.date).not.toEqual(initialSSGJson.date);
29+
});

0 commit comments

Comments
 (0)