Serving assets is very slow in development

2019-03-31 08:55发布

I have a standart rails 3 webapp with the default asset pipeline. All of a sudden, the assets took a very long time to load (my page loads went to ~1-2secs to ~1min). The server response time (/home) is normal, but some .css and .js files are pending for very long (up to 45 seconds). The only few assets that take this long are those provided by gems (eg: modernizr-rails/vendor/assets/javascripts/modernizr.js)

For example, for modernizr.js?body=1 :

Headers:

Request URL:http://dev.sharewizz.com:3000/assets/modernizr.js?body=1
Request Method:GET
Status Code:304 Not Modified

Stats:

DNS Lookup  5.00 s
Connecting  20.07 s
Sending     0
Waiting     10 ms
Receiving   3 ms

It it a problem with sprockets ? How do I know what's wrong ?

Notes: all other browsers behave the same. Edit: Actually the problem is only with chrome, not even safari. I've tried to launch my server with rails s on port 3000 and on port 80 (no changes)

If I access http://localhost:3000/assets/modernizr.js?body=1, most of the times it is instant, sometimes it wait for very long.

3条回答
放荡不羁爱自由
2楼-- · 2019-03-31 09:09

Precompile your assets and enable asset pipelining for development env. This may however not load the changes that you make in your source code without restarting the server.

查看更多
混吃等死
3楼-- · 2019-03-31 09:19

Another option - HDD isn't fast enough for logs, just bump into this (suddenly) on my dev pc. Try to set this into config/development.rb:

# Expands the lines which load the assets
config.assets.debug = false #true

If you have a lot of assets, each one take the time to write into dev log, adding up to several minutes total.

查看更多
地球回转人心会变
4楼-- · 2019-03-31 09:19

There is an issue in chrome where the browser tries a dns request and waits for a timeout (you can see in the developer tools / network tab if you hover on the colored timelines, it says waiting (30000ms or higher), receiving (4ms).

In the case you are soing something like this: 127.0.0.1 localhost.mysite.com in hostfile, and access http://localhost.mysite.com in the browser), you might try to just access it with localhost on port 80:

  • Start rails with rvmsudo rails server -p 80
  • Access http://localhost in chrome

That might solve the issue.

查看更多
登录 后发表回答