Skip to content

Commit

Permalink
retain utf8 for json content-type
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanirion committed Dec 5, 2023
1 parent 18b793a commit 9c1e78f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
11 changes: 9 additions & 2 deletions packages/open-next/src/adapters/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,12 @@ export default class S3Cache {
lastModified: LastModified?.getTime(),
value: {
kind: "ROUTE",
body: Buffer.from(cacheData.body ?? Buffer.alloc(0), "base64"),
body: Buffer.from(
cacheData.body ?? Buffer.alloc(0),
meta?.headers?.["content-type"]?.includes("application/json")
? "utf8"
: "base64",
),
status: meta?.status,
headers: meta?.headers,
},
Expand Down Expand Up @@ -276,7 +281,9 @@ export default class S3Cache {
"cache",
JSON.stringify({
type: "route",
body: body.toString("base64"),
body: body.toString(
headers["content-type"] === "application/json" ? "utf8" : "base64",
),
meta: {
status,
headers,
Expand Down
15 changes: 11 additions & 4 deletions packages/open-next/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,18 +494,25 @@ 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).toString("base64")
? fs
.readFileSync(files.body)
.toString(
cacheFileMeta.headers["content-type"] === "application/json"
? "utf8"
: "base64",
)
: undefined,
};
fs.writeFileSync(cacheFilePath, JSON.stringify(cacheFileContent));
Expand Down

0 comments on commit 9c1e78f

Please sign in to comment.