marcuslankenau

marcuslankenau

Conditional routes in router.ex

Hello,

I’m currently in the process of rewriting parts of a Phoenix application. The frontend is primarily a React single-page app, but we’re transitioning to LiveView. To ensure a smooth rollout, I’d like to migrate specific pages from React to LiveView for a small subset of users first.

At the moment, we’re using a URL prefix approach — for example, /live/entity/123 instead of /entity/123. However, many of our links (e.g., from emails or external systems) point to the original /entity/123 routes, and updating all those references isn’t feasible.

Ideally, I’d like to implement a mechanism in the router that conditionally serves either the LiveView or the React SPA based on a value in the session or assigns (e.g., a feature flag or user group).

Is this kind of conditional routing possible in the Phoenix router? I couldn’t find a clear way to do it. Alternatively, are there other recommended approaches to handle this kind of progressive migration?

Thanks in advance!

Most Liked

LostKobrakai

LostKobrakai

The router cannot do so. All the router looks at to decide on a route is method, path segments and hostname – as becomes obvious here: Phoenix.Router — Phoenix v1.8.0-rc.3

You could however dig into implementation details and wrap Phoenix.LiveView.Plug – which is the plug used with the Phoenix.LiveView.Router.live macro – and however you make your react frontend work and let that wrapping plug decide on either or for a given path.

Where Next?

Popular in Questions Top

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
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36820 110
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44265 214
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement