dc7a9ae12content: Update JS options07d3d8803npm: Use tilde ranges for versionse06362a13Minor grammar fixesc42db0838content: Algolia DocSearch clarificationd67412b82deps: Upgrade to TailwindCSS 4.1.0da1fb12d3theme: Update Lato font path for images.Text example140fa3bb9Update introduction.md7b1fcca27content: Fix links to embedded template source908a55532theme: Improve dark mode syntax highlighting backgroundd830e5962Update XxHash.md807be5dbfUpdate Defer.md4b56693f6content: Update GitHub Pages sample workflow57b766fbaUpdate TailwindCSS.md64982ab6aUpdate TailwindCSS.md1b6e879c6Update TailwindCSS.mde67cbcdd2content: Add caching to the GitHub Pages workflow exampleb7ca3b07cUpdate index.md14e1a3977Update XxHash.md980f344aeUpdate XxHash.md6b815f03etheme: Remove aria-label attribute from links1c00bbc45theme: Update npm dependencies533149bf9theme: Include section pages in related content2b6bda6bdcontent: Fix typos92ce95cbbcontent: Miscellaneous editsf12936681theme: Add a simple LRU cache for search339ca3388Fix the previous broken commit8537e59e0Make it into a non-linke13f17d29Add Algolia logo/link to the search listing dialogd3e09e886content: Fix typo7217f64b6content: Miscellaneous edits51aa1ae73content: Improve some examples8ebaa53f9theme: Adjust shortcodes898870438theme: Hide anchor until mouse-overc933ea237content: More front matter cleanupf26ca047acontent: Miscellaneous editse272b2039theme: Fix inline partial refsc540e6d29content: Replace note shortcode calls with blockquote alertsaef899bc5theme: Add title and QR code when printingbd46ef626theme: Implement blockquote render hookddefbefaacontent: Improve contributing pagef41d28ee1content: Adjust usage of whitespace removal with action delimiters03315336dtheme: Use full title in related content sidebar4f4076364misc: Document the front matter fields used on this site2f78d7632misc: Fix gitignore file7de6dbab3content: Fix front matter for several function pages5d3542ea6config: Disable tags taxonomy68bc28d67content: Add linkTitle to shortcode pages5f32c92edtheme: Restore deduplication logic for related itemsa943a4bb8theme: Implement related content toolingfd628be6econtent: Replace calls to the code shortcode with fenced code blocksb23c9a583content: Fix position of new-in badgesec056f251content: Fix typo245351c84theme: Adjust spacing in highlighting theme examples2fcd21ee5content: Remove "related" array from function and method pages71d8426eetheme: Create code block render hook4cdde6649theme: Adjust number of news items to display34ab45261content: Miscellaneous editsb6cae5cbccontent: Consolidate configuration documentation727ef6f66theme: Fix overflow issue for wide tablesc4f759e01Add es202493cce62c8Update support list of more recent targets with js.Build / esbuild974d0655fUpdate hosting-on-codeberg.mdb3f0ed9cecontent: Add hosting instructions for Codeberg Pages8217c0900content: Correct the SourceHut repository URLa8cf3d28fcontent: Add hosting instructions for SourceHut Pages8c059cbe1theme: Use content adapter for news section03938c600Remove some old new-in5c50a75e8content: Fix typo5cf89f2f6theme: Re-enable banner gtag outbound link tracking3c555d5f8One more copy button94bce999aAdd some copy buttons9e1cc0c2bUpdate PortableText.mddd26ac49fDocument transform.PortableText5f632ab32netlify: Hugo 0.145.059e057bb4Update index.mdd07e07d6cRemove some unused home page front matterd482657b7Add footnote about alias to the build front matter keyf0629b77ccontent: Fix typod91c4cccftheme: Fix news items URLsca931cd1ftheme: Restore RSS feed for news sectione6b870bc9theme: Adjust copy-to-clipboard button071851431Update netlify.tomle68431034theme: Format layoutsaa3cd839atheme: Format assets22ad3eee3theme: Add some more space on the right for copy buttonsf4a19083anetlify: Hugo 0.144.1974cb8795theme: Remove Internet Explorer configs (#2929)4a23a1f41content: Fix typosd49f15d03theme: Get the scrollspy back working720c7ff67config: Evict getresource cache hourly1f62ca97etheme: Hide the Turbo progress barf6449ace3Move the dark class up to the html element074cd1a07content: Miscellaneous editse098a7716content: Miscellaneous edits8e1e104aacontent: Updates for v0.144.018e1aa916theme: Add anchor links via JSac3b5505cClose new-inbfa4db6b8netlify: Hugo 0.144.0fd6e7feeeRegen CLI docsda9d1218aRegen docs helper5de494dedMerge branch 'tempv0.144.0'f683e6469theme: Minor improvements to base templateef8bf89d8resources/page: Revise the new contentbasename permalinks tokens901adb07bresources/page: Add :contentbasename and :contentbasenameorslug permalink tokens0fca8ef25all: Change shortcode usage and design to prevent invalid HTMLc41d76613Update RegularPagesRecursive.mde93574748content: Correct return type for strings.Split3d504abbaRevert "content: Add ids to the Netlify steps"c08c8e15econtent: Add ids to the Netlify steps5a3b470a2theme: "move" the id from content to articleb878613aatheme: Add page kind as a CSS class to body269657e8fcontent: Fix formatting error664f6c92fconfig: Change image cache locationfa6b719b1theme: Reduce ToC to level 2-4bc16341catheme: Render mathematical markup with transform.ToMath14bf9dc70theme: Add aria-label attribute to search fieldsed42af5b3theme: Adjust search field and search activationf042e4970dev: Add prettier-plugin-void-htmlf6ec83533content: Fix inline shortcode example0a74210e2theme: Remove readfile shortcode334ca06actheme: Fix some dark mode accessibility as reported by Axe core02626ff92theme: aria-lbabel => aria-label227b76ab7Add Prettier and config43ab22428content: Bump minimum required Go version to 1.23.003e54683fmodules: Add GOAUTH to module config9f06a3b9cjs/esbuild: Add drop optionddcd99369Merge commit 'a024bc7d76fcc5e49e8210f9b0896db9ef21861a'733731253helpers: Add Chroma styles to docs.yaml git-subtree-dir: docs git-subtree-split:dc7a9ae127
6.9 KiB
title, description, categories, keywords, params
| title | description | categories | keywords | params | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| resources.GetRemote | Returns a remote resource from the given URL, or nil if none found. |
|
{{< new-in 0.141.0 >}}
The Err method on the returned resource was removed in v0.141.0.
Use the try statement instead, as shown in the error handling example below.
{{< /new-in >}}
{{ $url := "https://example.org/images/a.jpg" }}
{{ with try (resources.GetRemote $url) }}
{{ with .Err }}
{{ errorf "%s" . }}
{{ else with .Value }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ else }}
{{ errorf "Unable to get remote resource %q" $url }}
{{ end }}
{{ end }}
Options
The resources.GetRemote function takes an optional map of options.
body
(string) The data you want to transmit to the server.
headers
(map[string][]string) The collection of key-value pairs that provide additional information about the request.
key
(string) The cache key. Hugo derives the default value from the URL and options map. See caching.
method
(string) The action to perform on the requested resource, typically one of GET, POST, or HEAD.
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.
Options examples
Note
For brevity, the examples below do not include error handling.
To include a header:
{{ $url := "https://example.org/api" }}
{{ $opts := dict
"headers" (dict "Authorization" "Bearer abcd")
}}
{{ $resource := resources.GetRemote $url $opts }}
To specify more than one value for the same header key, use a slice:
{{ $url := "https://example.org/api" }}
{{ $opts := dict
"headers" (dict "X-List" (slice "a" "b" "c"))
}}
{{ $resource := resources.GetRemote $url $opts }}
To post data:
{{ $url := "https://example.org/api" }}
{{ $opts := dict
"method" "post"
"body" `{"complete": true}`
"headers" (dict "Content-Type" "application/json")
}}
{{ $resource := resources.GetRemote $url $opts }}
To override the default cache key:
{{ $url := "https://example.org/images/a.jpg" }}
{{ $opts := dict
"key" (print $url (now.Format "2006-01-02"))
}}
{{ $resource := resources.GetRemote $url $opts }}
To extract specific headers from the server's response:
{{ $url := "https://example.org/images/a.jpg" }}
{{ $opts := dict
"method" "HEAD"
"responseHeaders" (slice "X-Frame-Options" "Server")
}}
{{ $resource := resources.GetRemote $url $opts }}
Remote data
When retrieving remote data, use the transform.Unmarshal function to unmarshal the response.
{{ $data := dict }}
{{ $url := "https://example.org/books.json" }}
{{ with try (resources.GetRemote $url) }}
{{ with .Err }}
{{ errorf "%s" . }}
{{ else with .Value }}
{{ $data = . | transform.Unmarshal }}
{{ else }}
{{ errorf "Unable to get remote resource %q" $url }}
{{ end }}
{{ end }}
Note
When retrieving remote data, a misconfigured server may send a response header with an incorrect Content-Type. For example, the server may set the Content-Type header to
application/octet-streaminstead ofapplication/json.In these cases, pass the resource
Contentthrough thetransform.Unmarshalfunction instead of passing the resource itself. For example, in the above, do this instead:
{{ $data = .Content | transform.Unmarshal }}
Error handling
Use the try statement to capture HTTP request errors. If you do not handle the error yourself, Hugo will fail the build.
Note
Hugo does not classify an HTTP response with status code 404 as an error. In this case
resources.GetRemotereturns nil.
{{ $url := "https://broken-example.org/images/a.jpg" }}
{{ with try (resources.GetRemote $url) }}
{{ with .Err }}
{{ errorf "%s" . }}
{{ else with .Value }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ else }}
{{ errorf "Unable to get remote resource %q" $url }}
{{ end }}
{{ end }}
To log an error as a warning instead of an error:
{{ $url := "https://broken-example.org/images/a.jpg" }}
{{ with try (resources.GetRemote $url) }}
{{ with .Err }}
{{ warnf "%s" . }}
{{ else with .Value }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ else }}
{{ warnf "Unable to get remote resource %q" $url }}
{{ end }}
{{ end }}
HTTP response
The Data method on a resource returned by the resources.GetRemote function returns information from the HTTP response.
Caching
Resources returned from resources.GetRemote are cached to disk. See configure file caches for details.
By default, Hugo derives the cache key from the arguments passed to the function. Override the cache key by setting a key in the options map. Use this approach to have more control over how often Hugo fetches a remote resource.
{{ $url := "https://example.org/images/a.jpg" }}
{{ $cacheKey := print $url (now.Format "2006-01-02") }}
{{ $opts := dict "key" $cacheKey }}
{{ $resource := resources.GetRemote $url $opts }}
Security
To protect against malicious intent, the resources.GetRemote function inspects the server response including:
- The Content-Type in the response header
- The file extension, if any
- The content itself
If Hugo is unable to resolve the media type to an entry in its allowlist, the function throws an error:
ERROR error calling resources.GetRemote: failed to resolve media type...
For example, you will see the error above if you attempt to download an executable.
Although the allowlist contains entries for common media types, you may encounter situations where Hugo is unable to resolve the media type of a file that you know to be safe. In these situations, edit your site configuration to add the media type to the allowlist. For example:
{{< code-toggle file=hugo >}} [security.http] mediaTypes = ['^image/avif$','^application/vnd.api+json$'] {{< /code-toggle >}}
Note that the entry above is:
- An addition to the allowlist; it does not replace the allowlist
- An array of regular expressions