Skip to content

Commit 4ef4d2d

Browse files
committed
changeset and lint
1 parent fc2e172 commit 4ef4d2d

File tree

3 files changed

+34
-34
lines changed

3 files changed

+34
-34
lines changed

.changeset/neat-hornets-call.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+
Add a new `withFilter` tag cache to allow to filter the tag cache used

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

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NextModeTagCache } from "@opennextjs/aws/types/overrides";
2-
import {beforeEach, describe, expect, it, vi } from "vitest";
2+
import { beforeEach, describe, expect, it, vi } from "vitest";
33

4-
import { softTagFilter,withFilter } from "./tag-cache-filter";
4+
import { softTagFilter, withFilter } from "./tag-cache-filter";
55

66
const mockedTagCache = {
77
name: "mocked",
@@ -28,17 +28,17 @@ describe("withFilter", () => {
2828

2929
await tagCache.writeTags(tags);
3030
expect(mockedTagCache.writeTags).toHaveBeenCalledWith(["valid_tag"]);
31-
})
31+
});
3232

33-
it('should not call writeTags if no tags are valid', async () => {
33+
it("should not call writeTags if no tags are valid", async () => {
3434
const tagCache = withFilter({
3535
originalTagCache: mockedTagCache,
3636
filterFn,
3737
});
3838
const tags = ["invalid_tag"];
3939
await tagCache.writeTags(tags);
4040
expect(mockedTagCache.writeTags).not.toHaveBeenCalled();
41-
})
41+
});
4242

4343
it("should filter out tags based on hasBeenRevalidated", async () => {
4444
const tagCache = withFilter({
@@ -51,10 +51,9 @@ describe("withFilter", () => {
5151

5252
await tagCache.hasBeenRevalidated(tags, lastModified);
5353
expect(mockedTagCache.hasBeenRevalidated).toHaveBeenCalledWith(["valid_tag"], lastModified);
54-
}
55-
)
54+
});
5655

57-
it('should not call hasBeenRevalidated if no tags are valid', async () => {
56+
it("should not call hasBeenRevalidated if no tags are valid", async () => {
5857
const tagCache = withFilter({
5958
originalTagCache: mockedTagCache,
6059
filterFn,
@@ -63,7 +62,7 @@ describe("withFilter", () => {
6362
const lastModified = Date.now();
6463
await tagCache.hasBeenRevalidated(tags, lastModified);
6564
expect(mockedTagCache.hasBeenRevalidated).not.toHaveBeenCalled();
66-
})
65+
});
6766

6867
it("should filter out tags based on getPathsByTags", async () => {
6968
const tagCache = withFilter({
@@ -75,28 +74,26 @@ describe("withFilter", () => {
7574

7675
await tagCache.getPathsByTags?.(tags);
7776
expect(mockedTagCache.getPathsByTags).toHaveBeenCalledWith(["valid_tag"]);
78-
}
79-
)
77+
});
8078

81-
it('should not call getPathsByTags if no tags are valid', async () => {
79+
it("should not call getPathsByTags if no tags are valid", async () => {
8280
const tagCache = withFilter({
8381
originalTagCache: mockedTagCache,
8482
filterFn,
8583
});
8684
const tags = ["invalid_tag"];
8785
await tagCache.getPathsByTags?.(tags);
8886
expect(mockedTagCache.getPathsByTags).not.toHaveBeenCalled();
89-
})
87+
});
9088

91-
it('should return the correct name', () => {
89+
it("should return the correct name", () => {
9290
const tagCache = withFilter({
9391
originalTagCache: mockedTagCache,
9492
filterFn,
9593
});
9694

97-
expect(tagCache.name).toBe('filtered-mocked');
98-
}
99-
)
95+
expect(tagCache.name).toBe("filtered-mocked");
96+
});
10097

10198
it("should not create a function if getPathsByTags is not defined", async () => {
10299
const tagCache = withFilter({
@@ -108,8 +105,7 @@ describe("withFilter", () => {
108105
});
109106

110107
expect(tagCache.getPathsByTags).toBeUndefined();
111-
}
112-
)
108+
});
113109

114110
it("should properly filter soft tags", () => {
115111
const tagCache = withFilter({
@@ -119,5 +115,5 @@ describe("withFilter", () => {
119115

120116
tagCache.writeTags(["valid_tag", "_N_T_/", "_N_T_/test", "_N_T_/layout"]);
121117
expect(mockedTagCache.writeTags).toHaveBeenCalledWith(["valid_tag"]);
122-
})
123-
});
118+
});
119+
});

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,19 @@ interface WithFilterOptions {
1818
* Creates a new tag cache that filters tags based on the provided filter function.
1919
* This is usefult to remove tags that are not used by the app, this could reduce the number of request to the underlying tag cache.
2020
*/
21-
export function withFilter({
22-
originalTagCache,
23-
filterFn,
24-
}: WithFilterOptions): NextModeTagCache {
21+
export function withFilter({ originalTagCache, filterFn }: WithFilterOptions): NextModeTagCache {
2522
return {
2623
name: `filtered-${originalTagCache.name}`,
2724
mode: "nextMode",
28-
getPathsByTags: originalTagCache.getPathsByTags ? async (tags) => {
29-
const filteredTags = tags.filter(filterFn);
30-
if (filteredTags.length === 0) {
31-
return [];
32-
}
33-
return originalTagCache.getPathsByTags!(filteredTags)
34-
} : undefined,
25+
getPathsByTags: originalTagCache.getPathsByTags
26+
? async (tags) => {
27+
const filteredTags = tags.filter(filterFn);
28+
if (filteredTags.length === 0) {
29+
return [];
30+
}
31+
return originalTagCache.getPathsByTags!(filteredTags);
32+
}
33+
: undefined,
3534
hasBeenRevalidated: async (tags, lastModified) => {
3635
const filteredTags = tags.filter(filterFn);
3736
if (filteredTags.length === 0) {
@@ -45,7 +44,7 @@ export function withFilter({
4544
return;
4645
}
4746
return originalTagCache.writeTags(filteredTags);
48-
},
47+
},
4948
};
5049
}
5150

@@ -56,4 +55,4 @@ export function withFilter({
5655
*/
5756
export function softTagFilter(tag: string): boolean {
5857
return !tag.startsWith("_N_T_");
59-
}
58+
}

0 commit comments

Comments
 (0)