rstacruz / sinatra-assetpack

Package your assets transparently in Sinatra.
http://ricostacruz.com/sinatra-assetpack/
MIT License
542 stars 97 forks source link

Routes causing the gem to fail? #171

Closed kevinkurpe closed 10 years ago

kevinkurpe commented 10 years ago

Certain routes in my sinatra app cause the sinatra assetpack gem to malfunction.

The gem plays nicely with this route:

# article view
get '/:url' do
  @article = Article.find_by_url(params[:url])
  if @article == nil
    flash[:error] = "Oops! That article doesn't exist."
    redirect "/startup-marketing-advice"
  else
    erb :article
  end
end
$ foreman start
03:03:00 web.1  | started with pid 5577
03:03:01 web.1  | WARNING: Nokogiri was built against LibXML version 2.8.0, but has dynamically loaded 2.7.8
03:03:18 web.1  | 127.0.0.1 - - [14/May/2014 03:03:18] "GET /54321-the-articles-url HTTP/1.1" 200 9846 0.0462
03:03:18 web.1  | 127.0.0.1 - - [14/May/2014 03:03:18] "GET /startups/stylesheets/foundation.css HTTP/1.1" 304 - 0.0019
03:03:18 web.1  | 127.0.0.1 - - [14/May/2014 03:03:18] "GET /startups/stylesheets/normalize.css HTTP/1.1" 304 - 0.0022
03:03:18 web.1  | 127.0.0.1 - - [14/May/2014 03:03:18] "GET /startups/stylesheets/responsive-nav.css HTTP/1.1" 304 - 0.0022
03:03:18 web.1  | 127.0.0.1 - - [14/May/2014 03:03:18] "GET /startups/stylesheets/idangerous.swiper.css HTTP/1.1" 304 - 0.0020
03:03:18 web.1  | 127.0.0.1 - - [14/May/2014 03:03:18] "GET /startups/stylesheets/override.css HTTP/1.1" 304 - 0.0022
03:03:18 web.1  | 127.0.0.1 - - [14/May/2014 03:03:18] "GET /startups/javascripts/vendor/jquery.js HTTP/1.1" 304 - 0.0023
03:03:18 web.1  | 127.0.0.1 - - [14/May/2014 03:03:18] "GET /startups/javascripts/foundation.min.js HTTP/1.1" 304 - 0.0021
03:03:18 web.1  | 127.0.0.1 - - [14/May/2014 03:03:18] "GET /startups/javascripts/foundation/foundation.topbar.js HTTP/1.1" 304 - 0.0022
03:03:18 web.1  | 127.0.0.1 - - [14/May/2014 03:03:18] "GET /startups/javascripts/foundation/foundation.abide.js HTTP/1.1" 304 - 0.0025
03:03:18 web.1  | 127.0.0.1 - - [14/May/2014 03:03:18] "GET /startups/javascripts/idangerous.swiper-2.4.min.js HTTP/1.1" 304 - 0.0022
03:03:18 web.1  | 127.0.0.1 - - [14/May/2014 03:03:18] "GET /startups/javascripts/responsive-nav.js HTTP/1.1" 304 - 0.0023
03:03:18 web.1  | 127.0.0.1 - - [14/May/2014 03:03:18] "GET /startups/javascripts/vendor/modernizr.js HTTP/1.1" 304 - 0.0023
03:03:18 web.1  | 127.0.0.1 - - [14/May/2014 03:03:18] "GET /images/newsletter.svg HTTP/1.1" 304 - 0.0047
03:03:18 web.1  | 127.0.0.1 - - [14/May/2014 03:03:18] "GET /images/write-already-book.png HTTP/1.1" 304 - 0.0033
03:03:18 web.1  | 127.0.0.1 - - [14/May/2014 03:03:18] "GET /images/map.svg HTTP/1.1" 304 - 0.0029
03:03:18 web.1  | 127.0.0.1 - - [14/May/2014 03:03:18] "GET /images/logo.svg HTTP/1.1" 304 - 0.0035
03:03:18 web.1  | 127.0.0.1 - - [14/May/2014 03:03:18] "GET /images/rss-icon.png HTTP/1.1" 304 - 0.0038
03:03:19 web.1  | 127.0.0.1 - - [14/May/2014 03:03:19] "GET /images/hamburger.gif HTTP/1.1" 304 - 0.0028

^CSIGINT received
03:03:25 system | sending SIGTERM to all processes
03:03:25 web.1  | exited with code 0

The gem does not play nicely with this route:

# article view
get '/startup-marketing-advice/:url' do
  @article = Article.find_by_url(params[:url])
  if @article == nil
    flash[:error] = "Oops! That article doesn't exist."
    redirect "/startup-marketing-advice"
  else
    erb :article
  end
