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

Assets:precompile will not change asset hash when image has changed #14638

Closed
joallard opened this issue Apr 7, 2014 · 15 comments
Closed

Assets:precompile will not change asset hash when image has changed #14638

joallard opened this issue Apr 7, 2014 · 15 comments
Assignees

Comments

@joallard
Copy link

joallard commented Apr 7, 2014

I have a project where I reference an image from my Sass. On precompile, a hash is added to the image, and referenced in the CSS.

However, when I change the image, the new intelligent compilation process will detect that the Sass has not changed, and hence not recompile it. So, with my image changed, the old hash/image will appear and still be there.

As a workaround, I add a phony CSS class to force recompilation.

@rafaelfranca
Copy link
Member

Are you using CSS, sass or less?

@joallard
Copy link
Author

joallard commented Apr 7, 2014

Sass, sorry.

@rafaelfranca
Copy link
Member

Is this sass using ERB too? Could you provide an example application reproducing this issue?

Thanks.

@joallard
Copy link
Author

joallard commented Apr 7, 2014

No ERB. I use the image-url function.

Here is the app: joallard/rails14638

rails14638 jon$ git st
On branch master
 M app/assets/images/foo.jpg
rails14638 jon$ RAILS_ENV=production brake assets:precompile
I, [2014-04-07T17:42:29.439623 #13494]  INFO -- : Writing /Users/jon/dev-tmp/rails14638/public/assets/foo-9b5273a5e80b9b63bff6d0eff048f5e4.jpg
rails14638 jon$ cat public/assets/application-35ead9e631979732f2d92b55603bd88f.css
body{background:url(/assets/foo-4df846b0982625418df5c457fcef364e.jpg)}

@rafaelfranca
Copy link
Member

Do you have only one application-*.css inside your assets folder?

@joallard
Copy link
Author

joallard commented Apr 9, 2014

I think so, I normally use the Heroku build process. I'm not 100% sure it calls assets:clean though

@rafaelfranca
Copy link
Member

@joallard maybe you are reading the wrong application.css and you already have other with the same content and with the right image digest. This is why sprockets doesn't precompile the css file again. Could you check?

@joallard
Copy link
Author

joallard commented Apr 9, 2014

From what I gather, a new CSS isn't made. Here are my reproduction steps using my example repo from master:

rails14638 jon$ git lol
* e4baa48 (HEAD, origin/master, master) new image
* 518f15a init
rails14638 jon$ git co @^ 
Note: checking out '@^'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at 518f15a... init
rails14638 jon$ RAILS_ENV=production brake assets:precompile
rails14638 jon$ git st
HEAD detached at 518f15a
 M public/assets/manifest-2d4d52624adac4603b6d41d5987ef8cd.json
rails14638 jon$ git co e4baa -- app/assets/images/foo.jpg
rails14638 jon$ git st
HEAD detached at 518f15a
M  app/assets/images/foo.jpg
 M public/assets/manifest-2d4d52624adac4603b6d41d5987ef8cd.json
rails14638 jon$ RAILS_ENV=production brake assets:precompile
I, [2014-04-09T11:03:23.065308 #2695]  INFO -- : Writing /Users/jon/dev-tmp/rails14638/public/assets/foo-9b5273a5e80b9b63bff6d0eff048f5e4.jpg
rails14638 jon$ ls public/assets/
application-35ead9e631979732f2d92b55603bd88f.css     application-50fb4c81b1f4ba7d7420eae678e0cd64.css.gz  foo-4df846b0982625418df5c457fcef364e.jpg             
application-35ead9e631979732f2d92b55603bd88f.css.gz  application-c8d048bf2b32f85ef4807549fa44b21b.js      foo-9b5273a5e80b9b63bff6d0eff048f5e4.jpg             
application-41de2112d184f9c9d62feb1a571daf91.js.gz   application-c8d048bf2b32f85ef4807549fa44b21b.js.gz   manifest-2d4d52624adac4603b6d41d5987ef8cd.json       
rails14638 jon$ git st
HEAD detached at 518f15a
M  app/assets/images/foo.jpg
 M public/assets/manifest-2d4d52624adac4603b6d41d5987ef8cd.json
?? public/assets/foo-9b5273a5e80b9b63bff6d0eff048f5e4.jpg

@rafaelfranca
Copy link
Member

I could reproduce your issue but not if I upgrade your application to use Rails 4.1 and sprockets-rails 2.1.2.

What sprockets-rails 2.1.2 basically does is add dependency on the assets used with asset_path.

Here is my reproduction steps:

[2.1.1 rails14638 ((518f15a...))]$ RAILS_ENV=production be rake assets:precompile
[2.1.1 rails14638 ((518f15a...) *)]$ gs
 M app/assets/stylesheets/other.sass
 M public/assets/manifest-2d4d52624adac4603b6d41d5987ef8cd.json
g[2.1.1 rails14638 ((518f15a...) *)]$ gd
diff --git a/app/assets/stylesheets/other.sass b/app/assets/stylesheets/other.sass
index 6f80c92..573f004 100644
--- a/app/assets/stylesheets/other.sass
+++ b/app/assets/stylesheets/other.sass
@@ -1,2 +1,5 @@
+//= depend_on_asset 'foo.jpg'
+//= depend_on 'foo.jpg'
+
 body
  background: image-url('foo.jpg')
diff --git a/public/assets/manifest-2d4d52624adac4603b6d41d5987ef8cd.json b/public/assets/manifest-2d4d52624adac4603b6d41d5987ef8cd.json
index db1e22d..76232cb 100644
--- a/public/assets/manifest-2d4d52624adac4603b6d41d5987ef8cd.json
+++ b/public/assets/manifest-2d4d52624adac4603b6d41d5987ef8cd.json
@@ -1 +1 @@
-{"files":{"foo-4df846b0982625418df5c457fcef364e.jpg":{"logical_path":"foo.jpg","mtime":"2014-04-07T17:38:49-04:00","size":9294,"digest":"4df846b0982625418df5
\ No newline at end of file
+{"files":{"foo-4df846b0982625418df5c457fcef364e.jpg":{"logical_path":"foo.jpg","mtime":"2014-04-09T13:20:41-03:00","size":9294,"digest":"4df846b0982625418df5
\ No newline at end of file
[2.1.1 rails14638 ((518f15a...) *)]$ git co e4baa -- app/assets/images/foo.jpg
[2.1.1 rails14638 ((518f15a...) *+)]$ RAILS_ENV=production be rake assets:precompile
I, [2014-04-09T13:21:32.486469 #85091]  INFO -- : Writing /Users/rafaelmfranca/Projects/github/rails14638/public/assets/foo-9b5273a5e80b9b63bff6d0eff048f5e4.jpg
I, [2014-04-09T13:21:32.549112 #85091]  INFO -- : Writing /Users/rafaelmfranca/Projects/github/rails14638/public/assets/application-bc1b8468f5d246c9ceb7e623f64e065e.css

@joallard
Copy link
Author

joallard commented Apr 9, 2014

Yup, looks like the update fixes it. Thanks for the followup.

@joallard joallard closed this as completed Apr 9, 2014
@joallard
Copy link
Author

joallard commented Apr 9, 2014

But wait, do you need to add depend_on for it to work? I'm thinking image_url should already do that. (Or was it added automatically?)

@rafaelfranca
Copy link
Member

No, you should not need. This is fixed on sprocket-rails 2.1.2 (Rails 4 doesn't work with it yet but I'm going to change this soon)

@joallard
Copy link
Author

joallard commented Apr 9, 2014

Excellent.

@rafaelfranca
Copy link
Member

@joallard 4-0-stable branch should work with sprockets-rails 2.1.2 now. But I still need to release sprockets-rails 2.1.2 😄

@joallard
Copy link
Author

joallard commented Apr 9, 2014

No problem, it's a pretty minor problem, I just wanted you folks to be aware of it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants