HUGO

Static pages which aren't rendered, or data pages that aren't json/toml/yaml

Currently I have a website in which I have a many HTML documents that are included in a template as so:

{{ $file := printf "static/theories/%s.html" $site.File.TranslationBaseName}}
{{ if fileExists $file -}}
    {{ readFile $file | safeHTML }}
{{- end }}

However as I use static files, these pages are rendered twice, once at the static link /theories/%s.html and once at the URL defined in the frontmatter. As ~100 of these HTML documents are large (1-22MB), site generation is slowed down by this page duplication.

The solutions here are either:

  • Being able to not publish static files
  • Data files which are not yaml, yml, json, or toml
  • (I’m not sure if I could make them assets, as they are not used in a Hugo Pipe?)

Unfortunately I don’t think put the HTML in the value of a JSON document as the HTML contains single and double quotes.

This is a niche edge case, but it would be useful to have it covered.

You can put it under /assets/, which don’t automatically get rendered. These don’t need to be used in a Hugo Pipe. You could just do, for example:

{{ $foo := resources.Get "foo.html" }}
{{ $foo.Content }}

Or something similar.

I’m not entirely sure I understand what you are asking though, and what your actual setup is. Do you mean you have HTML files under the /static/ folder, and you are including them via readFile?

It would be easier to help you if we could see your site code in a repo somewhere.

Wow I should’ve tested assets out before making this post — it works and my site generates in ~12% of the time it used to. I’ll update with the structure of my project and what’s going on.

I previously posted about my weird site here: Complex use of taxonomy or subpage generation?

My project is structured as follows:

  • content
    • entries
    • theories
  • assets
    • theories

Each theory in the content directory has it’s URL set to entries/%s/theories in the frontmatter so that the theories of any entry can be found at the standard subdirectory.

In layouts/theories/single.html I have the following:

{{- define "main" -}}
{{- $site := . -}}
<main>
  {{ $file := printf "assets/theories/%s.html" $site.File.TranslationBaseName}}
  {{ if fileExists $file -}}
    {{ readFile $file | safeHTML }}
  {{- end }}
</main>

{{- end -}}

Which inserts the HTML from the corresponding file in the assets directory into the template.

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