Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shortcode Scratch variable not rendering on local server after edit to the Parent Page template #8255

Closed
Tracked by #11549
onedrawingperday opened this issue Feb 17, 2021 · 14 comments

Comments

@onedrawingperday
Copy link
Contributor

onedrawingperday commented Feb 17, 2021

This report came up in the forum and I can confirm that a Scratch variable set in a Shortcode does not render when called from the template of its Parent Page.

The Scratch variable is rendered on first load of the local server in fast rendering mode.

However if --disabelFastRender is used then the variable is not rendered.

Also the variable is not rendered when the command hugo is executed.

Repo to reproduce: https://github.com/Oliver-Fish/hugo-scratchpad


Since Shortcodes are rendered first, I would expect a Scratchpad set in a Shortcode to be available from a Parent Page's template.

cc: @Oliver-Fish

@bep
Copy link
Member

bep commented Feb 17, 2021

Since Shortcodes are rendered first, I would expect a Scratchpad set in a Shortcode to be available from a Parent Page's template.

Sure, but since the shortcodes gets lazily rendered, you need to invoke .Content (or one of the other ´Contentmethods, e.g.WordCount`) first.

@onedrawingperday
Copy link
Contributor Author

onedrawingperday commented Feb 17, 2021

you need to invoke .Content (or one of the other ´Contentmethods, e.g.WordCount`) first.

Thank you for the reply.

With the above invocation the Scratch variable does render when the project is published with the command hugo.

Also the Scratch variable now renders on first load of both hugo server and hugo server --disableFastRender

However once changes are made then the Scratch variable is again missing when LiveReload refreshes the browser.

Is this due to the way the rendering order changes with LiveReload? Or can it be fixed?

Sorry to bother you with these questions.

The person who reported this issue is a Go developer and has offered himself to look into this issue if it can be fixed.

Although it now seems like a limitation to me.


By the way the use case for the above is the rendering of a FAQ schema through Nested Shortcodes, simliar to how Gutenberg blocks function in Wordpress.

@bep
Copy link
Member

bep commented Feb 17, 2021

With the above invocation the Scratch variable does render when the project is published with the command hugo.

Well, I'm just looking at the template code and I'm saying that

  1. The value in scratch does not exist until its somehow added
  2. According to the template code it is accessed before it is written

I suggest adjusting fixing the template first.

@onedrawingperday
Copy link
Contributor Author

onedrawingperday commented Feb 17, 2021

Before posting my reply above I adjusted the template to:

<body>
    {{ $content := .Content }}
    <h1>{{ .Scratch.Get "question" }}</h1>
   {{ $content }}
</body>

Simply calling .Content though a variable does make the Scratch available, without changing the order of the markup elements.

However the LiveReload issue on the local server persists, as noted above.

@Oliver-Fish
Copy link

I have updated the proof of concept repo to correct the issue with the template, this has actually changed the steps to re-create the issue slightly.

The scratch variable is now only empty if I trigger a live reload by editing layouts/test/single.html

So to be clear I did three tests

Edited layouts/test/single.html - Empty
Edited layouts/shortcodes/test.html - Value as expected
Edited content/test.md - Value as expected

Additionally, if I edit single.html I will get the issue but if I then edit test.html or test.md triggering another reload the issue is corrected.

So any change to single.html causes the issue but it can be reset by editing any of the other monitored files.

I am not familiar with the Hugo codebase however, a naive guess would be that the layout modification appears to not be following the same re-rendering steps as the other files.

@onedrawingperday onedrawingperday changed the title Scratch variable set in Shortcode not available from the template of a Parent Page Shortcode Scratch variable not rendering on local server after edit to the Parent Page template Feb 17, 2021
@onedrawingperday
Copy link
Contributor Author

Edited layouts/test/single.html - Empty
Edited layouts/shortcodes/test.html - Value as expected
Edited content/test.md - Value as expected

I changed the title of the issue to make it clearer of what the issue is about.

Basically if the Shortcode parent page template is edited and LiveReload refreshes the page, then the Scratch variable is not rendering.

If this can be fixed it would be nice to have.

