Skip to content

Add route path aliases#1998

Merged
jwoertink merged 2 commits into
luckyframework:mainfrom
wout:add-route-path-aliases
Nov 3, 2025
Merged

Add route path aliases#1998
jwoertink merged 2 commits into
luckyframework:mainfrom
wout:add-route-path-aliases

Conversation

@wout
Copy link
Copy Markdown
Contributor

@wout wout commented Oct 31, 2025

Purpose

This PR adds the possibility to create aliases for route paths as discussed in #1996.

Description

I found another approach that is less invasive and requires a lot less changes to the code. It also doesn't affect performance when the feature is not used.

The way it works is to allow passing more than one path to the HTTP verb routing methods in actions:

class About::Index < BrowserAction
  get "/about", "/:locale/about" do
    # ...
  end
end

The first path is still the “main” path, is required, and will set up the action. Any subsequent paths are just aliases and don't trigger the call setup. Every route is added through the add_route macro so the aliases have all the same features you'd expect, including prefixes, optional params, etc.

I started using them in Fluck's website and they work great, so far without any side effects.

Checklist

  • - An issue already exists detailing the issue/or feature request that this PR fixes
  • - All specs are formatted with crystal tool format spec src
  • - Inline documentation has been added and/or updated
  • - Lucky builds on docker with ./script/setup
  • - All builds and specs pass on docker with ./script/test

@jwoertink
Copy link
Copy Markdown
Member

It's funny because it's basically #542 😂

So I guess the main question is, how do you build the URL for one route vs the other?

Something like this?

About::Index.url #=> site.com/about
About::Index.with(locale: "nl").url #=> site.com/nl/about

Copy link
Copy Markdown
Member

@jwoertink jwoertink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, the solution is pretty clean, and it looks like it still does cover being able to use the helpers like normal 😄

I'm trying to think of if there's any weird edge cases that might come up with it, but nothing is coming to mind...

@wout
Copy link
Copy Markdown
Contributor Author

wout commented Oct 31, 2025

It's funny because it's basically #542 😂

So funny! 😄 I've never seen that issue, probably because it was before I discovered Lucky.

So I guess the main question is, how do you build the URL for one route vs the other?

Something like this?

About::Index.url #=> site.com/about
About::Index.with(locale: "nl").url #=> site.com/nl/about

Precisely. That just works because it's using the add_route macro.

There is one thing I'd like to add to simplify routing. Currently in the example above the locale param can't be nil to route to the alias without the parameter in it:

About::Index.with(locale: nil).url #=> site.com/about

In case of localization, having this option would make it super easy to handle routing for the "main" locale (e.g. without :locale) and the additional locales where :locale is required. But that's for another PR. First I want to give this a spin to see how it works in reality.

@jwoertink jwoertink merged commit e4f280a into luckyframework:main Nov 3, 2025
7 checks passed
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

Successfully merging this pull request may close these issues.

2 participants