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

Issue with preloadSingles and eager loading #583 #585

Closed
vandres opened this issue Nov 18, 2023 · 6 comments
Closed

Issue with preloadSingles and eager loading #583 #585

vandres opened this issue Nov 18, 2023 · 6 comments
Assignees
Labels
bug Something isn't working

Comments

@vandres
Copy link

vandres commented Nov 18, 2023

We entrified our installation and use Singles for all "global" tasks. With preloadSingles() in the general.php, the use is very painless.

Now I ran into an issue, that templates don't get invalidated, when e.g. the navigation changes.

To reproduce:

  • enable preloadSingles()
  • create a Single "header"
  • create a Structure "page"
  • create a field "navigation", which is an entry field for "page"
  • add that field to "header"
  • create some pages and put some of them in the header.navigation field
  • create the navigation template (see below)
  • you will get Blitz hint for eager loading navigation
  • add eager loading for "globals" (see below)
  • now the hint is gone, but the cache no longer refreshes

When I use this syntax {% set header = craft.entries.section('header').with(['navigation']).one() %}, everythings works as expected. But I would prefer to preload the Singles.

Find an example repo here: https://github.com/vandres/blitz-bug-eager-loading

In the DB dump (data/db.sql.gz) I already created some pages. So open the website in the browser, get the cache generated. Head to the backend, take one of the pages out of the navigation in the header Single. Run the queue from CLI. Refresh the browser, see no difference.

The backend login is "admin" with password "andres@voan.ch"

Template code

{# Navigation template #}
{% if header.navigation.all() | length %}
      <ul>
        {% for link in header.navigation.all() %}
          <li><a href="{{ link.url }}">{{ link.title }}</a></li>
        {% endfor %}
      </ul>
{% endif %}
{# Eager load navigation in header #}
{% do craft.app.elements.eagerLoadElements(
  className(header),
  [header],
  ['navigation']
) %}
@vandres vandres added the bug Something isn't working label Nov 18, 2023
@bencroker
Copy link
Collaborator

Thanks, I’m seeing the issue now. I’ll need to troubleshoot where exactly the issue lies. In the meantime I suggest you use the more explicit craft.entries.section('header').with(['navigation']).one().

@bencroker bencroker self-assigned this Nov 20, 2023
@bencroker
Copy link
Collaborator

Fixed in c29f0d7.

You can test this by running composer require "putyourlightson/craft-blitz:dev-develop as 4.9.2".

@vandres
Copy link
Author

vandres commented Nov 26, 2023

Thank you very much!

@bencroker
Copy link
Collaborator

I plan on releasing this week, but if you can test and confirm that this solves the issue for you then that would be great!

@vandres
Copy link
Author

vandres commented Dec 11, 2023

@bencroker Can confirm, it's working for me. Thank you very much!

@bencroker
Copy link
Collaborator

Great! This was released in 4.9.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants