Remote resource failing build when internet is off

The code below fails the build with the hugo command locally when the internet is switched off because it cannot access the remote resource. How to integrate resources.copy to solve this issue?

{{- with site.Config.Services.GoogleAnalytics.ID }}
    {{- if hugo.IsProduction}}
      {{- $minimalAnalytics := "" }}
      {{-  $url := "https://cdn.jsdelivr.net/npm/@minimal-analytics/ga4/dist/index.js" }}
      {{- with resources.GetRemote $url }}
        {{- with .Err }}
        {{- errorf "%s" . }}
        {{- else }}
        {{- $minimalAnalytics = . }}
        {{- end }}
        {{- else }}
        {{- errorf "Unable to get remote resource %q" $url }}
      {{- end }}
      {{- $track := resources.Get "js/analytics/track.js" }}
      {{- $opts := dict "params" (dict "trackingId" . ) }}
      {{- $track = $track | js.Build $opts -}}
      {{- $analytics := slice $track $minimalAnalytics | resources.Concat "js/analytics.js" | minify | fingerprint }}
      <script src="{{ $analytics.RelPermalink }}" integrity="{{ $analytics.Data.Integrity }}" defer></script>
    {{- end }}
  {{- end -}}

Remote resources are cached. You can change the error into warning and see if the cached resource is used.

Remote resources are not cached in your project’s resources directory, but you can change that if you want better visibility into the cache. That would also allow you to check the cached resources into source control.

https://gohugo.io/getting-started/configuration/#configure-file-caches

I have the cache set to 1 hour.

For something like this (getting a script that almost never changes), why only one hour?

I copied the time from Bep’s Tailwind starter template. I never bothered to change it.

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