Use Routes.static_path in LiveComponent

Is there a recommended way to use the Routes.static_path in a live component?

I guess I could pass the @conn in the session and then Rotes.static_path(@conn, ...), but was wondering if there is a better way to do this.


1 Like

You can also pass the Endpoint: Routes.static_path(MyAppWeb.Endpoint, ...)


Heads up – you can’t and also shouldn’t. A conn is not easily serializable like it’s needed for the data in the session.

1 Like


I think Routes.static_path(@socket, ...) should work.


Thanks everyone!

What about named paths? I’m converting my partials into components but I can’t do this anymore:

<%= Routes.job_path(@conn, :index) %>

What is the proper way to link to a non-live view from a live view, as we don’t have @conn or Routes?

Path helpers can take the @conn, @socket or even the Endpoint as 1st argument. As for Routes, it’s just a module: alias MyAppWeb.Router.Helpers, as: Routes (you probably have this in your my_app_web.ex file)


Ah, of course that was it! Thanks for the help.

For the record, the alias for Routes is defined like this:

In my case it wasn’t working because I was using:

use Phoenix.LiveComponent

when I should use:

use MyAppWeb, :live_component