Skip to content

Access a collection in an "eleventy.after"-function #3039

Answered by pdehaan
erikkroes asked this question in Q&A
Discussion options

You must be logged in to vote

I found a workaround, but not sure I like it. It involves manually [re]creating collections via .addCollection() and caching the values in a global object which you can access in eleventy.after event.

/**
 * @param {import("@11ty/eleventy/src/UserConfig")} eleventyConfig
 * @returns {ReturnType<import("@11ty/eleventy/src/defaultConfig")>}
 */
module.exports = function (eleventyConfig) {
  const collectionData = {};

  const tags = ["blog"];
  for (const t of tags) {
    eleventyConfig.addCollection(t, function (collectionApi) {
      collectionData[t] = collectionApi.getFilteredByTag(t);
      return collectionData[t];
    });
  }

  eleventyConfig.addCollection("docs", function (collecti…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@pdehaan
Comment options

pdehaan Sep 8, 2023
Collaborator

@erikkroes
Comment options

@erikkroes
Comment options

Answer selected by erikkroes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants