Middleman-minify-html: Content-Length header error

I am running ruby 1.9.3p429 (2013-05-15) [i386-mingw32] on a Windows 8 machine. Since updating to Middleman 3.1 (I am using 3.1.2) any time I run middleman build with the gem middleman-minify-html I get the following error (the values change depending on the page its trying to create):

Content-Length header was 35215, but should be 37540

Is there something I need to update or change to support version middleman 3.1 and middleman-minify-html?

Below is the full output include the error when running middleman build --verbose:

 == Activating: sprockets
 == File Change: data/defaults_cn.yml
 == File Change: data/defaults_en.yml
 == File Change: data/_partials/email_settings.xlsm
 == Reading:  Local config
 == Activating: i18n
 == Activating: livereload
 == Activating: edm_helpers
 == Activating: inline_premailer
 == Activating: inline_to_table
 == Activating: deploy_preview
 == Activating: litmus
 == Activating: sendpreview
 == Activating: minify_html
 == Activating: relative_assets
 == File Change: locales/cn.yml
 == File Change: locales/en.yml
 == File Change: locales/_partials/site_copy.xlsm
 Loaded extensions:
 == Extension: sprockets
 == Extension: i18n
 == Extension: livereload
 == Extension: edm_helpers
 == Extension: inline_premailer
 == Extension: inline_to_table
 == Extension: deploy_preview
 == Extension: litmus
 == Extension: sendpreview
 == Extension: minify_html
 == Extension: relative_assets
 == Extension: frontmatter
 == File Change: .gitattributes
 == File Change: config.rb
 == File Change: data/defaults_cn.yml
 == File Change: data/defaults_en.yml
 == File Change: data/_partials/email_settings.xlsm
 == File Change: extensions/helpers.rb
 == File Change: locales/cn.yml
 == File Change: locales/en.yml
 == File Change: locales/_partials/site_copy.xlsm
 == File Change: patches/cssparser.rb
 == File Change: patches/nokogiri.rb
 == File Change: patches/premailer_media.rb
 == File Change: README.md
 == File Change: source/layouts/layout.haml
 == File Change: source/localizable/cta_arrow.gif
 == File Change: source/localizable/herobanner_cn.jpg
 == File Change: source/localizable/herobanner_en.jpg
 == File Change: source/localizable/index.html.haml
 == File Change: source/localizable/index.txt.haml
 == File Change: source/localizable/logo_cn.gif
 == File Change: source/localizable/logo_en.gif
 == Rebuilding resource list
 == Prerendering CSS
 == Checking for Compass sprites
 == Building files
       create  build/en/herobanner_cn.jpg
    identical  build/cn/herobanner_en.jpg
    identical  build/en/herobanner_en.jpg
    identical  build/cn/herobanner_cn.jpg
    identical  build/cn/logo_cn.gif
       create  build/en/logo_cn.gif
       create  build/cn/logo_en.gif
    identical  build/en/logo_en.gif
 == Request: /cn/index.html     

 == Finishing Request: cn/index.html (0.29s)
        error  build/cn/index.html
 Content-Length header was 35215, but should be 37540
 C:/Ruby193/lib/ruby/gems/1.9.1/gems/rack-1.5.2/lib/rack/lint.rb:20:in `assert'
 C:/Ruby193/lib/ruby/gems/1.9.1/gems/rack-1.5.2/lib/rack/lint.rb:631:in `verify_c
 ontent_length'
 C:/Ruby193/lib/ruby/gems/1.9.1/gems/rack-1.5.2/lib/rack/lint.rb:655:in `each'
 C:/Ruby193/lib/ruby/gems/1.9.1/gems/rack-1.5.2/lib/rack/response.rb:36:in `initi
 alize'
 C:/Ruby193/lib/ruby/gems/1.9.1/gems/rack-1.5.2/lib/rack/mock.rb:161:in `initiali
 ze'
 C:/Ruby193/lib/ruby/gems/1.9.1/gems/rack-test-0.6.2/lib/rack/mock_session.rb:32:
 in `new'
 C:/Ruby193/lib/ruby/gems/1.9.1/gems/rack-test-0.6.2/lib/rack/mock_session.rb:32:
 in `request'
 C:/Ruby193/lib/ruby/gems/1.9.1/gems/rack-test-0.6.2/lib/rack/test.rb:230:in `pro
 cess_request'
 C:/Ruby193/lib/ruby/gems/1.9.1/gems/rack-test-0.6.2/lib/rack/test.rb:57:in `get'     

 C:/Ruby193/lib/ruby/gems/1.9.1/gems/middleman-core-3.1.2/lib/middleman-core/cli/
 build.rb:138:in `render_to_file'
 C:/Ruby193/lib/ruby/gems/1.9.1/gems/middleman-core-3.1.2/lib/middleman-core/cli/
 build.rb:280:in `block in execute!'
 C:/Ruby193/lib/ruby/gems/1.9.1/gems/middleman-core-3.1.2/lib/middleman-core/cli/
 build.rb:277:in `each'
 C:/Ruby193/lib/ruby/gems/1.9.1/gems/middleman-core-3.1.2/lib/middleman-core/cli/
 build.rb:277:in `execute!'
 C:/Ruby193/lib/ruby/gems/1.9.1/gems/middleman-core-3.1.2/lib/middleman-core/cli/
 build.rb:199:in `invoke!'
 C:/Ruby193/lib/ruby/gems/1.9.1/gems/thor-0.18.1/lib/thor/actions.rb:95:in `actio
 n'
 C:/Ruby193/lib/ruby/gems/1.9.1/gems/middleman-core-3.1.2/lib/middleman-core/cli/
 build.rb:64:in `build'
 C:/Ruby193/lib/ruby/gems/1.9.1/gems/thor-0.18.1/lib/thor/command.rb:27:in `run'
 C:/Ruby193/lib/ruby/gems/1.9.1/gems/thor-0.18.1/lib/thor/invocation.rb:120:in `i
 nvoke_command'
 C:/Ruby193/lib/ruby/gems/1.9.1/gems/thor-0.18.1/lib/thor.rb:363:in `dispatch'
 C:/Ruby193/lib/ruby/gems/1.9.1/gems/thor-0.18.1/lib/thor/base.rb:439:in `start'
 C:/Ruby193/lib/ruby/gems/1.9.1/gems/middleman-core-3.1.2/lib/middleman-core/cli.
 rb:77:in `method_missing'
 C:/Ruby193/lib/ruby/gems/1.9.1/gems/thor-0.18.1/lib/thor/command.rb:29:in `run'
 C:/Ruby193/lib/ruby/gems/1.9.1/gems/thor-0.18.1/lib/thor/command.rb:128:in `run'     

 C:/Ruby193/lib/ruby/gems/1.9.1/gems/thor-0.18.1/lib/thor/invocation.rb:120:in `i
 nvoke_command'
 C:/Ruby193/lib/ruby/gems/1.9.1/gems/thor-0.18.1/lib/thor.rb:363:in `dispatch'
 C:/Ruby193/lib/ruby/gems/1.9.1/gems/thor-0.18.1/lib/thor/base.rb:439:in `start'
 C:/Ruby193/lib/ruby/gems/1.9.1/gems/middleman-core-3.1.2/lib/middleman-core/cli.
 rb:22:in `start'
 C:/Ruby193/lib/ruby/gems/1.9.1/gems/middleman-core-3.1.2/bin/middleman:18:in `<t
 op (required)>'
 C:/Ruby193/bin/middleman:23:in `load'
 C:/Ruby193/bin/middleman:23:in `<main>'

