mruoss

mruoss

Dynamic routes or mapping URL aliases to internal routes with Phoenix LiveView

Hello Elixir/Phoenix Forum

Although I’m not planning on doing this anytime soon, I’ve been thinking about this topic for a while and I thought maybe the forum wants to join this thought process. :wink:

As far as I can see, there’s no way with Phoenix LiveView to build a system with dynamic routes (right?). Let’s assume we’d like to build a head for a headless CMS with LiveView. I see a way to serve “pages” defined in the CMS from a single route that looks something like /page/:id. The LiveView would fetch the data from the CMS API and render the contents.

Now usually for SEO reasons, in your CMS you’d like to give each page a “nice” URL path - let’s call them “URL aliases”. Obviously these routes would be defined dynamically, i.e. during runtime. But in Phoenix, routes are defined at compile time. There’s no way to make Phoenix add routes during compile time. Now I think that’s exactly how it should be and it’s not even necessary to dynamically add routes to Phoenix. Internally we can still use the compile-time defined URL /page/:id for the routing. But in order to serve the URL aliases, we would need to mainain a map %{alias_url => id} and a way to translate URL aliases to internal routes right before routing.

While I can see doing that for Non-LiveView Phoenix using a Plug, I don’t see a way doing this in LiveView. I think you’d have to add some translation hook mechanism into Phoenix.Router.route_info/4 or Phoenix.LiveView.Route.live_link_info/3. Or maybe I’m missing something…

Happy to read your thoughts…

Most Liked

mruoss

mruoss

Oh boy, I guess I forgot about the catch-all routes. Thanks for reminding me! :slight_smile:
I would not have to store aliases as they are retrieved from the headless CMS. I can simply keep the mapping in process memory (e.g. a GenServer) and refresh them from time to time or upon request. Then in mount of my LiveView I can check if the URL actually matches an existing alias and otherwise render a NotFound page.

Where Next?

Popular in Discussions Top

matthias_toepp
I’d love to hear what people think about Wisp, the new Gleam web framework started by Gleam’s primary creator Louis Pilfold. Gleam, alon...
New
Jayshua
I recently came across the javascript library htmx. It reminded me a lot of liveview so I thought the community here might be interested....
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
pillaiindu
In django there is a cache framework backed by memcached. Rails also puts a lot of emphasis on caching, and even the idea of russian-doll...
New
PragTob
Hey everyone, this has been brewing in my head some time and it came up again while reading Adopting Elixir. GenServers, supervisors et...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
rower687
Hi all, I’ve been reading a lot about the “let it crash” term and how supervising processes and the whole messaging passing make an elixi...
New
opsb
We’re considering our architecture from a viewpoint of scaling our traffic heavily over the next 6 months. Our current deployment is runn...
New
MarioFlach
Hello, I want to share a project I’ve been working on for a while: https://github.com/almightycouch/gitgud Background Some time ago I ...
New
paulanthonywilson
I like Umbrella projects and pretty much always use them for personal Elixir stuff, especially Nerves things. But I don’t think this is ...
New

Other popular topics Top

axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48342 226
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31013 112
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
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
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
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

We're in Beta

About us Mission Statement