Skip to content

Commit

Permalink
Redirecting with trailing slash with query parameters (tricknotes#595)
Browse files Browse the repository at this point in the history
Fixes the bug when redirecting with trailing slash with query parameters
resulting in an invalid url
eg: https://app.com?query=foo would redirect to
https://app.com?query=foo/, which is invalid

This PR fixes this issue adding the trailing slash before the query
parameters
eg: https://app.com?query=foo would be redirected to
https://app.com/?query=foo

In order to retain a passing test suite, use ember-new-output v4.0.0.
  • Loading branch information
mbackermann authored May 31, 2022
1 parent d68d804 commit 2b9830d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion bin/setup_ember
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ setup_ember() {
local target="${1-spec/dummy/my-app}"

if ! [ -d $target ]; then
git clone -b stable https://github.com/ember-cli/ember-new-output.git $target
git clone -b 'v4.0.0' https://github.com/ember-cli/ember-new-output.git $target

echo '-- Make router catchall routes'
sed -i -e 's/auto/hash/' $target/config/environment.js
Expand Down
2 changes: 1 addition & 1 deletion lib/ember_cli/route_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def mount_ember_app(app_name, to:, **options)
redirect_if_missing_trailing_slash = {
constraints: EmberCli::TrailingSlashConstraint.new,
to: redirect(-> (_, request) {
File.join(request.original_fullpath, "")
File.join(request.path, "/?#{request.query_parameters.to_query}")
}),
}

Expand Down
2 changes: 1 addition & 1 deletion lib/ember_cli/trailing_slash_constraint.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module EmberCli
class TrailingSlashConstraint
def matches?(request)
!request.original_fullpath.to_s.ends_with?("/")
!request.original_fullpath.to_s.split("?").first.end_with?("/")
end
end
end
16 changes: 16 additions & 0 deletions spec/features/user_views_ember_app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@
expect(current_path).to eq("/asset-helpers/")
end

scenario "is redirected with trailing slash with query params", js: false do
expect(embedded_path(query: "foo")).to eq("/asset-helpers?query=foo")

visit embedded_path(query: "foo")

expect(page).to have_current_path("/asset-helpers/?query=foo")
end

scenario "is not redirected with trailing slash with params", js: false do
expect(embedded_path(query: "foo")).to eq("/asset-helpers?query=foo")

visit "/asset-helpers/?query=foo"

expect(page).to have_current_path("/asset-helpers/?query=foo")
end

def have_client_side_asset
have_css %{img[src*="logo.png"]}
end
Expand Down

0 comments on commit 2b9830d

Please sign in to comment.