In my config.rb I have:

activate :minify_html, remove_https_protocol: false, remove_http_protocol: false, remove_form_attributes: false, remove_link_attributes: true, remove_input_attributes: false

Gems included by the bundle:

* activesupport (3.2.12)
* addressable (2.3.4)
* bundler (1.3.1)
* chunky_png (1.2.8)
* coffee-script (2.2.0)
* coffee-script-source (1.6.2)
* compass (0.12.2)
* css_parser (1.3.4)
* dimensions (1.2.0)
* em-websocket (0.5.0)
* eventmachine (1.0.3)
* execjs (1.4.0)
* ffi (1.9.0)
* fssm (0.2.10)
* haml (4.0.3)
* hike (1.2.3)
* htmlentities (4.3.1)
* http_parser.rb (0.5.3)
* i18n (0.6.4)
* kramdown (1.0.2)
* listen (1.2.2)
* mail (2.5.4)
* middleman (3.1.2)
* middleman-core (3.1.2)
* middleman-livereload (3.1.0)
* middleman-more (3.1.2)
* middleman-sprockets (3.1.2)
* mime-types (1.23)
* mini_portile (0.5.0)
* multi_json (1.7.7)
* nokogiri (1.6.0)
* polyglot (0.3.3)
* premailer (1.7.3)
* rack (1.5.2)
* rack-livereload (0.3.15)
* rack-test (0.6.2)
* rb-fsevent (0.9.3)
* rb-inotify (0.9.0)
* rb-kqueue (0.2.0)
* sass (3.2.9)
* sprockets (2.10.0)
* sprockets-helpers (1.0.1)
* sprockets-sass (1.0.1)
* thor (0.18.1)
* tilt (1.3.7)
* treetop (1.4.14)
* uglifier (2.1.1)
* wdm (0.1.0)

