1

I use a header global set to store various thing about my site header, including the company's logo.

I use it in my template like this:

{{ header.globalSetsHeaderLogo.one()logo.getImg() }}

Any idea on how I could eager-load this?

2

I know you can use this method to eager load fields on injected entry/category variables, so I'm assuming it would work for global sets as well:

{% do craft.app.elements.eagerLoadElements(
    className(header),
    [header],
    ['globalSetsHeaderLogo']
) %}

{{ header.globalSetsHeaderLogo[0].logo.getImg() }}

You can find out more about this method for eager loading in this blog post by Andrew Welch.

2

Do you mean lazy load or eager load?

Usually you lazy load images via JS to speed up site loading but not I'm not sure doing it to one image across multiple pages is all that beneficial?

After the initial load, the browser should cache that image and "reuse" it. However, querying the global set certainly has a cost on the database.

As far as eager loading, not sure it'll make a huge difference here and it looks like you're not outputting any transforms but you might consider using, say, {% cache globally %} tag on your header. That'll save you some db queries that can have an affect on performance.

1
  • 1
    Good catch! I meant eager-loading, to reduce the number of db queries. I'm not trying to solve a specific performance issue, just to improve my Craft knowledge. As for the cache, it's indeed a good solution but we're already using it. – nstCactus Sep 3 '20 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.