Skip to content
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/popular-tomatoes-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/aws": patch
---

fix: createAssets fails in case of broken JSON in fetch cache
20 changes: 13 additions & 7 deletions packages/open-next/src/build/createAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from "node:fs";
import path from "node:path";

import { loadConfig } from "config/util.js";
import { safeParseJsonFile } from "utils/safe-json-parse.js";
import logger from "../logger.js";
import type { TagCacheMetaFile } from "../types/cache.js";
import { isBinaryContentType } from "../utils/binary.js";
Expand Down Expand Up @@ -159,15 +160,20 @@ export function createCacheAssets(options: buildHelper.BuildOptions) {
// Generate cache file
Object.entries(cacheFilesPath).forEach(([cacheFilePath, files]) => {
const cacheFileMeta = files.meta
? JSON.parse(fs.readFileSync(files.meta, "utf8"))
? safeParseJsonFile(fs.readFileSync(files.meta, "utf8"), cacheFilePath)
: undefined;
const cacheJson = files.json
? safeParseJsonFile(fs.readFileSync(files.json, "utf8"), cacheFilePath)
: undefined;
if ((files.meta && !cacheFileMeta) || (files.json && !cacheJson)) {
logger.warn(`Skipping invalid cache file: ${cacheFilePath}`);
return;
}
const cacheFileContent = {
type: files.body ? "route" : files.json ? "page" : "app",
meta: cacheFileMeta,
html: files.html ? fs.readFileSync(files.html, "utf8") : undefined,
json: files.json
? JSON.parse(fs.readFileSync(files.json, "utf8"))
: undefined,
json: cacheJson,
rsc: files.rsc ? fs.readFileSync(files.rsc, "utf8") : undefined,
body: files.body
? fs
Expand Down Expand Up @@ -203,7 +209,7 @@ export function createCacheAssets(options: buildHelper.BuildOptions) {
() => true,
({ absolutePath, relativePath }) => {
const fileContent = fs.readFileSync(absolutePath, "utf8");
const fileData = JSON.parse(fileContent);
const fileData = safeParseJsonFile(fileContent, absolutePath);
fileData?.tags?.forEach((tag: string) => {
metaFiles.push({
tag: { S: path.posix.join(buildId, tag) },
Expand All @@ -227,8 +233,8 @@ export function createCacheAssets(options: buildHelper.BuildOptions) {
absolutePath.endsWith(".meta") && !isFileSkipped(relativePath),
({ absolutePath, relativePath }) => {
const fileContent = fs.readFileSync(absolutePath, "utf8");
const fileData = JSON.parse(fileContent);
if (fileData.headers?.["x-next-cache-tags"]) {
const fileData = safeParseJsonFile(fileContent, absolutePath);
if (fileData?.headers?.["x-next-cache-tags"]) {
fileData.headers["x-next-cache-tags"]
.split(",")
.forEach((tag: string) => {
Expand Down
16 changes: 16 additions & 0 deletions packages/open-next/src/utils/safe-json-parse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import logger from "../logger.js";

export function safeParseJsonFile<T = any>(
input: string,
filePath: string,
fallback?: T,
): T | undefined {
try {
return JSON.parse(input);
} catch (err) {
logger.warn(
`Failed to parse JSON file "${filePath}". Error: ${(err as Error).message}`,
);
return fallback;
}
}
Loading