Skip to content

Commit

Permalink
feat: hide x-opennext header from server requests when poweredByHeade…
Browse files Browse the repository at this point in the history
…r is false (#567)

* add: poweredByHeader can hide x-opennext header from server requests

* add e2e test

* add changeset

* Revert "add changeset"

This reverts commit 8cbd733.

* add changeset in the root folder
  • Loading branch information
sommeeeer authored Oct 18, 2024
1 parent bd2e058 commit d6d4b8f
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/giant-vans-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/aws": patch
---

Hides the x-opennext header from server requests when poweredByHeader is false in next config
1 change: 1 addition & 0 deletions examples/pages-router/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const nextConfig = {
},
],
trailingSlash: true,
poweredByHeader: true,
};

module.exports = nextConfig;
6 changes: 4 additions & 2 deletions packages/open-next/src/core/routing/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import crypto from "node:crypto";
import { OutgoingHttpHeaders } from "node:http";
import { Readable } from "node:stream";

import { BuildId, HtmlPages } from "config/index.js";
import { BuildId, HtmlPages, NextConfig } from "config/index.js";
import type { IncomingMessage, StreamCreator } from "http/index.js";
import { OpenNextNodeResponse } from "http/openNextResponse.js";
import { parseHeaders } from "http/util.js";
Expand Down Expand Up @@ -322,7 +322,9 @@ export function fixSWRCacheHeader(headers: OutgoingHttpHeaders) {
* @__PURE__
*/
export function addOpenNextHeader(headers: OutgoingHttpHeaders) {
headers["X-OpenNext"] = "1";
if (NextConfig.poweredByHeader) {
headers["X-OpenNext"] = "1";
}
if (globalThis.openNextDebug) {
headers["X-OpenNext-Version"] = globalThis.openNextVersion;
headers["X-OpenNext-RequestId"] = globalThis.__als.getStore()?.requestId;
Expand Down
1 change: 1 addition & 0 deletions packages/open-next/src/types/next-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export interface NextConfig {
appDir?: boolean;
};
images: ImageConfig;
poweredByHeader?: boolean;
}

export interface RouteDefinition {
Expand Down
4 changes: 4 additions & 0 deletions packages/tests-e2e/tests/appRouter/headers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ test("Headers", async ({ page }) => {
// Request header should be available in RSC
let el = page.getByText(`request-header`);
await expect(el).toBeVisible();

// Both these headers should not be present cause poweredByHeader is false in appRouter
expect(headers["x-powered-by"]).toBeFalsy();
expect(headers["x-opennext"]).toBeFalsy();
});
14 changes: 14 additions & 0 deletions packages/tests-e2e/tests/pagesRouter/header.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { expect, test } from "@playwright/test";

test("should test if poweredByHeader adds the correct headers ", async ({
page,
}) => {
const result = await page.goto("/");
expect(result).toBeDefined();
expect(result?.status()).toBe(200);
const headers = result?.headers();

// Both these headers should be present cause poweredByHeader is true in pagesRouter
expect(headers?.["x-powered-by"]).toBe("Next.js");
expect(headers?.["x-opennext"]).toBe("1");
});

0 comments on commit d6d4b8f

Please sign in to comment.