Next.js Discord

Discord Forum

Next Config Dynamic Headers

Unanswered
Argentine ant posted this in #help-forum
Open in Discord
Argentine antOP
Next version: 12.2.3

Hi folks.

I have in my next.config.js file a headers function. I want to know if it is possible to return dynamic headers values from that function based on the incoming request values? I have this:

  async headers() {
    return [
      {
        source: "/:path*",
        headers: [
          {
            key: "Content-Security-Policy",
            value: ContentSecurityPolicy.replace(/\s{2,}/g, " ").trim(),
          },
        ],
      },
    ];
  }


I need something like:

  async headers(req) {
    return req.headers.something ? [
      {
        // Apply these headers to all routes in your application.
        source: "/:path*",
        headers: [
          {
            key: "Content-Security-Policy",
            value: ContentSecurityPolicy.replace(/\s{2,}/g, " ").trim(),
          },
        ],
      },
    ] : [];
  }


Is it possible? Thanks.

0 Replies