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>
<p>
I have read a lot of books. But these {{ len $books }} books are my favourites and which i would like to recommend.
</p>
<ul class="books">
{{ range $books }}
<li>
{{ $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 }}
</li>
{{ end }}
</ul>
</div>
{{ 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.
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.