eleventy
eleventy copied to clipboard
What’s quicker: computed data and dumb templates, or simple data and complex templates?
As I play around with eleventyComputed, I’m realising I can move a lot of my template logic into directory data files. I have two questions:
-
What impact does this have on build times? Is it more effective to manipulate the data cascade and have ‘dumber’ templates, or to leave the logic in template, with filters and control flow tags?
-
If using the JS data files, does it increase build time if these files contain template logic, or is calculation faster if these contain only pure JS?
Example
I currently use a custom Liquid filter (pretty) to generate a pretty/extension-less URLs for my permalinks (and prepend to include the site’s URL): {{ item.url | prepend: app.url | pretty }}
I’m envisaging using eleventyComputed to calculate this value for all my content files instead:
module.exports = {
eleventyComputed: {
pretty_url: data => {
let {url} = data.page;
url = url.replace(/(?:index)?\.html/g, '');
return `${data.app.url}${url}`;
}
}
};
Alternatively, I could move the template logic into this data file:
module.exports = {
eleventyComputed: {
pretty_url: data => '{{ data.page.url | prepend: app.url | pretty }}'
}
};
Both options which would then mean I could use {{ item.data.pretty_url }} in my templates.
Now, imagine this many times over for other scenarios, potentially populating the data cascade with lots more data. Is this a good idea?
Since templates are most the time optimised on being dumb + they should encourage a MVC pattern, I can imagine, it pays off in the long run to keep the templates dumb. Keep in mind, that Eleventy is still in its early stages, so a lot of perf optimisation is yet to be done.
Or, if you want to hear the standard answer regarding performance questions: measure it! (Also if there are several gotchas which cause skewing of results)
I'd second "dumb templates". Historically this is an extremely good pattern to follow, promoting separation of concern.
Are there performance concerns for expensive operations with computed data? It seems like it is calculated multiple times.
I think as the dust settled on this one I’m not sure I would have a strong opinion either way. It’s just personal preference! I would emphasize the benefit of keeping the templates as clean as possible, personally!