Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not respecting Relative Root URL #42

Closed
bamnet opened this issue Sep 4, 2011 · 5 comments
Closed

Not respecting Relative Root URL #42

bamnet opened this issue Sep 4, 2011 · 5 comments

Comments

@bamnet
Copy link

bamnet commented Sep 4, 2011

I'm running an application in a folder via Passenger, which I've configured via RackBaseURI. All the Rails routes and helpers respect this and correctly prepend the /folder to paths. Helpers in my sass files don't do this, and always start with /assets instead of /folder/assets.

Is there a configuration setting I'm missing somewhere?

image-url('layout/button_norm_states.png') produces url(/assets/layout/button_norm_states.png) instead of url(/folder/assets/layout/button_norm_states.png)

@nbarthelemy
Copy link

After a bit of digging around, I have found the issue. The issue is in Rails, specifically Sprockets::Helpers::RailsHelper::AssetPaths#compute_public_path. Sprockets::Helpers::RailsHelper::AssetPaths inherits from ActionView::AssetPaths and overrides a number of methods. When compute_public_path is called through the Sass::Rails::Resolver#public_path method is sass-rails, the rails sprocket helper picks up the task of resolving the asset. Sprockets::Helpers::RailsHelper::AssetPaths#compute_public_path defers to super which is ActionView::AssetPaths#compute_public_path. In this method there is a condition of has_request? on rewrite_relative_url_root as seen below:

def compute_public_path(source, dir, ext = nil, include_host = true, protocol = nil)
  ...
  source = rewrite_relative_url_root(source, relative_url_root) if has_request?
  ...
end

def relative_url_root
  config = controller.config if controller.respond_to?(:config)
  config ||= config.action_controller if config.action_controller.present?
  config ||= config
  config.relative_url_root
end

If you look at the internals of rewrite_relative_url_root it relies on a request to be present and the ability to derive it from the controller variable in order to resolve the relative url root. The issue is that when sprockets resolves these assets for sass it does not have a controller present and therefore no request.

Here is the solution that I am using to make it work for now:

    module Sass
      module Rails
        module Helpers
          protected
          def public_path(asset, kind)
            resolver = options[:custom][:resolver]
            asset_paths = resolver.context.asset_paths
            path = resolver.public_path(asset, kind.pluralize)
            if !asset_paths.send(:has_request?) && ENV['RAILS_RELATIVE_URL_ROOT']
              path = ENV['RAILS_RELATIVE_URL_ROOT'] + path
            end
            path
          end
        end
      end
    end

@guilleiguaran
Copy link
Member

Can you test with rails/rails#2977 ?

@chriseppstein
Copy link
Contributor

sass-rails delegates url computation to rails. If not fixed there yet, please open a new issue against rails.

sribalakumar pushed a commit to sribalakumar/sprockets-rails that referenced this issue Dec 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants