Skip to content

Commit

Permalink
Fixes #3467
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Oct 21, 2024
1 parent 006e888 commit 79649dd
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/TemplateMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,10 @@ class TemplateMap {
} else {
result = this.collection.getFilteredByTag(tag);
}
debug(`Collection: collections.${tag || "all"} size: ${result.length}`);

// May not return an array (can be anything)
// https://www.11ty.dev/docs/collections-api/#return-values
debug(`Collection: collections.${tag || "all"} size: ${result?.length}`);

return result;
}
Expand All @@ -606,7 +609,9 @@ class TemplateMap {
// This works with async now
let result = await configCollections[name](this.collection);

debug(`Collection: collections.${name} size: ${result.length}`);
// May not return an array (can be anything)
// https://www.11ty.dev/docs/collections-api/#return-values
debug(`Collection: collections.${name} size: ${result?.length}`);
return result;
}

Expand Down
56 changes: 56 additions & 0 deletions test/Issue3467Test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import test from "ava";
import Eleventy from "../src/Eleventy.js";

test("Empty collections api #3467 (return undefined)", async (t) => {
let elev = new Eleventy("./test/stubs-virtual", "./test/stubs-virtual/_site", {
config: function (eleventyConfig) {
eleventyConfig.addTemplate("virtual.md", `# Hello`);

eleventyConfig.addCollection("brokenCollection", function(collection) {
// returns nothing
});
},
});

let results = await elev.toJSON();

t.deepEqual(results.length, 1);
t.deepEqual(results[0].content.trim(), `<h1>Hello</h1>`);
t.deepEqual(results[0].rawInput, `# Hello`);
});

test("Empty collections api #3467 (return false)", async (t) => {
let elev = new Eleventy("./test/stubs-virtual", "./test/stubs-virtual/_site", {
config: function (eleventyConfig) {
eleventyConfig.addTemplate("virtual.md", `# Hello`);

eleventyConfig.addCollection("brokenCollection", function(collection) {
return false;
});
},
});

let results = await elev.toJSON();

t.deepEqual(results.length, 1);
t.deepEqual(results[0].content.trim(), `<h1>Hello</h1>`);
t.deepEqual(results[0].rawInput, `# Hello`);
});

test("Empty collections api #3467 (return empty string)", async (t) => {
let elev = new Eleventy("./test/stubs-virtual", "./test/stubs-virtual/_site", {
config: function (eleventyConfig) {
eleventyConfig.addTemplate("virtual.md", `# Hello`);

eleventyConfig.addCollection("brokenCollection", function(collection) {
return "";
});
},
});

let results = await elev.toJSON();

t.deepEqual(results.length, 1);
t.deepEqual(results[0].content.trim(), `<h1>Hello</h1>`);
t.deepEqual(results[0].rawInput, `# Hello`);
});

0 comments on commit 79649dd

Please sign in to comment.