-
Notifications
You must be signed in to change notification settings - Fork 319
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
turbo-rails v2.0.2 explain to me why when I click on links I am taken to the top, “scroll: :preserve” does not work #575
Comments
@nikolaokonesh Did you figure this issue out? |
For scroll preservation to behave like you're describing, the navigation needs to:
This dovetails with Morphing and form redirects, as well as Do your navigations match that criteria? |
|
Does |
The link_to worked as expected with the appropriate data attribute. Thank-you. A |
That depends on where the submitted form redirects to. Is it back to the same page, or to a different URL? |
The If I do a |
I think I'm seeing the same issue with Controller: Regardless of whether the referrer is there, the fallback is the same URL I'm already on. turbo-rails 2.0.4 and @hotwired/turbo 8.0.3. I'm not quite sure I understand the criteria in #575 (comment) so please let me know if I'm missing something. |
I've done my best to reproduce the behavior you've all described above, but I'm having trouble. I've created the following reproduction script: require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails"
gem "propshaft"
gem "sqlite3"
gem "turbo-rails"
gem "capybara"
gem "cuprite", "~> 0.9", require: "capybara/cuprite"
end
require "active_record/railtie"
# require "active_storage/engine"
require "action_controller/railtie"
require "action_view/railtie"
# require "action_mailer/railtie"
# require "active_job/railtie"
# require "action_cable/engine"
# require "action_mailbox/engine"
# require "action_text/engine"
require "rails/test_unit/railtie"
class App < Rails::Application
config.load_defaults Rails::VERSION::STRING.to_f
config.root = __dir__
config.hosts << "example.org"
config.eager_load = false
config.session_store :cookie_store, key: "cookie_store_key"
config.secret_key_base = "secret_key_base"
config.consider_all_requests_local = true
config.turbo.draw_routes = false
Rails.logger = config.logger = Logger.new($stdout)
routes.append do
post "/" => "application#create"
root to: "application#index"
end
end
$template = DATA.read
class ApplicationController < ActionController::Base
include Rails.application.routes.url_helpers
def index
render inline: $template, formats: :html
end
def create
redirect_to root_url(count: params[:count].to_i + 1)
end
end
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :cuprite, using: :chrome, screen_size: [1400, 1400], options: { js_errors: true }
end
Capybara.configure do |config|
config.server = :webrick
config.default_normalize_ws = true
end
ENV["DATABASE_URL"] = "sqlite3::memory:"
ENV["RAILS_ENV"] ||= "test"
Rails.application.initialize!
require "rails/test_help"
class TurboSystemTest < ApplicationSystemTestCase
test "reproduces bug" do
visit root_path
scroll_to find_button("Morph")
assert_scroll_preserved do
click_button "Morph"
assert_text "Count 1"
end
assert_scroll_preserved do
click_button "Morph"
assert_text "Count 2"
end
assert_scroll_preserved do
click_button "Morph"
assert_text "Count 3"
end
end
def assert_scroll_preserved(&block)
assert_no_changes -> { evaluate_script("window.scrollY") }, &block
end
end
__END__
<html>
<head>
<script type="importmap">
{
"imports": {
"@hotwired/turbo-rails": "<%= asset_path("turbo.js") %>"
}
}
</script>
<script type="module">
import "@hotwired/turbo-rails"
</script>
<%= turbo_refreshes_with method: :morph, scroll: :preserve %>
<%= yield :head %>
</head>
<body>
<p style="margin-bottom: 100vh;">Count <%= params.fetch(:count, 0) %><p>
<%= button_to "Morph", root_path, method: "post",
data: {turbo_action: "replace"},
params: {count: params.fetch(:count, 0)} %>
</body>
</html> Could you each:
|
@aantix
This is just an example, There are doubts whether this is correct:
I should have read the Turbo.Drive documentation carefully My English is not that good =) |
My problem was that I had both Thanks for the help and the handy reproduction script. That will be useful in the future! |
It appears I have the same issue. Turbo 7.3.0 is fine, Turbo 8 Beta 1 up to latest ain't. I'm having this issue with turbo_streams. Basically I make an AJAX calls and I render that turbo stream response with Turbo.renderStreamMessage(response...)
Response is a simple turbo_stream that replaces an element:
It always scroll back to top. It seems to ignore whatever config I use with
|
@james-em thank you for sharing more details. Could you try and reproduce the behavior with the bug report template proposed in https://github.com/hotwired/turbo-rails/pull/593/files#diff-08735e4e085e5b9ef4c3d78d3bf64c3ed9cf68365fc9d19f9c627036bbb773b4? |
After further debugging it appears it happens when there are duplicated IDs in the page, more specifically when the element triggering the event is the one being duplicated. Here is a reproductible exemple : https://pastebin.com/ui0zzDZr (Simply run it with PS: An empty Turbo.renderStreamMessage("") can trigger the error just fine. While it ain't really a bug, it definitely is a breaking change that could potentially bring head aches to some people who aren't aware they have duplicated IDs. Here was our line of code in Rails:
All the checkboxes had "process" for an ID Edit: hotwired/turbo#1226 |
Guys, explain to me why when I click on links I am taken to the top, “scroll: :preserve” does not work
turbo-rails v2.0.2
rails 7.1.3
worked well in --pre versions
after update not work
appication.html.erb
importmap.rb
please help!
The text was updated successfully, but these errors were encountered: