Rails is making the same get request twice (update: only when I embed pinterest links on the page)
Asked Answered
P

1

7

I have a rails 4.2.x app, wherein I notice that for each request I make, the logs show that the whole request is being sent twice - One request gets fully completed, the page seems fully rendered, but then another one starts again in the logs and runs through its course, and the second request has no visible effect in the client browser.

Started GET "/path/to/request" for 10.0.1.5 at 2016-04-12 21:23:08 +0530
<... bunch of SQL and other logs to fulfill this request...>
Completed 200 OK in 11487ms (Views: 11279.3ms | ActiveRecord: 40.3ms)

Started GET "/path/to/request" for 10.0.1.5 at 2016-04-12 21:23:21 +0530
<... same bunch of SQL and other logs ...>
Completed 200 OK in 8928ms (Views: 8899.3ms | ActiveRecord: 18.2ms)

This happens both on dev and production. It happens independent of browser

Based on suggestions on other SO threads and general hunches, I have tried:

  1. Removing all links that point to '#' and using
    'javascript:void(0)' instead
  2. Calling bundle exec rake assets:clobber
  3. In development.rb, set config.assets.prefix = '/dev-assets'
  4. Removing async: true from javascript_include_tag
  5. Tried calling from chrome incognito window
  6. Restarting browser
  7. Clearing the tmp folder with bundle exec rake tmp:clear
  8. Restarting the (dev) machine
  9. I do not have turbolinks in my Gemfile or Gemfile.lock

None of these seem to work. I am all out of options right now - any suggestions?

Update: turns out the culprit was embedded pinterest links on the page, when I remove it, the issue goes away - here is the source that caused it - how do I fix it to embed pinterest links and still not have multiple calls occur?

The url and script I put in the page for an embedded pin:

<a data-pin-do='embedPin' href='https://in.pinterest.com/pin/384143043191907516/'></a>

<script src="//assets.pinterest.com/js/pinit.js"></script>

Note: I also had async defer in the script as shown below (that is what the pinterest site recommends for embedding pinterest links), but keeping or removing them made no difference to the two request issue:

<script async defer src="//assets.pinterest.com/js/pinit.js"></script>

The page source that was generated by the script running:

<span class="PIN_1460479728283_embed_pin PIN_1460479728283_en" data-pin-log="embed_pin" data-pin-href="https://www.pinterest.com/pin/384143043191907516/" data-pin-id="384143043191907516"><span class="PIN_1460479728283_bd" data-pin-href="https://www.pinterest.com/pin/384143043191907516/" data-pin-log="embed_pin">
<span class="PIN_1460479728283_hd" data-pin-href="https://www.pinterest.com/pin/384143043191907516/" data-pin-log="embed_pin">     
<span class="PIN_1460479728283_container" data-pin-href="https://www.pinterest.com/pin/384143043191907516/" data-pin-log="embed_pin" style="padding-bottom: 154.43%;"><span class="PIN_1460479728283_img" data-pin-href="https://www.pinterest.com/pin/384143043191907516/" data-pin-log="embed_pin_img" data-pin-src="http://media-cache-ak0.pinimg.com/237x/f0/75/0b/f0750bea877d546aa4a95f52a9007cc8.jpg" style="background-image: url(&quot;http://media-cache-ak0.pinimg.com/237x/f0/75/0b/f0750bea877d546aa4a95f52a9007cc8.jpg&quot;);">
</span></span>
<span class="PIN_1460479728283_repin" data-pin-href="https://www.pinterest.com/pin/384143043191907516/" data-pin-log="embed_pin_repin" data-pin-id="384143043191907516"></span></span><span class="PIN_1460479728283_source" data-pin-href="http://guineapiggies.tumblr.com/post/62520038756/perlita-by-gabitayuz" data-pin-log="embed_pin_domain"><span class="PIN_1460479728283_img" data-pin-href="http://guineapiggies.tumblr.com/post/62520038756/perlita-by-gabitayuz" data-pin-log="embed_pin_domain" data-pin-src="http://media-cache-ec0.pinimg.com/favicons/09b6198f50d7e654ebb5f88e2016b83b0bb345386f0c4b685df4bacd.png?66c0426c45356a3c0afbd2cf5c32c99e" style="background-image: url(&quot;http://media-cache-ec0.pinimg.com/favicons/09b6198f50d7e654ebb5f88e2016b83b0bb345386f0c4b685df4bacd.png?66c0426c45356a3c0afbd2cf5c32c99e&quot;);"></span><span class="PIN_1460479728283_domain" data-pin-href="http://guineapiggies.tumblr.com/post/62520038756/perlita-by-gabitayuz" data-pin-log="embed_pin_domain">Guinea Piggies
</span>
<span class="PIN_1460479728283_menu" data-pin-href="http://guineapiggies.tumblr.com/post/62520038756/perlita-by-gabitayuz" data-pin-log="embed_pin_domain"><span class="PIN_1460479728283_toggle" data-pin-href="" data-pin-log="embed_pin_toggle"></span><span class="PIN_1460479728283_dropdown" data-pin-href="https://www.pinterest.com/about/copyright/dmca-pin/?id=384143043191907516" data-pin-log="embed_pin_report">Copyright issue
</span>
</span>
</span><span class="PIN_1460479728283_description" data-pin-href="https://www.pinterest.com/pin/384143043191907516/" data-pin-log="embed_pin">guinea pig</span><span class="PIN_1460479728283_stats" data-pin-href="https://www.pinterest.com/pin/384143043191907516/" data-pin-log="embed_pin"><span class="PIN_1460479728283_repins" data-pin-href="https://www.pinterest.com/pin/384143043191907516/repins/" data-pin-log="embed_pin">1</span></span></span><span class="PIN_1460479728283_ft" data-pin-href="https://www.pinterest.com/beblydia/" data-pin-log="embed_pin_pinner"><span class="PIN_1460479728283_img" data-pin-href="https://www.pinterest.com/beblydia/" data-pin-log="embed_pin_pinner" data-pin-src="http://media-cache-ak0.pinimg.com/avatars/beblydia_1447515585_60.jpg" style="background-image: url(&quot;http://media-cache-ak0.pinimg.com/avatars/beblydia_1447515585_60.jpg&quot;);"></span><span class="PIN_1460479728283_pinner" data-pin-href="https://www.pinterest.com/beblydia/" data-pin-log="embed_pin_pinner">Lydia Batista</span><span class="PIN_1460479728283_board" data-pin-href="https://www.pinterest.com/beblydia/guinea-pigs/" data-pin-log="embed_pin_board">Guinea pigs
</span></span></span>
Prochronism answered 12/4, 2016 at 16:6 Comment(6)
When you say for each, for every request which you are calling or only for just for one request that shows /path/to/request?Ce
@Ce Great question! It turns out, it does not happen with many other requests - it happens with a specific pathProchronism
I think you need to rewrite your question to focus on the one specific link/ajax call/whatever that is having this problem. My guess is that you have a link which has an onclick event listener, and that listener is also calling the link's url. If this is the case you need to make the listener stop the link from doing its usual action, which you can do like so (jquery) w3schools.com/jquery/event_preventdefault.aspFirth
@Prochronism Can you update your post, for that specific route /path/to/request how triggering it, controller and model, also good to see route for that url.Ce
I recently changed from $(document).ready to using document.addEventListener('DomContentLoaded') across the board (to improve perf). Based on Max William's suggestion above, I added event.preventDefault(); to the top of each such handler. No difference - I still get the error on that page, but not on other pages - am looking closely at this page script and source - will post anything that seems relevant...Prochronism
Ok, I found the culprit - I had embedded pinterest links with a script section on the page. Removing that made the problem go away. I will update the question with details above in a secProchronism
S
1

I had a similar issue before.. it was only happening in Chrome and I narrowed it down to there being an issue with turbolinks by adding

data-no-turbolink

to the body tag of my template. This stopped the issue, and specifically updating turbolinks

bundle update turbolinks

seemed to sort it all out. I hope this helps!

Supernormal answered 12/4, 2016 at 16:23 Comment(1)
Thanks, I do not have the turbolinks gem in my gemfileProchronism

© 2022 - 2024 — McMap. All rights reserved.