question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

trubo:load gets triggered twice when GET request is a redirect 302

See original GitHub issue

Hi, is this on purpose and if so, how can I make sure that the first call doesn’t do anything?

I created a test where you can test this problem

Start there the server bundle exec rails s and open the page localhost:3000. You will see a link that points to page 1 but then gets redirected to page 2. On the first page load, you will see an alert which you then will see twice after clicking on this link.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
feliperaulcommented, Apr 2, 2022

I just hit this and I can’t think of a way to escape as it is.

Our page also contains javascript that as soon turbo:load fires, it will dispatch JS requests for analytics, counting 2 views while only one actually happened.

But that’s not all. We have pages that once turbo:load fires, dispatch JS POST requests that update the model’s lock_version (Rails’s OptimisticLocking).

Since turbo:load is firing twice, the app totally breaks because when the response’s HTML actually renders on the screen, the “ghost” render that was previously invoked by Turbo fired the JS POST requests updating the lock_version unadvertly, so the user already has a stale version in his screen.

The browser never triggers ‘DOMContentLoaded’ twice after receiving a 302 or 303 response, but Turbo is.

This has nothing to do with idempotency; the code is totally idempotent as it is.

Lastly, there’s the performance penalty: the browser is initializing a huge Vue application twice, making JS requests to fetch data, and so on. If it was only this, and they were only GET requests, it would be a minor bug, but considering the non-idempotent requests JS can make once turbo:load gets fired, this is actually a major problem on pages that can be loaded after a redirect.

0reactions
joker-777commented, Jul 19, 2022

@feliperaul Amazing!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Fix the HTTP 302 Error (5 Methods)
HTTP 302 codes are useful to temporarily redirect website users to another URL. If you're getting this error code, here are 5 ways...
Read more >
Why do I get two redirectResponses when receiving one ...
When using Curl I only get one response and tracedump tells the same, so I am sure that the server sends only one...
Read more >
302 Found - HTTP - MDN Web Docs
The HyperText Transfer Protocol (HTTP) 302 Found redirect status response code indicates that the resource requested has been temporarily ...
Read more >
Types of Redirects – Which Redirect Should You Use?
Like 301, 302 redirects, you can create a 307 redirect by selecting the 307 Temporary Redirect option from the redirection type in Rank...
Read more >
Double Redirect (HTTP 302) issued in Chrome, not IE and ...
Browser redirects back to login page without error message. "We could not process request" NOTE: IE, Firefox and other previous versions of chrome...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found