I don’t see middleman-minify-html in your Gemfile, what version of that are you using? We released a new version last night, v3.1.0

Copied the wrong gemfile (updated version below). I am using middleman-minify-html (3.1.0). This version fixed the conflicts I was having with latest ‘middleman-core’ but it now creates the content-length error for each html page it tries to create:

Content-Length header was 35215, but should be 37540

Gems included by the bundle:

  * activesupport (3.2.12)
  * addressable (2.3.4)
  * bundler (1.3.1)
  * chunky_png (1.2.8)
  * coffee-script (2.2.0)
  * coffee-script-source (1.6.2)
  * compass (0.12.2)
  * css_parser (1.3.4)
  * dimensions (1.2.0)
  * em-websocket (0.5.0)
  * eventmachine (1.0.3)
  * execjs (1.4.0)
  * ffi (1.9.0)
  * fssm (0.2.10)
  * haml (4.0.3)
  * hike (1.2.3)
  * htmlentities (4.3.1)
  * http_parser.rb (0.5.3)
  * i18n (0.6.4)
  * kramdown (1.0.2)
  * listen (1.2.2)
  * mail (2.5.4)
  * middleman (3.1.2)
  * middleman-core (3.1.2)
  * middleman-livereload (3.1.0)
  * middleman-minify-html (3.1.0)
  * middleman-more (3.1.2)
  * middleman-sprockets (3.1.2)
  * mime-types (1.23)
  * mini_portile (0.5.0)
  * multi_json (1.7.7)
  * nokogiri (1.6.0)
  * polyglot (0.3.3)
  * premailer (1.7.3)
  * rack (1.5.2)
  * rack-livereload (0.3.15)
  * rack-test (0.6.2)
  * rb-fsevent (0.9.3)
  * rb-inotify (0.9.0)
  * rb-kqueue (0.2.0)
  * sass (3.2.9)
  * sprockets (2.10.0)
  * sprockets-helpers (1.0.1)
  * sprockets-sass (1.0.1)
  * thor (0.18.1)
  * tilt (1.3.7)
  * treetop (1.4.14)
  * uglifier (2.1.1)
  * wdm (0.1.0)
  * win32-open3-19 (0.0.2)

After updating middleman today from 3.0.* to 3.1.*, I encounter the same error on all minified objects (html, css and js).

