diff --git a/.changeset/wicked-comics-trade.md b/.changeset/wicked-comics-trade.md new file mode 100644 index 000000000..a15538d41 --- /dev/null +++ b/.changeset/wicked-comics-trade.md @@ -0,0 +1,5 @@ +--- +"open-next": patch +--- + +Changes encoding on cache.body for binary data diff --git a/packages/open-next/src/adapters/cache.ts b/packages/open-next/src/adapters/cache.ts index 48e52d9a9..347a06621 100644 --- a/packages/open-next/src/adapters/cache.ts +++ b/packages/open-next/src/adapters/cache.ts @@ -13,6 +13,7 @@ import { } from "@aws-sdk/client-s3"; import path from "path"; +import { isBinaryContentType } from "./binary.js"; import { MAX_DYNAMO_BATCH_WRITE_ITEM_COUNT } from "./constants.js"; import { debug, error, warn } from "./logger.js"; import { chunk } from "./util.js"; @@ -226,7 +227,12 @@ export default class S3Cache { lastModified: LastModified?.getTime(), value: { kind: "ROUTE", - body: Buffer.from(cacheData.body ?? Buffer.alloc(0)), + body: Buffer.from( + cacheData.body ?? Buffer.alloc(0), + isBinaryContentType(String(meta?.headers?.["content-type"])) + ? "base64" + : "utf8", + ), status: meta?.status, headers: meta?.headers, }, @@ -276,7 +282,11 @@ export default class S3Cache { "cache", JSON.stringify({ type: "route", - body: body.toString("utf8"), + body: body.toString( + isBinaryContentType(String(headers["content-type"])) + ? "base64" + : "utf8", + ), meta: { status, headers, diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts index a184e56d5..079626f2f 100755 --- a/packages/open-next/src/build.ts +++ b/packages/open-next/src/build.ts @@ -10,6 +10,7 @@ import { buildSync, } from "esbuild"; +import { isBinaryContentType } from "./adapters/binary.js"; import logger from "./logger.js"; import { minifyAll } from "./minimize-js.js"; import openNextPlugin from "./plugin.js"; @@ -494,17 +495,26 @@ function createCacheAssets(monorepoRoot: string, disableDynamoDBCache = false) { // Generate cache file Object.entries(cacheFilesPath).forEach(([cacheFilePath, files]) => { + const cacheFileMeta = files.meta + ? JSON.parse(fs.readFileSync(files.meta, "utf8")) + : undefined; const cacheFileContent = { type: files.body ? "route" : files.json ? "page" : "app", - meta: files.meta - ? JSON.parse(fs.readFileSync(files.meta, "utf8")) - : undefined, + meta: cacheFileMeta, html: files.html ? fs.readFileSync(files.html, "utf8") : undefined, json: files.json ? JSON.parse(fs.readFileSync(files.json, "utf8")) : undefined, rsc: files.rsc ? fs.readFileSync(files.rsc, "utf8") : undefined, - body: files.body ? fs.readFileSync(files.body, "utf8") : undefined, + body: files.body + ? fs + .readFileSync(files.body) + .toString( + isBinaryContentType(cacheFileMeta.headers["content-type"]) + ? "base64" + : "utf8", + ) + : undefined, }; fs.writeFileSync(cacheFilePath, JSON.stringify(cacheFileContent)); });