asset_id 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/README.textile +63 -14
  2. data/lib/asset_id.rb +2 -1
  3. metadata +3 -3
data/README.textile CHANGED
@@ -1,36 +1,85 @@
1
- Asset ID
1
+ h1. Asset ID - Asset stamping and uploading to S3 for Rails
2
2
 
3
- Only works with Rails 3 +
3
+ h2. About
4
4
 
5
5
  Uploads static assets to Amazon S3 with unique identifiers encoded into the path of the asset.
6
6
 
7
- - Assets served from a cookie-less domain
8
- - Unique identifier is not encoded into a query parameter (so it is cacheable by proxy servers)
9
- - All assets have far-future expires headers for caching
10
- - Assets have the Cache-Control: public header for caching
11
- - CSS and javascript is GZipped
7
+ Only works with Rails 3.x because Rails 3 makes doing this sort of thing a lot easier and
8
+ that is all I needed it for.
12
9
 
13
- config/environments/production.rb
10
+ This library takes standard Rails asset paths such as <code>/stylesheets/main.css?1288717704</code> and converts them
11
+ into <code>http://my_bucket.s3.amazonaws.com/stylesheets/main-id-95df8d9bf5237ad08df3115ee74dcb10.css</code>.
14
12
 
15
- config.action_controller.asset_host = Proc.new do |source|
13
+ It uses an MD5 hash of the file contents to produce the id so the same asset will always have the same ID.
14
+
15
+ In my quest to achieve a Google Page Speed score of 100, this library achieves the following:
16
+
17
+ * Assets served from a cookie-less domain
18
+ * Unique identifier is not encoded into a query parameter (so it is cacheable by proxy servers)
19
+ * All assets have far-future expires headers for caching
20
+ * Assets have the Cache-Control: public header for caching
21
+ * CSS and javascript is GZipped and the correct headers are added
22
+
23
+ As an added bonus, all your assets are available over <code>https://</code> as well.
24
+
25
+ h2. Usage
26
+
27
+ Add the gem to your <code>Gemfile</code>
28
+
29
+ <code>gem "asset_id"</code>
30
+
31
+ Configure <code>config/environments/production.rb</code> to use Amazon S3 as the asset host
32
+ and to use the id-stamped asset paths in helpers
33
+
34
+ <pre><code>config.action_controller.asset_host = Proc.new do |source|
16
35
  'http://my_bucket.s3.amazonaws.com'
17
36
  end
18
37
  config.action_controller.asset_path = Proc.new do |source|
19
38
  AssetID::S3.fingerprint(source)
20
39
  end
40
+ </code></pre>
41
+
42
+ Add your Amazon S3 configuration details to <code>config/asset_id.yml</code>
21
43
 
22
- lib/tasks/asset_id_tasks.rake
44
+ <pre><code>production:
45
+ access_key_id: 'MY_ACCESS_KEY'
46
+ secret_access_key: 'MY_ACCESS_SECRET'
47
+ bucket: "my_live_bucket"
48
+ </code></pre>
23
49
 
24
- namespace :asset do
50
+ Optionally create a rake task in <code>lib/tasks/asset_id_tasks.rake</code> to
51
+ perform the upload for use in your deploy scripts
52
+
53
+ <pre><code>namespace :asset do
25
54
  namespace :id do
26
55
 
27
- desc "uploads the current assets to s3 with fingerprints"
56
+ desc "uploads the current assets to s3 with stamped ids"
28
57
  task :upload do
29
- AWS::S3::DEFAULT_HOST.replace "s3-eu-west-1.amazonaws.com"
30
- AssetID::Base.asset_paths += ['favicon.png']
58
+ AWS::S3::DEFAULT_HOST.replace "s3-eu-west-1.amazonaws.com" # If using EU bucket
59
+ AssetID::Base.asset_paths += ['favicon.png'] # Configure additional asset paths
31
60
  AssetID::S3.upload
32
61
  end
33
62
 
34
63
  end
35
64
  end
65
+ </code></pre>
66
+
67
+ h2. SSL configuration
68
+
69
+ If you want to use the SSL host in your configuration you can do so in <code>config/environments/production.rb</code>
70
+
71
+ <pre><code>config.action_controller.asset_host = Proc.new do |source|
72
+ request.ssl? 'https://my_bucket.s3.amazonaws.com' : 'http://my_bucket.s3.amazonaws.com'
73
+ end
74
+ </code></pre>
75
+
76
+ h2. Using Amazon CloudFront Content Delivery Network (CDN)
77
+
78
+ To use Amazon CloudFront as a CDN for your assets all you need to do is "create a CloudFront distribution":http://docs.amazonwebservices.com/AmazonCloudFront/latest/GettingStartedGuide/ for the bucket you have defined your configuration and then substitute your unique CloudFront domain name in <code>config/environments/production.rb</code>
79
+
80
+ <pre><code>config.action_controller.asset_host = Proc.new do |source|
81
+ 'http://my_domain.d374pjuyllr15e.cloudfront.net'
82
+ end
83
+ </code></pre>
36
84
 
85
+ SSL works fine here as well.
data/lib/asset_id.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'digest/md5'
2
2
  require 'mime/types'
3
3
  require 'aws/s3'
4
+ require 'time'
4
5
 
5
6
  module AssetID
6
7
 
@@ -81,7 +82,7 @@ module AssetID
81
82
  end
82
83
 
83
84
  def self.cache_headers
84
- {'Expires' => 1.year.from_now.httpdate, 'Cache-Control' => 'public'}
85
+ {'Expires' => (Time.now + (60*60*24*365)).httpdate, 'Cache-Control' => 'public'} # 1 year expiry
85
86
  end
86
87
 
87
88
  def self.gzip_headers
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: asset_id
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Richard Taylor