Skip to content

Commit

Permalink
Changes encoding on cache.body from utf8 to base64 (#329)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
dylanirion and conico974 authored Jan 5, 2024
1 parent eb08980 commit fd90b26
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
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

1 comment on commit fd90b26

@vercel
Copy link

@vercel vercel bot commented on fd90b26 Jan 5, 2024

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

open-next – ./

open-next-git-main-sst-dev.vercel.app
open-next-sst-dev.vercel.app
open-next.vercel.app

Please sign in to comment.