-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathexecutePlugins.ts
More file actions
46 lines (45 loc) · 1.67 KB
/
executePlugins.ts
File metadata and controls
46 lines (45 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import {scullyConfig} from '../utils/config';
import {plugins} from '../pluginManagement/pluginRepository';
import {HandledRoute} from '../routerPlugins/addOptionalRoutesPlugin';
import {logError, yellow} from '../utils/log';
import {puppeteerRender} from './puppeteerRenderPlugin';
import {pluginsError} from '../utils/cli-options';
export const executePluginsForRoute = async (route: HandledRoute) => {
/** make one array with all handlers for this route, filter out empty ones */
const handlers = [route.type, ...(route.postRenderers || scullyConfig.defaultPostRenderers)].filter(
Boolean
);
const preRender = route.config && route.config.preRenderer;
if (preRender) {
try {
const prResult = await preRender(route.route, route.config);
if (prResult === false) {
logError(`prerender stopped rendering for "${yellow(route.route)}". This route is skipped.`);
return '';
}
} catch (e) {
logError(`prerender trowed during rendering for "${yellow(route.route)}". This route is skipped.`);
/** abort when prerender throws */
return '';
}
}
const InitialPromise = puppeteerRender(route);
return handlers.reduce(async (updatedHTML, plugin) => {
const html = await updatedHTML;
const handler = plugins.render[plugin];
if (handler) {
try {
/** return result of plugin */
return await handler(html, route);
} catch {
logError(
`Error during content generation with plugin "${yellow(plugin)}" for ${yellow(
route.templateFile
)}. This hander is skipped.`
);
}
}
/** return unhandled result */
return html;
}, InitialPromise);
};