GEM
  remote: https://rubygems.org/
  specs:
    activesupport (3.2.12)
      i18n (~> 0.6)
      multi_json (~> 1.0)
    builder (3.2.2)
    chunky_png (1.2.8)
    coffee-script (2.2.0)
      coffee-script-source
      execjs
    coffee-script-source (1.6.2)
    compass (0.12.2)
      chunky_png (~> 1.2)
      fssm (>= 0.2.7)
      sass (~> 3.1)
    execjs (1.4.0)
      multi_json (~> 1.0)
    favicon_maker (0.2.1)
      mini_magick (~> 3.5)
    ffi (1.9.0)
    fssm (0.2.10)
    haml (4.0.3)
      tilt
    hike (1.2.3)
    i18n (0.6.4)
    json (1.8.0)
    kramdown (1.0.2)
    libv8 (3.11.8.17)
    listen (1.2.2)
      rb-fsevent (>= 0.9.3)
      rb-inotify (>= 0.9)
      rb-kqueue (>= 0.2)
    logutils (0.6.0)
    markdown (1.1.1)
      kramdown (>= 1.0.2)
      props (>= 1.0.0)
      textutils (>= 0.6.4)
    maruku (0.6.1)
      syntax (>= 1.0.0)
    middleman (3.1.2)
      coffee-script (~> 2.2.0)
      compass (>= 0.12.2)
      execjs (~> 1.4.0)
      haml (>= 3.1.6)
      kramdown (~> 1.0.0)
      middleman-core (= 3.1.2)
      middleman-more (= 3.1.2)
      middleman-sprockets (>= 3.1.2)
      sass (>= 3.1.20)
      uglifier (~> 2.1.0)
    middleman-blog (3.2.0)
      maruku (~> 0.6.0)
      middleman-core (~> 3.0)
      tzinfo (~> 0.3.0)
    middleman-core (3.1.2)
      activesupport (~> 3.2.6)
      bundler (~> 1.1)
      i18n (~> 0.6.1)
      listen (~> 1.2.2)
      rack (>= 1.4.5)
      rack-test (~> 0.6.1)
      thor (>= 0.15.2, < 2.0)
      tilt (~> 1.3.6)
    middleman-favicon-maker (3.2)
      favicon_maker (~> 0.2.0)
      middleman-core (>= 3.0.0)
    middleman-minify-html (3.1.0)
      middleman-core (~> 3.0)
    middleman-more (3.1.2)
    middleman-sprockets (3.1.2)
      middleman-core (>= 3.0.14)
      middleman-more (>= 3.0.14)
      sprockets (~> 2.1)
      sprockets-helpers (~> 1.0.0)
      sprockets-sass (~> 1.0.0)
    middleman-syntax (1.2.1)
      middleman-core (~> 3.0)
      rouge (~> 0.3.0)
    mini_magick (3.6.0)
      subexec (~> 0.2.1)
    mini_portile (0.5.0)
    multi_json (1.7.7)
    nokogiri (1.6.0)
      mini_portile (~> 0.5.0)
    props (1.0.0)
    rack (1.5.2)
    rack-test (0.6.2)
      rack (>= 1.0)
    rb-fsevent (0.9.3)
    rb-inotify (0.9.0)
      ffi (>= 0.5.0)
    rb-kqueue (0.2.0)
      ffi (>= 0.5.0)
    ref (1.0.5)
    rouge (0.3.7)
      thor
    sass (3.2.9)
    sprockets (2.10.0)
      hike (~> 1.2)
      multi_json (~> 1.0)
      rack (~> 1.0)
      tilt (~> 1.1, != 1.3.0)
    sprockets-helpers (1.0.1)
      sprockets (~> 2.0)
    sprockets-sass (1.0.1)
      sprockets (~> 2.0)
      tilt (~> 1.1)
    subexec (0.2.3)
    syntax (1.0.0)
    textutils (0.6.6)
      logutils (~> 0.5)
    therubyracer (0.11.4)
      libv8 (~> 3.11.8.12)
      ref
    thor (0.18.1)
    tilt (1.3.7)
    tzinfo (0.3.37)
    uglifier (2.1.1)
      execjs (>= 0.3.0)
      multi_json (~> 1.0, >= 1.0.2)

PLATFORMS
  ruby

DEPENDENCIES
  builder (~> 3.0)
  json (~> 1.7)
  markdown
  middleman (~> 3.1)
  middleman-blog (~> 3.2)
  middleman-favicon-maker (~> 3.2)
  middleman-minify-html
  middleman-sprockets
  middleman-syntax
  nokogiri
  rb-inotify
  therubyracer

Commenting out the activate lines provides a temporary fix.

Can’t reproduce on ruby 2.0, what version are you guys using?

I am running ruby 1.9.3p429 (2013-05-15) [i386-mingw32] on a Windows 8 machine.

ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux] @ ubuntu 12.04 64bit.

I’m using ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-linux] and I have the exact same bug.
Note that the bug only happens if activate :minify_html is set in config.rb.

Can anyone submit a failing test case on Github that we can add to our tests?

I would be happy to do this but have never done it before. Possible to point me to some resource which shows how to do it?

Check the page for weird characters. I spent forever trying to see what it was. Turns out our SEO person put in some crap from a word document. Weird commas, dashes, spanish characters, etc.

Issue appeared after gem update and not after an article change.

Issue is due to htmlcompressor, which wrongly computes Content-Length using String#length rather than String#bytesize. It results in bad Content-Length, this is why this error appears.

I submitted PRs to both middleman-minify-html (PR #14) and to htmlcompressor (PR #9), so fortunately, it should get fixed quickly.

If you want to try before it gets merged, feel free to temporarily add the following lines to your Gemfile:

gem 'middleman-minify-html', github: 'michaelbaudino/middleman-minify-html', branch: 'feature/use-htmlcompressor-as-an-external-gem'
gem 'htmlcompressor', github: 'michaelbaudino/htmlcompressor', branch: 'feature/fix-content-length-bug'

Also, @drewhjava, it’s not “some crap”, it’s just “other languages characters”, don’t be scared :wink:

@tdreyno kindly merged my PR, so you just need to add this line to your Gemfile to fix the bug:

gem 'middleman-minify-html', github: 'middleman/middleman-minify-html'

I’ll push out a new gem Monday morning. Thanks for tracking this down.

And it’s out: v3.1.1