Rails应用程序经常使用AJAX来呈现动态的javascript视图。例如。 update.js.erb
。视图的内容是简单的jQuery,用于交换页面元素和内联呈现的部分。
问题是部分渲染似乎比通过js请求时要长10倍。当在HTML格式的应用程序中的其他地方使用时,它们的呈现速度与预期一样快。
在用卷曲测试时,我终于找到了什么"导致"放缓。或者更确切地说,添加" .js"到URL的末尾修复了减速 - 部分以JS格式渲染,就像在HTML格式中一样快。为什么?无论哪种方式,请求和响应都是一样的; Rails正在渲染JS。只有URL中的.js会产生神秘的差异。
Accept: text/javascript
标头没有任何区别,也没有任何其他标头浏览器发送。为什么Javascript请求上没有.js会让它变得如此之慢?没有.js URL / Accept标题做同样的事情;告诉Rails格式。为什么Rails会做一些与众不同的事情呢?
我们使用不会生成.js的网址生成器,而我不想手动将format: :js
添加到所有网站。{我已经敲了几天头,所以任何帮助都会非常感激!
我做了一个简单的复制,没有数据库查询只是简单的视图:https://github.com/sfcgeorge/url-format-speed
日志的相关部分:
curl 'http://localhost:3000/test_nested' -H 'Accept: */*;q=0.5, text/javascript, application/javascript, application/ecmascript, application/x-ecmascript'
Started GET "/test_nested" for 127.0.0.1 at 2017-09-01 16:38:23 +0100
Processing by TestsController#test_nested as JS
Rendering tests/test_nested.js.erb
Rendered tests/_partial.html.erb (0.3ms)
Rendered tests/_partial.html.erb (0.0ms)
Rendered tests/_partial.html.erb (0.0ms)
Rendered tests/_partial.html.erb (0.0ms)
Rendered tests/_partial.html.erb (0.1ms)
Rendered tests/_partial.html.erb (0.8ms)
Rendered tests/_partial.html.erb (0.0ms)
Rendered tests/_partial.html.erb (0.0ms)
Rendered tests/_partial.html.erb (0.1ms)
Rendered tests/_partial.html.erb (0.1ms)
Rendered tests/test_nested.js.erb (129.4ms)
Completed 200 OK in 160ms (Views: 145.3ms)
^看看它有多缓慢?
curl 'http://localhost:3000/test_nested.js' -H 'Accept: */*;q=0.5, text/javascript, application/javascript, application/ecmascript, application/x-ecmascript'
Started GET "/test_nested.js" for 127.0.0.1 at 2017-09-01 16:39:11 +0100
Processing by TestsController#test_nested as JS
Rendering tests/test_nested.js.erb
Rendered tests/_partial.html.erb (0.3ms)
Rendered tests/_partial.html.erb (0.0ms)
Rendered tests/_partial.html.erb (0.0ms)
Rendered tests/_partial.html.erb (0.0ms)
Rendered tests/_partial.html.erb (0.0ms)
Rendered tests/_partial.html.erb (0.0ms)
Rendered tests/_partial.html.erb (0.0ms)
Rendered tests/_partial.html.erb (0.0ms)
Rendered tests/_partial.html.erb (0.0ms)
Rendered tests/_partial.html.erb (0.1ms)
Rendered tests/test_nested.js.erb (17.2ms)
Completed 200 OK in 24ms (Views: 22.0ms)
我向Rails提出了一个问题,因为在找到最小的复制后,我怀疑它是一个错误。 https://github.com/rails/rails/issues/30502