Next.js Discord

Discord Forum

CORS Issue

Answered
Great golden digger wasp posted this in #help-forum
Open in Discord
Great golden digger waspOP
I don't know how to make an api public in next js so everyone can use it when I host. I tried the documentation method but it doesn't work either. Any solution please.
Answered by Double-striped Thick-knee
have you tried this,?? it should work

/** @type {import('next').NextConfig} */
const nextConfig = {
    async headers() {
        return [
            {
                // matching all API routes
                source: "/api/:path*",
                headers: [{ key: "Access-Control-Allow-Origin", value: "*" }],
            },
        ];
    },
};

export default nextConfig;
View full answer

5 Replies

@Great golden digger wasp this is not working
Double-striped Thick-knee
have you tried this,?? it should work

/** @type {import('next').NextConfig} */
const nextConfig = {
    async headers() {
        return [
            {
                // matching all API routes
                source: "/api/:path*",
                headers: [{ key: "Access-Control-Allow-Origin", value: "*" }],
            },
        ];
    },
};

export default nextConfig;
Answer