diff --git a/app/controllers/avo/actions_controller.rb b/app/controllers/avo/actions_controller.rb index a8ebd870c9..71389fe746 100644 --- a/app/controllers/avo/actions_controller.rb +++ b/app/controllers/avo/actions_controller.rb @@ -99,6 +99,10 @@ def respond # Flash the messages collected from the action flash_messages + # Always execute turbo_stream.avo_close_modal on all responses, including redirects + # Exclude response types intended to keep the modal open + # This ensures the modal frame refreshes, preventing it from retaining the SRC of the previous action + # and avoids re-triggering that SRC during back navigation respond_to do |format| format.turbo_stream do turbo_response = case @response[:type] @@ -117,11 +121,14 @@ def respond turbo_stream.turbo_frame_set_src(Avo::MODAL_FRAME_ID, src) when :redirect - turbo_stream.redirect_to( - Avo::ExecutionContext.new(target: @response[:path]).handle, - turbo_frame: @response[:redirect_args][:turbo_frame], - **@response[:redirect_args].except(:turbo_frame) - ) + [ + turbo_stream.avo_close_modal, + turbo_stream.redirect_to( + Avo::ExecutionContext.new(target: @response[:path]).handle, + turbo_frame: @response[:redirect_args][:turbo_frame], + **@response[:redirect_args].except(:turbo_frame) + ) + ] when :close_modal # Close the modal and flash the messages [ @@ -132,7 +139,10 @@ def respond # Reload the page back_path = request.referer || params[:referrer].presence || resources_path(resource: @resource) - turbo_stream.redirect_to(back_path) + [ + turbo_stream.avo_close_modal, + turbo_stream.redirect_to(back_path) + ] end responses = if @action.appended_turbo_streams.present? diff --git a/spec/dummy/app/avo/actions/test/no_confirmation_posts_redirect.rb b/spec/dummy/app/avo/actions/test/no_confirmation_posts_redirect.rb new file mode 100644 index 0000000000..4874e9645e --- /dev/null +++ b/spec/dummy/app/avo/actions/test/no_confirmation_posts_redirect.rb @@ -0,0 +1,9 @@ +class Avo::Actions::Test::NoConfirmationPostsRedirect < Avo::BaseAction + self.name = "Redirect to Posts" + self.no_confirmation = true + self.standalone = true + + def handle(**args) + redirect_to avo.resources_posts_path + end +end diff --git a/spec/dummy/app/avo/resources/user.rb b/spec/dummy/app/avo/resources/user.rb index 1e790dd3f0..cc4beaa5e0 100644 --- a/spec/dummy/app/avo/resources/user.rb +++ b/spec/dummy/app/avo/resources/user.rb @@ -72,6 +72,7 @@ def actions action Avo::Actions::Sub::DummyAction action Avo::Actions::DownloadFile, icon: "heroicons/outline/arrow-left" divider + action Avo::Actions::Test::NoConfirmationPostsRedirect action Avo::Actions::Test::NoConfirmationRedirect action Avo::Actions::Test::CloseModal action Avo::Actions::Test::DoNothing diff --git a/spec/system/avo/actions_spec.rb b/spec/system/avo/actions_spec.rb index 3f5e75ae3a..6c82a53ea9 100644 --- a/spec/system/avo/actions_spec.rb +++ b/spec/system/avo/actions_spec.rb @@ -206,6 +206,22 @@ expect(page).to have_text "hey en" end + + it "redirects to posts and don't redirect when navigating back" do + visit avo.resources_users_path + + click_on "Actions" + click_on "Redirect to Posts" + + wait_for_loaded + + expect(current_path).to eq avo.resources_posts_path + + page.go_back + wait_for_loaded + + expect(current_path).to eq avo.resources_users_path + end end