Next.js Discord

Discord Forum

I get cors failed with capacitorjs

Unanswered
Maine Coon posted this in #help-forum
Open in Discord
Maine CoonOP
Hello! I have a backend + panel running in nextjs and i'm trying to build a capacitorjs app with nextjs as frontend framework, but i get CORS failed when i try to fetch a api call from the app to the backend.

The app is running on a server and i get that issue from my phone that is on another network, i tried to add the following config to nextjs config file.

import type { NextConfig } from 'next'

const nextConfig: NextConfig = {
  async rewrites() {
    return [
      {
        source: '/api/:path*',
        destination: 'http://REDACTED:3000/:path*'
      }
    ]
  },
  async headers() {
    return [
      {
        source: '/api/:path*',
        headers: [
          { key: 'Access-Control-Allow-Credentials', value: 'true' },
          { key: 'Access-Control-Allow-Origin', value: '*' },
          { key: 'Access-Control-Allow-Methods', value: 'GET,OPTIONS,PATCH,DELETE,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

0 Replies