6

I have a little Sinatra app including this module:

module Sprockets
  module Helpers
    def asset_path(source)
      "/assets/#{Environment.instance.find_asset(source).digest_path}"
    end

    def sprockets
      Environment.instance.call(env)
    end
  end

  class << self
    def precompile
      dir = 'public/assets'

      FileUtils.rm_rf(dir, secure: true)
      ::Sprockets::StaticCompiler.new(Environment.instance, 'public/assets', [/\.(png|jpg)$/, /^(application|ie)\.(css|js)$/]).compile
    end
  end

  class Environment < ::Sprockets::Environment
    include Singleton

    def initialize
      super
      %w[app lib vendor].each do |dir|
        %w[images javascripts stylesheets].each do |type|
          path = File.join(root, dir, 'assets', type)
          append_path(path) if File.exist?(path)
        end
      end

      js_compressor = Uglifier.new
      css_compressor = YUI::CssCompressor.new

      context_class.instance_eval do
        include Helpers
      end
    end
  end
end

and with following route defined:

get('/assets/*') do
  sprockets # Defined in the module above
end

Everything works just great, assets are loaded and displayed properly on my local machine using pow. But on Heroku no single asset is loaded, the server just returns 404 for every asset file.

Mario Uher
  • 12,249
  • 4
  • 42
  • 68
  • Have you tried: https://github.com/jeffrydegrande/sprockets_on_heroku, there is a similar [question for rails](http://stackoverflow.com/questions/2530584/how-to-use-sprockets-rails-plugin-on-heroku) – topek Nov 15 '11 at 22:08
  • are you sure that this has something to do with sprockets? a 404 is indicating a missing resource. did you put your route somewhere it might not be recognized by heroku? is your config.ru file properly configured? – phoet Nov 15 '11 at 22:58
  • where did u get this `"/assets/#{Environment.instance.find_asset(source).digest_path}"` ??, which version of Sprokets ?? – Synxmax Nov 22 '11 at 06:35
  • Latest version of Sprockets, and `Environment.instance` is the module above defined by myself. – Mario Uher Nov 22 '11 at 07:13
  • @ream88 look into `production.rb` and change `config.serve_static_assets` to `true` , in production mode you can't serve static files – Synxmax Nov 22 '11 at 08:05
  • 1
    This is a Sinatra app, not a Rails app. ;) – Mario Uher Nov 22 '11 at 08:57

1 Answers1

4

Simplified the module and now it works! Weird...

class Assets < Sprockets::Environment
  class << self
    def instance(root = nil)
      @instance ||= new(root)
    end
  end

  def initialize(root)
    super

    %w[app lib vendor].each do |dir|
      %w[images javascripts stylesheets].each do |type|
        path = File.join(root, dir, 'assets', type)
        self.append_path(path) if File.exist?(path)
      end
    end

    self.css_compressor = YUI::CssCompressor.new
    self.js_compressor = Uglifier.new

    context_class.instance_eval do
      include Helpers
    end
  end

  def precompile
    dir = 'public/assets'

    FileUtils.rm_rf(dir, secure: true)
    Sprockets::StaticCompiler.new(self, 'public/assets', ['*']).compile
  end

  module Helpers
    def asset_path(source)
      "/assets/#{Assets.instance.find_asset(source).digest_path}"
    end
  end
end
Mario Uher
  • 12,249
  • 4
  • 42
  • 68
  • newby question...how do I use this in sinatra..where to put this code/how do I add the javascripts/css/images to assets.js and assets.css.. – coool Jan 20 '12 at 20:43