Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent behaviour of verified routes with optional URL extensions #6033

Open
kieraneglin opened this issue Dec 31, 2024 · 0 comments
Open

Comments

@kieraneglin
Copy link

kieraneglin commented Dec 31, 2024

Environment

  • Elixir version (elixir -v): 1.17.0 (compiled with Erlang/OTP 26)
  • Phoenix version (mix deps): 1.7.17
  • Operating system: Debian

Preamble

I'm building an app that creates an RSS feed for podcast apps and I'm realizing that some popular podcast apps have extremely unexpected behaviours around the RSS feed URLs they accept. Some mandate that the feed/feed images/content URLs have extensions while others actually strip the provided extensions before making a request to my app. It's madness.

To that end, here's what I'm trying to do:

  • Define a route that can optionally accept an extension
    • eg: /feed/rss.xml and /feed/rss should resolve to the same controller. This currently works
  • Crucially, when generating the feed URL to display to users I should include the .xml extension as a best-practice

Actual behavior

When defining a route without any URL parameters, the above goals work as expected:

# In the router
scope "/", MyAppWeb do
  get "/rss/feed", FeedsController, :rss_feed
end

# In my view
url(@conn, ~p"/rss/feed") # This works!
url(@conn, ~p"/rss/feed.xml") # This works!

However, if the route contains a URL parameter then a no route path warning is emitted if I specify an extension:

# In the router
scope "/", MyAppWeb do
  get "/rss/:source_id/feed", FeedsController, :rss_feed
end

# In my view
url(@conn, ~p"/rss/#{source}/feed") # This works!
url(@conn, ~p"/rss/#{source}/feed.xml") # This emits a `no route path` warning

To be clear - both cases route traffic to the correct controller. The only issue is the warning emitted when generating the route.

Expected behavior

Route generation should act like the parameter-free route in the above examples. I should be able to optionally specify an extension in the ~p string

What I've tried/possible alternatives

  • I suppose I can define the route multiple times, but that's not ideal. Especially when I consider that I also need to implement this functionality for any image/media types that my app can serve
  • I can use concatenation like: url(@conn, ~p"/rss/#{source}/feed") <> ".xml" but that feels like a bodge
  • What I'm currently doing is that I've added a plug to strip and ignore incoming extensions but this doesn't solve the issue of the verified route warning
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant