Site Variables from API request

Hey everyone,
I’m building a website that is using Strapi as a CMS. For most of the content I’m able to fetch it as data template according to the docs here:

Now, I’m wondering how I can best fetch Site Variables from an API. I think being able to change – for example – the page’s title is a common usecase that you want people to allow in the CMS, but I couldn’t find anything that could help me with that. Having a webhook triggering a script that builds the config.toml might be a solution, but I’m wondering if that hasn’t been solved by someone else already in a better way.

Thanks so much!

1 Like

Static = Hugo
Dynamic = Javascript in your website that you create with Hugo.

But it’s still Javascript that does anything dynamic. You can’t change something (a title) that is already DONE (your site AFTER Hugo is done with it).

Changing stuff dynamically with Javascript is far beyond the expertise of most of us here :wink:

To get you going:

  • build your data templates into for instance JSON files in your finished website (for instance blog/title/data.json)
  • load that json file on the website via ajax
  • do stuff

Let me tell you a little story @davidsneighbour

10 years ago I switched my blog’s template on Blogspot to on an Ajax powered one.
The traffic tanked despite posting like there was no tomorrow (One Drawing Per Day -sic-)
My blog was actually demoted in Google’s SERPs despite the fact that Blogger is also owned by Google.

Search Engines are really not very clever and that is also true for Google -no matter what they tell you-. (BTW have you noticed link hijacking in Google SERPs during the past couple of years?)

Anyway the bottom line is this:
No matter what algorithm Search Engines use, depending on JS to add a Page’s title and other critical meta will not end up well.

I found out the hard way.


@mmmmd
Using a script to generate a config.toml via a Webhook and then regenerate static pages with Hugo, sounds much preferable to me, than relying on JS for critical attributes that should be available to crawlers without JS.

Let me tell you a grand solution to your problem @alexandros :slight_smile: Google “evergreen browser” and rejoice. Google for about 2 years now uses a Chrome based browser to index your website and these problems don’t exist anymore. They call it evergreen browser, but it’s a chrome engine. So, “if it is visible and looks good on Chrome it’s looking good for Google Bot”.

Hey @alexandros, hey @davidsneighbour,
thank you for your help. I guess my question was not formulated clearly. I’m totally aware that Hugo builds static websites and I don’t want to use JavaScript for any of the content. I was just wondering if there is a similar way to how the Data Templates fetch the data while building the static site to also use that implementation to fetch any other content (such as the Site Variables) while building the website.
Cheers!

Currently there are only the getJSON and getCSV functions to fetch data from remote sources.

Fetching .Site variables and other content or assets from remote sources is not possible, (hence the above debate).

Thanks @alexandros for the info.

In case someone else has the same problem, here is the script I set up to do fetch the json from the API and create a config.json that is then used by Hugo:

package main

import (
  "encoding/json"
  "fmt"
  "io/ioutil"
  "log"
  "net/http"
  "os"
)

type Meta struct {
  Title string `json:"Title"`
  LanguageCode string `json:"LanguageCode"`
  Description string `json:"Description"`
}

type Params struct {
  Description string `json:"description"`
}

type Config struct {
  Title string `json:"title"`
  LanguageCode string `json:"languageCode"`
  Params Params `json:"params"`
}

func main() {
  response, err := http.Get("…")

  if err != nil {
    fmt.Print(err.Error())
    os.Exit(1)
  }

  responseData, err := ioutil.ReadAll(response.Body)
  if err != nil {
    log.Fatal(err)
  }

  var responseObject Meta
  json.Unmarshal(responseData, &responseObject)

  config := Config{
    Title: responseObject.Title,
    LanguageCode: responseObject.LanguageCode,
    Params: Params{
      Description: responseObject.Description,
    },
  }

  file, _ := json.MarshalIndent(config, "", " ")

  _ = ioutil.WriteFile("config.json", file, 0644)
}

Then I’m running this command on Netlify:
go run script.go && hugo --gc --minify --config config.json

1 Like