diff --git a/src/TemplateMap.js b/src/TemplateMap.js index b56f40075..951a1131a 100644 --- a/src/TemplateMap.js +++ b/src/TemplateMap.js @@ -276,6 +276,7 @@ class TemplateMap { ); await this.initDependencyMap(fullTemplateOrder); + await this.refreshUserConfigCollections(fullTemplateOrder); await this.resolveRemainingComputedData(); let orderedPaths = this.#removeTagsFromTemplateOrder(fullTemplateOrder); @@ -465,6 +466,21 @@ class TemplateMap { return result; } + async refreshUserConfigCollections(templateOrder = []) { + for (let entry of templateOrder) { + if (!GlobalDependencyMap.isCollection(entry)) { + continue; + } + + let tagName = GlobalDependencyMap.getTagName(entry); + if (!this.isUserConfigCollectionName(tagName)) { + continue; + } + + await this.setCollectionByTagName(tagName); + } + } + populateCollectionsWithContent() { for (let collectionName in this.collectionsData) { // skip custom collections set in configuration files that have arbitrary types diff --git a/test/Issue4182Test.js b/test/Issue4182Test.js new file mode 100644 index 000000000..049e584cc --- /dev/null +++ b/test/Issue4182Test.js @@ -0,0 +1,46 @@ +import test from "ava"; +import Eleventy from "../src/Eleventy.js"; + +test("#4182 addCollection consuming a tagged collection should not run before tag publishers", async (t) => { + let elev = new Eleventy("test/noop", false, { + config(eleventyConfig) { + eleventyConfig.setLayoutsDirectory("_layouts"); + eleventyConfig.addCollection("primaryNav", (collectionApi) => { + return collectionApi.getFilteredByTag("primary"); + }); + + eleventyConfig.addTemplate( + "_layouts/base.liquid", + `{% for item in collections.primaryNav %}{{ item.url }} {% endfor %}{{ content }}`, + { + eleventyImport: { + collections: ["primaryNav"], + }, + }, + ); + + eleventyConfig.addTemplate("blog.liquid", "Blog", { + tags: ["primary"], + permalink: "/blog/", + layout: "base.liquid", + }); + + eleventyConfig.addTemplate("about.liquid", "About", { + tags: ["primary"], + permalink: "/about/", + layout: "base.liquid", + }); + + eleventyConfig.addTemplate("index.liquid", "Home", { + layout: "base.liquid", + }); + }, + }); + + let results = await elev.toJSON(); + let homepage = results.find((entry) => entry.url === "/"); + + t.truthy(homepage); + t.true(homepage.content.includes("/blog/")); + t.true(homepage.content.includes("/about/")); +});