Skip to content

Commit 5839217

Browse files
authored
fix fetch cache for next 15 (#601)
* fix fetch cache for next 15 * review & changeset
1 parent bfa1a8c commit 5839217

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

.changeset/sour-points-bake.md

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

examples/app-router/app/ssr/page.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ async function getTime() {
1414
export default async function SSR() {
1515
const time = await getTime();
1616
const headerList = await headers();
17+
const responseOpenNext = await fetch("https://opennext.js.org", {
18+
cache: "force-cache",
19+
});
1720
return (
1821
<div>
1922
<h1>Time: {time}</h1>
2023
<div> {headerList.get("host")}</div>
24+
<p>Cached fetch: {responseOpenNext.headers.get("date")}</p>
2125
</div>
2226
);
2327
}

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,28 @@ declare global {
108108
var lastModified: Record<string, number>;
109109
var isNextAfter15: boolean;
110110
}
111+
112+
function isFetchCache(
113+
options?:
114+
| boolean
115+
| {
116+
fetchCache?: boolean;
117+
kindHint?: "app" | "pages" | "fetch";
118+
kind?: "FETCH";
119+
},
120+
): boolean {
121+
if (typeof options === "boolean") {
122+
return options;
123+
}
124+
if (typeof options === "object") {
125+
return (
126+
options.kindHint === "fetch" ||
127+
options.fetchCache ||
128+
options.kind === "FETCH"
129+
);
130+
}
131+
return false;
132+
}
111133
// 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
112134
export default class S3Cache {
113135
constructor(_ctx: CacheHandlerContext) {}
@@ -122,21 +144,16 @@ export default class S3Cache {
122144
kindHint?: "app" | "pages" | "fetch";
123145
tags?: string[];
124146
softTags?: string[];
147+
kind?: "FETCH";
125148
},
126149
) {
127150
if (globalThis.disableIncrementalCache) {
128151
return null;
129152
}
130-
const isFetchCache =
131-
typeof options === "object"
132-
? options.kindHint
133-
? options.kindHint === "fetch"
134-
: options.fetchCache
135-
: options;
136153

137154
const softTags = typeof options === "object" ? options.softTags : [];
138155
const tags = typeof options === "object" ? options.tags : [];
139-
return isFetchCache
156+
return isFetchCache(options)
140157
? this.getFetchCache(key, softTags, tags)
141158
: this.getIncrementalCache(key);
142159
}

packages/tests-e2e/tests/appRouter/ssr.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,12 @@ test("Server Side Render and loading.tsx", async ({ page }) => {
5454
// await expect(el).toBeVisible();
5555
// await expect(time).not.toEqual(newTime);
5656
});
57+
58+
test("Fetch cache properly cached", async ({ page }) => {
59+
await page.goto("/ssr");
60+
const originalDate = await page.getByText("Cached fetch:").textContent();
61+
await wait(2000);
62+
await page.reload();
63+
const newDate = await page.getByText("Cached fetch:").textContent();
64+
expect(originalDate).toEqual(newDate);
65+
});

0 commit comments

Comments
 (0)