-
Notifications
You must be signed in to change notification settings - Fork 21.3k
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
Mounted engine route fix #21804
Mounted engine route fix #21804
Conversation
Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @sgrif (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
Opened this pull request per @rafaelfranca suggestion here: #21743 /cc @arthurnn |
@merhard I'm slightly confused - why are we pulling If 44ff031 is wrong then we need to fix it but this seems to just obfuscate matters. |
@pixeltrix Good question. The As you can see from my first commit, I originally just set I have an alternative solution to fix this bug at #20958. Maybe that solution is better? Let me try to explain the flow of the code when Rails generates the value for mounted engine named routes: module Foo
class Engine < Rails::Engine
isolate_namespace Foo
routes.draw do
get 'bar', to: 'bar#show'
end
end
end
module ExampleApp
class Application < Rails::Application
routes.draw do
mount Foo::Engine, at: '/foo'
root 'welcome#index'
end
config.relative_url_root = '/relative_url_root'
end
end
class WelcomeController < ActionController::Base
def index
render text: foo.bar_path
end
end If you were to navigate to the root path of the If you were to navigate to the root path of the In master (and Rails >= v4.2.3), the response body will be Now let's walk through what Rails does when resolving
Step 4 is where the bug resides. This step should return |
To make the case for this pull request clear: #21459 |
@eltiare there's no doubt that it needs fixing - just want to make sure that it's the right fix. Often with these routing changes in patch releases we can end up with cascading regressions from one release to another as we try to patch edge cases. |
@pixeltrix I pushed a new commit to this pull request. Do you think the end result is more intention revealing? |
@merhard I think that's much better - can you clean up the commit history and add a CHANGELOG entry, thanks |
r? @pixeltrix You seem to be on top of this one |
When generating the url for a mounted engine through its proxy, the path should be the sum of three parts: 1. Any `SCRIPT_NAME` request header or the value of `ActionDispatch::Routing::RouteSet#relative_url_root`. 2. A prefix (the engine's mounted path). 3. The path of the named route inside the engine. Since commit rails@44ff031, this has been broken. Step 2 has been changed to: 2. A prefix (the value of `ActionDispatch::Routing::RouteSet#relative_url_root` + the engine's mounted path). The value of `ActionDispatch::Routing::RouteSet#relative_url_root` is taken into account in step 1 of the route generation and should be ignored when generating the mounted engine's prefix in step 2. This commit fixes the regression by having `ActionDispatch::Routing::RouteSet#url_for` check `options[:relative_url_root]` before falling back to `ActionDispatch::Routing::RouteSet#relative_url_root`. The prefix generating code then sets `options[:relative_url_root]` to an empty string. This empty string is used instead of `ActionDispatch::Routing::RouteSet#relative_url_root` and avoids the duplicate `relative_url_root` value in the final result. This resolves rails#20920 and resolves rails#21459
45cc179
to
bcfbd8b
Compare
@pixeltrix CHANGELOG entry added. Commits squashed and rebased. |
@merhard thanks very much for your patience and perseverance 👍 |
@pixeltrix You're welcome. This was my first Rails commit. Thank you for the feedback and your help getting this merged. 👍 |
@pixeltrix Any plans to backport this to 4-2-stable? The pull request that introduced the regression (#18775) was backported at 0703453 |
This pull request aims to fix a regression in master that has been present in Rails since v4.2.3
When generating the url for a mounted engine through its proxy, the path should be the sum of three parts:
SCRIPT_NAME
request header or the value ofrelative_url_root
.Since 44ff031, this has been broken. Instead, the path is the sum of four parts:
SCRIPT_NAME
request header or the value ofrelative_url_root
.relative_url_root
.This commit fixes the regression by using the value of default_url_options[:script_name] or an empty string as a benign truthy value for options[:script_name] when the prefix is being generated, avoiding the duplicate relative_url_root value in the final result.
This addresses issues #20920 and #21459.