• CORS woes on Heroku

    ,

    After spending the past 4 hours attempting to solve what boiled down to a rather simple problem, I figure I’d better blog about it to save someone else the time and effort.

    If you’ve been leveraging Passenger’s new –nginx-config-template command line option to add CORS headers to static assets served from a Rails app hosted on Heroku, and the CORS headers recently disappeared under mysterious circumstances… read on.

    I’ve been using the method described here to add CORS headers to custom fonts served from a Heroku-hosted Rails app that’s proxied by Nginx which handles serving static files. I recently updated to Rails 4.2.2 and suddenly, my custom fonts (.woff and .woff2 files) no longer had CORS headers on them.

    After the aforementioned hours spent scratching my head, I discovered that the latest version of the sprockets gem is generating asset digests that are 64 chars in length, where previously they had been 32. Nginx’s default regexp for identifying requests for static assets assumes the digest will be 32 chars long, like so:

    # Rails asset pipeline support.
    location ~ "^/assets/.+-[0-9a-f]{32}\..+" {
      error_page 490 = @static_asset;
      error_page 491 = @dynamic_request;
      recursive_error_pages on;</code>
    
      if (-f $request_filename) {
        return 490;
      }
      if (!-f $request_filename) {
        return 491;
      }
    }
    

    Changing the regexp to recognize digests that are 64 chars in length immediately solved the problem:

    location ~ "^/assets/.+-[0-9a-f]{64}\..+" {
       ...
    }
    

    I had to laugh after something so stupid and silly cost me a good chunk of my Saturday to debug. But at least it’s working now. My statically served custom fonts have the correct CORS headers and Chrome and Firefox are happy again.


Need help?

I’m an independent software developer available for consulting, contract work, or training. Contact me if you’re interested.


  • Quote of the Week: William Penn

    “If man is not governed by God, he will be ruled by tyrants.” — William Penn

  • Speaking at RubyRX

    RubyRXAre you a Ruby developer working in the Triangle? Are you registered for the RubyRX conference next weekend? If not, hop over to the Terralien blog to find out why you should come, then get registered!

    The talk I’ll be giving at RubyRX is titled “Homesteading: The New Entrepreneurial Model.” Many years ago a community would come together to help a neighbor get started building a house. They’d all pitch in, knowing that when they needed help, those same friends would come to their aid as well. This model of community self-sufficiency has been recycled in the software world.

    In this talk we’ll discuss how I’ve been using the homesteading model to build products with Ruby, and how others are doing the same. You’ll get enough of a grounding to move forward on your own, or help someone else while you learn more.

    We’re going to have a great time at RubyRX. If you’re not planning on attending RailsConf or RubyConf this year, this may be your only chance to experience some expert teaching on Ruby. Best of all, it’s right in your backyard. Don’t miss it!

  • Quote of the Week: Alexis de Tocqueville

    “When after having thus successively taken each member of the community in its powerful grasp and fashioned him at will, the supreme power then extends its arm over the whole community. It covers the surface of society with a network of small complicated rules, minute and uniform, through which the most original minds and the most energetic characters cannot penetrate to rise above the crowd.

    The will of man is not shattered but softened, bent, and guided; men are seldom forced by it to act, but they are constantly restrained from acting. Such a power does not destroy, but it prevents existence; it does not tyrannize, but it compresses, enervates, extinguishes, and stupefies a people, till each nation is reduced to nothing better than a flock of timid and industrial animals, of which government is the shepherd.

    “I have always thought that servitude of the regular, quiet, and gentle kind which I have just described might be combined with some of the outward forms of freedom and that it might even establish itself under the wing of the sovereignty of the people.”

    — Alexis de Tocqueville, Democracy in America (1835)

  • Quote of the Week: Teddy Roosevelt

    “In the first place, we should insist that if the immigrant who comes here in good faith becomes an American and assimilates himself to us, he shall be treated on an exact equality with everyone else, for it is an outrage to discriminate against any such man because of creed, or birthplace, or origin. But this is predicated upon the person’s becoming in every facet an American, and nothing but an American… There can be no divided allegiance here. Any man who says he is an American, but something else also, isn’t an American at all. We have room for but one flag, the American flag… We have room for but one language here, and that is the English language… and we have room for but one sole loyalty and that is a loyalty to the American people.” — Teddy Roosevelt, 1907