Skip to content
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

Keep flash in Turbo Frame requests #699

Merged
merged 1 commit into from
Nov 7, 2024
Merged

Conversation

p8
Copy link
Contributor

@p8 p8 commented Nov 7, 2024

A POST request can set the flash and return a redirect response. If a Turbo-Frame request that gets made before the redirect location gets called, the flash gets discared by the Turbo-Frame request.

By making sure a Turbo-Frame request keeps the flash, we can avoid this problem.

A POST request can set the flash and return a redirect response.
If a Turbo-Frame request that gets made before the redirect location
gets called, the flash gets discared by the Turbo-Frame request.

By making sure a Turbo-Frame request keeps the flash, we can avoid this
problem.
Comment on lines +39 to +51
message = Message.create!

post messages_path
assert_equal @request.flash[:notice], 'Message was successfully created.'

get messages_path, headers: { "Turbo-Frame" => "true" }
assert_equal @request.flash[:notice], 'Message was successfully created.'

get messages_path
assert_equal @request.flash[:notice], 'Message was successfully created.'

get messages_path
assert_nil @request.flash[:notice]
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it necessary to call Message.create!? Is the @request instance variable accessible as request?

Suggested change
message = Message.create!
post messages_path
assert_equal @request.flash[:notice], 'Message was successfully created.'
get messages_path, headers: { "Turbo-Frame" => "true" }
assert_equal @request.flash[:notice], 'Message was successfully created.'
get messages_path
assert_equal @request.flash[:notice], 'Message was successfully created.'
get messages_path
assert_nil @request.flash[:notice]
post messages_path
assert_equal request.flash[:notice], "Message was successfully created."
get messages_path, headers: { "Turbo-Frame" => "true" }
assert_equal request.flash.notice, "Message was successfully created."
get messages_path
assert_equal request.flash.notice, "Message was successfully created."
get messages_path
assert_nil request.flash.notice

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, the message was required in my initial change as I was following the redirect to message_url(id: 1).
In the current implementation it isn't required. I think this could use a little clean up.

@seanpdoyle
Copy link
Contributor

@jorgemanrubia @brunoprietog are either of you able to enable CI to execute for this PR?

Copy link
Member

@jorgemanrubia jorgemanrubia left a comment

Choose a reason for hiding this comment

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

Good one! Thanks @p8.

@jorgemanrubia jorgemanrubia merged commit 0e42702 into hotwired:main Nov 7, 2024
22 checks passed
@p8 p8 deleted the flash-keep branch November 8, 2024 10:04
@p8
Copy link
Contributor Author

p8 commented Nov 8, 2024

Thanks @jorgemanrubia and @seanpdoyle !

@brendon
Copy link

brendon commented Nov 12, 2024

Just wondering, but what if a turbo frame response renders the flash messages? Would this not then keep the messages and display them again (potentially) on another subsequent unrelated request?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants