Skip to content

Commit

Permalink
server: add OpenNext response header to track version
Browse files Browse the repository at this point in the history
  • Loading branch information
fwang committed Jun 28, 2023
1 parent e7c732b commit 1bcb37b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-trains-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"open-next": patch
---

server: add OpenNext response header to track version
5 changes: 5 additions & 0 deletions packages/open-next/src/adapters/server-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export async function handler(

fixCacheHeaderForHtmlPages(internalEvent.rawPath, headers);
fixSWRCacheHeader(headers);
addOpenNextHeader(headers);
await revalidateIfRequired(
internalEvent.headers.host,
internalEvent.rawPath,
Expand Down Expand Up @@ -255,6 +256,10 @@ function fixSWRCacheHeader(headers: Record<string, string | undefined>) {
}
}

function addOpenNextHeader(headers: Record<string, string | undefined>) {
headers["X-OpenNext"] = process.env.OPEN_NEXT_VERSION;
}

async function revalidateIfRequired(
host: string,
rawPath: string,
Expand Down
12 changes: 9 additions & 3 deletions packages/open-next/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ function normalizeOptions(opts: BuildOptions) {
const appPath = process.cwd();
const outputDir = ".open-next";
return {
openNextVersion: getOpenNextVersion(),
appPath,
appPublicPath: path.join(appPath, "public"),
outputDir,
Expand Down Expand Up @@ -167,8 +168,8 @@ function printNextjsVersion() {
}

function printOpenNextVersion() {
const onVersion = require(path.join(__dirname, "../package.json")).version;
console.info(`OpenNext v${onVersion}`);
const { openNextVersion } = options;
console.info(`OpenNext v${openNextVersion}`);
}

function initOutputDir() {
Expand Down Expand Up @@ -526,7 +527,7 @@ function addCacheHandler(outputPath: string) {
/********************/

function esbuildSync(esbuildOptions: ESBuildOptions) {
const { appPath, debug } = options;
const { openNextVersion, debug } = options;
const result = buildSync({
target: "esnext",
format: "esm",
Expand All @@ -541,6 +542,7 @@ function esbuildSync(esbuildOptions: ESBuildOptions) {
"process.env.OPEN_NEXT_DEBUG": process.env.OPEN_NEXT_DEBUG
? "true"
: "false",
"process.env.OPEN_NEXT_VERSION": `"${openNextVersion}"`,
},
});

Expand Down Expand Up @@ -601,3 +603,7 @@ function getBuildId(dotNextPath: string) {
.readFileSync(path.join(dotNextPath, ".next/BUILD_ID"), "utf-8")
.trim();
}

function getOpenNextVersion() {
return require(path.join(__dirname, "../package.json")).version;
}

1 comment on commit 1bcb37b

@vercel
Copy link

@vercel vercel bot commented on 1bcb37b Jun 28, 2023

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.vercel.app
open-next-sst-dev.vercel.app

Please sign in to comment.