-
Notifications
You must be signed in to change notification settings - Fork 73
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", () => { | ||||||
|
it("should properly filter soft tags", () => { | |
it("should filter soft tags", () => { |
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; | ||||||||||||||||||||||
|
/** | |
* The original tag cache. | |
* Call to this will receive only the filtered tags. | |
*/ | |
originalTagCache: NextModeTagCache; | |
/** | |
* The wrapped tag cache. | |
* Call to this will receive only the filtered tags. | |
*/ | |
tagCache: NextModeTagCache; |
Outdated
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.
* @returns true if the tag should be forwarde, false otherwise. | |
* @returns true if the tag should be forwarded, false otherwise. |
Outdated
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.
* 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. | |
* This is usefull to remove tags that are not used by the app, this could reduce the number of requests to the underlying tag cache. |
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.
useful
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.