Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

CORs issue when run on production server

default discord avatar
tom.griffithslast year
4

I have setup my payload app and deployed on Railway. However, it seems its ignoring my CORs settings, as im getting CORs error on front end. Config setup as below



//at top of file im calling below
dotenv.config({
  path: path.resolve(__dirname, '../.env'),
})


// cors config key in payload.config.ts
cors: [
    process.env.PAYLOAD_PUBLIC_SERVER_URL || '',
    process.env.PAYLOAD_PUBLIC_SITE_URL || '',
    'https://checkout.stripe.com',
  ].filter(Boolean),


// env defined as
PAYLOAD_PUBLIC_SITE_URL=https://my-frontend-domain.vercel.app


Im even checking for env and throwing error if undefined, and it doesn't throw, but its doesn't seem to add the

PAYLOAD_PUBLIC_SITE_URL

to cors.



Feel like I'm missing something silly here, but I can't place it. What am I missing?



Note: This is in a monorepo, containing payload and next, structure as below. If I dont require dotenv in payload config, then it doesnt build.



app
  -next
  -payload


I should add that the payload app works fine. Have DB connection can login etc no problem.

  • default discord avatar
    ninotorres_last year

    Same error here

  • default discord avatar
    notchrlast year
    @711854352800809041

    @561194200704483349

    - This is not the permanent solution, but what happens if you modify your server.ts file to have CORS/CSRF config?



    (via express)



    const app = express();
    const router = express.Router();
    
    app.use(express.json());
    app.use(
      cors({
        origin: [
          "http://localhost:4200",
        ],
        credentials: true,
      })
    );


    I know it's ideal to fix the .env var issue, but this might help temporarily

  • default discord avatar
    tom.griffithslast year

    Thanks

    @1049775120559898725

    . I think I resolved my issue by fixing my Railway setup. I still had a docker file in my repo and I think Railway must have changed their auto build detection priorities, so it was building via the docker file instead of using nix packs. Once I deleted the docker file it picked up the vars and ran just fine.

  • default discord avatar
    notchrlast year

    No problem, glad to hear you solved it!

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.