How to use resources.copy in this instance

In the code below, how do you use resources.copy so that src points to the local copy? Also, how to apply minify and fingerprint? I checked this example but I am not sure how to include the resources.Copy code within the error check.

  {{- if hugo.IsProduction}}
    {{- $testScript := "" }}
    {{-  $url := "https://example.com/test.js" }}
    {{- with resources.GetRemote $url }}
      {{- with .Err }}
      {{- errorf "%s" . }}
      {{- else }}
      {{- $testScript = . }}
      {{- end }}
      {{- else }}
      {{- errorf "Unable to get remote resource %q" $url }}
    {{- end }}
    <script src="{{ $testScript.RelPermalink }}"></script>
  {{- end }}

I added that to baseof.html

  • separate check for fail on GetRemote and rest (left for you)
  • no need to Copy something - or I did not get the use case
  • md5 just for brevity
<!doctype html>
<html>
   <head>
      {{ partial "getres.html" . }}
   </head>
   <body>
      <main>
         {{ block "main" . }}{{ end }}
      </main>
   </body>
</html>

and the partial:

{{- $url := "https://cdn.jsdelivr.net/npm/dummy@0.1.5/lib/dummy.min.js" -}}
{{- with $res := resources.GetRemote $url -}}
   {{- with ($res | minify | fingerprint) -}}
   <source src="{{- .RelPermalink -}}"></source>
   {{- end -}}
{{- end -}}

as soon as you use the resource in one of your pages it’s saved to disk

PUBLIC
dummy.min_12013928901789625223.min.f2caf9cf6d5d6d2d380bc1d0fa9845ae.js
    index.html

index.html:

<!doctype html>
<html>
   <head>
	<meta name="generator" content="Hugo 0.124.0"><script src="/livereload.js?mindelay=10&amp;v=2&amp;port=1313&amp;path=livereload" data-no-instant defer></script>
      <source src="/dummy.min_12013928901789625223.min.f2caf9cf6d5d6d2d380bc1d0fa9845ae.js"></source>
   </head>
   <body>
      <main>
         
   <h2 id="discourse-49007">Discourse 49007</h2>
      </main>
   </body>
</html>

You didn’t. I need to copy the remote resource locally to link to it. Any tips @jmooring ?

couldn’t you just store the .RelPermalink in a variable and use that one it is located at the root of the generated site

if not here’s a copy snippet

use $noop to force rendering - will create /my.min…js

   {{- $copy := resources.Copy "/my.js" . -}}
   {{- $noop := $copy.RelPermalink -}}

@arif I don’t understand where or why resources.Copy is required. Perhaps I misunderstand the objective.

The easiest/safest way to get a remote JS resource, minify, and fingerprint is (working example):

{{ $url := "https://www.veriphor.com/shared/other/test.js" }}
{{ with resources.GetRemote $url }}
  {{ with .Err }}
    {{ errorf "%s" . }}
  {{ else }}
    {{ with . | minify | fingerprint }}
      <script src="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}" crossorigin="anonymous"></script>
    {{ end }}
  {{ end }}
{{ else }}
  {{ errorf "Unable to get remote resource %q" $url }}
{{ end }}

The site is published to:

public/
├── posts/
│   ├── post-1/
│   │   └── index.html
│   └── index.html
├── favicon.ico
├── index.html
└── test_13636365218788771077.min.41e690adda8eedd2839b283f4329347519a2c4d1734bcdf4a7e6cb97715ad356.js
1 Like

I need to rename the remote file locally for easier identification. But you just gave me an idea.

{{ $url := "https://example.com/test.js" }}
  {{ with resources.GetRemote $url }}
  {{ with .Err }}
    {{ errorf "%s" . }}
  {{ else }}
    {{ with . }}
      {{ with resources.Copy "js/local.js" . | minify | fingerprint }}
         <script src="{{ .RelPermalink }}"></script>
      {{- end }}
    {{ end }}
  {{ end }}
 {{ else }}
  {{ errorf "Unable to get remote resource %q" $url }}
 {{ end }}

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.