mirror of
https://github.com/gohugoio/hugo.git
synced 2026-08-24 15:28:54 +00:00
Add per-request timeout option to resources.GetRemote
This commit is contained in:
committed by
GitHub
parent
b96d58a144
commit
2d691c7e47
@@ -46,11 +46,12 @@ key
|
||||
method
|
||||
: (`string`) The action to perform on the requested resource, typically one of `GET`, `POST`, or `HEAD`.
|
||||
|
||||
timeout
|
||||
: (`string`) Cancels the request if it does not complete within this duration (e.g. "30s").
|
||||
|
||||
responseHeaders
|
||||
: {{< new-in 0.143.0 />}}
|
||||
: (`[]string`) The headers to extract from the server's response, accessible through the resource's [`Data.Headers`] method. Header name matching is case-insensitive.
|
||||
|
||||
[`Data.Headers`]: /methods/resource/data/#headers
|
||||
: (`[]string`) The headers to extract from the server's response, accessible through the resource's [`Data.Headers`] method. Header name matching is case-insensitive.[`Data.Headers`]: /methods/resource/data/#headers
|
||||
|
||||
## Options examples
|
||||
|
||||
@@ -110,6 +111,20 @@ To extract specific headers from the server's response:
|
||||
{{ $resource := resources.GetRemote $url $opts }}
|
||||
```
|
||||
|
||||
To set a per-request timeout (e.g. when fetching many feeds where a few slow ones should not stall the build):
|
||||
|
||||
```go-html-template
|
||||
{{ $url := "https://example.org/feed.rss" }}
|
||||
{{ $opts := dict "timeout" "10s" }}
|
||||
{{ with try (resources.GetRemote $url $opts) }}
|
||||
{{ with .Err }}
|
||||
{{ warnf "Failed to fetch feed: %s" . }}
|
||||
{{ else with .Value }}
|
||||
{{ $data = . | transform.Unmarshal }}
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
```
|
||||
|
||||
## Remote data
|
||||
|
||||
When retrieving remote data, use the [`transform.Unmarshal`] function to [unmarshal](g) the response.
|
||||
@@ -224,4 +239,5 @@ Note that the entry above is:
|
||||
|
||||
[`try`]: /functions/go-template/try
|
||||
[configure file caches]: /configuration/caches/
|
||||
|
||||
[error handling]: #error-handling
|
||||
|
||||
Reference in New Issue
Block a user