But since it does not interrupt the content editing workflow, I can live with it.
(It's not often one has to make changes in the template after a project has been developed).

Anyway, again thanks for the guidance.

I was not aware that Shortcodes are lazy-loaded and that the Content variable has to be called beforehand in the template.

@Oliver-Fish
Copy link

Oliver-Fish commented Feb 17, 2021

If this can be fixed it would be nice to have.

Agreed this unblocks me, my site is new so I am still going to hit this pain often but it won't last forever.

I was not aware that Shortcodes are lazy-loaded and that the Content variable has to be called beforehand in the template.

This wasn't clear from the docs, maybe we can add this somewhere? it could be quite a timesink of a bug to get stuck on this problem without knowing.

@bep

Is there a reason why Shortcodes are lazy-loaded like this? Isn't this the more common use-case that we should optimize for as this can be quite confusing?

But of course, don't know the reason this was done so I might be missing something here.

Either way, thank you both @onedrawingperday and @bep really great work on managing issues and support for Hugo this is above the standard I have seen most large enterprise support contracts offer.

@onedrawingperday
Copy link
Contributor Author

onedrawingperday commented Feb 17, 2021

Is there a reason why Shortcodes are lazy-loaded like this? Isn't this the more common use-case that we should optimize for as this can be quite confusing?

I'm not the maintainer, but I would guess that the lazy loading is used for performance reasons.

@Oliver-Fish
Copy link

Is there a reason why Shortcodes are lazy-loaded like this? Isn't this the more common use-case that we should optimize for as this can be quite confusing?

I'm not the maintainer, but I would guess that the lazy loading is used for performance reasons.

But isn't the common use-case to always have a content call in your file at some point? So doing it up-front, regardless of where it's called in the file, improves the UX for the developer as it prevents nasty bugs like this one.

However, I could be missing a use-case where this is valid but it'd be good to understand what that is.

@onedrawingperday
Copy link
Contributor Author

But isn't the common use-case to always have a content call in your file at some point?

Not necessarily. I do maintain a couple of e-commerce projects that rely on front matter parameters and do not call the .Content variable in the templates.

@Oliver-Fish
Copy link

Oliver-Fish commented Feb 17, 2021

But isn't the common use-case to always have a content call in your file at some point?

Not necessarily. I do maintain a couple of e-commerce projects that rely on front matter parameters and do not call the .Content variable in the templates.

Interesting, and in these cases if you called .Content would it actually contain any data?

@onedrawingperday
Copy link
Contributor Author

onedrawingperday commented Feb 17, 2021

Interesting, and in these cases if you called .Content would it actually contain any data?

No, .Content returns empty (not even <nil> gets printed). Anyway the forum is meant for discussion, not this issue.

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity. The resources of the Hugo team are limited, and so we are asking for your help.
If this is a bug and you can still reproduce this error on the master branch, please reply with all of the information you have about it in order to keep the issue open.
If this is a feature request, and you feel that it is still relevant and valuable, please tell us why.
This issue will automatically be closed in the near future if no further activity occurs. Thank you for all your contributions.

@github-actions
Copy link

github-actions bot commented Apr 2, 2022

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 2, 2022
@jmooring jmooring reopened this Jan 20, 2024
bep added a commit that referenced this issue Jan 26, 2024
…d some general spring cleaning

There are some breaking changes in this commit, see #11455.

Closes #11455
Closes #11549

Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
bep added a commit that referenced this issue Jan 27, 2024
…d some general spring cleaning

There are some breaking changes in this commit, see #11455.

Closes #11455
Closes #11549

Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
bep added a commit that referenced this issue Jan 27, 2024
…d some general spring cleaning

There are some breaking changes in this commit, see #11455.

Closes #11455
Closes #11549

This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.

The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.

A list of the notable new features:

* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.

New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.

This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):

Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.

Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).

Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
bep added a commit that referenced this issue Jan 27, 2024
…d some general spring cleaning

There are some breaking changes in this commit, see #11455.

Closes #11455
Closes #11549

This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.

The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.

A list of the notable new features:

* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.

New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.

This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):

Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.

Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).

Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
@bep bep closed this as completed in e39c0f4 Jan 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants