<p>Do you mean lazy load or <a href="https://craftcms.com/docs/3.x/dev/eager-loading-elements.html" rel="nofollow noreferrer">eager load?</a></p>
<p>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?</p>
<p>After the initial load, the browser should cache that image and &quot;reuse&quot; it. However, querying the global set certainly has a cost on the database.</p>
<p>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, <a href="https://craftcms.com/docs/3.x/dev/tags.html#cache" rel="nofollow noreferrer"><code>{% cache globally %}</code></a> tag on your header. That'll save you some db queries that can have an affect on performance.</p>

CC BY-SA 4.0
2020-09-03T20:48:52.120Z
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
CC BY-SA 4.0
2020-09-03T16:24:31.240Z
2020-09-03T16:33:11.553Z
2020-09-03T16:33:11.553Z
RitterKnight
<p>I know you can use this method to eager load fields on injected <code>entry</code>/<code>category</code> variables, so I'm assuming it would work for global sets as well:</p>
<pre><code>{% do craft.app.elements.eagerLoadElements(
    className(header),
    [header],
    ['globalSetsHeaderLogo']
) %}

{{ header.globalSetsHeaderLogo[0].logo.getImg() }}
</code></pre>
<p>You can find out more about this method for eager loading in <a href="https://nystudio107.com/blog/speed-up-your-craft-cms-templates-with-eager-loading#what-about-auto-injected-entrys" rel="noreferrer">this blog post</a> by Andrew Welch.</p>

CC BY-SA 4.0
2020-09-04T04:11:54.850Z
2020-09-04T04:11:54.850Z
Jalen Davenport
<p>I use a <code>header</code> global set to store various thing about my site header, including the company's logo.</p>
<p>I use it in my template like this:</p>
<pre><code>{{ header.globalSetsHeaderLogo.one()logo.getImg() }}
</code></pre>
<p>Any idea on how I could eager-load this?</p>

<globals><eager-loading>
Is there a way to eager-load fields in a global set?
# Is there a way to eager-load fields in a global set?

(Asked by: username_0 on 2020-09-03)

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?

**Tags:** globals, eager-loading


---

## 2 answers

### Answer by username_2 on 2020-09-04. Score: 5 | Accepted answer

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.


---

### Answer by username_1 on 2020-09-03. Score: 2

Do you mean lazy load or [eager load?](https://craftcms.com/docs/3.x/dev/eager-loading-elements.html)

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 %}`](https://craftcms.com/docs/3.x/dev/tags.html#cache) tag on your header. That'll save you some db queries that can have an affect on performance.

> **Comments:**
> **username_0**: 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.  

---