end
$ foreman start
03:03:45 web.1  | started with pid 5597
03:03:46 web.1  | WARNING: Nokogiri was built against LibXML version 2.8.0, but has dynamically loaded 2.7.8
03:03:50 web.1  | 127.0.0.1 - - [14/May/2014 03:03:50] "GET /startup-marketing-advice/54321-the-articles-url HTTP/1.1" 200 9846 0.0460
03:03:50 web.1  | 127.0.0.1 - - [14/May/2014 03:03:50] "GET /startup-marketing-advice/startups/stylesheets/foundation.css HTTP/1.1" 404 5923 0.0140
03:03:50 web.1  | 127.0.0.1 - - [14/May/2014 03:03:50] "GET /startup-marketing-advice/startups/stylesheets/normalize.css HTTP/1.1" 404 5923 0.0126
03:03:50 web.1  | 127.0.0.1 - - [14/May/2014 03:03:50] "GET /startup-marketing-advice/startups/stylesheets/responsive-nav.css HTTP/1.1" 404 5923 0.0077
03:03:50 web.1  | 127.0.0.1 - - [14/May/2014 03:03:50] "GET /startup-marketing-advice/startups/stylesheets/idangerous.swiper.css HTTP/1.1" 404 5923 0.0075
03:03:51 web.1  | 127.0.0.1 - - [14/May/2014 03:03:51] "GET /startup-marketing-advice/startups/stylesheets/override.css HTTP/1.1" 404 5923 0.0339
03:03:51 web.1  | 127.0.0.1 - - [14/May/2014 03:03:51] "GET /startup-marketing-advice/startups/javascripts/vendor/jquery.js HTTP/1.1" 404 5923 0.0102
03:03:51 web.1  | 127.0.0.1 - - [14/May/2014 03:03:51] "GET /startup-marketing-advice/startups/javascripts/foundation.min.js HTTP/1.1" 404 5923 0.0081
03:03:51 web.1  | 127.0.0.1 - - [14/May/2014 03:03:51] "GET /startup-marketing-advice/startups/javascripts/foundation/foundation.topbar.js HTTP/1.1" 404 5923 0.0091
03:03:51 web.1  | 127.0.0.1 - - [14/May/2014 03:03:51] "GET /startup-marketing-advice/startups/javascripts/foundation/foundation.abide.js HTTP/1.1" 404 5923 0.0089
03:03:51 web.1  | 127.0.0.1 - - [14/May/2014 03:03:51] "GET /startup-marketing-advice/startups/javascripts/idangerous.swiper-2.4.min.js HTTP/1.1" 404 5923 0.0107
03:03:51 web.1  | 127.0.0.1 - - [14/May/2014 03:03:51] "GET /startup-marketing-advice/startups/javascripts/responsive-nav.js HTTP/1.1" 404 5923 0.0098
03:03:51 web.1  | 127.0.0.1 - - [14/May/2014 03:03:51] "GET /startup-marketing-advice/startups/javascripts/vendor/modernizr.js HTTP/1.1" 404 5923 0.0083

The asset configuration:

class Startups < Sinatra::Application
    set :root, File.join(File.dirname(__FILE__), '../') # You must set app root,

    register Sinatra::AssetPack

    assets do
        serve '/js',            from: 'public'  # Default
        serve '/css',           from: 'public'  # Default

        # css :application, 'assets/application.css', [
        css :application, [
            'startups/stylesheets/foundation.css',
            'startups/stylesheets/normalize.css',
            'startups/stylesheets/responsive-nav.css',
            'startups/stylesheets/idangerous.swiper.css', #Homepage Slider
            'startups/stylesheets/override.css'
        ]

        # js :application, 'assets/application.js', [
        js :application, [
            'startups/javascripts/vendor/jquery.js',
            'startups/javascripts/foundation.min.js',
            'startups/javascripts/foundation/foundation.topbar.js',
            'startups/javascripts/foundation/foundation.abide.js',
            'startups/javascripts/idangerous.swiper-2.4.min.js',
            'startups/javascripts/responsive-nav.js',
            'startups/javascripts/vendor/modernizr.js'
        ]

        js_compression  :jsmin  # :jsmin | :yui | :closure | :uglify
        css_compression :sqwish # :simple | :sass | :yui | :sqwish
    end

end

I should mention that I have been using this gem for several months now without any issue.

I recently split the views directory of my app into three directories (views/admin, views/startups and views/other).

To accomplish this I added the following to my app:

set :views, ['views/admin', 'views/other', 'views/startups']

def find_template(views, name, engine, &block)
    views.each { |v| super(v, name, engine, &block) }
end

Every other view renders correctly, with the exception of a very simpliar view.

# resource view
get '/startup-marketing-resources/:url' do
    @resource = Resource.find_by_url(params[:url])
    if @resource == nil
        flash[:error] = "Oops! That resource doesn't exist."
        redirect "/startup-marketing-resources"
    else
        erb :resource
    end
end

Also, every view uses the same :layout, which in turn calls the same :header partial.

Thoughts? Why in the world does a route cause this gem to malfunction?

j15e commented 10 years ago

human-programmer

Seems like your assets URL are relative when they should be absolute, this is you get 404 when your page is in a subfolder. You should trying setting your serve paths with a slash like this :

class Startups < Sinatra::Application
    set :root, File.join(File.dirname(__FILE__), '../') # You must set app root,

    register Sinatra::AssetPack

    assets do
        serve '/js',            from: 'public'  # Default
        serve '/css',           from: 'public'  # Default

        # css :application, 'assets/application.css', [
        css :application, [
            '/startups/stylesheets/foundation.css',
            '/startups/stylesheets/normalize.css',
            '/startups/stylesheets/responsive-nav.css',
            '/startups/stylesheets/idangerous.swiper.css', #Homepage Slider
            '/startups/stylesheets/override.css'
        ]

        # js :application, 'assets/application.js', [
        js :application, [
            '/startups/javascripts/vendor/jquery.js',
            '/startups/javascripts/foundation.min.js',
            '/startups/javascripts/foundation/foundation.topbar.js',
            '/startups/javascripts/foundation/foundation.abide.js',
            '/startups/javascripts/idangerous.swiper-2.4.min.js',
            '/startups/javascripts/responsive-nav.js',
            '/startups/javascripts/vendor/modernizr.js'
        ]

        js_compression  :jsmin  # :jsmin | :yui | :closure | :uglify
        css_compression :sqwish # :simple | :sass | :yui | :sqwish
    end

end
kevinkurpe commented 10 years ago

@j15e Thanks!

That fixed the issue I was experiencing!

I greatly appreciate it!