Skip to content

Commit 55a6bcc

Browse files
authored
fix cache for next 15 (#512)
* fix for next 15 * review fix * Create tasty-cherries-sort.md
1 parent 4ec9265 commit 55a6bcc

File tree

3 files changed

+63
-9
lines changed

3 files changed

+63
-9
lines changed

.changeset/tasty-cherries-sort.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"open-next": patch
3+
---
4+
5+
fix incremental cache for next 15

packages/open-next/src/adapters/cache.ts

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ interface CachedRedirectValue {
2121
}
2222

2323
interface CachedRouteValue {
24-
kind: "ROUTE";
24+
kind: "ROUTE" | "APP_ROUTE";
2525
// this needs to be a RenderResult so since renderResponse
2626
// expects that type instead of a string
2727
body: Buffer;
@@ -39,7 +39,7 @@ interface CachedImageValue {
3939
}
4040

4141
interface IncrementalCachedPageValue {
42-
kind: "PAGE";
42+
kind: "PAGE" | "PAGES";
4343
// this needs to be a string since the cache expects to store
4444
// the string value
4545
html: string;
@@ -48,9 +48,21 @@ interface IncrementalCachedPageValue {
4848
headers?: Record<string, undefined | string>;
4949
}
5050

51+
interface IncrementalCachedAppPageValue {
52+
kind: "APP_PAGE";
53+
// this needs to be a string since the cache expects to store
54+
// the string value
55+
html: string;
56+
rscData: Buffer;
57+
headers?: Record<string, undefined | string | string[]>;
58+
postponed?: string;
59+
status?: number;
60+
}
61+
5162
type IncrementalCacheValue =
5263
| CachedRedirectValue
5364
| IncrementalCachedPageValue
65+
| IncrementalCachedAppPageValue
5466
| CachedImageValue
5567
| CachedFetchValue
5668
| CachedRouteValue;
@@ -94,6 +106,7 @@ declare global {
94106
var disableDynamoDBCache: boolean;
95107
var disableIncrementalCache: boolean;
96108
var lastModified: Record<string, number>;
109+
var isNextAfter15: boolean;
97110
}
98111
// We need to use globalThis client here as this class can be defined at load time in next 12 but client is not available at load time
99112
export default class S3Cache {
@@ -203,7 +216,7 @@ export default class S3Cache {
203216
return {
204217
lastModified: _lastModified,
205218
value: {
206-
kind: "ROUTE",
219+
kind: globalThis.isNextAfter15 ? "APP_ROUTE" : "ROUTE",
207220
body: Buffer.from(
208221
cacheData.body ?? Buffer.alloc(0),
209222
isBinaryContentType(String(meta?.headers?.["content-type"]))
@@ -215,10 +228,22 @@ export default class S3Cache {
215228
},
216229
} as CacheHandlerValue;
217230
} else if (cacheData?.type === "page" || cacheData?.type === "app") {
231+
if (globalThis.isNextAfter15 && cacheData?.type === "app") {
232+
return {
233+
lastModified: _lastModified,
234+
value: {
235+
kind: "APP_PAGE",
236+
html: cacheData.html,
237+
rscData: Buffer.from(cacheData.rsc),
238+
status: meta?.status,
239+
headers: meta?.headers,
240+
},
241+
} as CacheHandlerValue;
242+
}
218243
return {
219244
lastModified: _lastModified,
220245
value: {
221-
kind: "PAGE",
246+
kind: globalThis.isNextAfter15 ? "PAGES" : "PAGE",
222247
html: cacheData.html,
223248
pageData:
224249
cacheData.type === "page" ? cacheData.json : cacheData.rsc,
@@ -259,7 +284,7 @@ export default class S3Cache {
259284
.getStore()
260285
?.pendingPromiseRunner.withResolvers<void>();
261286
try {
262-
if (data?.kind === "ROUTE") {
287+
if (data?.kind === "ROUTE" || data?.kind === "APP_ROUTE") {
263288
const { body, status, headers } = data;
264289
await globalThis.incrementalCache.set(
265290
key,
@@ -277,8 +302,8 @@ export default class S3Cache {
277302
},
278303
false,
279304
);
280-
} else if (data?.kind === "PAGE") {
281-
const { html, pageData } = data;
305+
} else if (data?.kind === "PAGE" || data?.kind === "PAGES") {
306+
const { html, pageData, status, headers } = data;
282307
const isAppPath = typeof pageData === "string";
283308
if (isAppPath) {
284309
await globalThis.incrementalCache.set(
@@ -287,6 +312,10 @@ export default class S3Cache {
287312
type: "app",
288313
html,
289314
rsc: pageData,
315+
meta: {
316+
status,
317+
headers,
318+
},
290319
},
291320
false,
292321
);
@@ -301,6 +330,21 @@ export default class S3Cache {
301330
false,
302331
);
303332
}
333+
} else if (data?.kind === "APP_PAGE") {
334+
const { html, rscData, headers, status } = data;
335+
await globalThis.incrementalCache.set(
336+
key,
337+
{
338+
type: "app",
339+
html,
340+
rsc: rscData.toString("utf8"),
341+
meta: {
342+
status,
343+
headers,
344+
},
345+
},
346+
false,
347+
);
304348
} else if (data?.kind === "FETCH") {
305349
await globalThis.incrementalCache.set<true>(key, data, true);
306350
} else if (data?.kind === "REDIRECT") {

packages/open-next/src/build.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,11 +374,12 @@ async function createImageOptimizationBundle(config: OpenNextConfig) {
374374
// Build Lambda code (2nd pass)
375375
// note: bundle in user's Next.js app again b/c the adapter relies on the
376376
// "next" package. And the "next" package from user's app should
377-
// be used.
377+
// be used. We also set @opentelemetry/api as external because it seems to be
378+
// required by Next 15 even though it's not used.
378379
esbuildSync(
379380
{
380381
entryPoints: [path.join(outputPath, "index.mjs")],
381-
external: ["sharp"],
382+
external: ["sharp", "@opentelemetry/api"],
382383
allowOverwrite: true,
383384
outfile: path.join(outputPath, "index.mjs"),
384385
banner: {
@@ -685,6 +686,9 @@ async function createCacheAssets(monorepoRoot: string) {
685686
export function compileCache(format: "cjs" | "esm" = "cjs") {
686687
const ext = format === "cjs" ? "cjs" : "mjs";
687688
const outfile = path.join(options.outputDir, ".build", `cache.${ext}`);
689+
690+
const isAfter15 = compareSemver(options.nextVersion, "15.0.0") >= 0;
691+
688692
esbuildSync(
689693
{
690694
external: ["next", "styled-jsx", "react", "@aws-sdk/*"],
@@ -700,6 +704,7 @@ export function compileCache(format: "cjs" | "esm" = "cjs") {
700704
`globalThis.disableDynamoDBCache = ${
701705
config.dangerous?.disableTagCache ?? false
702706
};`,
707+
`globalThis.isNextAfter15 = ${isAfter15};`,
703708
].join(""),
704709
},
705710
},

0 commit comments

Comments
 (0)