<li><p><aclass="github-button"href="https://github.com/dairiki/lektor-limit-dependencies"data-icon="octicon-star"data-size="large"data-show-count="true"aria-label="Star lektor-limit-dependencies on GitHub"target="_blank">Star</a></p></li>
<p>This is not without drawbacks, however. To sort the post query by
date, Lektor iterates through <em><strong>all</strong></em> of the blog’s posts, then sorts
them. In so doing, it records all of the blog posts as dependencies
<em>of every page on which this most-recent-post query is used</em>. If this
is in the sidebar of every page on your site, <em>now every page on your
site will be rebuilt whenever any blog post at all</em> (not just one of
the three most recent posts) <em>is edited</em>.</p>
<p>Technically, it is true that all pages now depend on all posts. You
might well edit the <code>pub_date</code> of one of your older posts, such that
it should now appear in the most-recent listing. However, it is not
true that all pages need to be rebuilt for <em>any</em> edit of any post.
Unfortunately, Lektor’s dependency tracking system is not elaborate
enough to be able to express details about <em>how</em> pages are
dependent on other pages; it only records that they <em>are</em>
dependent, so Lektor has no option but to rebuild everything.</p>
<h3>A Solution?</h3>
<p>This plugin introduces a Jinja filter, <code>limit_dependencies</code>. It
expects, as input, a Lektor <ahref="https://www.getlektor.com/docs/api/db/query/"rel="nofollow"title="Lektor documentation on the Query class">query</a> instance. It iterates through the
input query, and returns a new query instance which will yield the
same results. While it is doing its iteration, it — essentially —
monkey-patches Lektor’s dependency tracking machinery to prevent it
from recording any dependencies.</p>
<p>At the end, <code>limit_dependencies</code> records one dependency on a <ahref="https://www.getlektor.com/docs/api/db/obj/#virtual-source-objects"rel="nofollow"title="Lektor documentation on Virtual Source Objects">virtual
source object</a> which depends only on the sequence of the identities
of the records in the query result. (Lektor provides a means by which
virtual source objects can report checksums. The
dependency tracking mechanism records those checksums, and will
trigger a rebuild should the checksum change. <code>Limit_dependencies</code>
generates a virtual source object whose checksum depends on the
sequence of identities in the query result.)</p>
<p>In the above example, this is exactly what we want. We only want to
trigger a rebuild if the order or composition of the most-recent three
posts changes. (Or if any of their titles change. Note that this
gets covered, too, since when the resulting query is iterated over in
the <code>{% for %}</code> loop, dependencies will be recorded on the three
Please enable JavaScript to view the <ahref="https://disqus.com/?ref_noscript"
rel="nofollow">comments powered by Disqus.</a>
</noscript>
</div>
</div>
</div>
</div>
</div>
</div>
<divclass="bottomsummary">
<divclass="container">
</div>
</div>
<footer>
<divclass="container">
<divclass="row">
<divclass="col-sm-4 icon-bar">
<ahref="https://github.com/lektor/lektor/"title="Lektor on GitHub"
><iclass="fa fa-github"></i></a>
<ahref="https://github.com/lektor/lektor/issues/"title="Report Issues for Lektor"
><iclass="fa fa-bug"></i></a>
<ahref="https://twitter.com/getlektor"title="Find Lektor on Twitter"
><iclass="fa fa-twitter"></i></a>
<ahref="https://gitter.im/lektor/lektor"title="Chat on Gitter"
><iclass="fa fa-comment"></i></a>
<ahref="https://github.com/lektor/lektor-website/tree/master/content/plugins/lektor-limit-dependencies/contents.lr"title="View source for this page"><iclass="fa fa-code"></i></a>
</div>
<divclass="col-sm-8">
<ahref="../../license/">License & Copyright</a> •
<ahref="../../contact/">Contact</a> •
Made with <iclass="fa fa-fw fa-heart"title="Heart"><spanhidden>Heart</span></i> in Carinthia