Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render templates in parallel. #3277

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
"node-retrieve-globals": "^6.0.0",
"normalize-path": "^3.0.0",
"nunjucks": "^3.2.4",
"p-map": "^7.0.2",
"please-upgrade-node": "^3.2.0",
"posthtml": "^0.16.6",
"recursive-copy": "^2.0.14",
Expand Down
52 changes: 29 additions & 23 deletions src/TemplateMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import { DepGraph as DependencyGraph } from "dependency-graph";
import { isPlainObject } from "@11ty/eleventy-utils";
import debugUtil from "debug";
import os from "node:os";
import pMap from "p-map";

import TemplateCollection from "./TemplateCollection.js";
import EleventyErrorUtil from "./Errors/EleventyErrorUtil.js";
Expand Down Expand Up @@ -547,31 +549,35 @@ class TemplateMap {

async populateContentDataInMap(orderedMap) {
let usedTemplateContentTooEarlyMap = [];
for (let map of orderedMap) {
if (!map._pages) {
throw new Error(`Content pages not found for ${map.inputPath}`);
}
await pMap(
orderedMap.filter((map) => {
if (!map._pages) {
throw new Error(`Content pages not found for ${map.inputPath}`);
}

if (!map.template.behavior.isRenderable()) {
// Note that empty pagination templates will be skipped here as not renderable
continue;
}

// IMPORTANT: this is where template content is rendered
try {
for (let pageEntry of map._pages) {
pageEntry.templateContent =
await pageEntry.template.renderPageEntryWithoutLayout(pageEntry);
}
} catch (e) {
if (EleventyErrorUtil.isPrematureTemplateContentError(e)) {
usedTemplateContentTooEarlyMap.push(map);
} else {
throw e;
}
}
debugDev("Added this.map[...].templateContent, outputPath, et al for one map entry");
}
return map.template.behavior.isRenderable();
}),
(map) =>
new Promise(async function (resolve, reject) {
// IMPORTANT: this is where template content is rendered
try {
for (let pageEntry of map._pages) {
pageEntry.templateContent =
await pageEntry.template.renderPageEntryWithoutLayout(pageEntry);
}
} catch (e) {
if (EleventyErrorUtil.isPrematureTemplateContentError(e)) {
usedTemplateContentTooEarlyMap.push(map);
} else {
reject(e);
}
}
debugDev("Added this.map[...].templateContent, outputPath, et al for one map entry");
resolve();
}),
{ concurrency: os.availableParallelism() },
);

for (let map of usedTemplateContentTooEarlyMap) {
try {
Expand Down