getJSON cache

I am using getJSON in one of my template files, as it is only required on that page.

 {{ define "content" }} {{ partial "navigation" . }}
    <div class="container">
      {{ $books := getJSON "url" }}
      <h1>Book Recommendations</h1>
        I have read a lot of books. But these {{ len $books }} books are my favourites and which i would like to recommend.
      <ul class="books">
        {{ range $books }}
          {{ $url := index .image_url 0 }}
          {{ $slicedTitle := index (split (index .title 0) ":") 0 }}
          <img src="{{ $url }}" alt="{{ $slicedTitle }}" />
          <h2>{{ $slicedTitle }}</h2>
          {{ $authors := index .authors 0 }} {{ range $authors }} {{ range . }}
          <h3>by {{ index .name 0 }}</h3>
          {{ end }} {{ end }}
        {{ end }}
    {{ end }}

The problem happens if the API data changes. I tried to look into setting cache in my config.toml file but I am still confused as to for which directory I should set the cache.

If I understand your problem correctly, something like this may help:

dir = ":cacheDir/:project"
maxAge = "10s"

The above should make it so you get fresh content on every build.

1 Like

Hey @bep Thank you for replying. Loving Hugo and learning new things every day.

Maybe I misunderstood how it works. Does this mean, fresh content is only fetched if a new build is generated?

I have published my site on Netlify and for the most part I won’t be adding new static content to it regularly.

This goodreads API I built, was to display some of the books, and the API would have new content if and when I add new books on goodreads. So now I am thinking, even if the API refreshes on it’s own on the server, it won’t reflect anything unless I redeploy the site.

Yes. Hugo is a static site generator. The short expiry of 10s will make sure that you get fresh data on every build. That said, you can set up your site to build every hour or something.

1 Like

Thank you! I will try automating the build.

There should be plenty written about scheduling builds on Netlify.