Phoenix router for /:name.rss?

When I was porting my site to Phoenix a while back one of the things that I got hung up on was porting the RSS route, which was previously “tag.rss”. With the period there I couldn’t figure out how to get it working and ended up just going with :name/rss in the short term.

Any suggestions for how to make this work?

2 Likes

There’s a plug just for this situation!

3 Likes

Thanks! Very vague on the usage details but hopefully simple enough to just dig into the code.

Lifted from the readme:

# If you are using phoenix:
# Add the plug to your endpoint.ex file:

defmodule MyProject.Endpoint do
  # ...
  plug TrailingFormatPlug   # add this
  plug Plug.RequestId
  plug Plug.Logger
  # ...
end

That’s all there is to it

3 Likes

One detail that took some trial and error on my part to find was that you should not specify the .format route separately. That led to a rabbit hole of confusion about not allowed characters due to trying to map /:id.format.

I.e. you do need to adjust your plug :accepts list accordingly for the pipeline (add extension, in my case ics).

Speaking of which, it would be cool if you could specify accepts for individual routes rather than just an entire pipeline.

2 Likes

This is the point of pipelines :slight_smile: You could create a scope for the routes needing specific content negotiation and then pipe_through :special_content_negotation

1 Like

Yeah, I get that. It’s just kind of cumbersome when you are dealing with a single action on a resources derived route. Particularly when it’s a nested resource.

In my case, I just want to add a text/calendar representation to :show action for a resource.

1 Like

Just my opinion, but it sounds like that is a scenario that’s more the exception than the rule, meaning it should be a cumbersome 1-off rather than something cumbersome that you’d be doing over and over.

1 Like

Fair enough.

Just raising it as something that has a bit more friction than I expect from Phoenix’s otherwise really excellent routing.

I’ve also encountered this when wanting to provide RSS representations for search results, etc. That was a bit easier though because the routes weren’t nested.

Having said that, I could see adding RSS for index actions in a number of places. Time will tell.

1 Like