HUGO

Reusing data pulled via getJSON

Hi - a general question about getJSON 's data. I understand that:

  • getJSON will store its data under $TMPDIR/hugo_cache/projectname/filecache/getjson/ in a file with a uuid name like 7d6d5027480532086d04c13c997c3057.
  • you can force a re-download of this data by using the switch --ignoreCache on your hugo command, so taking that off will re-use whatever is there.

Instead of manually downloading some json file to data under the project, I converted to use getJSON.

When you use it in an index where the resulting files are few, running without --ignoreCache is not a problem. But when you put it in a single.html template, especially when there’s a lot of pages, you are looking at lots of API calls to re-get the same file.

What I’m wondering about is, while I can assign the getJSON result into a variable and use it on that page, can I “re-use” that variable on the single.html in some way? If I can, I can leave off --ignoreCache, let the data be downloaded newly by the index and then re-use it in the single.

If I could specify the location exactly without a random uuid, I could call it consistently across local and CI environments perhaps. It also looks like I can use getJSON on a local file so, that should be possible. However, is that uuid going to be referenceable between, say, local and a CI.

Is there a strategy for this? I hope someone might have wrestled with this previously.

Ref:

I would think that once you set a Scratch variable within the context of a getJSON call, then its value should be available globally.

However if this is about a new project and if the remote content does not change, then consider storing the JSON files locally, so that you could avoid possible future problems with remote content going 404.

1 Like

Thanks for the tip! Right, reviewing scratch, maybe newscratch will work!

https://gohugo.io/functions/scratch#the-local-newscratch

What I tend to do as a general thing is to use partialCached function with your own custom key. Remember that partials can 1) Return data (the return keyword) and 2) Can be inlined (so you can put them close to where you use them).

1 Like

Note that you can set a more fine grained file cache config:

You can, as an example, do:

caches:
  getjson:
    dir: :cacheDir/:project
    maxAge: "2h"

The max age of 2 hours will last a build, but, probably, expire before the next build.

2 Likes

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

Thanks a bunch, using return from a partial, and calling it via partialcached works nicely.

Setup the partial:

{{ $token := getenv "PRODBTOKEN15331" }}
{{ $bearertoken := printf "Bearer %s" $token }} 
{{ $holdata := getJSON "https://pro.dbflex.net/secure/api/v2/15331/Work%20Holiday/API%20Holidays/select.json" (dict "Authorization" $bearertoken)  }}
{{ return $holdata }}

Call in index and single:

...
{{ $dt := .Date.Format "02 Jan 2006" }}
{{ $holdata := partialCached "holidays-json.html" . }}
{{ range where $holdata ".Date with Month Name" $dt }}
{{ with (index . "Name") }}
...

Resulting in index:

And single, here:

I did get newscratch to work, but did not need it with this technique.