Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -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,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 (!cacheFileMeta || !cacheJson) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is not correct meta does not exist for page router, doing that means that we are skipping any entry from page router.
You should do something like if ((files.meta && !cacheFileMeta) || (files.json && !cacheJson))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the catch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @conico974, I confirm issue originated in App Router app and was tested only there. I've applied requested fix, please rereview.

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
10 changes: 10 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,10 @@
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;
}
}