Skip to content

fix: fetch cache does not depend on tag cache being enabled #684

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

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading