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

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews