Skip to content

Commit

Permalink
[close #221] Favor charset from processor
Browse files Browse the repository at this point in the history
If a charset is returned by a pre-processor use it instead of automatically assigning one. This is needed so that non-gzippable assets will not accidentally be assigned a charset and gzipped later.

We're also removing a hash allocation needed for `Hash.merge!`.
  • Loading branch information
schneems committed Jan 18, 2016
1 parent 65504dd commit a4038b5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
9 changes: 9 additions & 0 deletions guides/extending_sprockets.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ Sprockets expects an array of hashes for this map. Each hash must have a `:sourc
return {data: data, map: [{ source: "original.coffee", # ... }]}
```

- charset: This key contains the mime charset for an asset

A charset is the encoding for text based assets. If you do not specify a charset then one will be automatically assigned by sprockets based on the encoding type of the contents returned in the `:data` key. Normally you want that, the only time you don't want that is if you're working with binary data, or data you don't want to be compressed. If sprockets sees a `charset` then it will think that the contents of the file are text and can be compressed via GZIP. You can avoid this by setting the field manually

```ruby
return { data: data, charset: nil }
```


WIP the format of the source map may be subject to change before 4.0 is released. Currently it takes a `:original` and `:generated` key which each hold an array of line and column numbers. Line numbers are 1 indexed column numbers are 0 indexed. The first character of a file will always be `[1,0]`.

- WIP - other metadata keys
Expand Down
9 changes: 4 additions & 5 deletions lib/sprockets/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,10 @@ def load_from_unloaded(unloaded)
})
validate_processor_result!(result)
source = result.delete(:data)
metadata = result.merge!(
charset: source.encoding.name.downcase,
digest: digest(source),
length: source.bytesize
)
metadata = result
metadata[:charset] = source.encoding.name.downcase unless metadata.key?(:charset)
metadata[:digest] = digest(source)
metadata[:length] = source.bytesize
else
dependencies << build_file_digest_uri(unloaded.filename)
metadata = {
Expand Down

0 comments on commit a4038b5

Please sign in to comment.