Skip to content

collectionsApi.getAll() only works sometimes (when used multiple times in one config?) #3931

@hikatamika

Description

@hikatamika

Operating system

Windows 11

Eleventy

3.1.2

Describe the bug

I have 3 custom collections that're put together using collectionsApi.getAll(). One of them only properly gets-all each time. Most times, the other two fail to get all, and return empty.

Only one collection always manages to get all and then carry out the rest of the function to make the proper collection.

Reproduction steps

This answer here is the code I'm using; to make arrays of every of blog/note specific category or tag (blog tag, not 11ty collection) used across my website.

With minor tweaks, the code looks like this in my 11ty config file:

eleventyConfig.addCollection("blogTagList", async function(collectionsApi) {
  const blogTagSet = new Set();

  collectionsApi.getAll().map( item => {
    if (item.data.blogTags) { // handle pages that don't have tags
        item.data.blogTags.map( tag => blogTagSet.add(tag))
    };
  });

  const blogTagArray = Array.from(blogTagSet).sort(async function(a,b) {
    a = a.toLowerCase();
    b = b.toLowerCase();
    if(a == b) return 0;
    return a < b ? -1 : 1;
  });

  // console.log(collectionsApi.getFilteredByTags("blog"));
  // console.log(blogTagSet);
  console.log(blogTagArray);

  return blogTagArray;
});

eleventyConfig.addCollection("blogCategList", async function(collectionsApi) {
  const blogCategSet = new Set();

  collectionsApi.getAll().map( item => {
    if (item.data.blogCategs) { // handle pages that don't have tags
        item.data.blogCategs.map( tag => blogCategSet.add(tag))
    };
  });

  const blogCategArray = Array.from(blogCategSet).sort(async function(a,b) {
    a = a.toLowerCase();
    b = b.toLowerCase();
    if(a == b) return 0;
    return a < b ? -1 : 1;
  });

  // console.log(collectionsApi.getFilteredByTags("blog"));
  // console.log(blogCategSet);
  console.log(blogCategArray);

  return blogCategArray;
});

eleventyConfig.addCollection("noteTagList", async function(collectionsApi) {
  const noteTagSet = new Set();

  collectionsApi.getAll().map( item => {
    if (item.data.noteTags) { // handle pages that don't have tags
        item.data.noteTags.map( tag => noteTagSet.add(tag))
    };
  });

  const noteTagArray = Array.from(noteTagSet).sort(async function(a,b) {
    a = a.toLowerCase();
    b = b.toLowerCase();
    if(a == b) return 0;
    return a < b ? -1 : 1;
  });

  // console.log(collectionsApi.getFilteredByTags("note"));
  // console.log(noteTagSet);
  console.log(noteTagArray);

  return noteTagArray;
});

I find myself having to run 11ty serve or 11ty build multiple times, because on some runs, blogTagList and blogCategList manage to getAll() successfully and fill out. Other times, only noteTagList fills out successfully. I'm wondering what could be going on internally to cause only one collection out of this trio to build properly on every run.

Expected behavior

Ideally, each time I run 11ty, blogTagList, blogCategList, and noteTagList all fill out correctly instead of me having to spam Ctrl+S in VS Code until blogTagList and blogCategList run properly too. The fact that all 3 fill out sometimes makes me assume that the addCollection code is fine, so I'm quite stumped.

Reproduction URL

No response

Screenshots

Image Image

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions