statamic / ideas

đŸ’¡Discussions on ideas and feature requests for Statamic
https://statamic.dev
32 stars 1 forks source link

Lazy computed properties #1200

Open martyf opened 4 months ago

martyf commented 4 months ago

My current build uses computed properties to pull out a handful of content from a builder approach - headings, bard, etc - so that it can be queried when searching the site.

Collection::computed(['pages', 'support'], function($entry) {
    // get all of the content and return it
});

This site also has a repeater in a Global that is used for some footer links - the repeater includes a "link" and a "text" field for a label.

image

These links are being accessed using something like:

{{ settings:footer_links }}
    <a href="{{ url }}">{{ label }}</a>
{{ /settings:footer_links }}

The issue is that any time these footer links are accessed, the entry Entry is loaded - including its computed properties, even if they're not being accessed, such as the code above. All that is being accessed is the Entry's url and the label text field from the repeater. But Statamic is still running the computed fields code.

Sure, the footer links could be a Nav and then use the select param to only select the fields I need - but that is a bandaid to this issue, not a solution, as it still crops up whenever an entry is accessed, such as a link in content.

It is also possible to cache the result of the computed callback, but it still adds additional calls to the processing that are not necessary - the computed function still gets run, and now the Cache is involved getting and/or setting - this feels like unnecessary code being run.

It would be awesome if computed properties could be lazy - either by default, or by a new param - so that they are only called and computed when they're actually accessed the first time.

This way you could load an Entry, which has computed properties that may not be used, and take no performance costs until they are actually accessed.