Configure segments
The segments configuration applies only to segmented rendering. While it controls when content is rendered, it doesn’t restrict access to Hugo’s complete object graph (sites and pages), which remains fully available.
Segmented rendering offers several advantages:
- Faster builds: Process large sites more efficiently.
- Rapid development: Render only a subset of your site for quicker iteration.
- Scheduled rebuilds: Rebuild specific sections at different frequencies (e.g., home page and news hourly, full site weekly).
- Targeted output: Generate specific output formats (like JSON for search indexes).
Segment definition
Each segment is defined by an includes key and an excludes key, both of which accept an array of filters.
A filter is a collection of one or more conditions, represented as an item in the configuration array. A condition compares a specific page field to a given glob pattern.
Evaluation rules
The evaluation logic adheres to three rules:
- All conditions within a single filter item must match for that filter to evaluate as true, creating an AND relationship.
- If the
includesorexcludesarray contains multiple filters, only one filter needs to evaluate as true for the entire array to match, creating an OR relationship. - The
excludesarray takes absolute precedence. If a page matches any filter in theexcludesarray, Hugo omits it from the segment regardless of whether it matches theincludesarray.
Performance optimization
Using the excludes array to target sites or output formats allows Hugo to skip entire groups of pages during evaluation instead of checking every page. This optimization helps with performance in larger setups.
For example, excluding unwanted output formats is faster:
segments:
segment1:
excludes:
- output: '! json'
[segments]
[segments.segment1]
[[segments.segment1.excludes]]
output = '! json'
{
"segments": {
"segment1": {
"excludes": [
{
"output": "! json"
}
]
}
}
}
Including only the desired output format is slower:
segments:
segment1:
includes:
- output: json
[segments]
[segments.segment1]
[[segments.segment1.includes]]
output = 'json'
{
"segments": {
"segment1": {
"includes": [
{
"output": "json"
}
]
}
}
}
Fields
kind- (
string) A glob pattern matching the page kind. For example:{taxonomy,term}. lang- Deprecated in v0.153.0
- Use
sitesinstead. output- (
string) A glob pattern matching the output format of the page. For example:{html,json}. path- (
string) A glob pattern matching the page’s logical path. For example:{/books,/books/**}. sites- New in v0.153.0
- (
map) A map to define sites matrix.
Targeting segments
To specify which segments Hugo builds, add the renderSegments setting to your project configuration:
renderSegments:
- segment1
- segment2
renderSegments = ['segment1', 'segment2']
{
"renderSegments": [
"segment1",
"segment2"
]
}
Alternatively, pass the segment names directly to the --renderSegments command-line flag during a build:
hugo build --renderSegments segment1You can target multiple segments by providing a comma-separated list:
hugo build --renderSegments segment1,segment2Example
Consider a project with this content structure:
content/
├── books/
│ ├── _index.en.md
│ ├── _index.nb.md
│ ├── _index.nn.md
│ ├── book-1.en.md
│ ├── book-1.nb.md
│ └── book-1.nn.md
├── films/
│ ├── _index.en.md
│ ├── _index.nb.md
│ ├── _index.nn.md
│ ├── film-1.en.md
│ ├── film-1.nb.md
│ └── film-1.nn.md
├── _index.en.md
├── _index.nb.md
└── _index.nn.mdAnd this project configuration:
baseURL: https://example.org/
defaultContentLanguage: en
defaultContentLanguageInSubdir: true
languages:
en:
direction: ltr
label: English
locale: en-US
weight: 1
nb:
direction: ltr
label: Bokmål
locale: nb-NO
weight: 2
nn:
direction: ltr
label: Norsk
locale: nn-NO
weight: 3
segments:
segment1:
excludes:
- sites:
matrix:
languages:
- n*
- output: rss
sites:
matrix:
languages:
- en
includes:
- kind: '{home,term,taxonomy}'
- path: '{/books,/books/**}'
taxonomies:
tag: tags
title: Segmentation
baseURL = 'https://example.org/'
defaultContentLanguage = 'en'
defaultContentLanguageInSubdir = true
title = 'Segmentation'
[languages]
[languages.en]
direction = 'ltr'
label = 'English'
locale = 'en-US'
weight = 1
[languages.nb]
direction = 'ltr'
label = 'Bokmål'
locale = 'nb-NO'
weight = 2
[languages.nn]
direction = 'ltr'
label = 'Norsk'
locale = 'nn-NO'
weight = 3
[segments]
[segments.segment1]
[[segments.segment1.excludes]]
[segments.segment1.excludes.sites]
[segments.segment1.excludes.sites.matrix]
languages = ['n*']
[[segments.segment1.excludes]]
output = 'rss'
[segments.segment1.excludes.sites]
[segments.segment1.excludes.sites.matrix]
languages = ['en']
[[segments.segment1.includes]]
kind = '{home,term,taxonomy}'
[[segments.segment1.includes]]
path = '{/books,/books/**}'
[taxonomies]
tag = 'tags'
{
"baseURL": "https://example.org/",
"defaultContentLanguage": "en",
"defaultContentLanguageInSubdir": true,
"languages": {
"en": {
"direction": "ltr",
"label": "English",
"locale": "en-US",
"weight": 1
},
"nb": {
"direction": "ltr",
"label": "Bokmål",
"locale": "nb-NO",
"weight": 2
},
"nn": {
"direction": "ltr",
"label": "Norsk",
"locale": "nn-NO",
"weight": 3
}
},
"segments": {
"segment1": {
"excludes": [
{
"sites": {
"matrix": {
"languages": [
"n*"
]
}
}
},
{
"output": "rss",
"sites": {
"matrix": {
"languages": [
"en"
]
}
}
}
],
"includes": [
{
"kind": "{home,term,taxonomy}"
},
{
"path": "{/books,/books/**}"
}
]
}
},
"taxonomies": {
"tag": "tags"
},
"title": "Segmentation"
}
When you run this command:
hugo build --renderSegments segment1The published project has this structure:
public/
├── en/
│ ├── books/
│ │ ├── book-1/
│ │ │ └── index.html
│ │ └── index.html
│ ├── tags/
│ │ ├── tag-a/
│ │ │ └── index.html
│ │ ├── tag-b/
│ │ │ └── index.html
│ │ └── index.html
│ └── index.html
└── index.html