Debian Codegraph @

docs/content/en/configuration/segments.md

raw
1 ---
2 title: Configure segments
3 linkTitle: Segments
4 description: Configure your site for segmented rendering.
5 categories: []
6 keywords: []
7 ---
8
9 > [!NOTE]
10 > 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.
11
12 Segmented rendering offers several advantages:
13
14 - Faster builds: Process large sites more efficiently.
15 - Rapid development: Render only a subset of your site for quicker iteration.
16 - Scheduled rebuilds: Rebuild specific sections at different frequencies (e.g., home page and news hourly, full site weekly).
17 - Targeted output: Generate specific output formats (like JSON for search indexes).
18
19 ## Segment definition
20
21 Each segment is defined by an `includes` key and an `excludes` key, both of which accept an array of filters.
22
23 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](#fields) to a given [glob pattern](g).
24
25 ### Evaluation rules
26
27 The evaluation logic adheres to three rules:
28
29 - All conditions within a single filter item must match for that filter to evaluate as true, creating an AND relationship.
30 - If the `includes` or `excludes` array contains multiple filters, only one filter needs to evaluate as true for the entire array to match, creating an OR relationship.
31 - The `excludes` array takes absolute precedence. If a page matches any filter in the `excludes` array, Hugo omits it from the segment regardless of whether it matches the `includes` array.
32
33 ### Performance optimization
34
35 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.
36
37 For example, excluding unwanted output formats is faster:
38
39 {{< code-toggle file=hugo >}}
40 [segments]
41 [segments.segment1]
42 [[segments.segment1.excludes]]
43 output = '! json'
44 {{< /code-toggle >}}
45
46 Including only the desired output format is slower:
47
48 {{< code-toggle file=hugo >}}
49 [segments]
50 [segments.segment1]
51 [[segments.segment1.includes]]
52 output = 'json'
53 {{< /code-toggle >}}
54
55 ## Fields
56
57 `kind`
58 : (`string`) A [glob pattern](g) matching the [page kind](g). For example: `{taxonomy,term}`.
59
60 `lang`
61 : {{< deprecated-in 0.153.0 />}}
62 : Use [`sites`](#sites) instead.
63
64 `output`
65 : (`string`) A [glob pattern](g) matching the [output format](g) of the page. For example: `{html,json}`.
66
67 `path`
68 : (`string`) A [glob pattern](g) matching the page's [logical path](g). For example: `{/books,/books/**}`.
69
70 `sites`
71 : {{< new-in 0.153.0 />}}
72 : (`map`) A map to define [sites matrix](g).
73
74 ## Targeting segments
75
76 To specify which segments Hugo builds, add the [`renderSegments`][] setting to your project configuration:
77
78 {{< code-toggle file=hugo >}}
79 renderSegments = ['segment1','segment2']
80 {{< /code-toggle >}}
81
82 Alternatively, pass the segment names directly to the `--renderSegments` command-line flag during a build:
83
84 ```sh
85 hugo build --renderSegments segment1
86 ```
87
88 You can target multiple segments by providing a comma-separated list:
89
90 ```sh
91 hugo build --renderSegments segment1,segment2
92 ```
93
94 ## Example
95
96 <!--
97 To test the example below:
98
99 git clone --single-branch -b segmentation-test https://github.com/jmooring/hugo-testing segmentation-test
100 cd segmentation-test
101 rm -rf public/ && hugo build --renderSegments segment1 && tree public
102 -->
103
104 Consider a project with this content structure:
105
106 ```tree
107 content/
108 ├── books/
109 │ ├── _index.en.md
110 │ ├── _index.nb.md
111 │ ├── _index.nn.md
112 │ ├── book-1.en.md
113 │ ├── book-1.nb.md
114 │ └── book-1.nn.md
115 ├── films/
116 │ ├── _index.en.md
117 │ ├── _index.nb.md
118 │ ├── _index.nn.md
119 │ ├── film-1.en.md
120 │ ├── film-1.nb.md
121 │ └── film-1.nn.md
122 ├── _index.en.md
123 ├── _index.nb.md
124 └── _index.nn.md
125 ```
126
127 And this project configuration:
128
129 {{< code-toggle file=hugo >}}
130 baseURL = 'https://example.org/'
131 title = 'Segmentation'
132 defaultContentLanguage = 'en'
133 defaultContentLanguageInSubdir = true
134
135 [languages.en]
136 direction = 'ltr'
137 label = 'English'
138 locale = 'en-US'
139 weight = 1
140
141 [languages.nb]
142 locale = 'nb-NO'
143 direction = 'ltr'
144 label = 'Bokmål'
145 weight = 2
146
147 [languages.nn]
148 locale = 'nn-NO'
149 direction = 'ltr'
150 label = 'Norsk'
151 weight = 3
152
153 [segments]
154 [segments.segment1]
155 [[segments.segment1.excludes]]
156 [segments.segment1.excludes.sites.matrix]
157 languages = ['n*']
158 [[segments.segment1.excludes]]
159 output = 'rss'
160 [segments.segment1.excludes.sites.matrix]
161 languages = ['en']
162 [[segments.segment1.includes]]
163 kind = '{home,term,taxonomy}'
164 [[segments.segment1.includes]]
165 path = '{/books,/books/**}'
166
167 [taxonomies]
168 tag = 'tags'
169 {{< /code-toggle >}}
170
171 When you run this command:
172
173 ```sh
174 hugo build --renderSegments segment1
175 ```
176
177 The published project has this structure:
178
179 ```tree
180 public/
181 ├── en/
182 │ ├── books/
183 │ │ ├── book-1/
184 │ │ │ └── index.html
185 │ │ └── index.html
186 │ ├── tags/
187 │ │ ├── tag-a/
188 │ │ │ └── index.html
189 │ │ ├── tag-b/
190 │ │ │ └── index.html
191 │ │ └── index.html
192 │ └── index.html
193 └── index.html
194 ```
195
196 [`renderSegments`]: /configuration/all/#rendersegments