eleventy icon indicating copy to clipboard operation
eleventy copied to clipboard

some collections built with collectionsApi in configuration file return empty if they are then paginated in template files

Open hikatamika opened this issue 4 months ago • 7 comments

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

hikatamika avatar Aug 27 '25 22:08 hikatamika

Finding that if I make and undo a change in my 11ty config file, things work as intended? But I have to do that each time I start 11ty.

hikatamika avatar Aug 27 '25 23:08 hikatamika

Hi...Can I work on this?

Hi...Can I work on this?

By all means, if you know how to fix it! 🙏

hikatamika avatar Sep 21 '25 20:09 hikatamika

I think am encountering the same underlying problem, though it appears for me as getFilteredByTag() returning no results. If I switch to getAll() I observe that many posts are missing.

binaryf0x avatar Oct 02 '25 04:10 binaryf0x

Attempted to create a test case using eleventy-base-blog using both standard and incremental builds on both macOS and Windows with the configuration that you’ve provided and was unable to reproduce. Please attach a reproducible test case so that we can move forward with this one!

zachleat avatar Oct 29 '25 18:10 zachleat

In making a slimmed-down version of my website to share as a test case… the problem stopped occurring in that slimmed-down test version. That is to say, all collections were properly formed on the first run of 11ty. This makes me wonder if 11ty fails to do collectionsApi.getAll() on my real website because I bogged it down with so much stuff…?

In any case, here is a remote copy of the full, error-having version of my site. I'm gonna see if there's anything I can do to get it to work like the test case version I made did,

Edit: 2025.10.31: I tried stethoscoping with some console.logs within one of the collectionsApi codeblocks I'm trying to use. I find that, in the case of my site, console.log(collectionsApi.getAll().filter(function (item) {return "page" in item.data;})); works as intended, but I can't consistently filter for "blog" or "note", the collections I'm trying to look within, despite those collections properly fulfilling their purpose (appearing in for loops, having pagination, etc.) on the website.

hikatamika avatar Oct 29 '25 23:10 hikatamika

I came to a realization that may hopefully shed some light. I discovered it when adding a new collection-generated-from-every-data-value-option.

When I then used that collection to drive a pagination, its array would return empty. When I deleted the page that was paginating with the collection and re-ran 11ty serve, the custom collection would return data again. And I could repeat to the same results (returning properly if not paginated, returning empty if being paginated).

As someone with only surface knowledge of JavaScript, it makes me wonder my paginations with these collections are affecting performance, or cause the functions that generate the arrays to be called too early or something?

Like in the original post, some of my configuration-file-located, collectionsAPI-built collections have been working fine, but, for the ones that don't work, removing their pagination in my site always allows the collection to properly return with data.

hikatamika avatar Nov 07 '25 22:11 hikatamika