Next.js Discord

Discord Forum

nextjs standalone cors

Unanswered
Levriero Sardo posted this in #help-forum
Open in Discord
Levriero SardoOP
I created an api using nextjs, then in next.config.mjs, I put this code:

{
key: "Access-Control-Allow-Origin",
value: "https://dashboard.scanhadir.com",
},

when running the project in dev or next dev mode and doing port forwarding, the domain that I gave access to was accepted by cors, then I built the project with standalone output, but when I ran it, the domain that should have been able to, was blocked by cors, okay so next config headers only work during development not production

next config code:
/** @type {import('next').NextConfig} */
const nextConfig = {
  output: "standalone",
  distDir: "source",
  images: {
    remotePatterns: [
      {
        protocol: "https",
        hostname: "**",
      }
    ],
  },
  async headers() {
    return [
      {
        // matching all API routes
        source: "/:path*",
        headers: [
          { key: "Access-Control-Allow-Credentials", value: "true" },
          {
            key: "Access-Control-Allow-Origin",
            value: "https://dashboard.scanhadir.com",
          },
          {
            key: "Access-Control-Allow-Methods",
            value: "GET,DELETE,PATCH,POST,PUT",
          },
          {
            key: "Access-Control-Allow-Headers",
            value:
              "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version",
          },
        ],
      },
    ];
  },
};

export default nextConfig;

2 Replies

Levriero SardoOP
This is the entire file from the build result with standalone output