Rails似乎两次为该页面提供服务

时间:2021-05-24 01:16:04

this is what I see in my local log file when I make a single page request:

当我发出单页请求时,这就是我在本地日志文件中看到的内容:

Started GET "/" for 127.0.0.1 at Mon Apr 25 22:12:22 -0700 2011
  User Load (0.9ms)  SELECT "users".* FROM "users" WHERE ("users"."id" = 2) LIMIT 1
  Processing by PagesController#beta as HTML
Rendered pages/beta.html.erb within layouts/beta (16.2ms)
Completed 200 OK in 23ms (Views: 18.8ms | ActiveRecord: 7.7ms)


Started GET "/" for 127.0.0.1 at Mon Apr 25 22:12:23 -0700 2011
  User Load (0.6ms)  SELECT "users".* FROM "users" WHERE ("users"."id" = 2) LIMIT 1
  Processing by PagesController#beta as */*
Rendered pages/beta.html.erb within layouts/beta (17.9ms)
Completed 200 OK in 27ms (Views: 21.5ms | ActiveRecord: 6.6ms)

Anyone have any ideas what could be causing this? the Processing line is interesting?

任何人有什么想法可能导致这个?加工线很有意思?

PagesController#beta as HTML
PagesController#beta as */*

What does that mean?

那是什么意思?

5 个解决方案

#1


35  

I've seen this happen because of an img tag with a blank src attribute. Giving an img, script, link or similar tag a src or href attribute ="" will resolve to the current page. So it will try to load the page a second time as it tried to load the asset. It could also be in a CSS url() property or an AJAX call.

我见过这种情况是因为带有空白src属性的img标签。给img,脚本,链接或类似标签src或href attribute =“”将解析为当前页面。因此,当它尝试加载资源时,它会尝试第二次加载页面。它也可以在CSS url()属性或AJAX调用中。

One easy way to tell is to temporarily put something like <base href="http://example.com" /> in your <head> section. That will prefix any links on the page, which should prevent your local page from being called twice if it is that sort of problem.

一个简单的方法是暂时在部分中添加之类的内容。这将为页面上的任何链接添加前缀,如果出现这种问题,这将阻止您的本地页面被调用两次。

I've also seen some browser extensions that query the page in a separate request. I've noticed this specifically with the Web Server Notifier and Web Technology Notifier extensions for Chrome. Try an "incognito" window or a separate browser and see if you get the same result.

我还看到了一些浏览器扩展,它们在单独的请求中查询页面。我特别注意到Chrome的Web服务器通知程序和Web技术通知程序扩展。尝试“隐身”窗口或单独的浏览器,看看你是否得到相同的结果。

#2


8  

I experienced this before and the culprit was poor implementation of Turbolinks (on my end).

我以前经历过这个,罪魁祸首是Turbolinks的实施很糟糕(在我的最后)。

I had to learn the proper way of getting Turbolinks to work (I've included my learning resources at the bottom) ... watch the Ryan Bates video to see what a properly working implementation looks like by the way.

我必须学习使Turbolinks正常工作的正确方法(我已将我的学习资源包含在底部)...观看Ryan Bates视频,看看顺便说一下正常工作的实现是什么样的。

Problem/Culprit:

问题/罪魁祸首:

Is Javascript. As described on the Turbolinks Github page ...

是Javascript。如Turbolinks Github页面所述......

With Turbolinks pages will change without a full reload, so you can't rely on DOMContentLoaded or jQuery.ready() to trigger your code. Instead Turbolinks fires events on document to provide hooks into the lifecycle of the page.

使用Turbolinks页面将在没有完全重新加载的情况下更改,因此您不能依赖DOMContentLoaded或jQuery.ready()来触发代码。相反,Turbolinks会在文档上触发事件,以便为页面的生命周期提供挂钩。

Basically anything that implies 'run after DOM/page is loaded' will break/not-work-as-expected ... this will cause Turbolinks to not be effective. And since Turbolinks gracefully fails, your browser will load the page twice (check logs/console) to and hide the problem for you.

基本上暗示“在DOM /页面加载后运行”的任何内容都将打破/不按预期工作......这将导致Turbolinks无效。由于Turbolinks正常失败,您的浏览器将加载页面两次(检查日志/控制台)并为您隐藏问题。

In my case I was able to notice the above behaviour when in 'incognito mode' ... all my Javascript that depended on document/DOM/page-ready .... broke. So ...

在我的情况下,我能够在'隐身模式'时注意到上述行为......所有依赖于文档/ DOM /页面就绪的Javascript都破了。所以......

Solution:

解:

If you have anything like this in your javascript (below) ...

如果你的javascript中有这样的东西(下面)......

$(function() {
  alert("DOM is ready!");
});

$(document).ready(function() {
  alert("DOM is ready!");
});

jQuery(document).ready(function() {
  alert("DOM is ready!");
});

Change to this ...

换到这个......

$(document).on('page:change', function() {
  alert("DOM is ready!");
});

Turbolinks don't actually don't request for an entirely new page so you'll notice your Javascript only works on full page loads but not when you click from a Turbolink enabled link.

Turbolinks实际上并没有请求一个全新的页面,因此您会注意到您的Javascript仅适用于整页加载,但是当您从支持Turbolink的链接中单击时则不会。

Which means that we should rely on Turbolinks to detect what DOM ready actually is.

这意味着我们应该依靠Turbolinks来检测DOM实际上是什么。

In my opinion the current answers are (could) wrong as I have Chrome extensions, blank-hrefs and my problems are gone.

在我看来,目前的答案是(可能)错误,因为我有Chrome扩展,空白hrefs,我的问题已经消失。

Further reading:

进一步阅读:

#3


2  

We had this issue with a blank background url css, forcing a page reload with rails.

我们有一个空白的背景url css这个问题,迫使页面重新加载rails。

If the cover_url param was blank, it would force a page reload.

如果cover_url参数为空,则会强制页面重新加载。

"style" = "background: url( #{cover_url} );"

#4


1  

This behavior was occurring for me due to the SPOF-O-Matic Chrome extension. Disabling it removed the extra request. You can disable it from chrome://extensions/

由于SPOF-O-Matic Chrome扩展,我的这种行为正在发生。禁用它会删除额外的请求。你可以从chrome:// extensions /中禁用它

#5


0  

Following on from the clues above I started disabling Chrome extensions one by one. Eventually I disabled AdBlock Plus extension and this cured the problem. Strange thing is, when I re-enabled the extension it still worked. I have no idea what this might have been.

继上述线索后,我开始逐一停用Chrome扩展程序。最终我禁用了AdBlock Plus扩展,这解决了这个问题。奇怪的是,当我重新启用扩展时,它仍然有效。我不知道这可能是什么。

#1


35  

I've seen this happen because of an img tag with a blank src attribute. Giving an img, script, link or similar tag a src or href attribute ="" will resolve to the current page. So it will try to load the page a second time as it tried to load the asset. It could also be in a CSS url() property or an AJAX call.

我见过这种情况是因为带有空白src属性的img标签。给img,脚本,链接或类似标签src或href attribute =“”将解析为当前页面。因此,当它尝试加载资源时,它会尝试第二次加载页面。它也可以在CSS url()属性或AJAX调用中。

One easy way to tell is to temporarily put something like <base href="http://example.com" /> in your <head> section. That will prefix any links on the page, which should prevent your local page from being called twice if it is that sort of problem.

一个简单的方法是暂时在部分中添加之类的内容。这将为页面上的任何链接添加前缀,如果出现这种问题,这将阻止您的本地页面被调用两次。

I've also seen some browser extensions that query the page in a separate request. I've noticed this specifically with the Web Server Notifier and Web Technology Notifier extensions for Chrome. Try an "incognito" window or a separate browser and see if you get the same result.

我还看到了一些浏览器扩展,它们在单独的请求中查询页面。我特别注意到Chrome的Web服务器通知程序和Web技术通知程序扩展。尝试“隐身”窗口或单独的浏览器,看看你是否得到相同的结果。

#2


8  

I experienced this before and the culprit was poor implementation of Turbolinks (on my end).

我以前经历过这个,罪魁祸首是Turbolinks的实施很糟糕(在我的最后)。

I had to learn the proper way of getting Turbolinks to work (I've included my learning resources at the bottom) ... watch the Ryan Bates video to see what a properly working implementation looks like by the way.

我必须学习使Turbolinks正常工作的正确方法(我已将我的学习资源包含在底部)...观看Ryan Bates视频,看看顺便说一下正常工作的实现是什么样的。

Problem/Culprit:

问题/罪魁祸首:

Is Javascript. As described on the Turbolinks Github page ...

是Javascript。如Turbolinks Github页面所述......

With Turbolinks pages will change without a full reload, so you can't rely on DOMContentLoaded or jQuery.ready() to trigger your code. Instead Turbolinks fires events on document to provide hooks into the lifecycle of the page.

使用Turbolinks页面将在没有完全重新加载的情况下更改,因此您不能依赖DOMContentLoaded或jQuery.ready()来触发代码。相反,Turbolinks会在文档上触发事件,以便为页面的生命周期提供挂钩。

Basically anything that implies 'run after DOM/page is loaded' will break/not-work-as-expected ... this will cause Turbolinks to not be effective. And since Turbolinks gracefully fails, your browser will load the page twice (check logs/console) to and hide the problem for you.

基本上暗示“在DOM /页面加载后运行”的任何内容都将打破/不按预期工作......这将导致Turbolinks无效。由于Turbolinks正常失败,您的浏览器将加载页面两次(检查日志/控制台)并为您隐藏问题。

In my case I was able to notice the above behaviour when in 'incognito mode' ... all my Javascript that depended on document/DOM/page-ready .... broke. So ...

在我的情况下,我能够在'隐身模式'时注意到上述行为......所有依赖于文档/ DOM /页面就绪的Javascript都破了。所以......

Solution:

解:

If you have anything like this in your javascript (below) ...

如果你的javascript中有这样的东西(下面)......

$(function() {
  alert("DOM is ready!");
});

$(document).ready(function() {
  alert("DOM is ready!");
});

jQuery(document).ready(function() {
  alert("DOM is ready!");
});

Change to this ...

换到这个......

$(document).on('page:change', function() {
  alert("DOM is ready!");
});

Turbolinks don't actually don't request for an entirely new page so you'll notice your Javascript only works on full page loads but not when you click from a Turbolink enabled link.

Turbolinks实际上并没有请求一个全新的页面,因此您会注意到您的Javascript仅适用于整页加载,但是当您从支持Turbolink的链接中单击时则不会。

Which means that we should rely on Turbolinks to detect what DOM ready actually is.

这意味着我们应该依靠Turbolinks来检测DOM实际上是什么。

In my opinion the current answers are (could) wrong as I have Chrome extensions, blank-hrefs and my problems are gone.

在我看来,目前的答案是(可能)错误,因为我有Chrome扩展,空白hrefs,我的问题已经消失。

Further reading:

进一步阅读:

#3


2  

We had this issue with a blank background url css, forcing a page reload with rails.

我们有一个空白的背景url css这个问题,迫使页面重新加载rails。

If the cover_url param was blank, it would force a page reload.

如果cover_url参数为空,则会强制页面重新加载。

"style" = "background: url( #{cover_url} );"

#4


1  

This behavior was occurring for me due to the SPOF-O-Matic Chrome extension. Disabling it removed the extra request. You can disable it from chrome://extensions/

由于SPOF-O-Matic Chrome扩展,我的这种行为正在发生。禁用它会删除额外的请求。你可以从chrome:// extensions /中禁用它

#5


0  

Following on from the clues above I started disabling Chrome extensions one by one. Eventually I disabled AdBlock Plus extension and this cured the problem. Strange thing is, when I re-enabled the extension it still worked. I have no idea what this might have been.

继上述线索后,我开始逐一停用Chrome扩展程序。最终我禁用了AdBlock Plus扩展,这解决了这个问题。奇怪的是,当我重新启用扩展时,它仍然有效。我不知道这可能是什么。