statamic / ssg

The official Statamic Static Site Generator
229 stars 23 forks source link

Antler stacks are broken #98

Closed imacrayon closed 2 years ago

imacrayon commented 2 years ago

I have a {{ stack:scripts }} tag in my main layout and a few pages with:

{{ push:scripts }}
<script defer src="{{ mix src='js/parallax.js' }}"></script>
{{ /push:scripts }}

When my site is server rendered, stacks work as expected, I see a single <script> tag rendered in my layout. However when I run SSG the same page will end up with repeated script tags like this:

<script defer src="/js/parallax.js"></script>
<script defer src="/js/parallax.js"></script>
<script defer src="/js/parallax.js"></script>

It appears that the same stack is repeatedly pushed to on every page that SSG creates. I think the stacking context needs to be cleared before each new page is rendered.

jackmcdade commented 2 years ago

Are you using the Runtime parser?

imacrayon commented 2 years ago

Correct, version is set to runtime.

jackmcdade commented 2 years ago

Any ideas on this one @JohnathonKoster?

jasonvarga commented 2 years ago

Can we just clear the stack when the stack tag is used?

JohnathonKoster commented 2 years ago

Can we just clear the stack when the stack tag is used?

We could but would make it impossible to use the stack in two spots (use case: push items to a navigation and a table of contents without needing two stacks. Relatively niche but would be super useful if this PR that lets you access stacks as an array is merged: https://github.com/statamic/cms/pull/6110/).

I will look into adding it at this point: https://github.com/statamic/cms/blob/3.3/src/View/View.php#L112 if you want them to be usable more than once. This is the relevant methods that would be used: https://github.com/statamic/cms/blob/3.3/src/View/Antlers/Language/Runtime/StackReplacementManager.php#L10