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

Rails Sprockets file uses local manifest for digests instead of asset host version #427

Open
iamdriz opened this issue May 29, 2018 · 0 comments

Comments

@iamdriz
Copy link

iamdriz commented May 29, 2018

In our web application built in Rails we have several clients using the same application who will have different assets that are used dependant on which subdomain is used.

To achieve this we swap out what folder is being used on the CDN like so:

config.action_controller.asset_host = Proc.new { |source, request|
  if request.subdomain.present?
    "http#{request.ssl? ? 's' : ''}://cdn.domain.com/#{request.subdomain}/"
  else
    "http#{request.ssl? ? 's' : ''}://#{request.host_with_port}/"
  end
}

Each time we create a new client we compile the assets manually using a custom build tool that uses Sprockets to build the assets the same way Rails would and then upload them to our CDN under a folder that matches the subdomain. This then allows us to have different sets of assets based purely on the subdomain.

Now this works fine except that when we update an asset the digest will change for that file but Rails will still try and load the old asset digests because the sprockets-manifest file (which is in /public/assets) e.g. .sprockets-manifest-12345.json is being loaded instead of the one that's on the CDN. Even though the asset host is different it still loads the local one.

Rails it seems doesn't care about other manifest files as the file itself only stores the filename to the fingerprinted version so even when things like the host changes it would normally be able to find the correct asset. It would seem as though Rails has been designed this way deliberately.

However we really need to get Rails to use the manifest file that is on the CDN itself rather than use the one in the public folder local to the application.

After reading the docs, it seems you can change the manifest location. We tried doing it by using the same logic as above for the manifest like so:

  config.assets.manifest = Proc.new { |source, request|
    if request.subdomain.present?
      "http#{request.ssl? ? 's' : ''}://cdn.domain.com/#{request.subdomain}/"
    else
      "http#{request.ssl? ? 's' : ''}://#{request.host_with_port}/"
    end
  }

But Rails/Sprockets is still using the local sprockets file... Any ideas why?

We've debugged the code and the manifest has definitely changed but Rails is still using the previous digests so is not using the manifest file on the CDN server.

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

1 participant