0

I have a controller action that's being called three times per browser request. Any ideas? config/routes.rb and application_controller.rb are totally straightforward.

Code below,

config/routes.rb

Rails.application.routes.draw do
    root 'tiles#index'

+

controllers/tiles_controller.rb

class TilesController < ApplicationController
def index
  puts "this line prints three times in the console per request"
end

+

tiles/index.html.erb
<% if notice %>
  <p id="notice"><%= notice %></p>
<% end %>

<div id="tiles">
  <%= render "tile", tile: { type: "sam", id: "0" } %>
  <%= render "tile", tile: { type: "inputs", id: "1" } %>
  <%= render collection: @tiles, partial: "tile" %>
  <%= render "tile", tile: { type: "social", id: "1000" } %>
</div>

+

This is the console log:

log/development.log
Started GET "/" for 127.0.0.1 at 2017-08-31 16:56:28 -0400
Processing by TilesController#index as HTML
Started GET "/" for 127.0.0.1 at 2017-08-31 16:56:28 -0400
Processing by TilesController#index as HTML
  [1m[36mProject Load (0.5ms)[0m  [1m[34mSELECT  "projects".* FROM "projects" ORDER BY "projects"."launch_date" ASC LIMIT $1[0m  [["LIMIT", 10]]
  [1m[36mProject Load (9.1ms)[0m  [1m[34mSELECT  "projects".* FROM "projects" ORDER BY "projects"."launch_date" ASC LIMIT $1[0m  [["LIMIT", 10]]
  Rendering tiles/index.html.erb within layouts/application
  Rendered tiles/_tile_sam.html.erb (0.3ms)
  Rendered tiles/_tile.html.erb (8.0ms)
  Rendering tiles/index.html.erb within layouts/application
  Rendered tiles/_tile_sam.html.erb (0.0ms)
  Rendered tiles/_tile.html.erb (0.9ms)
  Rendered tiles/_tile_instagram.html.erb (12.6ms)  
  ...
  Rendered tiles/_tile_twitter.html.erb (0.5ms)
  Rendered collection of tiles/_tile.html.erb [48 times] (125.9ms)
  Rendered tiles/_tile_project.html.erb (0.1ms)
  Rendered tiles/_tile_social.html.erb (0.6ms)
  Rendered tiles/_tile.html.erb (2.7ms)
  Rendered tiles/index.html.erb within layouts/application (166.1ms)
  Rendered tiles/_tile_twitter.html.erb (0.6ms)
  ...
  Rendered collection of tiles/_tile.html.erb [48 times] (158.5ms)
  Rendered tiles/_tile_social.html.erb (0.1ms)
  Rendered tiles/_tile.html.erb (1.0ms)
  Rendered tiles/index.html.erb within layouts/application (165.3ms)
Completed 200 OK in 1310ms (Views: 217.1ms | ActiveRecord: 9.1ms)


Completed 200 OK in 1325ms (Views: 204.5ms | ActiveRecord: 0.5ms)

Help!

sambecker
  • 1,099
  • 12
  • 28
  • Can you show the complete request log and the _app/views/tiles/index.html.erb_ code? – Gerry Aug 31 '17 at 20:40
  • @Gerry adding now ... – sambecker Aug 31 '17 at 20:46
  • @Gerry how can I find the request log? – sambecker Aug 31 '17 at 20:48
  • inside your `log/` folder – Arup Rakshit Aug 31 '17 at 20:49
  • If it's recording two requests it's highly probable you're making two requests. Are you double-clicking a link? – tadman Aug 31 '17 at 21:03
  • @Gerry development.log added (with omissions to balance my question/code ratio) – sambecker Aug 31 '17 at 21:03
  • @tadman I tried across three browsers and seems very consistently redundant? Maybe I just need to add caching and allow all requests to be tight multiples? – sambecker Aug 31 '17 at 21:06
  • Caching seems like hiding the actual issue. A) Does this happen on a brand new Rails project? B) Does this happen when using the command-line `curl` tool? C) Is using [Pow](http://pow.cx) an option? – tadman Aug 31 '17 at 21:07
  • @tadman `curl` makes one request! Does that mean that Safari, Chrome and Firefox are all hitting the server 2-3X every time a page is requested? That seems so crazy. – sambecker Aug 31 '17 at 21:39
  • 2
    That does seem really odd, but it could be some quirk of local configuration. The browsers generally use system preferences for proxy settings, plus can have plugins (or malware!?) installed in them that make them behave oddly. I'd double check that nothing odd is in your settings. Do they make double requests for external URLs as well? Even after quitting or rebooting? – tadman Aug 31 '17 at 21:41
  • @tadman can you believe the answer? Had you ever heard of such a thing? – sambecker Sep 01 '17 at 15:13

1 Answers1

3

WOW

Finally figured this one out by removing code related to this action/view line by line. Basically, I was generating a few img tags with empty src fields. Apparently this causes many modern browsers to load a website several times, possibly in an attempt to find missing images or assuming the root URL is the image itself?

Found the behavior cited here:

https://forums.asp.net/t/1804472.aspx?Bizzare+safari+thing+two+page+loads+for+each+page+

page loads twice in Google chrome

sambecker
  • 1,099
  • 12
  • 28
  • 1
    @tadman explains why this behavior could not be replicated w/ cURL! – sambecker Sep 01 '17 at 05:25
  • Yeah, it wouldn't load associated assets. Good find! I'm surprised these calls overlapped as much as they did considering you usually need to finish rendering the page be complete before the next load is initiated, but looks like Chrome is really eager to get going. – tadman Sep 01 '17 at 16:41