Skip to content

Commit 312cbdb

Browse files
authored
Cache busting for _sass and individual files (alshedivat#1557)
Addressing alshedivat#1395 but with a different solution from alshedivat#1478, using semantic versioning based on implementation found [here](https://distresssignal.org/busting-css-cache-with-jekyll-md5-hash). Implemented a way of calculating the hash for both a single file or directory. This way we can calculate the hash for when there is a change in the `_sass` dir, for example, or in the contents of a single file. Examples of generated outputs of the plugin: ``` /al-folio/assets/css/main.css?d41d8cd98f00b204e9800998ecf8427e /al-folio/assets/js/theme.js?96d6b3e1c3604aca8b6134c7afdd5db6 /al-folio/assets/js/dark_mode.js?9b17307bb950ffa2e34be0227f53558f /al-folio/assets/js/no_defer.js?d633890033921b33e0ceb13d22340a9c /al-folio/assets/js/common.js?acdb9690d7641b2f8d40529018c71a01 /al-folio/assets/js/copy_code.js?c9d9dd48933de3831b3ee5ec9c209cac /al-folio/assets/img/prof_pic.jpg?974957d202f671e4fa6700c04e68deae ``` Signed-off-by: George Araujo <george.gcac@gmail.com>
1 parent 7238998 commit 312cbdb

File tree

7 files changed

+75
-25
lines changed

7 files changed

+75
-25
lines changed

_includes/audio.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<figure>
22

33
<audio
4-
src="{{ include.path | relative_url }}"
4+
src="{% if include.cache_bust %}{{ include.path | relative_url | bust_file_cache }}{% else %}{{ include.path | relative_url }}{% endif %}"
55
{% if include.class %}class="{{ include.class }}"{% endif %}
66
{% if include.alt %}alt="{{ include.alt }}"{% endif %}
77
{% if include.title %}title="{{ include.title }}"{% endif %}

_includes/figure.html

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,29 @@
55
<picture>
66
{% if site.imagemagick.enabled %}
77
{% for i in site.imagemagick.widths -%}
8-
<source
8+
<source
99
class="responsive-img-srcset"
10-
media="(max-width: {{ i }}px)"
10+
media="(max-width: {{ i }}px)"
1111
srcset="{{ img_path | relative_url }}-{{ i }}.webp"
1212
/>
1313
{% endfor -%}
1414
{% endif %}
1515

1616
<!-- Fallback to the original file -->
17-
<img
18-
src="{{ include.path | relative_url }}"
19-
{% if include.class %}class="{{ include.class }}"{% endif %}
20-
{% if include.width %}width="{{ include.width }}"{% else %}width="auto"{% endif %}
21-
{% if include.height %}height="{{ include.height }}"{% else %}height="auto"{% endif %}
22-
{% if include.min-width %}min-width="{{ include.min-width }}"{% endif %}
23-
{% if include.min-height %}min-height="{{ include.min-height }}"{% endif %}
24-
{% if include.max-width %}max-width="{{ include.max-width }}"{% endif %}
25-
{% if include.max-height %}max-height="{{ include.max-height }}"{% endif %}
26-
{% if include.alt %}alt="{{ include.alt }}"{% endif %}
27-
{% if include.title %}title="{{ include.title }}"{% endif %}
17+
<img
18+
src="{% if include.cache_bust %}{{ include.path | relative_url | bust_file_cache }}{% else %}{{ include.path | relative_url }}{% endif %}"
19+
{% if include.class %}class="{{ include.class }}"{% endif %}
20+
{% if include.width %}width="{{ include.width }}"{% else %}width="auto"{% endif %}
21+
{% if include.height %}height="{{ include.height }}"{% else %}height="auto"{% endif %}
22+
{% if include.min-width %}min-width="{{ include.min-width }}"{% endif %}
23+
{% if include.min-height %}min-height="{{ include.min-height }}"{% endif %}
24+
{% if include.max-width %}max-width="{{ include.max-width }}"{% endif %}
25+
{% if include.max-height %}max-height="{{ include.max-height }}"{% endif %}
26+
{% if include.alt %}alt="{{ include.alt }}"{% endif %}
27+
{% if include.title %}title="{{ include.title }}"{% endif %}
2828
{% if include.zoomable %}data-zoomable{% endif %}
2929
onerror="this.onerror=null; $('.responsive-img-srcset').remove();"
3030
/>
31-
3231
</picture>
3332

3433
{%- if include.caption -%}<figcaption class="caption">{{ include.caption }}</figcaption>{%- endif %}

_includes/head.html

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@
2727
{% elsif site.icon != blank %}
2828
<link rel="shortcut icon" href="{{ site.icon | prepend: '/assets/img/' | relative_url}}"/>
2929
{% endif %}
30-
<link rel="stylesheet" href="{{ '/assets/css/main.css' | relative_url }}">
30+
<link rel="stylesheet" href="{{ '/assets/css/main.css' | relative_url | bust_css_cache }}">
3131
<link rel="canonical" href="{{ page.url | replace:'index.html','' | absolute_url }}">
3232

3333
<!-- Dark Mode -->
3434
{% if site.enable_darkmode %}
3535
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/jwarby/jekyll-pygments-themes@master/{{ site.highlight_theme_dark | append: '.css' }}" media="none" id="highlight_theme_dark" />
36-
37-
<script src="{{ '/assets/js/theme.js' | relative_url }}"></script>
38-
<script src="{{ '/assets/js/dark_mode.js' | relative_url }}"></script>
36+
<script src="{{ '/assets/js/theme.js' | relative_url | bust_file_cache }}"></script>
37+
<script src="{{ '/assets/js/dark_mode.js' | relative_url | bust_file_cache }}"></script>
3938
{% endif %}

_includes/scripts/misc.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@
2020
<script defer src="https://unpkg.com/bootstrap-table@{{ site.bootstrap-table.version }}/dist/bootstrap-table.min.js"></script>
2121

2222
<!-- Load Common JS -->
23-
<script src="{{ '/assets/js/no_defer.js' | relative_url }}"></script>
24-
<script defer src="{{ '/assets/js/common.js' | relative_url }}"></script>
25-
<script defer src="{{ '/assets/js/copy_code.js' | relative_url }}" type="text/javascript"></script>
23+
<script src="{{ '/assets/js/no_defer.js' | relative_url | bust_file_cache }}"></script>
24+
<script defer src="{{ '/assets/js/common.js' | relative_url | bust_file_cache }}"></script>
25+
<script defer src="{{ '/assets/js/copy_code.js' | relative_url | bust_file_cache }}" type="text/javascript"></script>

_includes/video.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
{% if extension == "mp4" or extension == "webm" or extension == "ogg" %}
66

77
<video
8-
src="{{ include.path | relative_url }}"
8+
src="{% if include.cache_bust %}{{ include.path | relative_url | bust_file_cache }}{% else %}{{ include.path | relative_url }}{% endif %}"
99
{% if include.class %}class="{{ include.class }}"{% endif %}
1010
{% if include.width %}width="{{ include.width }}"{% else %}width="auto"{% endif %}
1111
{% if include.height %}height="{{ include.height }}"{% else %}height="auto"{% endif %}

_layouts/about.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<div class="post">
77
<header class="post-header">
88
<h1 class="post-title">
9-
{% if site.title == "blank" -%}<span class="font-weight-bold">{{ site.first_name }}</span> {{ site.middle_name }} {{ site.last_name }}{%- else -%}{{ site.title }}{%- endif %}
9+
{% if site.title == "blank" -%}<span class="font-weight-bold">{{ site.first_name }}</span> {{ site.middle_name }} {{ site.last_name }}{%- else -%}{{ site.title }}{%- endif %}
1010
</h1>
1111
<p class="desc">{{ page.subtitle }}</p>
1212
</header>
@@ -26,7 +26,8 @@ <h1 class="post-title">
2626
{% include figure.html
2727
path=profile_image_path
2828
class=profile_image_class
29-
alt=page.profile.image -%}
29+
alt=page.profile.image
30+
cache_bust=true -%}
3031
{% endif -%}
3132
{%- if page.profile.address %}
3233
<div class="address">

