Local preview
Two commands cover daily work. hugo server rebuilds the site every time you
save and reloads the browser for you; hugo writes the finished site to
public/, the directory you deploy:
Neither needs Node.js, npm or a CDN. The tools they do need, Git, Go and Hugo, are listed in Get started.
The preview server
Run hugo server from the site root, the directory that holds hugo.yml, and
open http://localhost:1313/. Each save, and each switch of Git branch,
triggers a rebuild and a browser refresh. The first start is slower because
Hugo downloads the theme module into its cache; later starts read the cache.
The switches worth knowing
A good combination while writing:
-DFE is short for -D -F -E, so drafts, future-dated and expired pages all
appear while you work on them. --renderToMemory keeps those pages out of
public/, so a later deploy cannot pick them up by accident.
A change that did not take effect
By default Hugo re-renders only what it judges affected by a change, and edits
to layouts, configuration, data/ or an included file sometimes slip past
that judgment. When a page looks unchanged:
- Restart with
--disableFastRender. - Force a browser reload (
Cmd/Ctrl+Shift+R) to rule out the browser cache. - If the page is still stale, clear the caches and restart.
Reaching it from another device
The server listens on 127.0.0.1 only, so a phone or another computer cannot
reach it. Bind to every interface and give the address the other device will
use:
Without --baseURL, the page opens but its absolute links, the stylesheet and
the search index among them, still point at localhost.
Production build
Build what you deploy with hugo, never with hugo server:
The output in public/ is self-contained, so you can copy it anywhere
without the source tree. Each switch earns its place:
A successful build ends with a Total in … line. Read any warnings before you
deploy: each one names a setting the theme ignored, and the build kept going
with a safe default.
Two settings matter as soon as the output leaves your machine. baseURL
decides every absolute link, including a subpath such as /docs/; see
Getting baseURL right. The build environment,
production for hugo and development for hugo server, decides whether
pages ask to be indexed and whether analytics load; see
Keeping preview deployments unindexed.
Previewing in a container
A container is optional. It helps when a team wants everyone on the same toolchain, or does not want Hugo installed on every machine.
Set HUGO_VERSION to the release your team builds with (OINK is tested on
0.170+). A site still pinned to OINK 1.5.1 or older needs a different
package; see Upgrade.
Go is in the image because Hugo needs it to download a theme installed as a Hugo Module. A site that uses a submodule, an offline archive or a plain clone can drop Go and get a much smaller image. Nothing in the image needs Node.js, npm or PostCSS, and the build fetches no browser assets, so for a network-isolated environment you only have to mirror the base image and the Hugo and Go packages.
public/A container runs as root unless told otherwise, and a public/ written by
root cannot be deleted from the host without sudo. Map your user ID with
--user "$(id -u):$(id -g)", as the production build command above does.
Clearing caches
Hugo keeps intermediate output in three places. Clear the smallest one that explains the symptom:
List both public/ and resources/ in .gitignore. They are regenerated on
every build, and committed copies go stale and bloat the repository.
Previewing against a theme checkout
When you change the theme and the site together, point the preview at your
local theme checkout for one command with HUGO_MODULE_REPLACEMENTS. go.mod
stays untouched, so the substitution cannot be committed by accident:
From scratch shows the directory layout and the Go workspace alternative.
While the variable is set or a Go workspace is active, every build uses the
local checkout, whatever go.mod says, and a CI job can inherit either. To
build the published release, unset HUGO_MODULE_REPLACEMENTS, set
GOWORK=off and HUGO_MODULE_WORKSPACE=off, look for permanent replace
directives in go.mod and the Hugo configuration and for a _vendor/ copy,
then confirm the exact version with hugo mod graph in that same
environment. Never commit a workspace file that names a path on your
machine.
Verifying an offline build
If the site must work in a network-isolated environment, test both the build and the browser with the network cut off:
- Start from a verified theme archive and an empty module cache (
hugo mod clean --all). - Block outbound HTTP, HTTPS and the Go module proxy.
- Run the production build
hugo --gc --minify --printPathWarnings. - Browse pages in every language: a documentation page, a blog page, the home page, the 404.
- Use search, the light/dark toggle, diagrams and the content components the site relies on.
- Check every subresource origin and confirm that no unexpected remote host appears.
For the last step, run the output checker from a theme checkout of the same release your site pins. It needs only Python 3. Replace both paths and use the base URL you built with:
The script reads every href, src, srcset, poster and form action in
the HTML, print, Markdown and RSS output. Each must be a site-relative path or
use http, https, mailto or tel, and inline on* handlers and
javascript: URLs are rejected. An <iframe>, <script>, <link>, <img>,
<video>, <audio>, <embed>, <object> or <source> that points to
another host is an error. If the site embeds third-party content on purpose,
pass --third-party; if its languages live on several domains, list the extra
first-party hosts with --allow-host.
A pass covers that build only. Run the check again after every theme upgrade.
Verify
Start from empty caches so nothing stale hides a problem:
The build passes when it ends with Total in … and reports no ERROR. Then
check:
- The log shows no npm, PostCSS, Autoprefixer or asset-download step. One that appears comes from leftover Docsy configuration.
public/containssitemap.xmlandrobots.txt, androbots.txtreadsAllow: /.- With local search enabled,
public/holds an index per language, namedoffline-search-index.<language>.<hash>.jsonin production (development builds omit the hash). Open search and confirm that the URL in the page’sdata-td-index-srcattribute returns 200. - In
hugo server, a documentation page, a blog page, the home page and the 404 render correctly in every language and in both color schemes.
After deployment, repeat the checks on the live site with the deployment checklist. For a build that fails or looks wrong, see Troubleshooting.
Related
- Deploy — getting
public/to GitHub Pages, Cloudflare or elsewhere - Troubleshooting — build, language, search and platform faults
- From scratch and other install methods — Hugo Module, submodule or offline archive
- Configuration — every key in
hugo.yml