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

Take into account when a sprite image changes and is recompiled #3

Merged
merged 1 commit into from
Jun 26, 2014

Conversation

agrobbin
Copy link
Owner

Before, the sprite wasn't correctly recompiled, but the CSS was updated, causing all kinds of hell.

before, the sprite wasn't correctly recompiled, but the CSS was updated, causing all kinds of hell
@santry
Copy link

santry commented Jun 25, 2014

This is still mostly black magic to me, but it seems like reasonable black magic.

@agrobbin
Copy link
Owner Author

For posterity, a few things need to happen for sprockets to notice that the generated sprite image has changed in the middle of a compilation.

First is removing the asset from the environment's cache:

sprockets_environment.instance_variable_get(:@assets).delete_if { |_, asset| asset.pathname == sprite_map.filename }

This removes the BundledAsset object from the @assets Hash when its pathname is the same as the sprite_map.filename.

Next is tapping into the environment's trail object, which is a Hike::Index. This object is responsible for keeping a cache of what is currently on the filesystem, including stats. This is used to determine whether an asset is fresh?, basically if the cached mtime is older than the filesystem's mtime for a particular asset. We have to clear out 2 things from the trail:

sprockets_environment.send(:trail).tap do |trail|
  trail.instance_variable_get(:@entries).delete(Spritely.directory.to_s)
  trail.instance_variable_get(:@stats).delete(sprite_map.filename.to_s)
end

We remove the entire directory from the cached set of Dir.entries as well as the sprite file itself from the cached set of stat results.

agrobbin added a commit that referenced this pull request Jun 26, 2014
Take into account when a sprite image changes and is recompiled
@agrobbin agrobbin merged commit a727ec7 into master Jun 26, 2014
@agrobbin agrobbin deleted the more-sprockets-cache-busting branch June 26, 2014 13:19
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

Successfully merging this pull request may close these issues.

2 participants