gohugoio / hugo

The world’s fastest framework for building websites.
https://gohugo.io
Apache License 2.0
73.52k stars 7.38k forks source link

Hugo server - Race conditon/timing issue when using generated css in html #12532

Open hlpmenu opened 1 month ago

hlpmenu commented 1 month ago

What version of Hugo are you using (0.126.1)?

$ hugo version:
0.125.x and 0.126.x

Does this issue reproduce with the latest release?

Yes

Setup

Hugo 1.125/126 PostCss Node 22

Issue

When using hugo pipes to process/generate a css file, and then using the css file in the html, since 0.125+ hugo tries to render the html pages before the css has ben processed, causing any change in a file detected by livereload to cause hugo server to fail, needing to be manually restarted.

This is due to the resource trying to be accessed not yet existing with message

Rebuild failed: render: failed to render pages: render of "page" failed: "/REDACTED/file.html:4:3": execute of template failed at <partial "head-content.html" .>: error calling partial: "/REDACTED/file2.html:114:27": execute of template failed at <$processedCSS.Content>: error calling Content: file does not exist

Hugo server is ran like:

hugo server --noHTTPCache --disableFastRender --ignoreCache --gc

Restarting hugo server fixes it, and its only when livereload triggers a refresh.

Example: htmlfile.html

{{/* Combine, purge, minify and cssnano these css files */}}
{{ $twCSS := resources.Get "assets/css/file1.css"}}
{{ $mainCSS := resources.Get "assets/css/main.css" }}
{{ $tailwindDevCSS := resources.Get "assets/css/file2.css"}}
{{ $mdCSS := resources.Get "assets/css/markdown.css" }}
{{ $gradiaCSS := resources.Get "assets/css/file3.css"}}
{{ $combinedCSS := slice  $file1 $file2 $file3 $mainCSS $mdCSS | resources.Concat "assets/css/outputfilename.css" }}
{{ $processedCSS := $combinedCSS | resources.PostCSS | fingerprint }}

Inlining the css in the same file:

<style keep>

{{printf "%s" $processedCSS.Content | safeCSS}}

</style>

Fix

I havent had time to look trough the code but the issue started from 0.125+ and is only appearing when running hugo server and is consistently happening on every change livereload detects. Does disablefastrendersomehow not cause a full reload?

jmooring commented 1 month ago

Please edit your description to make it legible.

When posting code, configuration, or data, please wrap the text within backticks or use the </> button in the menu.

my code

hlpmenu commented 1 month ago

Please edit your description to make it legible.

When posting code, configuration, or data, please wrap the text within backticks or use the </> button in the menu.

my code

Sorry, pasted it and the backticks were not kept

jmooring commented 1 month ago

Out of curiosity, what happens if you disable PostCSS when running in development mode?

hlpmenu commented 1 month ago

Out of curiosity, what happens if you disable PostCSS when running in development mode?

So, i did some testing, and removing the postCSS pipe/transform dident do anythng.

However, removing "| fingerprint" DOES fix it.

So: fingerprint + postcss = fails on rebuild postcss only = rebuilds as it should

loveminimal commented 1 month ago

I use scss to gen css , and it has the same problem, like this:

image

Now , I set back to v0.100.2 .