hugo.isServer as a environment variable

Hello, thanks for Hugo. It’s really great.

I’ve run into a minor inconvenience. It’s that I’ve created a theme that have postcss, however my users have different Hugo environments. e.g. “internal” and “external”. This causes postcss to not work. In other words; I believe a potential solution to usecases like this can be to have a variable that I can use to see if the development server is running or if it’s a production build without checking HUGO_ENVIRONMENT variable as that can be anything set by the user.

Potential solution could look like:

const autoprefixer = require('autoprefixer');
module.exports = {
  plugins: [
    !process.env.HUGO_ISSERVER ? autoprefixer : null
  ]
}

Thanks again!

If we did something like this, you’d have to do a string comparison:

const autoprefixer = require('autoprefixer');
module.exports = {
  plugins: [
   (process.env.HUGO_ISSERVER === 'false') ? autoprefixer : null
  ]
}

Environment variables are always strings.

1 Like

So, in the templates, my current favourite pattern for this is:

{{ if not hugo.IsDevelopment }}
// minify, fingerprint etc.
{{ end }}

While the above, as you say, is not perfect, I find it is much better than IsServer because I want to test how “production looks” when running the server, so I do hugo server -e production– and that approach fails if my conditional uses IsServer.

2 Likes

I understand, but then the flexibility is lost. Say if I design my Hugo module to have any environment like “dev-external” to be able to run the configuration of such, then this will not work. In those cases something like .IsServer is preferred.

Sure, I mean that would be great to have something like that. :slight_smile:

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