Skip to content

Add beforeOnce and afterOnce events for single execution use cases #2964

Description

@johnwargo

Is your feature request related to a problem? Please describe.

I created a node module to generate paginated category pages for a site (https://www.npmjs.com/package/eleventy-generate-category-pages). When I run an Eleventy build using --watch or --serve, it creates an infinite loop during the build process where regenerating the category pages causes a site rebuild.

What I need for this is a beforeOnce event so the category page generation only happens once, at the beginning of the --serve process.

Describe the solution you'd like

Something like this:

var beforeFirstRun = true;
if (beforeFirstRun) {
  beforeFirstRun = false;
  await this.config.events.emit("eleventy.beforeOnce", eventsArg);
}

await this.config.events.emit("beforeBuild", eventsArg);
await this.config.events.emit("eleventy.before", eventsArg);

we can do the same for after, an afterOnce event:

var afterFirstRun = true;
if (afterFirstRun) {
  afterFirstRun = false;
  await this.config.events.emit("eleventy.afterOnce", eventsArg);
}

await this.config.events.emit("afterBuild", eventsArg);
await this.config.events.emit("eleventy.after", eventsArg);

Describe alternatives you've considered

To solve this, I added a similar check in my eleventy.before listener:

var firstRun = true;
eleventyConfig.on('eleventy.before', async ({ dir, runMode, outputMode }) => {
  if (firstRun) {
    firstRun = false;
    generateCategoryPages({}, true, false);
  }
});

Additional context

I can add this as a PR, buut wanted to see what the community thinks of it first. and, I'd have to figure out how to write a test for this.

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