Skip to content

Commit fd90b26

Browse files
Changes encoding on cache.body from utf8 to base64 (#329)
* changes encoding on cache.body from utf8 to base64 * retain utf8 for json content-type * opting for less greedy base64 * use isBinaryContentType * changeset --------- Co-authored-by: Dorseuil Nicolas <[email protected]>
1 parent eb08980 commit fd90b26

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

.changeset/wicked-comics-trade.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"open-next": patch
3+
---
4+
5+
Changes encoding on cache.body for binary data

packages/open-next/src/adapters/cache.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
} from "@aws-sdk/client-s3";
1414
import path from "path";
1515

16+
import { isBinaryContentType } from "./binary.js";
1617
import { MAX_DYNAMO_BATCH_WRITE_ITEM_COUNT } from "./constants.js";
1718
import { debug, error, warn } from "./logger.js";
1819
import { chunk } from "./util.js";
@@ -226,7 +227,12 @@ export default class S3Cache {
226227
lastModified: LastModified?.getTime(),
227228
value: {
228229
kind: "ROUTE",
229-
body: Buffer.from(cacheData.body ?? Buffer.alloc(0)),
230+
body: Buffer.from(
231+
cacheData.body ?? Buffer.alloc(0),
232+
isBinaryContentType(String(meta?.headers?.["content-type"]))
233+
? "base64"
234+
: "utf8",
235+
),
230236
status: meta?.status,
231237
headers: meta?.headers,
232238
},
@@ -276,7 +282,11 @@ export default class S3Cache {
276282
"cache",
277283
JSON.stringify({
278284
type: "route",
279-
body: body.toString("utf8"),
285+
body: body.toString(
286+
isBinaryContentType(String(headers["content-type"]))
287+
? "base64"
288+
: "utf8",
289+
),
280290
meta: {
281291
status,
282292
headers,

packages/open-next/src/build.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
buildSync,
1111
} from "esbuild";
1212

13+
import { isBinaryContentType } from "./adapters/binary.js";
1314
import logger from "./logger.js";
1415
import { minifyAll } from "./minimize-js.js";
1516
import openNextPlugin from "./plugin.js";
@@ -494,17 +495,26 @@ function createCacheAssets(monorepoRoot: string, disableDynamoDBCache = false) {
494495

495496
// Generate cache file
496497
Object.entries(cacheFilesPath).forEach(([cacheFilePath, files]) => {
498+
const cacheFileMeta = files.meta
499+
? JSON.parse(fs.readFileSync(files.meta, "utf8"))
500+
: undefined;
497501
const cacheFileContent = {
498502
type: files.body ? "route" : files.json ? "page" : "app",
499-
meta: files.meta
500-
? JSON.parse(fs.readFileSync(files.meta, "utf8"))
501-
: undefined,
503+
meta: cacheFileMeta,
502504
html: files.html ? fs.readFileSync(files.html, "utf8") : undefined,
503505
json: files.json
504506
? JSON.parse(fs.readFileSync(files.json, "utf8"))
505507
: undefined,
506508
rsc: files.rsc ? fs.readFileSync(files.rsc, "utf8") : undefined,
507-
body: files.body ? fs.readFileSync(files.body, "utf8") : undefined,
509+
body: files.body
510+
? fs
511+
.readFileSync(files.body)
512+
.toString(
513+
isBinaryContentType(cacheFileMeta.headers["content-type"])
514+
? "base64"
515+
: "utf8",
516+
)
517+
: undefined,
508518
};
509519
fs.writeFileSync(cacheFilePath, JSON.stringify(cacheFileContent));
510520
});

0 commit comments

Comments
 (0)