Two requests after form submission redirect

Hi,

I have a case in application where I need to redirect user to page with different layout (and different CSS / JS) after form submission. That redirection works but Turbo sends two requests for new page

  1. First one is XHR
  2. Another one is for full reload - because Turbo decides that changed

The problem is that for short period of time - (1) is rendered. And then replaced by (2). Which is not noticeable by users but Capybara / Webdriver raises following error making test flaky

unknown error: unhandled inspector error: {"code":-32000,"message":"Node with given id does not belong to the document"}

I can add “data-turbo=false” to form but was just wondering - is that the only option?

Looking at this from the Capybara aspect only: this error sounds like a timing issue inside the test, which can be solved by expecting a change on the page with one of Capybara’s built-in matchers. They use a wait-retry-until mechanism under the hood, so they’ll wait until the final expected state is reached.

For example, expect the text of a flash message:

# ... submit form ...
expect(page).to have_content("Thing was successfully updated.")

Make sure there’s no within wrapped around the expectation which could hold on to DOM elements that get removed.

I know about waiting behaviour. But you see - this page is the same both times. So it might happen that between “find” and “act” page is reloaded rendering, the same DOM visually but different.

I see, so the behaviour documented here probably? Turbo Handbook

I don’t know. Waiting for aria-busy might be worth a try:

# ... submit form ...
expect(page).to_not have_selector("html[aria-busy]")

That’s a good suggestion. I’ll try that. However, I think there might still be race condition present

  • Page is rendered after XHR
  • There is no busy indicator anymore but browser starts getting page through full request
  • Before page is loaded Capybara checks for existence of some element
  • It finds that and tries to act on it
  • Page is reloaded
  • And then I might have the same issue

But that’s just theory so I’ll just check first.

1 Like