Skip to content

Commit

Permalink
fix: fetch cache does not depend on tag cache being enabled (#684)
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb authored Dec 20, 2024
1 parent aaf312f commit 9595714
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 37 deletions.
5 changes: 5 additions & 0 deletions .changeset/many-cooks-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/aws": patch
---

fix: fetch cache does not depend on tag cache being enabled
73 changes: 36 additions & 37 deletions packages/open-next/src/build/createAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,43 @@ export function createCacheAssets(options: buildHelper.BuildOptions) {
fs.writeFileSync(cacheFilePath, JSON.stringify(cacheFileContent));
});

if (!options.config.dangerous?.disableTagCache) {
// Generate dynamodb data
// We need to traverse the cache to find every .meta file
const metaFiles: {
tag: { S: string };
path: { S: string };
revalidatedAt: { N: string };
}[] = [];
// We need to traverse the cache to find every .meta file
const metaFiles: {
tag: { S: string };
path: { S: string };
revalidatedAt: { N: string };
}[] = [];

// Copy fetch-cache to cache folder
const fetchCachePath = path.join(
appBuildOutputPath,
".next/cache/fetch-cache",
);
if (fs.existsSync(fetchCachePath)) {
const fetchOutputPath = path.join(outputDir, "cache", "__fetch", buildId);
fs.mkdirSync(fetchOutputPath, { recursive: true });
fs.cpSync(fetchCachePath, fetchOutputPath, { recursive: true });

buildHelper.traverseFiles(
fetchCachePath,
() => true,
({ absolutePath, relativePath }) => {
const fileContent = fs.readFileSync(absolutePath, "utf8");
const fileData = JSON.parse(fileContent);
fileData?.tags?.forEach((tag: string) => {
metaFiles.push({
tag: { S: path.posix.join(buildId, tag) },
path: {
S: path.posix.join(buildId, relativePath),
},
revalidatedAt: { N: "1" },
});
});
},
);
}

if (!options.config.dangerous?.disableTagCache) {
// Compute dynamodb cache data
// Traverse files inside cache to find all meta files and cache tags associated with them
buildHelper.traverseFiles(
Expand Down Expand Up @@ -194,35 +222,6 @@ export function createCacheAssets(options: buildHelper.BuildOptions) {
},
);

// Copy fetch-cache to cache folder
const fetchCachePath = path.join(
appBuildOutputPath,
".next/cache/fetch-cache",
);
if (fs.existsSync(fetchCachePath)) {
const fetchOutputPath = path.join(outputDir, "cache", "__fetch", buildId);
fs.mkdirSync(fetchOutputPath, { recursive: true });
fs.cpSync(fetchCachePath, fetchOutputPath, { recursive: true });

buildHelper.traverseFiles(
fetchCachePath,
() => true,
({ absolutePath, relativePath }) => {
const fileContent = fs.readFileSync(absolutePath, "utf8");
const fileData = JSON.parse(fileContent);
fileData?.tags?.forEach((tag: string) => {
metaFiles.push({
tag: { S: path.posix.join(buildId, tag) },
path: {
S: path.posix.join(buildId, relativePath),
},
revalidatedAt: { N: "1" },
});
});
},
);
}

if (metaFiles.length > 0) {
useTagCache = true;
const providerPath = path.join(outputDir, "dynamodb-provider");
Expand Down

0 comments on commit 9595714

Please sign in to comment.