Our app makes API calls that tend to take a few seconds. The results come back in various turbo frames that are updated using turbo streams from the controllers. When the user accesses a page that fires off an API call in the controller, and then navigates to a different page while the API call is ongoing, the resulting turbo stream renders as plain text
def data
response = read_lan_status_response('LanStatus', 2, lan_status_params)
stream = response.success? ? '' : turbo_flash_message(type: :danger, message: response.message)
stream = (stream || '') + turbo_stream.update(
'lan_status',
partial: 'ip/dashboard/lan_status',
locals: { data: response.result[:body] }
)
render turbo_stream: stream
The above controller action involves an API call that typically takes a few seconds, the result is displayed in a lan_status partial that is used to update a lan_status turbo frame on the page that fired triggered the controller action, in this case the dashboard page:
= turbo_frame_tag 'lan_status'
= render 'lan_status'
When the user navigates to a different page in the app while the controller action is ongoing, the request will still come back and render as a string that looks like a turbo stream:
<turbo-stream action="update" target="lan_status"><template> ... </template></turbo-stream>
I want it to simply ignore these turbo streams if the user has navigated to a different page. I have tried creating hidden turbo frames with the same target names on the other pages to "catch" the incoming streams but this didn't work (and in any case, the problem still seems to happen if the user navigates to a different page, and then back to the original page, so the issue is not the presence or absence of the turbo frame target, I think). Is there an obvious solution to this that I'm not seeing?