| 1 |
--- |
| 2 |
title: Configure the HTTP cache |
| 3 |
linkTitle: HTTP cache |
| 4 |
description: Configure the HTTP cache. |
| 5 |
categories: [] |
| 6 |
keywords: [] |
| 7 |
--- |
| 8 |
|
| 9 |
> [!NOTE] |
| 10 |
> This configuration is only relevant when using the [`resources.GetRemote`][] function. |
| 11 |
|
| 12 |
## Layered caching |
| 13 |
|
| 14 |
Hugo employs a layered caching system. |
| 15 |
|
| 16 |
```goat {.w-40} |
| 17 |
.-----------. |
| 18 |
| dynacache | |
| 19 |
'-----+-----' |
| 20 |
| |
| 21 |
v |
| 22 |
.----------. |
| 23 |
| HTTP cache | |
| 24 |
'-----+----' |
| 25 |
| |
| 26 |
v |
| 27 |
.----------. |
| 28 |
| file cache | |
| 29 |
'-----+----' |
| 30 |
``` |
| 31 |
|
| 32 |
Dynacache |
| 33 |
: An in-memory cache employing a Least Recently Used (LRU) eviction policy. Entries are removed from the cache when changes occur, when they match [cache-busting][] patterns, or under low-memory conditions. |
| 34 |
|
| 35 |
HTTP Cache |
| 36 |
: An HTTP cache for remote resources as specified in [RFC 9111][]. Optimal performance is achieved when resources include appropriate HTTP cache headers. The HTTP cache utilizes the file cache for storage and retrieval of cached resources. |
| 37 |
|
| 38 |
File cache |
| 39 |
: See [configure file caches][]. |
| 40 |
|
| 41 |
The HTTP cache involves two key aspects: determining which content to cache (the caching process itself) and defining the frequency with which to check for updates (the polling strategy). |
| 42 |
|
| 43 |
## HTTP caching |
| 44 |
|
| 45 |
The HTTP cache behavior is defined for a configured set of resources. Stale resources will be refreshed from the file cache, even if their configured Time-To-Live (TTL) has not expired. If HTTP caching is disabled for a resource, Hugo will bypass the cache and access the file directly. |
| 46 |
|
| 47 |
This is the default configuration for HTTP caching: |
| 48 |
|
| 49 |
{{< code-toggle config=HTTPCache />}} |
| 50 |
|
| 51 |
`respectCacheControlNoStoreInRequest` |
| 52 |
: {{< new-in 0.151.0 />}} |
| 53 |
: (`bool`) Whether to respect the `no-store` directive in the server's `Cache-Control` request header when fetching remote resources via the [`resources.GetRemote`][] function. Default is `true`. |
| 54 |
|
| 55 |
`respectCacheControlNoStoreInResponse` |
| 56 |
: {{< new-in 0.151.0 />}} |
| 57 |
: (`bool`) Whether to respect the `no-store` directive in the server's `Cache-Control` response header when fetching remote resources via the [`resources.GetRemote`][] function. Default is `false`. |
| 58 |
|
| 59 |
`cache.for.excludes` |
| 60 |
: (`[]string`) A slice of [glob patterns](g) to exclude from caching. In its default configuration HTTP caching excludes all files. |
| 61 |
|
| 62 |
`cache.for.includes` |
| 63 |
: (`[]string`) A slice of [glob patterns](g) to cache. |
| 64 |
|
| 65 |
`polls` |
| 66 |
: (`[]PollConfig`) A slice of polling configurations. |
| 67 |
|
| 68 |
`polls.disable` |
| 69 |
: (`bool`) Whether to disable polling for this configuration. Default is `true`. |
| 70 |
|
| 71 |
`polls.high` |
| 72 |
: (`string`) The maximum polling interval expressed as a [duration](g). This is used when the resource is considered stable. Default is `0s`. |
| 73 |
|
| 74 |
`polls.low` |
| 75 |
: (`string`) The minimum polling interval expressed as a [duration](g). This is used after a recent change and gradually increases towards `polls.high`. Default is `0s`. |
| 76 |
|
| 77 |
`polls.for.excludes` |
| 78 |
: (`[]string`) A slice of [glob patterns](g) to exclude from polling for this configuration. |
| 79 |
|
| 80 |
`polls.for.includes` |
| 81 |
: (`[]string`) A slice of [glob patterns](g) to include in polling for this configuration. |
| 82 |
|
| 83 |
## HTTP polling |
| 84 |
|
| 85 |
Polling is used in watch mode (e.g., `hugo server`) to detect changes in remote resources. Polling can be enabled even if HTTP caching is disabled. Detected changes trigger a rebuild of pages using the affected resource. Polling can be disabled for specific resources, typically those known to be static. |
| 86 |
|
| 87 |
The default configuration disables everything: |
| 88 |
|
| 89 |
{{< code-toggle file=hugo >}} |
| 90 |
[[HTTPCache.polls]] |
| 91 |
disable = true |
| 92 |
high = '0s' |
| 93 |
low = '0s' |
| 94 |
[HTTPCache.polls.for] |
| 95 |
includes = ['**'] |
| 96 |
excludes = [] |
| 97 |
{{< /code-toggle >}} |
| 98 |
|
| 99 |
`polls` |
| 100 |
: (`[]PollConfig`) A slice of polling configurations. |
| 101 |
|
| 102 |
`polls.disable` |
| 103 |
: (`bool`) Whether to disable polling for this configuration. Default is `true`. |
| 104 |
|
| 105 |
`polls.high` |
| 106 |
: (`string`) The maximum polling interval expressed as a [duration](g). This is used when the resource is considered stable. Default is `0s`. |
| 107 |
|
| 108 |
`polls.low` |
| 109 |
: (`string`) The minimum polling interval expressed as a [duration](g). This is used after a recent change and gradually increases towards `polls.high`. Default is `0s`. |
| 110 |
|
| 111 |
`polls.for.excludes` |
| 112 |
: (`[]string`) A list of [glob patterns](g) to exclude from polling for this configuration. |
| 113 |
|
| 114 |
`polls.for.includes` |
| 115 |
: (`[]string`) A list of [glob patterns](g) to include in polling for this configuration. |
| 116 |
|
| 117 |
## Behavior |
| 118 |
|
| 119 |
Polling and HTTP caching interact as follows: |
| 120 |
|
| 121 |
- With polling enabled, rebuilds are triggered only by actual changes, detected via `eTag` changes (Hugo generates an MD5 hash if the server doesn't provide one). |
| 122 |
- If polling is enabled but HTTP caching is disabled, the remote is checked for changes only after the file cache's TTL expires (e.g., a `maxAge` of `10h` with a `1s` polling interval is inefficient). |
| 123 |
- If both polling and HTTP caching are enabled, changes are checked for even before the file cache's TTL expires. Cached `eTag` and `last-modified` values are sent in `if-none-match` and `if-modified-since` headers, respectively, and a cached response is returned on HTTP [304][]. |
| 124 |
|
| 125 |
## Example |
| 126 |
|
| 127 |
Consider a site that renders remote data fetched with the [`resources.GetRemote`][] function. With the default configuration Hugo bypasses the HTTP cache for every remote resource, and polling is disabled. As a result, while running `hugo server` you will not detect changes to the remote data until you restart the server. |
| 128 |
|
| 129 |
The configuration below enables HTTP caching for remote JSON resources, and polls them for changes while watching. Hugo checks as often as every 10 seconds after a recent change, then backs off towards once every 5 minutes when the resource is stable: |
| 130 |
|
| 131 |
{{< code-toggle file=hugo >}} |
| 132 |
[HTTPCache.cache.for] |
| 133 |
includes = ['**.json'] |
| 134 |
excludes = [] |
| 135 |
|
| 136 |
[[HTTPCache.polls]] |
| 137 |
disable = false |
| 138 |
high = '5m' |
| 139 |
low = '10s' |
| 140 |
[HTTPCache.polls.for] |
| 141 |
includes = ['**.json'] |
| 142 |
excludes = [] |
| 143 |
{{< /code-toggle >}} |
| 144 |
|
| 145 |
The glob patterns are matched against the full remote URL, using `/` as the path separator. With this configuration Hugo will: |
| 146 |
|
| 147 |
- Cache responses for remote JSON resources in the [file cache][configure file caches], honoring the `ETag` and `Last-Modified` headers returned by the server. |
| 148 |
- Detect changes to those resources while watching, triggering a rebuild of every page that depends on them. |
| 149 |
|
| 150 |
[304]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/304 |
| 151 |
[RFC 9111]: https://datatracker.ietf.org/doc/html/rfc9111 |
| 152 |
[`resources.GetRemote`]: /functions/resources/getremote/ |
| 153 |
[cache-busting]: /configuration/build/#cache-busters |
| 154 |
[configure file caches]: /configuration/caches/ |