This repository has been archived by the owner on Jul 13, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Leverage browser caching and proxy caching by setting far future Expires headers and changing filenames when file contents change. This can make your web app faster for users and also reduce your bandwidth costs. By adding the column :avatar_fingerprint to our db table and including :fingerprint in the attachment filename, we ensure the filename will change whenever the file contents do. has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :path => "users/:id/:attachment/:fingerprint-:style.:extension", :storage => :s3, :s3_headers => {'Expires' => 1.year.from_now.httpdate}, :s3_credentials => "#{RAILS_ROOT}/config/s3.yml", :include_updated_timestamp => false This enables us to set far future expire headers so that browsers don't need to check for a newer version. If a change does occur, say because a user uploads a new avatar, the new filename will be rendered in your html and the cached version will be ignored. The example above will set Expires headers in S3. If you're using local storage you can configure your webserver to do something similar. We disable the timestamped query string because some proxies refuse to cache items with query strings. For more info on optimizing for caching: http://code.google.com/speed/page-speed/docs/caching.html
- Loading branch information
Showing
6 changed files
with
54 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2cdeb39
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Somehow this commit is completely breaking image uploading for me. Maybe because I am using the S3 gem (not aws-s3, because of European bucket support) in combination with a custom Paperclip::Storage module. Although this commit doesn't make any changes in paperclip/storage/*.
2cdeb39
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I was able to find the problem. The thing is, Upfile#fingerprint looks like a safe (idempotent) method, however, it is not. The method calls IO#read, without calling IO#rewind. Thus, any Paperclip::Storage backend must make sure to call call IO#rewind before reading the contents of the file. See http://github.com/qoobaa/s3/blob/master/extra/s3_paperclip.rb#L114 for an example of where things might go wrong.
2cdeb39
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 have the same problem with the custom code which does attachment.to_file.read (expecting it to return file contents of course). It would be nice if it cleans up with something like
self.rewind