Skip to content

Commit e07a2ed

Browse files
conico974vicb
andauthored
Feat perf improvement (#641)
* perf improvement * remove cache interception for PPR app * lint fix * changeset * fix e2e * set `enableCacheInterception` to false by default * Update packages/cloudflare/src/api/config.ts Co-authored-by: Victor Berchet <victor@suumit.com> --------- Co-authored-by: Victor Berchet <victor@suumit.com>
1 parent 145146f commit e07a2ed

File tree

7 files changed

+33
-4
lines changed

7 files changed

+33
-4
lines changed

.changeset/lovely-rooms-taste.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
some performance improvements
6+
7+
- `enableCacheInterception` is now enabled by default, it loads ISR/SSG pages from cache without waiting for the js page bundle to load
8+
- `routePreloadingBehavior` is now set to `withWaitUntil`, which means a single route js will be lazy loaded on cold start, but other routes will be preloaded using `waitUntil` for better performance

examples/e2e/app-pages-router/open-next.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ import memoryQueue from "@opennextjs/cloudflare/overrides/queue/memory-queue";
55
export default defineCloudflareConfig({
66
incrementalCache: r2IncrementalCache,
77
queue: memoryQueue,
8+
enableCacheInterception: true,
89
});

examples/e2e/app-router/e2e/isr.test.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ test.describe("dynamicParams set to true", () => {
103103
test("should be HIT on a path that was prebuilt", async ({ page }) => {
104104
const res = await page.goto("/isr/dynamic-params-true/1");
105105
expect(res?.status()).toEqual(200);
106-
expect(res?.headers()["x-nextjs-cache"]).toEqual("HIT");
106+
// TODO: sync this to aws
107+
const cacheHeader = res?.headers()["x-nextjs-cache"] ?? res?.headers()["x-opennext-cache"];
108+
expect(cacheHeader).toEqual("HIT");
107109
const title = await page.getByTestId("title").textContent();
108110
const content = await page.getByTestId("content").textContent();
109111
expect(title).toEqual("Post 1");
@@ -115,7 +117,9 @@ test.describe("dynamicParams set to true", () => {
115117
// We are gonna skip this one for now, turborepo caching can cause this page to be STALE once deployed
116118
test.skip("should SSR on a path that was not prebuilt", async ({ page }) => {
117119
const res = await page.goto("/isr/dynamic-params-true/11");
118-
expect(res?.headers()["x-nextjs-cache"]).toEqual("MISS");
120+
// TODO: sync this to aws
121+
const cacheHeader = res?.headers()["x-nextjs-cache"] ?? res?.headers()["x-opennext-cache"];
122+
expect(cacheHeader).toEqual("MISS");
119123
const title = await page.getByTestId("title").textContent();
120124
const content = await page.getByTestId("content").textContent();
121125
expect(title).toEqual("Post 11");
@@ -140,7 +144,9 @@ test.describe("dynamicParams set to false", () => {
140144
test("should be HIT on a path that was prebuilt", async ({ page }) => {
141145
const res = await page.goto("/isr/dynamic-params-false/1");
142146
expect(res?.status()).toEqual(200);
143-
expect(res?.headers()["x-nextjs-cache"]).toEqual("HIT");
147+
// TODO: sync this to aws
148+
const cacheHeader = res?.headers()["x-nextjs-cache"] ?? res?.headers()["x-opennext-cache"];
149+
expect(cacheHeader).toEqual("HIT");
144150
const title = await page.getByTestId("title").textContent();
145151
const content = await page.getByTestId("content").textContent();
146152
expect(title).toEqual("Post 1");

examples/e2e/app-router/open-next.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ export default defineCloudflareConfig({
1414
},
1515
}),
1616
queue: doQueue,
17+
enableCacheInterception: true,
1718
});

examples/playground14/open-next.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ import kvIncrementalCache from "@opennextjs/cloudflare/overrides/incremental-cac
33

44
export default defineCloudflareConfig({
55
incrementalCache: kvIncrementalCache,
6+
enableCacheInterception: true,
67
});

examples/playground15/open-next.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ import kvIncrementalCache from "@opennextjs/cloudflare/overrides/incremental-cac
33

44
export default defineCloudflareConfig({
55
incrementalCache: kvIncrementalCache,
6+
enableCacheInterception: true,
67
});

packages/cloudflare/src/api/config.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ export type CloudflareOverrides = {
2828
* Sets the revalidation queue implementation
2929
*/
3030
queue?: "direct" | Override<Queue>;
31+
32+
/**
33+
* Enable cache interception
34+
* Should be `false` when PPR is used
35+
* @default false
36+
*/
37+
enableCacheInterception?: boolean;
3138
};
3239

3340
/**
@@ -37,7 +44,7 @@ export type CloudflareOverrides = {
3744
* @returns the OpenNext configuration object
3845
*/
3946
export function defineCloudflareConfig(config: CloudflareOverrides = {}): OpenNextConfig {
40-
const { incrementalCache, tagCache, queue } = config;
47+
const { incrementalCache, tagCache, queue, enableCacheInterception = false } = config;
4148

4249
return {
4350
default: {
@@ -49,12 +56,16 @@ export function defineCloudflareConfig(config: CloudflareOverrides = {}): OpenNe
4956
tagCache: resolveTagCache(tagCache),
5057
queue: resolveQueue(queue),
5158
},
59+
routePreloadingBehavior: "withWaitUntil",
5260
},
5361
// node:crypto is used to compute cache keys
5462
edgeExternals: ["node:crypto"],
5563
cloudflare: {
5664
useWorkerdCondition: true,
5765
},
66+
dangerous: {
67+
enableCacheInterception,
68+
},
5869
};
5970
}
6071

0 commit comments

Comments
 (0)