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..201792864 --- /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 }}