From b444f031857befd8c94e75a653bca7d3119e1db8 Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Sat, 14 May 2022 22:26:04 +0200 Subject: [PATCH] Don't use scratchpad to pass values into partials We previously used .Scratch which is a global scratchpad, that was later passed over to the partial with the rest of the context. The partial then checked if this value was set, and had a fallback if it wasn't (default value). This approach does work in most of the cases, however in about 5% of runs, it fails. I assume this is due to hugo spinning up multiple threads, and each of them is changing this global scratchpad in arbitrary order. At some point, a thread then removed this value from the scratchpad to reset it, but another thread already checked that this variable does exist, and has now set the no longer available variable. To avoid this, we can instead directly pass a custom dict, which contains the original context (.) along with another variable, which will hold the dateformat. This however means that we can't use the simple syntax with a default value, and the dateformat will need to be set each time (though technically, it could be set to nil and checked later, but if we're already specifying it, we may as well pass in this dateformat directly) --- layouts/_default/list.html | 2 +- layouts/partials/list_item.html | 24 +++++++++++------------- layouts/posts/list.html | 3 +-- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/layouts/_default/list.html b/layouts/_default/list.html index 9f27127..741ce83 100644 --- a/layouts/_default/list.html +++ b/layouts/_default/list.html @@ -11,7 +11,7 @@ diff --git a/layouts/partials/list_item.html b/layouts/partials/list_item.html index 1d1a2ca..3d86270 100644 --- a/layouts/partials/list_item.html +++ b/layouts/partials/list_item.html @@ -1,18 +1,16 @@ - -{{ if not (.Scratch.Get "dateformat") }} -{{ .Scratch.Set "dateformat" "Jan 02, 2006" }} -{{ end }} -{{ $dateformat := .Scratch.Get "dateformat" }} - -{{ .Scratch.Set "dateformat" nil }} + - - {{ .Title }} - {{ if not .Date.IsZero }} - {{ .Date.Format $dateformat }} + + {{ .context.Title }} + {{ if not .context.Date.IsZero }} + {{ .context.Date.Format .dateformat }} {{ end }} -{{ partial "content-details.html" . }} +{{ partial "content-details.html" .context }} -

{{ partial "summary.html" . }}

+

{{ partial "summary.html" .context }}

diff --git a/layouts/posts/list.html b/layouts/posts/list.html index c048b0d..ae48a4f 100644 --- a/layouts/posts/list.html +++ b/layouts/posts/list.html @@ -26,8 +26,7 @@