Skip to content

Commit 76bfff6

Browse files
committed
Improves initial cache registration by handling missing page data and prefetch data gracefully.
1 parent 2cdc4ab commit 76bfff6

File tree

5 files changed

+84
-41
lines changed

5 files changed

+84
-41
lines changed
Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,33 @@
11
interface Post {
2-
id: string
3-
title: string
4-
content: string
5-
}
6-
7-
// Next.js will invalidate the cache when a
8-
// request comes in, at most once every 60 seconds.
9-
export const revalidate = 60
10-
11-
export async function generateStaticParams() {
12-
const posts: Post[] = await fetch('https://api.vercel.app/blog').then((res) =>
13-
res.json()
14-
)
15-
return posts.map((post) => ({
16-
id: String(post.id),
17-
}))
18-
}
19-
20-
export default async function Page({
21-
params,
22-
}: {
23-
params: Promise<{ id: string }>
24-
}) {
25-
const { id } = await params
26-
const post: Post = await fetch(`https://api.vercel.app/blog/${id}`).then(
27-
(res) => res.json()
28-
)
29-
return (
30-
<main>
31-
<h1>{post.title}</h1>
32-
<p>{post.content}</p>
33-
</main>
34-
)
35-
}
2+
id: string;
3+
title: string;
4+
content: string;
5+
}
6+
7+
export const revalidate = 3600;
8+
9+
export async function generateStaticParams() {
10+
const posts: Post[] = await fetch("https://api.vercel.app/blog").then((res) =>
11+
res.json()
12+
);
13+
return posts.map((post) => ({
14+
id: String(post.id),
15+
}));
16+
}
17+
18+
export default async function Page({
19+
params,
20+
}: {
21+
params: Promise<{ id: string }>;
22+
}) {
23+
const { id } = await params;
24+
const post: Post = await fetch(`https://api.vercel.app/blog/${id}`).then(
25+
(res) => res.json()
26+
);
27+
return (
28+
<main>
29+
<h1>{post.title}</h1>
30+
<p>{post.content}</p>
31+
</main>
32+
);
33+
}

examples/redis-minimal/src/pages/posts/[id].tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const getStaticProps: GetStaticProps<PostPageProps> = async ({
3636
props: {
3737
post,
3838
},
39-
revalidate: 60,
39+
revalidate: 3600,
4040
};
4141
};
4242

packages/nextjs-cache-handler/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/nextjs-cache-handler/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"next",
1919
"redis"
2020
],
21-
"version": "2.1.0-canary.11",
21+
"version": "2.1.0-canary.12",
2222
"type": "module",
2323
"license": "MIT",
2424
"description": "Next.js cache handlers",

packages/nextjs-cache-handler/src/instrumentation/register-initial-cache.ts

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,15 @@ export async function registerInitialCache(
261261
let pageData: string | object | undefined;
262262
let meta: NextRouteMetadata | undefined;
263263
let rscData: string | undefined;
264+
265+
if (debug) {
266+
console.info(
267+
"[CacheHandler] [%s] %s",
268+
"registerInitialCache",
269+
"Reading file system cache",
270+
);
271+
}
272+
264273
try {
265274
[html, pageData, rscData, meta] = await Promise.all([
266275
fsPromises.readFile(`${pathToRouteFiles}.html`, "utf-8"),
@@ -269,11 +278,31 @@ export async function registerInitialCache(
269278
`${pathToRouteFiles}.${isAppRouter ? "rsc" : "json"}`,
270279
"utf-8",
271280
)
272-
.then((data) => (isAppRouter ? data : (JSON.parse(data) as object))),
281+
.then((data) => (isAppRouter ? data : (JSON.parse(data) as object)))
282+
.catch((error) => {
283+
console.warn(
284+
"[CacheHandler] [%s] %s %s",
285+
"registerInitialCache",
286+
"Failed to read page data, assuming it does not exist",
287+
`Error: ${error}`,
288+
);
289+
290+
return undefined;
291+
}),
273292
isAppRouter
274293
? fsPromises
275294
.readFile(`${pathToRouteFiles}.prefetch.rsc`, "utf-8")
276295
.then((data) => data)
296+
.catch((error) => {
297+
console.warn(
298+
"[CacheHandler] [%s] %s %s",
299+
"registerInitialCache",
300+
"Failed to read page prefetch data, assuming it does not exist",
301+
`Error: ${error}`,
302+
);
303+
304+
return undefined;
305+
})
277306
: undefined,
278307
isAppRouter
279308
? fsPromises
@@ -294,9 +323,17 @@ export async function registerInitialCache(
294323
return;
295324
}
296325

326+
if (debug) {
327+
console.info(
328+
"[CacheHandler] [%s] %s",
329+
"registerInitialCache",
330+
"Saving file system cache to cache handler",
331+
);
332+
}
333+
297334
try {
298335
const value: IncrementalCachedAppPageValue &
299-
Pick<IncrementalCachedPageValue, "pageData"> = {
336+
Partial<Pick<IncrementalCachedPageValue, "pageData">> = {
300337
kind: (isAppRouter ? "APP_PAGE" : "PAGES") as unknown as any,
301338
html,
302339
pageData,
@@ -305,13 +342,21 @@ export async function registerInitialCache(
305342
status: meta?.status,
306343
rscData:
307344
isAppRouter && rscData ? Buffer.from(rscData, "utf-8") : undefined,
308-
segmentData: undefined,
345+
segmentData: undefined, // TODO: Add segment data
309346
};
310347

311348
await cacheHandler.set(cachePath, value, {
312349
revalidate,
313350
internal_lastModified: lastModified,
314351
});
352+
353+
if (debug) {
354+
console.info(
355+
"[CacheHandler] [%s] %s",
356+
"registerInitialCache",
357+
"Saved file system cache to cache handler",
358+
);
359+
}
315360
} catch (error) {
316361
if (debug) {
317362
console.warn(

0 commit comments

Comments
 (0)