Skip to content

Commit

Permalink
fix issue with fetch cache for new page (#497)
Browse files Browse the repository at this point in the history
* fix issue with fetch cache for new page

* fix typo

* Create tame-foxes-compete.md
  • Loading branch information
conico974 authored Aug 26, 2024
1 parent 85af1ce commit 2094c9b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/tame-foxes-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"open-next": patch
---

fix issue with fetch cache for new page
38 changes: 19 additions & 19 deletions packages/open-next/src/adapters/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,27 +147,24 @@ export default class S3Cache {

if (value === undefined) return null;

// For cases where we don't have tags, we need to ensure that we insert at least an entry
// for this specific paths, otherwise we might not be able to invalidate it
// For cases where we don't have tags, we need to ensure that the soft tags are not being revalidated
// We only need to check for the path as it should already contain all the tags
if ((tags ?? []).length === 0) {
// First we check if we have any tags for the given key
const storedTags = await globalThis.tagCache.getByPath(key);
if (storedTags.length === 0) {
// Then we need to find the path for the given key
const path = softTags?.find(
(tag) =>
tag.startsWith("_N_T_/") &&
!tag.endsWith("layout") &&
!tag.endsWith("page"),
// Then we need to find the path for the given key
const path = softTags?.find(
(tag) =>
tag.startsWith("_N_T_/") &&
!tag.endsWith("layout") &&
!tag.endsWith("page"),
);
if (path) {
const pathLastModified = await globalThis.tagCache.getLastModified(
path.replace("_N_T_/", ""),
lastModified,
);
if (path) {
// And write the path with the tag
await globalThis.tagCache.writeTags([
{
path: key,
tag: path,
},
]);
if (pathLastModified === -1) {
// In case the path has been revalidated, we don't want to use the fetch cache
return null;
}
}
}
Expand Down Expand Up @@ -338,6 +335,9 @@ export default class S3Cache {
tagsToWrite.map((tag) => ({
path: key,
tag: tag,
// In case the tags are not there we just need to create them
// but we don't want them to return from `getLastModified` as they are not stale
revalidatedAt: 1,
})),
);
}
Expand Down
5 changes: 3 additions & 2 deletions packages/open-next/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,8 @@ async function createCacheAssets(monorepoRoot: string) {
path.relative(outputPath, filePath).replace(".meta", ""),
),
},
revalidatedAt: { N: `${Date.now()}` },
// We don't care about the revalidation time here, we just need to make sure it's there
revalidatedAt: { N: "1" },
});
});
}
Expand Down Expand Up @@ -632,7 +633,7 @@ async function createCacheAssets(monorepoRoot: string) {
path.relative(fetchCachePath, filepath),
),
},
revalidatedAt: { N: `${Date.now()}` },
revalidatedAt: { N: "1" },
});
});
},
Expand Down

0 comments on commit 2094c9b

Please sign in to comment.