Best way to detect environment

I want a word count on development but I don’t want it to display on production. The way I’d have this is to include the .WordCount variable as a data attribute on some tag that will get shown in the DOM by JS if the hostname matches development i.e localhost:1313 . Is there a better way I could go about this without having to include JS?

See this thread. How to Exclude Google Analytics When Running Under Hugo Local Server

Particularly @budparr 's contributions.

Thanks @alexandros . I figured this just have to be with JS as I’m using it on a theme. I don’t want to set an environment variable on my machine just for this and if I’m checking for production it’d be something like:

{{ if not eq (getenv "HUGO_ENV") "production" }}
        {{ .WordCount }}
{{ end }}

which will then mean for people using the theme that don’t set the env on production or don’t have enough knowledge on environment variables, the word count will still show.

So what I did is I gave it a class which just checks the hostname in JS and toggles it to display only when hostname is localhost.

1 Like