Browse Source

[nextjs] Use config to set HTTP `Server` header

[According to Next.js maintainers][suggestion], this can improve
performance:

> If you only need custom headers, you might see better performance by
> using the `headers` option in `next.config.js` instead of middleware.

[suggestion]: https://github.com/vercel/next.js/discussions/75930#discussioncomment-12187436
Jonathan Hefner 5 months ago
parent
commit
f5afe651b0

+ 0 - 7
frameworks/TypeScript/nextjs/middleware.ts

@@ -1,7 +0,0 @@
-import { NextRequest, NextResponse } from "next/server"
-
-export function middleware(request: NextRequest) {
-  const response = NextResponse.next()
-  response.headers.set("Server", "Next.js")
-  return response
-}

+ 11 - 0
frameworks/TypeScript/nextjs/next.config.ts

@@ -2,6 +2,17 @@ import type { NextConfig } from "next";
 
 const nextConfig: NextConfig = {
   output: "standalone",
+
+  async headers() {
+    return [
+      {
+        source: "/(.*?)",
+        headers: [
+          { key: "Server", value: "Next.js" },
+        ],
+      },
+    ]
+  },
 };
 
 export default nextConfig;