New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Order of operation between 11tydata files and collections #2529
Comments
|
FWIW, when using the |
|
Ah, and I think the other issue is that if you dynamically set |
|
Oh fascinating... I can reproduce the issue, but interestingly it might be a difference between string For example, this worked for me: module.exports = {
eleventyComputed: {
permalink: data => {
if(data.draft) return false;
},
tags: data => {
- if(!data.draft) return 'posts';
- return '';
+ if(!data.draft) return ['posts'];
+ return [];
}
}
}Although, then I had to modify your .eleventy.js config file to work w/ arrays (using module.exports = function(eleventyConfig) {
//this doesnt work
eleventyConfig.addCollection("blogPosts2", function(collectionApi) {
const res = collectionApi.getFilteredByTag("posts");
console.log(`blogPosts2:`, res.length);
return res;
});
eleventyConfig.addCollection("blogPosts", function(collectionApi) {
let initial = collectionApi.getFilteredByGlob("posts/*.md");
return initial.filter(i => i.data.tags?.includes("posts"));
});
};But also, by switching to using the nicer array syntax (IIRC, Eleventy converts strings to arrays behind the scenes eventually anyways), your original "blogPosts" collection worked (renamed to "blogPosts2" to avoid naming conflicts). TL;DR: Use array syntax when setting tags. |
|
So in theory, if I return the array and it works, I do not need the custom collection at all, right? |
|
Nope, so I tried your modified logic for tags, and collections.posts is still empty. |
|
Ah, to be clear, creating the custom collection in .eleventy.js DID work. This still feels half way fixed though - why is collections.posts still failing in index.liquid? |
In my experience, you still need a custom collection in your .eleventy.js config file if you're setting the |
|
And just because I still had the tab open and I'm bored on a Sunday, here's my proposed solution: // posts/posts.11tydata.js
module.exports = {
eleventyComputed: {
permalink: data => (data.draft) ? false : data.permalink,
eleventyExcludeFromCollections: (data) => data.draft,
},
tags: ["posts"]
};Since we're hardcoding the Since that explanation was hot garbage, here's my modified output from your /_site/index.html file: <h2>collections.posts (1)</h2>
<ol>
<li>a post: /posts/alpha/, title: alpha, tags: posts</li>
</ol>
<h2>collections.all (2)</h2>
<ol>
<li>page: /, title: , tags: </li>
<li>page: /posts/alpha/, title: alpha, tags: posts</li>
</ol>And that also means I don't need any sneaky .eleventy.js // .eleventy.js
module.exports = function(eleventyConfig) {
}; |
|
All very cool and thank you - going to edit my blog post with a link to this discussion. Still curious to hear though if this would be considered a bug. It feels like a bug to me. :) |
Describe the bug
Imagine a directory, named
posts, and inside there I'm using a data directory file to dynamically set thetagsvalue based on front matter:Specifically the idea here is - if I included
draft: truein front matter, I don't want to publish it, and if not, I want the post to be assigned to the tags value.In my front end, I then tried to loop over it:
And nothing was output. But if I switch to
all:I correctly see my blog post and it correctly shows the right value for
tags.I then switched to using a custom collection in
.eleventy.js:But this also didn't work. I had to switch to this:
So I guess the collection stuff ran before the processing of the posts, but if collections are based on reading files and their tags, why wouldn't my
posts.11tydata.jsrun properly there?To Reproduce
See what I said above. Source may be found here: https://github.com/cfjedimaster/eleventy-demos/tree/master/eleventy_draft_test
Expected behavior
I'd expect
collections.tagsto have the right results based on the custom logic in my directory data file.Environment:
eleventy --versionornpx @11ty/eleventy --version] 1.0.1The text was updated successfully, but these errors were encountered: