# Cache busting CSS/JS files

**URL:** <https://discourse.gohugo.io/t/cache-busting-css-js-files/36488>\
**Category:** support\
**Created:** [January 10, 2022, 10:42am UTC](https://discourse.gohugo.io/t/cache-busting-css-js-files/36488 "2022-01-10T10:42:13Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![askel45](https://avatars.discourse-cdn.com/v4/letter/a/bbce88/32.png) [@askel45](https://discourse.gohugo.io/u/askel45)\
**Post date:** [January 10, 2022, 10:42am UTC](https://discourse.gohugo.io/t/cache-busting-css-js-files/36488/1 "2022-01-10T10:42:13Z")

</div>

I am not sure if this is possible with Hugo. I have added custom query strings to my CSS and JS files in templates which I change manually via `params.toml` to allow purging when external hosts build the site (like Cloudflare pages). However, I would like to automate this process and have the query string change only when a (CSS or JS) file is modified (in local and pushed to Github) so that a site like Cloudflare pages detects the file has changed and updates the query string, but does not change the query string if the file has not been edited. Is this possible to achieve?

---

<div class="post-metadata">

**Author:** ![alexandros](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@alexandros](https://discourse.gohugo.io/u/alexandros)\
**Post date:** [January 10, 2022, 11:11am UTC](https://discourse.gohugo.io/t/cache-busting-css-js-files/36488/2 "2022-01-10T11:11:21Z")

</div>

Basically the term you are looking for is called cache busting assets.

The general Idea is to use a timestamp with the [`Unix`](https://gohugo.io/functions/unix/#prose) function.

OR

If you are using Hugo Pipes you can [`fingerprint`](https://gohugo.io/hugo-pipes/fingerprint/) the assets.

Also see this post:

> [@Cache busting of CSS/JS](https://discourse.gohugo.io/t/cache-busting-of-css-js/175/27):
>
> I have a solution that appends a query string that only changes when the file is modified. For my theme css file: {{ $stylemin := os.Stat "themes/coder/static/css/style.min.css" }} \<link rel="stylesheet" href="{{ "css/style.min.css" | absURL }}?{{ $stylemin.ModTime.Unix }}"\> and for any custom\_css files in config.toml: {{ range .Site.Params.custom\_css }} {{ $stylecustom := os.Stat (print "static/" . ) }} \<link rel="stylesheet" href="{{ . | absURL }}?{{ $stylecustom.ModTime.Unix }}"\> {{ e…

P.S. A support ticket is best when it is tagged as such.

---

<div class="post-metadata">

**Author:** ![askel45](https://avatars.discourse-cdn.com/v4/letter/a/bbce88/32.png) [@askel45](https://discourse.gohugo.io/u/askel45)\
**Post date:** [January 10, 2022, 11:28am UTC](https://discourse.gohugo.io/t/cache-busting-css-js-files/36488/3 "2022-01-10T11:28:05Z")

</div>

> [@alexandros](#):
>
> P.S. A support ticket is best when it is tagged as such.

I am not a dev. Sorry. Just a Jekyll to Hugo user who knows their way around following docs to get things done.

---

<div class="post-metadata">

**Author:** ![alexandros](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@alexandros](https://discourse.gohugo.io/u/alexandros)\
**Post date:** [January 10, 2022, 11:30am UTC](https://discourse.gohugo.io/t/cache-busting-css-js-files/36488/4 "2022-01-10T11:30:24Z")

</div>

You can surely select the **support** tag when opening a forum topic.  
Thank you.

 ![Screenshot 2022-01-10 at 1.30.01 PM](https://canada1.discourse-cdn.com/flex036/uploads/gohugo/original/2X/e/eb7d78fdb6e9d73a5adeb58f4e638049ff4c8249.jpeg)

---

<div class="post-metadata">

**Author:** ![askel45](https://avatars.discourse-cdn.com/v4/letter/a/bbce88/32.png) [@askel45](https://discourse.gohugo.io/u/askel45)\
**Post date:** [January 10, 2022, 11:32am UTC](https://discourse.gohugo.io/t/cache-busting-css-js-files/36488/5 "2022-01-10T11:32:09Z")

</div>

OOh. That…I sometimes forget to do that. You’re right.

---

<div class="post-metadata">

**Author:** ![askel45](https://avatars.discourse-cdn.com/v4/letter/a/bbce88/32.png) [@askel45](https://discourse.gohugo.io/u/askel45)\
**Post date:** [January 10, 2022, 12:24pm UTC](https://discourse.gohugo.io/t/cache-busting-css-js-files/36488/6 "2022-01-10T12:24:49Z")

</div>

~~I tried this but it throws an error. It is looking for the file inside the content folder instead of my assets folder that I have defined as static directory~~

~~```  
{{ $stylemin := os.Stat “css/style.min.css” }}~~

~~```~~

Forget that. This worked

```auto
{{ $stylemin := os.Stat "assets/css/style.min.css" }}
<link rel="stylesheet" href="{{ "css/style.min.css" | absURL }}?{{ $stylemin.ModTime.Unix }}">

```

~~But once I modify the file locally, the query string does not change.~~ It does not change when `--watch` is on (I use Git for command line). But it changes when I close Git and do `hugo serve` again. So I will test this on the production site and see.

---

<div class="post-metadata">

**Author:** ![nfriedli](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/nfriedli/32/26027_2.png) [@nfriedli](https://discourse.gohugo.io/u/nfriedli)\
**Post date:** [January 10, 2022, 5:33pm UTC](https://discourse.gohugo.io/t/cache-busting-css-js-files/36488/7 "2022-01-10T17:33:45Z")

</div>

Fingerprinting is better (IMHO) than a ModTime:

```auto
{{ $style := resources.Get "css/style.css" 
  | minify
  | fingerprint 
}}
<link rel="stylesheet" href="{{ $style.RelPermalink }}">

```

---

<div class="post-metadata">

**Author:** ![askel45](https://avatars.discourse-cdn.com/v4/letter/a/bbce88/32.png) [@askel45](https://discourse.gohugo.io/u/askel45)\
**Post date:** [January 10, 2022, 6:46pm UTC](https://discourse.gohugo.io/t/cache-busting-css-js-files/36488/8 "2022-01-10T18:46:33Z")

</div>

> [@nfriedli](#):
>
> ```auto
> {{ $style := resources.Get "css/style.css" 
> | minify
> | fingerprint 
> }}
> <link rel="stylesheet" href="{{ $style.RelPermalink }}">
> 
> ```

This works for CSS, but it makes JS not to work even though it shows it is loaded via `CTRL+U`.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [January 10, 2022, 6:52pm UTC](https://discourse.gohugo.io/t/cache-busting-css-js-files/36488/9 "2022-01-10T18:52:24Z")

</div>

Please share your repository.

---

<div class="post-metadata">

**Author:** ![askel45](https://avatars.discourse-cdn.com/v4/letter/a/bbce88/32.png) [@askel45](https://discourse.gohugo.io/u/askel45)\
**Post date:** [January 10, 2022, 7:03pm UTC](https://discourse.gohugo.io/t/cache-busting-css-js-files/36488/10 "2022-01-10T19:03:32Z")

</div>

Solved. Missing `</script>`

---

<div class="post-metadata">

**Author:** ![askel45](https://avatars.discourse-cdn.com/v4/letter/a/bbce88/32.png) [@askel45](https://discourse.gohugo.io/u/askel45)\
**Post date:** [January 10, 2022, 7:05pm UTC](https://discourse.gohugo.io/t/cache-busting-css-js-files/36488/11 "2022-01-10T19:05:53Z")

</div>

What works for me now since my CSS files are already minified.

```auto
{{ $css := resources.Get "css/style.min.css" | fingerprint "sha512" }}
<link rel="stylesheet" href="{{ $css.Permalink }}">
{{ $js := resources.Get "js/scripts.min.js" | fingerprint "sha512" }}
<script type="text/javascript" src="{{ $js.Permalink }}" defer></script>

```

---

<div class="post-metadata">

**Author:** ![askel45](https://avatars.discourse-cdn.com/v4/letter/a/bbce88/32.png) [@askel45](https://discourse.gohugo.io/u/askel45)\
**Post date:** [January 10, 2022, 7:12pm UTC](https://discourse.gohugo.io/t/cache-busting-css-js-files/36488/12 "2022-01-10T19:12:00Z")

</div>

Thanks everyone for the help @alexandros @nfriedli @jmooring . Cheers!

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [January 12, 2022, 7:12pm UTC](https://discourse.gohugo.io/t/cache-busting-css-js-files/36488/13 "2022-01-12T19:12:51Z")

</div>

Closed on January 12, 2022, 7:12pm UTC

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
