Skip to content

Changes encoding on cache.body from utf8 to base64 #329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 5, 2024
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/wicked-comics-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"open-next": patch
---

Changes encoding on cache.body for binary data
14 changes: 12 additions & 2 deletions packages/open-next/src/adapters/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -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,
Expand Down
18 changes: 14 additions & 4 deletions packages/open-next/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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));
});
Expand Down