Safari 7.0 cannot load localhost

After upgrading to 10.9 and Safari 7.0, I can no longer load http://localhost:4000 in Safari. Yes, my site still loads perfect when I load it in Chrome and Firefox, so this is something new in Safari.

These are the error messages that I see in the developer window. The error message is

Failed to load resource: The network connection was lost.

Developer Window

It even looks like the server is doing its job correctly. Here's the output from the rake console. Everything is returning 200.

127.0.0.1 - - [24/Oct/2013 06:25:49] "GET / HTTP/1.1" 200 - 0.0039
127.0.0.1 - - [24/Oct/2013 06:25:49] "GET /stylesheets/screen.css HTTP/1.1" 200 - 0.0022
127.0.0.1 - - [24/Oct/2013 06:25:49] "GET /javascripts/modernizr-2.0.js HTTP/1.1" 200 - 0.0014
127.0.0.1 - - [24/Oct/2013 06:25:49] "GET /javascripts/octopress.js HTTP/1.1" 200 - 0.0018
127.0.0.1 - - [24/Oct/2013 06:25:49] "GET /javascripts/ender.js HTTP/1.1" 200 - 0.0048

Solution 1:

I believe this is a problem with WEBrick v1.3. WEBrick isn't returning the correct content length & notifies users of this:

WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true

Trying to wget the same resource will also flag an error in the network connection as per: https://github.com/imathis/octopress/issues/1395

Chrome & Firefox just display whatever content was received & don't worry about the mismatch in content length. Safari, starting with Safari 7.0 on Mavericks' 10.9 is stricter about the response & treats the mismatch as a "Failure to load resource" error. (If I had to guess they're probably trying to improve the speed so don't retry the resource and so don't get the file fully retrieved response that wget does).

If you replace WEBrick with thin, this problem should go away. To do this on a Rack-based application try:

echo gem \"thin\" >> Gemfile
bundle install
rbenv rehash # if you're using rbenv
bundle exec rackup -s thin

Solution 2:

It might be related to your local development server not sending all the bytes of one of those CSS or JS files to Safari?

It seems like Safari is not very forgiving where other browsers are. SteveLTN thinks this is might be the case in a similar sounding Octopress issue on GitHub:

I have found that the file octopress.js should be 8482 bytes, but somehow the server reported 8502 bytes to the browser ( and wget too ), which leads the browser to think that the network connection is lost when received 8482 bytes. Wget, Chrome and Firefox could probably automatically retry it, but Safari didn't somehow. Thank you.

Solution 3:

I experienced a very similar problem running a Sinatra app on Mavericks with Safari. I can confirm that installing 'thin' (gem install thin) and adding it to the app's gemfile were all that was needed to solve this.