Skip to content

Commit 2cdc4ab

Browse files
committed
Improves caching and pre-rendering
1 parent 37aa435 commit 2cdc4ab

File tree

5 files changed

+40
-31
lines changed

5 files changed

+40
-31
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { MetadataRoute } from "next";
2+
3+
export default function manifest(): MetadataRoute.Manifest {
4+
return {
5+
name: "Next.js App",
6+
short_name: "Next.js App",
7+
description: "Next.js App",
8+
start_url: "/",
9+
display: "standalone",
10+
background_color: "#fff",
11+
theme_color: "#fff",
12+
icons: [
13+
{
14+
src: "/favicon.ico",
15+
sizes: "any",
16+
type: "image/x-icon",
17+
},
18+
],
19+
};
20+
}

examples/redis-minimal/src/app/ppr-example/page.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export default function Page({
99
return (
1010
<section>
1111
<h1>This will be prerendered</h1>
12+
<hr />
13+
<h1>This will be dynamic</h1>
1214
<Suspense fallback={<Skeleton />}>
1315
<Example searchParams={searchParams} />
1416
</Suspense>
@@ -17,3 +19,5 @@ export default function Page({
1719
}
1820

1921
export const experimental_ppr = true;
22+
23+
export const revalidate = 3600;

packages/nextjs-cache-handler/src/handlers/cache-handler.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -660,13 +660,6 @@ export class CacheHandler implements NextCacheHandler {
660660
implicitTags: softTags ?? [],
661661
});
662662

663-
if (cachedData?.value?.kind === "APP_ROUTE") {
664-
cachedData.value.body = Buffer.from(
665-
cachedData.value.body.toString(),
666-
"base64",
667-
);
668-
}
669-
670663
if (!cachedData && CacheHandler.#fallbackFalseRoutes.has(cacheKey)) {
671664
cachedData = await CacheHandler.#readPagesRouterPage(cacheKey);
672665

@@ -734,18 +727,6 @@ export class CacheHandler implements NextCacheHandler {
734727
cacheHandlerValueTags = getTagsFromHeaders(value.headers ?? {});
735728
break;
736729
}
737-
case "APP_ROUTE": {
738-
// create a new object to avoid mutating the original value
739-
value = {
740-
// replace the body with a base64 encoded string to save space
741-
body: value.body.toString("base64") as unknown as Buffer,
742-
headers: value.headers,
743-
kind: value.kind,
744-
status: value.status,
745-
};
746-
747-
break;
748-
}
749730
default: {
750731
break;
751732
}

packages/nextjs-cache-handler/src/helpers/buffer.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ export function parseBuffersToStrings(cacheHandlerValue: CacheHandlerValue) {
1414
return;
1515
}
1616

17-
const value: IncrementalCacheValue | null = {
18-
...cacheHandlerValue.value,
19-
};
17+
const value: IncrementalCacheValue | null = cacheHandlerValue.value;
2018

2119
const kind = value?.kind;
2220

@@ -27,7 +25,7 @@ export function parseBuffersToStrings(cacheHandlerValue: CacheHandlerValue) {
2725
if (appRouteValue?.body) {
2826
// Convert body Buffer to string
2927
// See: https://github.com/vercel/next.js/blob/f5444a16ec2ef7b82d30048890b613aa3865c1f1/packages/next/src/server/response-cache/types.ts#L97
30-
appRouteData.body = appRouteValue.body.toString();
28+
appRouteData.body = appRouteValue.body.toString("base64");
3129
}
3230
} else if (kind === "APP_PAGE") {
3331
const appPageData = value as unknown as RedisCompliantCachedAppPageValue;
@@ -36,7 +34,7 @@ export function parseBuffersToStrings(cacheHandlerValue: CacheHandlerValue) {
3634
if (appPageValue?.rscData) {
3735
// Convert rscData Buffer to string
3836
// See: https://github.com/vercel/next.js/blob/f5444a16ec2ef7b82d30048890b613aa3865c1f1/packages/next/src/server/response-cache/types.ts#L76
39-
appPageData.rscData = appPageValue.rscData.toString();
37+
appPageData.rscData = appPageValue.rscData.toString("base64");
4038
}
4139

4240
if (appPageValue?.segmentData) {
@@ -45,7 +43,7 @@ export function parseBuffersToStrings(cacheHandlerValue: CacheHandlerValue) {
4543
appPageData.segmentData = Object.fromEntries(
4644
Array.from(appPageValue.segmentData.entries()).map(([key, value]) => [
4745
key,
48-
value.toString(),
46+
value.toString("base64"),
4947
]),
5048
);
5149
}
@@ -63,7 +61,7 @@ export function convertStringsToBuffers(cacheValue: CacheHandlerValue) {
6361
// Convert body string to Buffer
6462
// See: https://github.com/vercel/next.js/blob/f5444a16ec2ef7b82d30048890b613aa3865c1f1/packages/next/src/server/response-cache/types.ts#L97
6563
const appRouteValue = value as unknown as CachedRouteValue;
66-
appRouteValue.body = Buffer.from(appRouteData.body, "utf-8");
64+
appRouteValue.body = Buffer.from(appRouteData.body, "base64");
6765
}
6866
} else if (kind === "APP_PAGE") {
6967
const appPageData = value as unknown as RedisCompliantCachedAppPageValue;
@@ -72,7 +70,7 @@ export function convertStringsToBuffers(cacheValue: CacheHandlerValue) {
7270
if (appPageData.rscData) {
7371
// Convert rscData string to Buffer
7472
// See: https://github.com/vercel/next.js/blob/f5444a16ec2ef7b82d30048890b613aa3865c1f1/packages/next/src/server/response-cache/types.ts#L76
75-
appPageValue.rscData = Buffer.from(appPageData.rscData, "utf-8");
73+
appPageValue.rscData = Buffer.from(appPageData.rscData, "base64");
7674
}
7775

7876
if (appPageData.segmentData) {
@@ -81,7 +79,7 @@ export function convertStringsToBuffers(cacheValue: CacheHandlerValue) {
8179
appPageValue.segmentData = new Map(
8280
Object.entries(appPageData.segmentData).map(([key, value]) => [
8381
key,
84-
Buffer.from(value, "utf-8"),
82+
Buffer.from(value, "base64"),
8583
]),
8684
);
8785
}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,16 +260,21 @@ export async function registerInitialCache(
260260
let html: string | undefined;
261261
let pageData: string | object | undefined;
262262
let meta: NextRouteMetadata | undefined;
263-
263+
let rscData: string | undefined;
264264
try {
265-
[html, pageData, meta] = await Promise.all([
265+
[html, pageData, rscData, meta] = await Promise.all([
266266
fsPromises.readFile(`${pathToRouteFiles}.html`, "utf-8"),
267267
fsPromises
268268
.readFile(
269269
`${pathToRouteFiles}.${isAppRouter ? "rsc" : "json"}`,
270270
"utf-8",
271271
)
272272
.then((data) => (isAppRouter ? data : (JSON.parse(data) as object))),
273+
isAppRouter
274+
? fsPromises
275+
.readFile(`${pathToRouteFiles}.prefetch.rsc`, "utf-8")
276+
.then((data) => data)
277+
: undefined,
273278
isAppRouter
274279
? fsPromises
275280
.readFile(`${pathToRouteFiles}.meta`, "utf-8")
@@ -298,7 +303,8 @@ export async function registerInitialCache(
298303
postponed: meta?.postponed,
299304
headers: meta?.headers,
300305
status: meta?.status,
301-
rscData: undefined,
306+
rscData:
307+
isAppRouter && rscData ? Buffer.from(rscData, "utf-8") : undefined,
302308
segmentData: undefined,
303309
};
304310

0 commit comments

Comments
 (0)