Skip to content

Commit 236ce14

Browse files
authored
fix(tag-cache): forward isStale() in withFilter (#1189)
The `withFilter` utility did not forward the optional isStale() method from the underlying tag cache. This broke SWR (stale-while-revalidate) support added in v1.19.0.
1 parent c4b1687 commit 236ce14

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

.changeset/major-actors-make.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
fix(tag-cache): forward `isStale()` in `withFilter`

packages/cloudflare/src/api/overrides/tag-cache/tag-cache-filter.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const mockedTagCache = {
1010
getPathsByTags: vi.fn(),
1111
hasBeenRevalidated: vi.fn(),
1212
writeTags: vi.fn(),
13+
isStale: vi.fn(),
1314
} satisfies NextModeTagCache;
1415

1516
const filterFn = (tag: string) => tag.startsWith("valid_");
@@ -108,6 +109,42 @@ describe("withFilter", () => {
108109
expect(tagCache.getPathsByTags).toBeUndefined();
109110
});
110111

112+
it("should filter out tags based on isStale", async () => {
113+
const tagCache = withFilter({
114+
tagCache: mockedTagCache,
115+
filterFn,
116+
});
117+
118+
const tags = ["valid_tag", "invalid_tag"];
119+
const lastModified = Date.now();
120+
121+
await tagCache.isStale?.(tags, lastModified);
122+
expect(mockedTagCache.isStale).toHaveBeenCalledWith(["valid_tag"], lastModified);
123+
});
124+
125+
it("should not call isStale if no tags are valid", async () => {
126+
const tagCache = withFilter({
127+
tagCache: mockedTagCache,
128+
filterFn,
129+
});
130+
const tags = ["invalid_tag"];
131+
const lastModified = Date.now();
132+
await tagCache.isStale?.(tags, lastModified);
133+
expect(mockedTagCache.isStale).not.toHaveBeenCalled();
134+
});
135+
136+
it("should not create a function if isStale is not defined", async () => {
137+
const tagCache = withFilter({
138+
tagCache: {
139+
...mockedTagCache,
140+
isStale: undefined,
141+
},
142+
filterFn,
143+
});
144+
145+
expect(tagCache.isStale).toBeUndefined();
146+
});
147+
111148
it("should filter soft tags", () => {
112149
const tagCache = withFilter({
113150
tagCache: mockedTagCache,

packages/cloudflare/src/api/overrides/tag-cache/tag-cache-filter.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ export function withFilter({ tagCache, filterFn }: WithFilterOptions): NextModeT
5252
}
5353
return tagCache.writeTags(filteredTags);
5454
},
55+
isStale: tagCache.isStale
56+
? async (tags, lastModified) => {
57+
const filteredTags = tags.filter(filterFn);
58+
if (filteredTags.length === 0) {
59+
return false;
60+
}
61+
return tagCache.isStale!(filteredTags, lastModified);
62+
}
63+
: undefined,
5564
};
5665
}
5766

0 commit comments

Comments
 (0)