Skip to content

Commit

Permalink
Merge pull request #67 from wildlyinaccurate/enable-caching-in-config…
Browse files Browse the repository at this point in the history
…-yml

Enable "cache" option in _config.yml
  • Loading branch information
wildlyinaccurate authored Jul 16, 2017
2 parents b1ac375 + aba1d58 commit c9da267
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ responsive_image:
# Whether or not to save the generated assets into the source folder.
save_to_source: false

# [Optional, Default: false]
# Cache the result of {% responsive_image %} and {% responsive_image_block %}
# tags. See the "Caching" section of the README for more information.
cache: false

# [Optional, Default: []]
# By default, only images referenced by the responsive_image and responsive_image_block
# tags are resized. Here you can set a list of paths or path globs to resize other
Expand Down Expand Up @@ -213,3 +218,28 @@ Image objects (like `original` and each object in `resized`) contain the followi
| `dirname` | String | Directory of the file relative to `base_path` (`assets/sub/dir/some-file.jpg` => `sub/dir`). |
| `filename` | String | Basename without the extension (`assets/some-file.jpg` => `some-file`). |
| `extension` | String | Extension of the file (`assets/some-file.jpg` => `jpg`). |

### Caching

You may be able to speed up the build of large sites by enabling render caching. This is usually only effective when the same image is used many times, for example a header image that is rendered in every post.

The recommended way to enable caching is on an image-by-image basis, by adding `cache: true` to the tag:

```twig
{% responsive_image path: 'assets/my-file.jpg' cache: true %}
{% responsive_image_block %}
path: assets/my-file.jpg
cache: true
{% endresponsive_image_block %}
```

You can also enable it for all images by adding `cache: true` to your `_config.yml`:

```yaml
responsive_image:
cache: true
template: _includes/responsive-image.html
sizes:
- ...
```
3 changes: 2 additions & 1 deletion lib/jekyll-responsive-image/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class Config
'sizes' => [],
'extra_images' => [],
'auto_rotate' => false,
'save_to_source' => true
'save_to_source' => true,
'cache' => false
}

def initialize(site)
Expand Down
6 changes: 3 additions & 3 deletions lib/jekyll-responsive-image/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ def initialize(site, attributes)
end

def render_responsive_image
config = Config.new(@site).to_h
use_cache = config['cache'] || @attributes['cache']
cache_key = @attributes.to_s
result = @attributes['cache'] ? RenderCache.get(cache_key) : nil
result = use_cache ? RenderCache.get(cache_key) : nil

if result.nil?
config = Config.new(@site).to_h

image = ImageProcessor.process(@attributes['path'], config)
@attributes['original'] = image[:original]
@attributes['resized'] = image[:resized]
Expand Down
2 changes: 1 addition & 1 deletion lib/jekyll-responsive-image/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Jekyll
module ResponsiveImage
VERSION = '1.3.1'.freeze
VERSION = '1.4.0'.freeze
end
end

0 comments on commit c9da267

Please sign in to comment.