Skip to content

Commit 3fbf663

Browse files
committed
review
1 parent 4f5785b commit 3fbf663

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { getTagsFromValue, hasBeenRevalidated, writeTags } from "utils/cache";
77
import { isBinaryContentType } from "../utils/binary";
88
import { debug, error, warn } from "./logger";
99

10+
export const SOFT_TAG_PREFIX = "_N_T_/";
11+
1012
function isFetchCache(
1113
options?:
1214
| boolean
@@ -75,15 +77,15 @@ export default class Cache {
7577
// Then we need to find the path for the given key
7678
const path = softTags?.find(
7779
(tag) =>
78-
tag.startsWith("_N_T_/") &&
80+
tag.startsWith(SOFT_TAG_PREFIX) &&
7981
!tag.endsWith("layout") &&
8082
!tag.endsWith("page"),
8183
);
8284
if (path) {
8385
const hasPathBeenUpdated = cachedEntry.shouldBypassTagCache
8486
? false
8587
: await hasBeenRevalidated(
86-
path.replace("_N_T_/", ""),
88+
path.replace(SOFT_TAG_PREFIX, ""),
8789
[],
8890
cachedEntry,
8991
);
@@ -356,11 +358,11 @@ export default class Cache {
356358
}));
357359

358360
// If the tag is a soft tag, we should also revalidate the hard tags
359-
if (tag.startsWith("_N_T_/")) {
361+
if (tag.startsWith(SOFT_TAG_PREFIX)) {
360362
for (const path of paths) {
361363
// We need to find all hard tags for a given path
362364
const _tags = await globalThis.tagCache.getByPath(path);
363-
const hardTags = _tags.filter((t) => !t.startsWith("_N_T_/"));
365+
const hardTags = _tags.filter((t) => !t.startsWith(SOFT_TAG_PREFIX));
364366
// For every hard tag, we need to find all paths and revalidate them
365367
for (const hardTag of hardTags) {
366368
const _paths = await globalThis.tagCache.getByTag(hardTag);
@@ -384,7 +386,7 @@ export default class Cache {
384386
new Set(
385387
toInsert
386388
// We need to filter fetch cache key as they are not in the CDN
387-
.filter((t) => t.tag.startsWith("_N_T_/"))
389+
.filter((t) => t.tag.startsWith(SOFT_TAG_PREFIX))
388390
.map((t) => `/${t.path}`),
389391
),
390392
);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable sonarjs/no-duplicate-string */
2-
import Cache from "@opennextjs/aws/adapters/cache.js";
2+
import Cache, {SOFT_TAG_PREFIX} from "@opennextjs/aws/adapters/cache.js";
33
import { vi } from "vitest";
44

55
declare global {
@@ -553,13 +553,13 @@ describe("CacheHandler", () => {
553553
it("Should call invalidateCdnHandler.invalidatePaths", async () => {
554554
globalThis.tagCache.getByTag.mockResolvedValueOnce(["/path"]);
555555
globalThis.tagCache.getByPath.mockResolvedValueOnce([]);
556-
await cache.revalidateTag("_N_T_/path");
556+
await cache.revalidateTag(`${SOFT_TAG_PREFIX}path`);
557557

558558
expect(tagCache.writeTags).toHaveBeenCalledTimes(1);
559559
expect(tagCache.writeTags).toHaveBeenCalledWith([
560560
{
561561
path: "/path",
562-
tag: "_N_T_/path",
562+
tag: `${SOFT_TAG_PREFIX}path`,
563563
},
564564
]);
565565

@@ -731,7 +731,7 @@ describe("CacheHandler", () => {
731731

732732
const result = await cache.get("key", {
733733
kind: "FETCH",
734-
softTags: ["_N_T_/path"],
734+
softTags: [`${SOFT_TAG_PREFIX}path`],
735735
});
736736

737737
expect(getFetchCacheSpy).toHaveBeenCalled();

0 commit comments

Comments
 (0)