#Hugo

How to Disable Scripts like Google Analytics on Hugo Localhost

Available for new projects

Author: Radu

I wanted to go cookieless on this website, so I had to swap Google Analytics with Umami.

Since I’m on the free plan, I didn’t want the script to register events while I’m working on my website on localhost. So, I had to rely on a conditional script inclusion, which I’ll share with you in this post.

Disable Scripts on Hugo Localhost

There’s more than one way to do this, but I’ll show you the one I’m using, which I think is the easiest and most practical.

I used the hugo.IsServer function, which checks whether the built-in development server is running. The “built-in development server” refers to the hugo server command.

All you have to do is add this code into your <head> section, or whenever you need to place the script.

{{ if not hugo.IsServer }}
       // code or a partial with the code goes here
{{ end }}

How it works

hugo.IsServer returns true when you run your site in development using hugo server, so the script won’t appear.

When you build for production using hugo, hugo.IsServer is false, so the script will appear.

That’s a Wrap

I hope this post has helped you out.

If you're interested in my web design services or free templates, want to collaborate, or just have something to say, feel free to shoot me a message.

About Radu

I've been working online from home for over 11 years, gaining valuable experience and developing a wide range of skills, including web design and development, optimization, and SEO.