Skip to content

Commit 4482611

Browse files
committed
js/css: use fingerprinting to avoid using stale cached copies
When the JavaScript or CSS files are updated, caching rules often prevent the new versions from being picked up, whether due to browser-side caching or server-side caching. Let's avoid this by fingerprinting the files as described in https://gohugo.io/functions/resources/fingerprint/. We are technically not even interested in the cryptographic aspect of this, the only benefit we are looking for is that the file _names_ change in a predictable manner based on the file _contents_ (similar to how Git objects work...). While we are technically not interested in the cryptographic aspect, we can just as well benefit from it by adding the corresponding `integrity` attribute. However, we only do that if the base URL indicates that this will be accessed via HTTPS; Otherwise loading the pages locally via `file:///` would no longer work for those resources (see https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity for more information about this issue). Inspired-by: Toon Claes <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 0c3d60d commit 4482611

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed

layouts/_default/baseof.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ <h1>Redirecting&hellip;</h1>
6464

6565
<link href="{{ relURL "favicon.ico" }}" rel='shortcut icon' type='image/x-icon'>
6666

67-
{{ $style := resources.Get "sass/application.scss" | resources.ExecuteAsTemplate "application.scss" . | css.Sass | resources.Minify }}
68-
<link rel="stylesheet" href="{{ $style.RelPermalink }}">
67+
{{ $style := resources.Get "sass/application.scss" | resources.ExecuteAsTemplate "application.scss" . | css.Sass | resources.Minify | fingerprint }}
68+
<link rel="stylesheet" href="{{ $style.RelPermalink }}"{{ if (hasPrefix .Site.BaseURL "https://") }} integrity="{{ $style.Data.Integrity }}"{{ end }}>
6969
<!--[if (gte IE 6)&(lte IE 8)]>
7070
<script src="{{ relURL "js/selectivizr-min.js" }}"></script>
7171
<![endif]-->

layouts/partials/footer.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818
<script src="{{ relURL "js/modernizr.js" }}"></script>
1919
<script src="{{ relURL "js/modernize.js" }}"></script>
2020
{{ if eq (.Scratch.Get "section") "search" }}<script src="{{ relURL "pagefind/pagefind-ui.js" }}"></script>{{ end }}
21-
{{ $js := resources.Get "js/application.js" | resources.ExecuteAsTemplate "js/application.js" . | resources.Minify }}
22-
<script src="{{ $js.RelPermalink }}"></script>
21+
{{ $js := resources.Get "js/application.js" | resources.ExecuteAsTemplate "js/application.js" . | resources.Minify | fingerprint }}
22+
<script src="{{ $js.RelPermalink }}"{{ if (hasPrefix .Site.BaseURL "https://") }} integrity="{{ $js.Data.Integrity }}"{{ end }}></script>

tests/git-scm.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ test('404', async ({ page }) => {
254254
await expect(page.locator('.inner h1')).toHaveText(`That page doesn't exist.`)
255255

256256
// the 404 page should be styled
257-
await expect(page.locator('link[rel="stylesheet"]')).toHaveAttribute('href', /application(\.min)?\.css$/)
257+
await expect(page.locator('link[rel="stylesheet"]')).toHaveAttribute('href', /application(\.min)?(\.[0-9a-f]+)?\.css$/)
258258

259259
// the search box is shown
260260
await expect(page.locator('#search-text')).toBeVisible()

0 commit comments

Comments
 (0)