Handling root path GET requests via URL routing?

If I wanted to emulate how Twitter (X) accepts a username in the URL via the root path how would I do this with Nitrogen?

Within the index.erl module I can call wf:path_info() to handle this use case, but what is the way to force all requests to the root that would normally 404 to flow through the index.erl module? Would this need to be handled by modifications to the dynamic_route_handler?

Ex: https://twitter.com/nitrogenproject

I have read Nitrogen - Documentation but I was hoping for something like Routing — Phoenix v1.7.11 that wouldn’t require modifications to the dynamic_route_handler provided by Nitrogren.

Hey @jbcorwin,

The two options available to you are to:

  1. Make a custom route handler, or
  2. Make a web_404 module to handle that.

(2) is the easiest. It acts like a normal module, it just has to be named web_404. Then you can pull the requested path with wf:path_info()

(1) however is more likely the proper solution.

You can make a copy of dynamic_route_handler.erl and make a few changes to it to handle some more specific cases.

But at the end of the day, either method works.

I’m also open to the possibility of augmenting the dynamic_route_handler to read some sort of configuration rules for pattern-matched routing - since the dynamic_route_handler does do a decent amount of stuff.

I agree with you that #1 would be the preferred way and to your suggestion of pattern matching in the dynamic_route_handler all I have in mind is just a configuration flag to treat unknowns to root to be handled by index.erl. Other paths (Ala reddit style) can already by managed by the exiting routing/modules - at least that I can think of.

I should add that I’d prefer to have this in the default install versus a separate dynamic_route_handler that I have to manage / coalesce through upgrades. I haven’t looked into what that would entail but anytime I can avoid extra work I’ll take it.

1 Like