gohugoio / hugo

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

Shortcode scratch side-effects are lost during live reload #8707

Closed EmpireJones closed 9 months ago

EmpireJones commented 3 years ago

What version of Hugo are you using (hugo version)?

0.84.1

Does this issue reproduce with the latest release?

Yes

Issue Description

To reproduce:

Result:

Expected:


I made a simple repo for testing on the scratch_issue branch at: https://github.com/EmpireJones/hugoBasicExample/tree/scratch_issue


First Load (summary is generated from page's scratch):

image

After change occurs (summary is generated from page's scratch - but has no data):

image

https://github.com/gohugoio/hugo/issues/8255 seems similar, but I don't think it's discussing the exact same thing.

gj52 commented 2 years ago

Had a "fight" with this problem in the last two days.

Want to use SVG sprites references in the page, saving the needed sprites in a scratch map, to include the sprite at the end of the page. Must set scratch deep in called partials and process it from baseof.html ( with a partial ).

Please don't push it to later versions, it is open for 18 months.

bep commented 2 years ago

So, I fighted with this myself some time ago, and figured out that I couldn't change the current behaviour without breaking stuff, so I added a new scratch variable.

Now there are:

.Store
.Scratch

On Page. The .Store will never be erased.

gj52 commented 2 years ago

Thanks, nothing in the DOCS about .Store, looked in the source. Seems not to work in lists templates.
This is the place where the sprites can have a big space impact.

bep commented 2 years ago

Seems not to work in lists templates.

It's a new method on the Page interface, so it should work everywhere.

Also, I'm working on a revised documentation with a complete reference section as I'm writing this.

jmooring commented 9 months ago

I made a simple repo for testing on the scratch_issue branch at: https://github.com/EmpireJones/hugoBasicExample/tree/scratch_issue

I can reproduce the problem using this example with v0.101.0 and earlier. With v0.102.0 and later there are no problems.

There were 3 commits in v0.102.0 that affected LiveReload:

For anyone stumbling across this in the future...

1) Use .Page.Store instead of .Page.Scratch

The Store method on a Page object creates a persistent scratch pad to store and manipulate data. In contrast with the Scratch method, the scratch pad created by the Store method is not reset on server rebuilds.

2) Make sure that the content is rendered before reading the Scratch/Store. For example, if you need a Scratch/Store value at the top of a template, before the content has been rendered, force content rendering with something like:

{{ $noop := .WordCount }}
{{ .Store.Get "foo" }}

There are several methods you can call to force content rendering. See this FAQ.

github-actions[bot] commented 8 months ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.