sezaru

sezaru

I have some components that will be used in multiple routes, these components need to generate link to other routes, but these links change depending on the liveview they were loaded.

For example, if my liveview route is “/myroute”, then the component would generate a route like “/myroute/something”.

To achieve this, what I tried to do was pass a prefix_url field to the component with the “/myroute” part of the route, and then the component itself would concat with the “/something” part.

This works fine, but if I try to use verified routes with it, it will start generating warnings.

This is a small example of what I’m trying to do:

defmodule CoreWeb.Components.MyComponent do
  use CoreWeb, :html

  attr :prefix_url, :string

  def render(assigns) do
    ~H"""
    <button phx-click={~p"#{@prefix_url}/something"}></button>
    """
  end
end

defmodule CoreWeb.Live.Trash.Blibs do
  use CoreWeb, :live_view

  def mount(_, _, socket), do: {:ok, socket}

  def render(assigns) do
    ~H"""
    <div>
      <CoreWeb.Components.MyComponent.render prefix_url={~p"/blibs"} />
    </div>
    """
  end
end

This will generate the following compilation error:

== Compilation error in file lib/core_web/live/trash/blibs.ex ==
** (ArgumentError) paths must begin with /, got: "#{assigns.prefix_url}/something"
    (phoenix 1.7.7) lib/phoenix/verified_routes.ex:548: Phoenix.VerifiedRoutes.verify_segment/2
    (phoenix 1.7.7) lib/phoenix/verified_routes.ex:734: Phoenix.VerifiedRoutes.rewrite_path/4
    (phoenix 1.7.7) lib/phoenix/verified_routes.ex:720: Phoenix.VerifiedRoutes.build_route/5
    (phoenix 1.7.7) expanding macro: Phoenix.VerifiedRoutes.sigil_p/2
    lib/core_web/live/trash/blibs.ex:8: CoreWeb.Components.MyComponent.render/1
    (phoenix_live_view 0.20.0) expanding macro: Phoenix.Component.sigil_H/2
    lib/core_web/live/trash/blibs.ex:7: CoreWeb.Components.MyComponent.render/1

If I change the button link to this:

<button phx-click={~p"/#{@prefix_url}/something"}></button>

Now it returns the following warning:

warning: no route path for CoreWeb.Router matches "/#{assigns.prefix_url}/something"
  lib/core_web/live/trash/blibs.ex:8: CoreWeb.Components.MyComponent.render/1

Of course, I can just remove the ~p and it will work fine, but I would prefer to keep using verified routes in this case.

Is there any workaround to make this work?

Showing Posts 1 to 4

cblavier

cblavier

I’m not sure what you want to do is feasible: you want to verify at compile time a dynamic route evaluated at runtime.

Seems paradoxical to me :man_shrugging:

sezaru

sezaru OP

I mean, I’m passing a static string to the component, I was guessing that the compiler would know that in this case, guess not.

cblavier

cblavier

I think you either need to have your router declaring a route with a dynamic prefix parameter:

# router.ex
get "/:prefix/something", SomethingController, :show

or your component to handle a static list of prefixes that can be used at compile-time

defp prefixed_something_path("blibs"), do: ~p"/blibs/something"
defp prefixed_something_path("foo"), do: ~p"/foo/something"
defp prefixed_something_path("bar"), do: ~p"/bar/something"
LostKobrakai

LostKobrakai

Yes, you’re passing a static string. But nothing in the system knows at compile time how that static string is composed in some other computations and what the result of that computation would be. But that result is what you’d probably want to have verified. If not you can always skip verified routes.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
csmr
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley: Mutonex gamedesign was finish...
New
perzanko
Do we have maintainer of absinthe-graphql/dataloader here? We have a PR that we’d like to merge to the library. Relatively small additive...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews