diff --git a/layouts/partials/func/GetEnvironmentName.html b/layouts/partials/func/GetEnvironmentName.html new file mode 100644 index 000000000..06edd5a9c --- /dev/null +++ b/layouts/partials/func/GetEnvironmentName.html @@ -0,0 +1,35 @@ +{{/* + GetEnvironmentName + + This partial returns either the configured or the default build environment + name. + + The four options for setting the build environment name (ordered by priority) + are: + 1. The "HUGO_ENV" environment variable. + 2. The "env" site config theme parameter. + 3. Setting the "useHugoEnv" site config theme parameter to true, which will + use the value of ".Hugo.Environment" for the build environment name. + + 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". + 4. Use the default build environment name. + + @return build environment name. +*/}} + +{{ $env := "development" }} +{{ $site := site }} +{{ $hugo := hugo }} + +{{ if (getenv "HUGO_ENV") }} + {{ $env = getenv "HUGO_ENV" }} +{{ else if $site.Params.env }} + {{ $env = $site.Params.env }} +{{ else if (eq $site.Params.useHugoEnv true) }} + {{ $env = $hugo.Environment }} +{{ end }} + +{{ return $env }} diff --git a/layouts/partials/func/IsProduction.html b/layouts/partials/func/IsProduction.html new file mode 100644 index 000000000..63609729c --- /dev/null +++ b/layouts/partials/func/IsProduction.html @@ -0,0 +1,15 @@ +{{/* + IsProduction + + @return false, unless the build environment is set to production via one of + the methods supported in the func/GetEnvironment partial. +*/}} + +{{ $isProduction := false }} +{{ $env := partial "func/GetEnvironmentName.html" . }} + +{{ if eq $env "production" }} + {{ $isProduction := true }} +{{ end }} + +{{ return $isProduction }} diff --git a/layouts/partials/func/style/GetMainCSS.html b/layouts/partials/func/style/GetMainCSS.html index c4daa88a3..ad18a82ca 100644 --- a/layouts/partials/func/style/GetMainCSS.html +++ b/layouts/partials/func/style/GetMainCSS.html @@ -59,7 +59,7 @@ {{ $options := dict "enableSourceMap" true "precision" 6 }} {{ $style = $style | resources.ToCSS $options | minify }} {{/* We fingerprint in production for cache busting purposes */}} - {{ if eq (getenv "HUGO_ENV") "production" }} + {{ if (partial "func/IsProduction.html" .) }} {{ $style = $style | fingerprint }} {{ end }} {{/* We're ready to set returning variable with resulting resource */}} diff --git a/layouts/robots.txt b/layouts/robots.txt index 7a24e829e..864a83297 100644 --- a/layouts/robots.txt +++ b/layouts/robots.txt @@ -1,6 +1,6 @@ User-agent: * -# robotstxt.org - if ENV production variable is false robots will be disallowed. -{{ if eq (getenv "HUGO_ENV") "production" | or (eq .Site.Params.env "production") }} +# robotstxt.org - if the build is not configured for production, robots will be disallowed. +{{ if (partial "func/IsProduction.html" .) }} Allow: / Sitemap: {{.Site.BaseURL}}/sitemap.xml {{ else }} From 108995800bd5bd43022bb9a00d8d9c24e31f0fef Mon Sep 17 00:00:00 2001 From: Troy Lindsay Date: Mon, 3 Jan 2022 13:20:29 -0600 Subject: [PATCH 2/3] Tweak readme production config statement (#453) --- README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index f6de6415c..85487d23c 100644 --- a/README.md +++ b/README.md @@ -296,7 +296,7 @@ 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), you must set the build environment name. The four options for setting the build environment name (ordered by priority) are: +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: 1. The `HUGO_ENV` environment variable. @@ -338,8 +338,6 @@ To run in production (e.g. to have Google Analytics show up), you must set the b useHugoEnv = true ``` -1. Use the default build environment name. - ## Contributing If you find a bug or have an idea for a feature, feel free to use the [issue tracker](https://github.com/theNewDynamic/gohugo-theme-ananke/issues) to let me know. From 3ec1afbfcefeb8d86c981cb5523ff07455f3974d Mon Sep 17 00:00:00 2001 From: Troy Lindsay Date: Sun, 9 Jan 2022 09:25:53 -0600 Subject: [PATCH 3/3] Fix assignment context (#453) --- layouts/partials/func/IsProduction.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/layouts/partials/func/IsProduction.html b/layouts/partials/func/IsProduction.html index 63609729c..201792864 100644 --- a/layouts/partials/func/IsProduction.html +++ b/layouts/partials/func/IsProduction.html @@ -9,7 +9,7 @@ {{ $env := partial "func/GetEnvironmentName.html" . }} {{ if eq $env "production" }} - {{ $isProduction := true }} + {{ $isProduction = true }} {{ end }} {{ return $isProduction }}