diff --git a/README.md b/README.md index d5e16360c..85487d23c 100644 --- a/README.md +++ b/README.md @@ -296,18 +296,47 @@ Now enter [`localhost:1313`](http://localhost:1313/) in the address bar of your ## Production -To run in production (e.g. to have Google Analytics show up), run `HUGO_ENV=production` before your build command. For example: +To run in production (e.g. to have Google Analytics show up), you must set the build environment name to `production`. The three options for setting the build environment name (ordered by priority) are: -``` -HUGO_ENV=production hugo -``` +1. The `HUGO_ENV` environment variable. -Note: The above command will not work on Windows. If you are running a Windows OS, use the below command: + Examples: -``` -set HUGO_ENV=production -hugo -``` + **macOS/Linux** + + ```bash + HUGO_ENV=production hugo + ``` + + **Windows Command shell** + + ```bat + set HUGO_ENV=production + hugo + ``` + + **PowerShell** + + ```powershell + $env:HUGO_ENV='production' + hugo + ``` + +1. The `env` site config theme parameter, for example: + + ```toml + [params] + env = "production" + ``` + +1. Setting the `useHugoEnv` site config theme parameter to true, which will use the value of [`.Hugo.Environment`](https://gohugo.io/variables/hugo/) for the build environment name. This option is recommended if using [environment-specific config directories](https://gohugo.io/getting-started/configuration/#configuration-directory), such as for minifying only your production builds. + + The default environment name for the `hugo server` command is `development`, and the default for the `hugo` build command is `production`. The environment name can be configured via the `--environment` flag, such as `hugo --environment test`. + + ```toml + [params] + useHugoEnv = true + ``` ## Contributing diff --git a/exampleSite/config.toml b/exampleSite/config.toml index 684f52426..b158993cb 100644 --- a/exampleSite/config.toml +++ b/exampleSite/config.toml @@ -36,6 +36,8 @@ enableRobotsTXT = true background_color_class = "bg-black" featured_image = "/images/gohugo-default-sample-hero-image.jpg" recent_posts_number = 2 + # Uncomment to use the value of .Hugo.Environment for defining the build environment + # useHugoEnv = true [[params.ananke_socials]] name = "twitter" diff --git a/layouts/_default/baseof.html b/layouts/_default/baseof.html index dc5e33bd2..68b8d9b41 100755 --- a/layouts/_default/baseof.html +++ b/layouts/_default/baseof.html @@ -1,3 +1,5 @@ +{{ $env := partial "func/GetEnvironmentName.html" . }} +{{ $isProduction := partial "func/IsProduction.html" . }}
@@ -8,8 +10,7 @@ {{ hugo.Generator }} - {{/* NOTE: For Production make sure you add `HUGO_ENV="production"` before your build command */}} - {{ if eq (getenv "HUGO_ENV") "production" | or (eq .Site.Params.env "production") }} + {{ if $isProduction }} {{ else }} @@ -34,13 +35,13 @@ {{- template "_internal/schema.html" . -}} {{- template "_internal/twitter_cards.html" . -}} - {{ if eq (getenv "HUGO_ENV") "production" | or (eq .Site.Params.env "production") }} + {{ if $isProduction }} {{ template "_internal/google_analytics_async.html" . }} {{ end }} {{ block "head" . }}{{ partial "head-additions.html" . }}{{ end }} - + {{ block "header" . }}{{ partial "site-header.html" .}}{{ end }}