Please can you clarify me when to use static_path/2 or not for serving assets in a template?

For example in app.html.eex template both the following lines work…

<link rel="stylesheet" href="/css/app.css">
<link rel="stylesheet" href="<%= static_path(@conn, "/css/app.css") %>">

Or in another template:

<img src="/images/none/80x80.png" alt="photo">
<img src="<%= static_path(@conn, "/images/none/80x80.png") %>" alt="photo">

I would like to know If there is no problem not using it… Maybe that just works because I am in dev environment ?

static_path will still work even if you decide to mount your assets to a different path than the default / with the Plug.Static plug, while with hardcoded assets you’d need to update every single one of them.

2 Likes

Thank you, I would better use it then !^^

1 Like