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

{% js %} / {% script %} inside {% cache %} only works on first page load #7758

Closed
ttempleton opened this issue Apr 2, 2021 · 6 comments
Closed
Labels
enhancement improvements to existing features site development 👩‍💻 features related to website/API development

Comments

@ttempleton
Copy link

Description

On a new Craft install, when using either the {% js %} or {% script %} tags inside a {% cache %} tag, the <script> is output to a page on the first load, but not on subsequent loads of that page.

Steps to reproduce

  1. Use a {% js %} or {% script %} tag inside a {% cache %} tag
  2. Load a page where that template is used, open the browser's developer tools and see that the <script> tag has been output
  3. Reload the page, open the browser's developer tools and see that the <script> tag hasn't been output

Additional info

  • Craft version: 3.6.11.2
  • PHP version: 8.0.3
  • Database driver & version: PostgreSQL 13.2
  • Plugins & versions: None
@brandonkelly
Copy link
Member

That’s expected; those tags can only capture direct tag output, but {% js %} and {% script %} will register JS code which gets output once the rest of the page has been rendered. You should avoid calling those tags within {% cache %} tags.

@ttempleton
Copy link
Author

ttempleton commented Apr 3, 2021

Sorry about this, I had misunderstood how {% js %} and {% script %} work.

@brandonkelly brandonkelly reopened this Apr 5, 2021
@brandonkelly brandonkelly added enhancement improvements to existing features site development 👩‍💻 features related to website/API development and removed expected behavior labels Apr 5, 2021
@brandonkelly
Copy link
Member

Yeah fair enough. This has come up a few times, so I agree it’s probably not expected behavior, and tricky to debug.

Just went ahead and added JS & CSS code caching for {% cache %} tags, for Craft 3.7.

@brandonkelly brandonkelly added this to the 3.7 milestone Apr 5, 2021
@brandonkelly brandonkelly mentioned this issue Apr 5, 2021
@schwarzdesign
Copy link

@brandonkelly Just noticed that caching only works for inline code, not JS / CSS files. Is this by design or is there something stopping this from working correctly? It would be a great improvement if the cache tag worked with the JS / CSS tags when registering files. At least for us, inline code is not used very much, but it's very common to have a component (that may be included in a part of the page that's cached) that needs to register its own JS / CSS files. Not being able to do that inside components that may be cached is a rather hefty limitation. Is there any chance to get this in the future?

@brandonkelly
Copy link
Member

@schwarzdesign It’s doable. Can you please post that as a new feature request?

@MoritzLost
Copy link
Contributor

@schwarzdesign It’s doable. Can you please post that as a new feature request?

@brandonkelly A bit late, but I've opened a discussion regarding this feature request: #9987

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement improvements to existing features site development 👩‍💻 features related to website/API development
Projects
None yet
Development

No branches or pull requests

4 participants