CSS Ressources Postprocessing breaks permalink when used in multiple output formats

Hi there,

I faced a very strange issue.
All my website is generated in HTML format and was working fine.
But I get the issue when I added a PHP format for a contact form.
Hugo version is v0.81

I use a partial to load my resources, here is the code :

    Loads a resources

    Example :
    {{ partial "tools/resource-load.html" (dict "Page" . "resource" $resourcevar) }}
    {{ partial "tools/resource-load.html" (dict "Page" . "resource" $resourcevar "option" "onload=\"renderMathInElement(document.body,{throwOnError:false});\"") }}

    Parameters :
    - Page is the current page '.' context
    - resource is a resource variable
    - option is a string to add in final tag

{{- $resource := .resource -}}
{{- $restype := (string $resource.MediaType) -}}
{{- $resintegrity := false -}}
{{- $relbase := "" -}}

{{- if eq $restype "text/css" -}}
    {{- $resource = $resource | resources.PostCSS (dict "config" "assets/cfg/") -}}
{{- end -}}

{{- if .Page.Param "res-minify" -}}
    {{- $resource = $resource | resources.Minify -}}
{{- end -}}

{{- if .Page.Param "res-fingerprint" -}}
    {{- $resource = $resource | resources.Fingerprint (.Page.Param "res-hash") -}}
    {{- $resintegrity = true -}}
{{- end -}}

{{- if eq $restype "text/css" -}}
    {{- if .Page.Param "res-postprocess" -}}
        {{- $resource = $resource | resources.PostProcess -}}
    {{- end -}}
{{- end -}} 

{{- if eq $restype "application/javascript" -}}       
    <script defer src="{{ $resource.RelPermalink }}" {{- if $resintegrity }} integrity="{{ $resource.Data.Integrity }}"{{ end -}} {{- with .option }} {{ . | safeHTMLAttr }}{{ end -}}></script>
{{ else if eq $restype "text/css" -}}
    <link rel="stylesheet" href="{{ $relbase }}{{ $resource.RelPermalink }}" {{- if $resintegrity }} integrity="{{ $resource.Data.Integrity }}"{{ end -}} {{- with .option }} {{ . | safeHTMLAttr }}{{ end -}}>
{{- else -}}
   {{ errorf "Cannot load resource %s, resource type %s is not compatible" $resource $resource.MediaType }}
{{- end -}}

HTML output for that partial is
<link rel=stylesheet href=/css/konidocs.min.0daeee14164f8f28b32af1ac3cbb6f28e0a560209ff443264ab8e9c6427233aa.css integrity=sha256-Da7uFBZPjyizKvGsPLtvKOClYCCf9EMmSrjpxkJyM6o=>

This is normal :slight_smile:

PHP output for that partial is
<link rel=stylesheet href=__h_pp_l1_1_RelPermalink__e integrity=__h_pp_l1_1_Data.Integrity__e>
This is a failure :frowning:

For information, I duplicated my layouts baseof.html and single.html to baseof.php and single.php
So the resources-load.html partial is called twice with the same resource.

By checking my partial, I found that issue was located inside the PostProcess block. I managed to fix the issue by changing my partial and moving postprocess part to the end.

It looks like an issue, please check if you need more data from my side.


If it stopped working when you added PHP as outputformat (paradox?) then let’s post how you configure PHP as outputformat please :slight_smile:

Let me give more explanations first.
When I add PHP format, HTML format still works fine, but PHP format output is broken. Both format are using the same partials.

Here is what I added in my config.toml to create a php output format

    name = "php"
    baseName = "index"
    mediaType = "text/x-php"
    permalinkable = true
    isHTML = true
    isPlainText = false
    noUgly = true

    suffixes = ["php"]

Then in my page frontmatter I added :

outputs = ["php"]

To bo honest it makes me feel like it is an issue when calling twice resources.PostProcess function on the same ressource, because moving this block at the end of my partial solved the issue.

{{- if eq $restype "text/css" -}}
    {{- if .Page.Param "res-postprocess" -}}
        {{- $resource = $resource | resources.PostProcess -}}
    {{- end -}}
{{- end -}} 

Hi there @koni ,

It’s easier to help you if we can replicate your issue exactly. Please include a link to your repo where we can see your site code. See: Requesting Help

Hi @pointyfar,
Code is available there : GitHub - konilabs/konidoctmp

Partial template is available there : konidoctmp/resource-load.html at master · konilabs/konidoctmp · GitHub

I added a commit so you can see the difference between working and non working version.

File that fail to render is : konidoctmp/contact.md at master · konilabs/konidoctmp · GitHub

Please let me know if you need anything else.

See docs here: PostProcess | Hugo

There are currently two limitations to this:

  1. This only works in *.html templates (i.e. templates that produces HTML files).

I agree with that, but PHP is a HTML file with renamed extension.
My worry is that Hugo produces a strange output without any error and for me it looks really like an issue or a corner case.
You are certainly more used to Hugo internals, if you think this is normal then we can close the point.