_plugins/cache-bust.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# based on https://distresssignal.org/busting-css-cache-with-jekyll-md5-hash
2+
# https://gist.github.com/BryanSchuetz/2ee8c115096d7dd98f294362f6a667db
3+
module Jekyll
4+
module CacheBust
5+
class CacheDigester
6+
require 'digest/md5'
7+
require 'pathname'
8+
9+
attr_accessor :file_name, :directory
10+
11+
def initialize(file_name:, directory: nil)
12+
self.file_name = file_name
13+
self.directory = directory
14+
end
15+
16+
def digest!
17+
[file_name, '?', Digest::MD5.hexdigest(file_contents)].join
18+
end
19+
20+
private
21+
22+
def directory_files_content
23+
target_path = File.join(directory, '**', '*')
24+
Dir[target_path].map{|f| File.read(f) unless File.directory?(f) }.join
25+
end
26+
27+
def file_content
28+
local_file_name = file_name.slice((file_name.index('assets/')..-1))
29+
File.read(local_file_name)
30+
end
31+
32+
def file_contents
33+
is_directory? ? file_content : directory_files_content
34+
end
35+
36+
def is_directory?
37+
directory.nil?
38+
end
39+
end
40+
41+
def bust_file_cache(file_name)
42+
CacheDigester.new(file_name: file_name, directory: nil).digest!
43+
end
44+
45+
def bust_css_cache(file_name)
46+
CacheDigester.new(file_name: file_name, directory: 'assets/_sass').digest!
47+
end
48+
end
49+
end
50+
51+
Liquid::Template.register_filter(Jekyll::CacheBust)

0 commit comments

Comments
 (0)