Eleventy project logo

Updated Eleventy Future Post Plugin

Print Friendly and PDF

Posted: January 11, 2024 | | Categories: Eleventy

In yesterday's post, I announced my Future Post plugin for Eleventy and mentioned that I didn't configure the plugin so it would render posts with a future date when testing the site. Since then I found some time to look at the implementation for that and decided to go ahead and add it to version 0.0.2 which I published today.

The Eleventy Base Blog does something weird with a environment variable to implement this and I wanted to make my code cleaner.

In the plugin's code, I added a new Boolean variable called isServing:

var isServing: boolean = false;

Next, as the Eleventy Base Blog does, I added a before event handler where the plugin can access the runMode and set the isServing variable. This allows that check between the two options (serve and watch as shown below) to happen only once, at the start of the Eleventy build process, and should help improve overall performance.

eleventyConfig.on("eleventy.before", ({ runMode }: { runMode: string }) => {
	// initialize the `isServing` flag once before the build starts
	isServing = runMode === "serve" || runMode === "watch";
	if (isServing) log.debug('Serving site, not excluding any posts');
});

Finally, I updated the addGlobalData method to skip the date check when isServing is true:

eleventyConfig.addGlobalData("eleventyComputed.eleventyExcludeFromCollections", () => {
	return (data: any) => {
		// If we're serving the site, don't exclude anything
		if (isServing) return data.eleventyExcludeFromCollections;
		// when not serving, check the date
		var pageDate = new Date(data.page.date);
		pageDate.setTime(pageDate.getTime() + timeOffsetInMS);
		log.debug(`${data.title}: Date: ${pageDate}`);
		return (pageDate > currentDate) ? true : data.eleventyExcludeFromCollections;
	}
});

This approach eliminates the date comparison while serving the site which should improve performance while testing.


Next Post: Package Could Not Be Delivered Spam

Previous Post: Hiding Future Posts in Eleventy

If this content helps you in some way, please consider buying me a coffee.

Header image: Eleventy Project Home Page.