Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update build environment logic #477

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 38 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions exampleSite/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 5 additions & 4 deletions layouts/_default/baseof.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{{ $env := partial "func/GetEnvironmentName.html" . }}
{{ $isProduction := partial "func/IsProduction.html" . }}
<!DOCTYPE html>
<html lang="{{ $.Site.LanguageCode | default "en" }}">
<head>
Expand All @@ -8,8 +10,7 @@
<meta name="viewport" content="width=device-width,minimum-scale=1">
<meta name="description" content="{{ with .Description }}{{ . }}{{ else }}{{if .IsPage}}{{ .Summary }}{{ else }}{{ with .Site.Params.description }}{{ . }}{{ end }}{{ end }}{{ end }}">
{{ 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 }}
<META NAME="ROBOTS" CONTENT="INDEX, FOLLOW">
{{ else }}
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
Expand All @@ -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 }}
</head>

<body class="ma0 {{ $.Param "body_classes" | default "avenir bg-near-white"}}{{ with getenv "HUGO_ENV" }} {{ . }}{{ end }}">
<body class="ma0 {{ $.Param "body_classes" | default "avenir bg-near-white"}}{{ with $env }} {{ . }}{{ end }}">

{{ block "header" . }}{{ partial "site-header.html" .}}{{ end }}
<main class="pb7" role="main">
Expand Down
35 changes: 35 additions & 0 deletions layouts/partials/func/GetEnvironmentName.html
Original file line number Diff line number Diff line change
@@ -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 }}
15 changes: 15 additions & 0 deletions layouts/partials/func/IsProduction.html
Original file line number Diff line number Diff line change
@@ -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 }}
2 changes: 1 addition & 1 deletion layouts/partials/func/style/GetMainCSS.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 */}}
Expand Down
4 changes: 2 additions & 2 deletions layouts/robots.txt
Original file line number Diff line number Diff line change
@@ -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 }}
Expand Down