Skip to content

Commit 9595714

Browse files
authored
fix: fetch cache does not depend on tag cache being enabled (#684)
1 parent aaf312f commit 9595714

File tree

2 files changed

+41
-37
lines changed

2 files changed

+41
-37
lines changed

.changeset/many-cooks-breathe.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/aws": patch
3+
---
4+
5+
fix: fetch cache does not depend on tag cache being enabled

packages/open-next/src/build/createAssets.ts

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,43 @@ export function createCacheAssets(options: buildHelper.BuildOptions) {
156156
fs.writeFileSync(cacheFilePath, JSON.stringify(cacheFileContent));
157157
});
158158

159-
if (!options.config.dangerous?.disableTagCache) {
160-
// Generate dynamodb data
161-
// We need to traverse the cache to find every .meta file
162-
const metaFiles: {
163-
tag: { S: string };
164-
path: { S: string };
165-
revalidatedAt: { N: string };
166-
}[] = [];
159+
// We need to traverse the cache to find every .meta file
160+
const metaFiles: {
161+
tag: { S: string };
162+
path: { S: string };
163+
revalidatedAt: { N: string };
164+
}[] = [];
165+
166+
// Copy fetch-cache to cache folder
167+
const fetchCachePath = path.join(
168+
appBuildOutputPath,
169+
".next/cache/fetch-cache",
170+
);
171+
if (fs.existsSync(fetchCachePath)) {
172+
const fetchOutputPath = path.join(outputDir, "cache", "__fetch", buildId);
173+
fs.mkdirSync(fetchOutputPath, { recursive: true });
174+
fs.cpSync(fetchCachePath, fetchOutputPath, { recursive: true });
167175

176+
buildHelper.traverseFiles(
177+
fetchCachePath,
178+
() => true,
179+
({ absolutePath, relativePath }) => {
180+
const fileContent = fs.readFileSync(absolutePath, "utf8");
181+
const fileData = JSON.parse(fileContent);
182+
fileData?.tags?.forEach((tag: string) => {
183+
metaFiles.push({
184+
tag: { S: path.posix.join(buildId, tag) },
185+
path: {
186+
S: path.posix.join(buildId, relativePath),
187+
},
188+
revalidatedAt: { N: "1" },
189+
});
190+
});
191+
},
192+
);
193+
}
194+
195+
if (!options.config.dangerous?.disableTagCache) {
168196
// Compute dynamodb cache data
169197
// Traverse files inside cache to find all meta files and cache tags associated with them
170198
buildHelper.traverseFiles(
@@ -194,35 +222,6 @@ export function createCacheAssets(options: buildHelper.BuildOptions) {
194222
},
195223
);
196224

197-
// Copy fetch-cache to cache folder
198-
const fetchCachePath = path.join(
199-
appBuildOutputPath,
200-
".next/cache/fetch-cache",
201-
);
202-
if (fs.existsSync(fetchCachePath)) {
203-
const fetchOutputPath = path.join(outputDir, "cache", "__fetch", buildId);
204-
fs.mkdirSync(fetchOutputPath, { recursive: true });
205-
fs.cpSync(fetchCachePath, fetchOutputPath, { recursive: true });
206-
207-
buildHelper.traverseFiles(
208-
fetchCachePath,
209-
() => true,
210-
({ absolutePath, relativePath }) => {
211-
const fileContent = fs.readFileSync(absolutePath, "utf8");
212-
const fileData = JSON.parse(fileContent);
213-
fileData?.tags?.forEach((tag: string) => {
214-
metaFiles.push({
215-
tag: { S: path.posix.join(buildId, tag) },
216-
path: {
217-
S: path.posix.join(buildId, relativePath),
218-
},
219-
revalidatedAt: { N: "1" },
220-
});
221-
});
222-
},
223-
);
224-
}
225-
226225
if (metaFiles.length > 0) {
227226
useTagCache = true;
228227
const providerPath = path.join(outputDir, "dynamodb-provider");

0 commit comments

Comments
 (0)