Skip to content

Commit 83ed943

Browse files
authored
Add OpenNext version header only in debug mode (#274)
* Add OpenNext version header only in debug mode * Sync * Sync
1 parent 277d30f commit 83ed943

File tree

4 files changed

+41
-20
lines changed

4 files changed

+41
-20
lines changed

.changeset/smooth-snakes-tease.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+
Add OpenNext version header only in debug mode

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
declare global {
2+
var openNextDebug: boolean;
3+
}
4+
15
export function debug(...args: any[]) {
2-
if (process.env.OPEN_NEXT_DEBUG) {
6+
if (globalThis.openNextDebug) {
37
console.log(...args);
48
}
59
}

packages/open-next/src/adapters/plugins/routing/util.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import { IncomingMessage } from "../../http/request.js";
66
import { ServerlessResponse } from "../../http/response.js";
77
import { awsLogger, debug } from "../../logger.js";
88

9+
declare global {
10+
var openNextDebug: boolean;
11+
var openNextVersion: string;
12+
}
13+
914
enum CommonHeaders {
1015
CACHE_CONTROL = "cache-control",
1116
}
@@ -73,7 +78,10 @@ export function fixSWRCacheHeader(
7378
}
7479

7580
export function addOpenNextHeader(headers: Record<string, string | undefined>) {
76-
headers["X-OpenNext"] = process.env.OPEN_NEXT_VERSION;
81+
headers["X-OpenNext"] = "1";
82+
if (globalThis.openNextDebug) {
83+
headers["X-OpenNext-Version"] = globalThis.openNextVersion;
84+
}
7785
}
7886

7987
export async function revalidateIfRequired(

packages/open-next/src/build.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -744,11 +744,14 @@ function addCacheHandler(outputPath: string, options?: DangerousOptions) {
744744
target: ["node18"],
745745
format: "cjs",
746746
banner: {
747-
js: `globalThis.disableIncrementalCache = ${
748-
options?.disableIncrementalCache ?? false
749-
}; globalThis.disableDynamoDBCache = ${
750-
options?.disableDynamoDBCache ?? false
751-
};`,
747+
js: [
748+
`globalThis.disableIncrementalCache = ${
749+
options?.disableIncrementalCache ?? false
750+
};`,
751+
`globalThis.disableDynamoDBCache = ${
752+
options?.disableDynamoDBCache ?? false
753+
};`,
754+
].join(""),
752755
},
753756
});
754757
}
@@ -767,12 +770,13 @@ function esbuildSync(esbuildOptions: ESBuildOptions) {
767770
minify: debug ? false : true,
768771
sourcemap: debug ? "inline" : false,
769772
...esbuildOptions,
770-
define: {
771-
...esbuildOptions.define,
772-
"process.env.OPEN_NEXT_DEBUG": process.env.OPEN_NEXT_DEBUG
773-
? "true"
774-
: "false",
775-
"process.env.OPEN_NEXT_VERSION": `"${openNextVersion}"`,
773+
banner: {
774+
...esbuildOptions.banner,
775+
js: [
776+
esbuildOptions.banner?.js || "",
777+
`globalThis.openNextDebug = ${process.env.OPEN_NEXT_DEBUG ?? false};`,
778+
`globalThis.openNextVersion = ${openNextVersion};`,
779+
].join(""),
776780
},
777781
});
778782

@@ -796,13 +800,13 @@ async function esbuildAsync(esbuildOptions: ESBuildOptions) {
796800
minify: debug ? false : true,
797801
sourcemap: debug ? "inline" : false,
798802
...esbuildOptions,
799-
// "process.env.OPEN_NEXT_DEBUG" determines if the logger writes to console.log
800-
define: {
801-
...esbuildOptions.define,
802-
"process.env.OPEN_NEXT_DEBUG": process.env.OPEN_NEXT_DEBUG
803-
? "true"
804-
: "false",
805-
"process.env.OPEN_NEXT_VERSION": `"${openNextVersion}"`,
803+
banner: {
804+
...esbuildOptions.banner,
805+
js: [
806+
esbuildOptions.banner?.js || "",
807+
`globalThis.openNextDebug = ${process.env.OPEN_NEXT_DEBUG ?? false};`,
808+
`globalThis.openNextVersion = ${openNextVersion};`,
809+
].join(""),
806810
},
807811
});
808812

0 commit comments

Comments
 (0)