2019-10-21 04:22:28 -04:00
---
title: Hugo Pipes Introduction
2021-12-13 15:04:12 -05:00
linkTitle: Hugo Pipes
2019-10-21 04:22:28 -04:00
description: Hugo Pipes is Hugo's asset processing set of functions.
date: 2018-07-14
publishdate: 2018-07-14
lastmod: 2018-07-14
categories: [asset management]
keywords: []
menu:
docs:
parent: "pipes"
weight: 20
weight: 01
sections_weight: 01
draft: false
2021-12-13 15:04:12 -05:00
toc: true
2019-10-21 04:22:28 -04:00
aliases: [/assets/]
2020-06-16 08:18:51 -04:00
---
2019-10-21 04:22:28 -04:00
2022-01-12 02:15:10 -05:00
## Get Resource with resources.Get and resources.GetRemote
2019-10-21 04:22:28 -04:00
2022-01-12 02:15:10 -05:00
In order to process an asset with Hugo Pipes, it must be retrieved as a `Resource` using `resources.Get` or `resources.GetRemote` .
With `resources.Get` , the first argument is a local path relative to the `assets` directory/directories:
2021-12-13 15:04:12 -05:00
```go-html-template
{{ $local := resources.Get "sass/main.scss" }}
```
2022-01-12 02:15:10 -05:00
With `resources.GetRemote` , the first argument is a remote URL:
```go-html-template
{{ $remote := resources.GetRemote "https://www.example.com/styles.scss" }}
```
`resources.Get` and `resources.GetRemote` return `nil` if the resource is not found.
2021-12-13 15:04:12 -05:00
### Error Handling
2022-01-12 02:15:10 -05:00
{{< new-in " 0 . 91 . 0 " > }}
2021-12-13 15:04:12 -05:00
2022-01-12 02:15:10 -05:00
The return value from `resources.GetRemote` includes an `.Err` method that will return an error if the call failed. If you want to just log any error as a `WARNING` you can use a construct similar to the one below.
2021-12-13 15:04:12 -05:00
```go-html-template
2022-01-12 02:15:10 -05:00
{{ with resources.GetRemote "https://gohugo.io/images/gohugoio-card-1.png" }}
2021-12-13 15:04:12 -05:00
{{ with .Err }}
{{ warnf "%s" . }}
{{ else }}
< img src = "{{ .RelPermalink }}" width = "{{ .Width }}" height = "{{ .Height }}" alt = "" >
{{ end }}
{{ end }}
```
Note that if you do not handle `.Err` yourself, Hugo will fail the build the first time you start using the `Resource` object.
### Remote Options
2022-01-12 02:15:10 -05:00
When fetching a remote `Resource` , `resources.GetRemote` takes an optional options map as the last argument, e.g.:
2019-10-21 04:22:28 -04:00
2021-12-13 15:04:12 -05:00
```go-html-template
2022-01-12 02:15:10 -05:00
{{ $resource := resources.GetRemote "https://example.org/api" (dict "headers" (dict "Authorization" "Bearer abcd")) }}
2021-12-13 15:04:12 -05:00
```
2019-10-21 04:22:28 -04:00
2021-12-13 15:04:12 -05:00
If you need multiple values for the same header key, use a slice:
2019-10-21 04:22:28 -04:00
```go-html-template
2022-01-12 02:15:10 -05:00
{{ $resource := resources.GetRemote "https://example.org/api" (dict "headers" (dict "X-List" (slice "a" "b" "c"))) }}
2019-10-21 04:22:28 -04:00
```
2021-12-13 15:04:12 -05:00
You can also change the request method and set the request body:
```go-html-template
2022-01-12 02:15:10 -05:00
{{ $postResponse := resources.GetRemote "https://example.org/api" (dict
2021-12-13 15:04:12 -05:00
"method" "post"
"body" `{"complete": true}`
"headers" (dict
"Content-Type" "application/json"
)
)}}
```
### Caching of Remote Resources
2022-01-12 02:15:10 -05:00
Remote resources fetched with `resources.GetRemote` will be cached on disk. See [Configure File Caches ](/getting-started/configuration/#configure-file-caches ) for details.
2019-10-21 04:22:28 -04:00
2021-12-13 15:04:12 -05:00
## Asset directory
2019-10-21 04:22:28 -04:00
2021-12-13 15:04:12 -05:00
Asset files must be stored in the asset directory. This is `/assets` by default, but can be configured via the configuration file's `assetDir` key.
### Asset Publishing
2022-01-12 02:15:10 -05:00
Hugo publishes assets to the to the `publishDir` (typically `public` ) when you invoke `.Permalink` , `.RelPermalink` , or `.Publish` . You can use `.Content` to inline the asset.
2021-12-13 15:04:12 -05:00
## Go Pipes
2019-10-21 04:22:28 -04:00
For improved readability, the Hugo Pipes examples of this documentation will be written using [Go Pipes ](/templates/introduction/#pipes ):
2021-12-13 15:04:12 -05:00
2019-10-21 04:22:28 -04:00
```go-html-template
{{ $style := resources.Get "sass/main.scss" | resources.ToCSS | resources.Minify | resources.Fingerprint }}
< link rel = "stylesheet" href = "{{ $style.Permalink }}" >
```
2021-12-13 15:04:12 -05:00
## Method aliases
2019-10-21 04:22:28 -04:00
Each Hugo Pipes `resources` transformation method uses a __camelCased__ alias (`toCSS` for `resources.ToCSS` ).
Non-transformation methods deprived of such aliases are `resources.Get` , `resources.FromString` , `resources.ExecuteAsTemplate` and `resources.Concat` .
The example above can therefore also be written as follows:
2021-12-13 15:04:12 -05:00
2019-10-21 04:22:28 -04:00
```go-html-template
{{ $style := resources.Get "sass/main.scss" | toCSS | minify | fingerprint }}
< link rel = "stylesheet" href = "{{ $style.Permalink }}" >
```
2020-08-14 12:31:01 -04:00
2021-12-13 15:04:12 -05:00
## Caching
2020-08-14 12:31:01 -04:00
Hugo Pipes invocations are cached based on the entire _pipe chain_ .
An example of a pipe chain is:
```go-html-template
{{ $mainJs := resources.Get "js/main.js" | js.Build "main.js" | minify | fingerprint }}
```
The pipe chain is only invoked the first time it is encountered in a site build, and results are otherwise loaded from cache. As such, Hugo Pipes can be used in templates which are executed thousands or millions of times without negatively impacting the build performance.