mruoss

mruoss

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…

Showing Posts 1 to 3

derek-zhou

derek-zhou

I don’t understand, why is /page/:id not a nice URL? :id can be a meaningful string too.
If you insist on having one level of route going directly to each page, eg: /:id then why bother with the router since you don’t use it. You can just write a plug.

christhekeele

christhekeele

You could always store the alias of the page in the db, and have /page/:id issue a redirect to a catch-all /page/:id/*alias route.

mruoss

mruoss OP

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.

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 91898 914
New
AstonJ
The obligatory hello world thread! Who are you and where are you from? :stuck_out_tongue:
4616 55835 594
New
byu
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project. My initial shotgu...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
matt-savvy
Is there a word for the ~> symbol used in Version strings? Do you also just call it a Squiggle Arrow™ ?!
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews