Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
11 changes: 6 additions & 5 deletions packages/open-next/src/build/createAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import logger from "../logger.js";
import type { TagCacheMetaFile } from "../types/cache.js";
import { isBinaryContentType } from "../utils/binary.js";
import * as buildHelper from "./helper.js";
import { safeParseJsonFile } from "utils/safe-json-parse.js";

/**
* Copy the static assets to the output folder
Expand Down Expand Up @@ -159,14 +160,14 @@ 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 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"))
? safeParseJsonFile(fs.readFileSync(files.json, "utf8"), cacheFilePath)
: undefined,
rsc: files.rsc ? fs.readFileSync(files.rsc, "utf8") : undefined,
body: files.body
Expand Down Expand Up @@ -203,7 +204,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 +228,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
8 changes: 8 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,8 @@
export function safeParseJsonFile<T = any>(input: string, filePath: string, fallback?: T): T | undefined {
try {
return JSON.parse(input);
} catch (err) {
console.warn(`Failed to parse JSON file "${filePath}". Error: ${(err as Error).message}`);
return fallback;
}
}