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?

@aaronjensen
Copy link
Contributor

@jorgemanrubia I have the same question as Brendon above. I now have to audit my entire application to see if this behavior has a negative impact on our notices.

It was just released as a patch release, but as far as I can tell this is a potentially breaking change with a pretty good question asked not too long after it was merged.

Is there a chance that this can cause breaking issues in existing applications? If so, should it be rolled back? Thanks.

@jorgemanrubia
Copy link
Member

I think you are right, this looks like a breaking change. Sorry I missed the comment above. I think we should release a patch with the revert, and look for a non-breaking way of achieving what we want here. What do you think @p8 @seanpdoyle?

@p8
Copy link
Contributor Author

p8 commented Mar 3, 2025

I'm ok with reverting it.

@aaronjensen
Copy link
Contributor

@jorgemanrubia Thank you for the quick response. A patch release with a revert sounds good to me.

Is removing gem versions from rubygems ever done for this library? I don't know if this has a significant enough impact to warrant that, but just want to mention it in case you think it is.

For what it's worth, a note in the README about flashes may be something that can be done after the reversion to let people know about the impact of turbo frame requests and flash. We have our own way of handling notices that involves rendering a turbo stream along with any response, so there are likely many ways to address this in user-space.

jorgemanrubia added a commit that referenced this pull request Mar 3, 2025
jorgemanrubia added a commit that referenced this pull request Mar 3, 2025
@jorgemanrubia
Copy link
Member

@aaronjensen
Copy link
Contributor

Thank you.

@brendon
Copy link

brendon commented Mar 3, 2025

Thanks @jorgemanrubia. Can't remember why I locked on to this issue well before release :D Must have seen it in the upcoming changelog.

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.

5 participants