Skip to content

Commit 3d8e546

Browse files
committed
feat: invoke a remove method on the queue after cache set
1 parent 0b8705a commit 3d8e546

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

.changeset/stupid-gifts-cough.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/aws": minor
3+
---
4+
5+
Invoke a remove method on the queue after an incremental cache set for a route takes place.

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,19 @@ export default class Cache {
393393
} finally {
394394
// We need to resolve the promise even if there was an error
395395
detachedPromise?.resolve();
396+
397+
switch (data?.kind) {
398+
case "ROUTE":
399+
case "APP_ROUTE":
400+
case "PAGE":
401+
case "PAGES":
402+
case "APP_PAGE":
403+
case "REDIRECT":
404+
globalThis.queue.remove?.(key);
405+
break;
406+
default:
407+
// Fetch cache entries and images do not use the queue
408+
}
396409
}
397410
}
398411

packages/open-next/src/types/overrides.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ export interface QueueMessage {
2424
}
2525

2626
export interface Queue {
27-
send(message: QueueMessage): Promise<void>;
2827
name: string;
28+
send(message: QueueMessage): Promise<void>;
29+
remove?(path: string): void;
2930
}
3031

3132
// Incremental cache

packages/tests-unit/tests/adapters/cache.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ describe("CacheHandler", () => {
4141
};
4242
globalThis.tagCache = tagCache;
4343

44+
const queue = {
45+
name: "mock",
46+
send: vi.fn(),
47+
remove: vi.fn(),
48+
};
49+
globalThis.queue = queue;
50+
4451
const invalidateCdnHandler = {
4552
name: "mock",
4653
invalidatePaths: vi.fn(),
@@ -335,6 +342,7 @@ describe("CacheHandler", () => {
335342
{ type: "route", body: "{}", meta: { status: 200, headers: {} } },
336343
false,
337344
);
345+
expect(globalThis.queue.remove).toHaveBeenCalledWith("key");
338346
});
339347

340348
it("Should set cache when for APP_ROUTE", async () => {
@@ -356,6 +364,7 @@ describe("CacheHandler", () => {
356364
},
357365
false,
358366
);
367+
expect(globalThis.queue.remove).toHaveBeenCalledWith("key");
359368
});
360369

361370
it("Should set cache when for PAGE", async () => {
@@ -376,6 +385,7 @@ describe("CacheHandler", () => {
376385
},
377386
false,
378387
);
388+
expect(globalThis.queue.remove).toHaveBeenCalledWith("key");
379389
});
380390

381391
it("Should set cache when for PAGES", async () => {
@@ -397,6 +407,7 @@ describe("CacheHandler", () => {
397407
},
398408
false,
399409
);
410+
expect(globalThis.queue.remove).toHaveBeenCalledWith("key");
400411
});
401412

402413
it("Should set cache when for APP_PAGE", async () => {
@@ -418,6 +429,7 @@ describe("CacheHandler", () => {
418429
},
419430
false,
420431
);
432+
expect(globalThis.queue.remove).toHaveBeenCalledWith("key");
421433
});
422434

423435
it("Should set cache when for FETCH", async () => {
@@ -448,6 +460,7 @@ describe("CacheHandler", () => {
448460
},
449461
true,
450462
);
463+
expect(globalThis.queue.remove).not.toHaveBeenCalled();
451464
});
452465

453466
it("Should set cache when for REDIRECT", async () => {
@@ -461,6 +474,7 @@ describe("CacheHandler", () => {
461474
},
462475
false,
463476
);
477+
expect(globalThis.queue.remove).toHaveBeenCalledWith("key");
464478
});
465479

466480
it("Should not set cache when for IMAGE (not implemented)", async () => {
@@ -472,6 +486,7 @@ describe("CacheHandler", () => {
472486
});
473487

474488
expect(incrementalCache.set).not.toHaveBeenCalled();
489+
expect(globalThis.queue.remove).not.toHaveBeenCalled();
475490
});
476491

477492
it("Should not throw when set cache throws", async () => {

0 commit comments

Comments
 (0)