Page Resources Content

Is there any way to prevent a Page Resource from being copied to the publishDir?

I know copying is the most common expected behavior, but if I’m in-lining a page resource using something like the example in the docs:

.Resources.GetMatch "myscript.js").Content

copying that script file over is then redundant, if a minor issue.

Main concern is avoiding confusion. If someone notices a css or js file that’s in the published page bundle directory but NOT linked in the page, they might think something is wrong.

See Build Options | Hugo and the publishResources setting. Put that in a cascade section in your home page.

Note that it will still be published if you do “.RelPermalink”.

1 Like

It’s interesting, I haven’t heard anyone else mention this. In your case, who is the one noticing? Like, another dev, or a site visitor?

Oh, I see @bep answering! Everyone: listen up!


Short answer: no one.

I was working on making a recent project available as a Theme Component, and adding some hooks/customization points etc. I was thinking about if someone was trying to use this in the future, they might notice while setting it up.

Looks perfect, thanks. I’ll play around with it. I was using .RelPermalink, but was asking about .Content as a potential way to not need it to be copied over.

1 Like

Just for anyone else looking into this, here’s what worked (and didn’t):

Given a page bundle with resources as follows (note that there is also a css template resource to be included via resources.ExecuteAsTemplate and .Concat):


with the following in the front matter:

  publishResources: false

In the head (or footer for js) partial, the following will include the pageSpecific Page Resources without copying them directly over:

{{- /* note final path in following not ever used */ -}}
{{- $dynamicCSS := resources.Get "templates/dynamic.css" | resources.ExecuteAsTemplate "/css/compare-dynamic.css" . }} 
{{- $cssResources := .Resources.Match "css/*.css" -}}
{{- $cssResources = $cssResources | append $dynamicCSS}}
{{- $finalCSSPath := print .Page.RelPermalink "css/myBundle.css" }}
{{- $css := $cssResources | resources.Concat $finalCSSPath }}
<link rel="stylesheet" type="text/css" href="{{ $css.RelPermalink }}">

I’m guessing that since .RelPermalink is being called on the output of resources.Concat, that the publishResources: false isn’t being overridden for the pageSpecific.* files. If you didn’t have a template resource to concat together, you might be able to work around .RelPermalink by using Concat on a slice with only one file. Can’t think why it wouldn’t work.

While playing around with different combinations, if you didn’t want to turn off publishResources for the whole bundle (say you had some images in there), the you could have the output from the Concat go to css/pageSpecific.css which overwrites the copied resource. That does only leave you with files in the publishDir that are actually linked:

{{- $finalCSSPath := print .Page.RelPermalink "css/pageSpecific.css" }}

A couple limitations of that:

  • you can only replace one file that way. So if you were picking up more than one css file with the .ResourcesMatch in the page bundle, the others that would be copied and not linked to.
  • this relies on the undocumented order of if the resource is copied before the Concat written out. It did work for me, but not sure it would always.

Final note, I did try using .Resource.Content but it didn’t work when I tried to feed it into a Concat to get it to output (figured was a long shot). Without that I didn’t know a way to output the contents to a file.

Hope that helps someone else sometime.

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