-
Notifications
You must be signed in to change notification settings - Fork 117
Feat tag-cache-filter #608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@opennextjs/cloudflare": patch | ||
| --- | ||
|
|
||
| Add a new `withFilter` tag cache to allow to filter the tag cache used | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,119 @@ | ||||||
| import { NextModeTagCache } from "@opennextjs/aws/types/overrides"; | ||||||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||||||
|
|
||||||
| import { softTagFilter, withFilter } from "./tag-cache-filter"; | ||||||
|
|
||||||
| const mockedTagCache = { | ||||||
| name: "mocked", | ||||||
| mode: "nextMode", | ||||||
| getPathsByTags: vi.fn(), | ||||||
| hasBeenRevalidated: vi.fn(), | ||||||
| writeTags: vi.fn(), | ||||||
| } satisfies NextModeTagCache; | ||||||
|
|
||||||
| const filterFn = (tag: string) => tag.startsWith("valid_"); | ||||||
|
|
||||||
| describe("withFilter", () => { | ||||||
| beforeEach(() => { | ||||||
| vi.clearAllMocks(); | ||||||
| }); | ||||||
|
|
||||||
| it("should filter out tags based on writeTags", async () => { | ||||||
| const tagCache = withFilter({ | ||||||
| originalTagCache: mockedTagCache, | ||||||
| filterFn, | ||||||
| }); | ||||||
|
|
||||||
| const tags = ["valid_tag", "invalid_tag"]; | ||||||
|
|
||||||
| await tagCache.writeTags(tags); | ||||||
| expect(mockedTagCache.writeTags).toHaveBeenCalledWith(["valid_tag"]); | ||||||
| }); | ||||||
|
|
||||||
| it("should not call writeTags if no tags are valid", async () => { | ||||||
| const tagCache = withFilter({ | ||||||
| originalTagCache: mockedTagCache, | ||||||
| filterFn, | ||||||
| }); | ||||||
| const tags = ["invalid_tag"]; | ||||||
| await tagCache.writeTags(tags); | ||||||
| expect(mockedTagCache.writeTags).not.toHaveBeenCalled(); | ||||||
| }); | ||||||
|
|
||||||
| it("should filter out tags based on hasBeenRevalidated", async () => { | ||||||
| const tagCache = withFilter({ | ||||||
| originalTagCache: mockedTagCache, | ||||||
| filterFn, | ||||||
| }); | ||||||
|
|
||||||
| const tags = ["valid_tag", "invalid_tag"]; | ||||||
| const lastModified = Date.now(); | ||||||
|
|
||||||
| await tagCache.hasBeenRevalidated(tags, lastModified); | ||||||
| expect(mockedTagCache.hasBeenRevalidated).toHaveBeenCalledWith(["valid_tag"], lastModified); | ||||||
| }); | ||||||
|
|
||||||
| it("should not call hasBeenRevalidated if no tags are valid", async () => { | ||||||
| const tagCache = withFilter({ | ||||||
| originalTagCache: mockedTagCache, | ||||||
| filterFn, | ||||||
| }); | ||||||
| const tags = ["invalid_tag"]; | ||||||
| const lastModified = Date.now(); | ||||||
| await tagCache.hasBeenRevalidated(tags, lastModified); | ||||||
| expect(mockedTagCache.hasBeenRevalidated).not.toHaveBeenCalled(); | ||||||
| }); | ||||||
|
|
||||||
| it("should filter out tags based on getPathsByTags", async () => { | ||||||
| const tagCache = withFilter({ | ||||||
| originalTagCache: mockedTagCache, | ||||||
| filterFn, | ||||||
| }); | ||||||
|
|
||||||
| const tags = ["valid_tag", "invalid_tag"]; | ||||||
|
|
||||||
| await tagCache.getPathsByTags?.(tags); | ||||||
| expect(mockedTagCache.getPathsByTags).toHaveBeenCalledWith(["valid_tag"]); | ||||||
| }); | ||||||
|
|
||||||
| it("should not call getPathsByTags if no tags are valid", async () => { | ||||||
| const tagCache = withFilter({ | ||||||
| originalTagCache: mockedTagCache, | ||||||
| filterFn, | ||||||
| }); | ||||||
| const tags = ["invalid_tag"]; | ||||||
| await tagCache.getPathsByTags?.(tags); | ||||||
| expect(mockedTagCache.getPathsByTags).not.toHaveBeenCalled(); | ||||||
| }); | ||||||
|
|
||||||
| it("should return the correct name", () => { | ||||||
| const tagCache = withFilter({ | ||||||
| originalTagCache: mockedTagCache, | ||||||
| filterFn, | ||||||
| }); | ||||||
|
|
||||||
| expect(tagCache.name).toBe("filtered-mocked"); | ||||||
| }); | ||||||
|
|
||||||
| it("should not create a function if getPathsByTags is not defined", async () => { | ||||||
| const tagCache = withFilter({ | ||||||
| originalTagCache: { | ||||||
| ...mockedTagCache, | ||||||
| getPathsByTags: undefined, | ||||||
| }, | ||||||
| filterFn, | ||||||
| }); | ||||||
|
|
||||||
| expect(tagCache.getPathsByTags).toBeUndefined(); | ||||||
| }); | ||||||
|
|
||||||
| it("should properly filter soft tags", () => { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| const tagCache = withFilter({ | ||||||
| originalTagCache: mockedTagCache, | ||||||
| filterFn: softTagFilter, | ||||||
| }); | ||||||
|
|
||||||
| tagCache.writeTags(["valid_tag", "_N_T_/", "_N_T_/test", "_N_T_/layout"]); | ||||||
| expect(mockedTagCache.writeTags).toHaveBeenCalledWith(["valid_tag"]); | ||||||
| }); | ||||||
| }); | ||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,58 @@ | ||||||||||||||||||||||
| import { NextModeTagCache } from "@opennextjs/aws/types/overrides"; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| interface WithFilterOptions { | ||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * The original tag cache. | ||||||||||||||||||||||
| * Call to this will receive only the filtered tags. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| originalTagCache: NextModeTagCache; | ||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe?
Suggested change
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * The function to filter tags. | ||||||||||||||||||||||
| * @param tag The tag to filter. | ||||||||||||||||||||||
| * @returns true if the tag should be forwarde, false otherwise. | ||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| filterFn: (tag: string) => boolean; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Creates a new tag cache that filters tags based on the provided filter function. | ||||||||||||||||||||||
| * 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. | ||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| export function withFilter({ originalTagCache, filterFn }: WithFilterOptions): NextModeTagCache { | ||||||||||||||||||||||
| return { | ||||||||||||||||||||||
| name: `filtered-${originalTagCache.name}`, | ||||||||||||||||||||||
| mode: "nextMode", | ||||||||||||||||||||||
| getPathsByTags: originalTagCache.getPathsByTags | ||||||||||||||||||||||
| ? async (tags) => { | ||||||||||||||||||||||
| const filteredTags = tags.filter(filterFn); | ||||||||||||||||||||||
| if (filteredTags.length === 0) { | ||||||||||||||||||||||
| return []; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| return originalTagCache.getPathsByTags!(filteredTags); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| : undefined, | ||||||||||||||||||||||
| hasBeenRevalidated: async (tags, lastModified) => { | ||||||||||||||||||||||
| const filteredTags = tags.filter(filterFn); | ||||||||||||||||||||||
| if (filteredTags.length === 0) { | ||||||||||||||||||||||
| return false; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| return originalTagCache.hasBeenRevalidated(filteredTags, lastModified); | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| writeTags: async (tags) => { | ||||||||||||||||||||||
| const filteredTags = tags.filter(filterFn); | ||||||||||||||||||||||
| if (filteredTags.length === 0) { | ||||||||||||||||||||||
| return; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| return originalTagCache.writeTags(filteredTags); | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Filter function to exclude tags that start with "_N_T_". | ||||||||||||||||||||||
| * This is used to filter out internal soft tags. | ||||||||||||||||||||||
| * Can be used if `revalidatePath` is not used. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| export function softTagFilter(tag: string): boolean { | ||||||||||||||||||||||
| return !tag.startsWith("_N_T_"); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.