itzmidinesh

itzmidinesh

Phoenix App CORS Configuration Issue with Deployment

I’m encountering a CORS configuration issue in my Phoenix app during deployment due to a mismatch between compile-time and runtime configurations.

Current Setup

In endpoint.ex, before MyApp.Router, I have:

plug CORSPlug, origin: Application.compile_env(:my_app, :cors_origin)

In config.exs (for dev and test environments):

config :my_app,
  cors_origins: System.get_env("CORS_ORIGINS", "*") |> String.split(",")

In runtime.exs:

config :my_app,
  cors_origins: System.fetch_env!("CORS_ORIGINS") |> String.split(",")

The Issue

When deploying to production (using Fly.io), I encounter this error:

ERROR! the application :my_app has a different value set for key :cors_origins during runtime compared to compile time. Since this application environment entry was marked as compile time, this difference can lead to unexpected behavior:
* Compile time value was not set
* Runtime value was set to: ["http://localhost:5173"]

If I change endpoint.ex to use Application.get_env/2:

plug CORSPlug, origin: Application.get_env(:my_app, :cors_origin)

the app deploys successfully, but I see a warning that get_env/2 is discouraged in this context.

I’ve been troubleshooting this for about eight hours and could use some guidance. Am I overlooking something? Any help would be greatly appreciated!

Marked As Solved

itzmidinesh

itzmidinesh

If anyone encounters the same issue in the future, here’s how I resolved it:

I moved the environment variable fetching logic to a helper module, which fixed the problem.

plug CORSPlug, origin: &MyAppWeb.Config.cors_origins/1

Here’s the helper module:

defmodule MyAppWeb.Config do
  def cors_origins, do: Application.get_env(:my_app, :cors_origins)
end

Note: Ensure you use the capture operator (&) when calling the function in the plug. Calling it without the capture operator will not work—I made this mistake and spent an entire day debugging it.

I hope this helps anyone facing a similar issue!

Also Liked

karlosmid

karlosmid

Hi, you can control check_origin via config files (runtime.exs, config.exs, dev.exs and test.exs:

config :your_app, YourAppWeb.Endpoint,
check_origin: "your_value"

and control prod, dev, and test env as you wish, no need to edit endpoint.ex file where you can only set values during compilation phase.

itzmidinesh

itzmidinesh

I opted not to use Application.compile_env because I needed the flexibility to update the origins dynamically without requiring a recompilation.

dimitarvp

dimitarvp

Thought so, thanks for elaborating.

Where Next?

Popular in Questions Top

WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement