Skip to content

Commit 26c3f7b

Browse files
committed
feat(scully): added the option to ignore a list of ResourceTypes
1 parent 7d85938 commit 26c3f7b

3 files changed

Lines changed: 31 additions & 1 deletion

File tree

docs/scully-configuration.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ export interface ScullyConfig {
7979
guessParserOptions?: GuessParserOptions;
8080
/** the maximum of concurrent puppeteer tabs open. defaults to the available amounts of cores */
8181
maxRenderThreads?: number;
82+
/** the resource types to ignore when generating pages via Puppeteer */
83+
ignoreResourceTypes?: ResourceType[];
8284
}
8385
```
8486

@@ -198,3 +200,9 @@ Connects your application to a different host. This is useful when using your ow
198200

199201
The`guessParserOptions` that get passed to the `guess-parser` library. Currently, the only supported property is
200202
`excludedFiles`, and it excludes files from the `guess-parser` route discovery process.
203+
204+
### ignoreResourceTypes
205+
206+
The `ignoreResourceTypes` array that get passed to the `puppeteerRenderPlugin`.
207+
Any `ResourceType` that is listed here will be ignored by the Puppeteer instance rendering the requested page.
208+
For example if you add `image` and `font` all requests to images and fonts loaded on your pages will be ignored.

libs/scully/src/lib/renderPlugins/puppeteerRenderPlugin.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@ export const puppeteerRender = async (route: HandledRoute): Promise<string> => {
5353
page.on('requestfailed', unRegisterRequest);
5454

5555
const requests = new Set();
56+
5657
// eslint-disable-next-line no-inner-declarations
5758
function registerRequest(request) {
5859
request.continue();
5960
requests.add(requests);
6061
}
62+
6163
// eslint-disable-next-line no-inner-declarations
6264
function unRegisterRequest(request) {
6365
// request.continue();
@@ -78,6 +80,24 @@ export const puppeteerRender = async (route: HandledRoute): Promise<string> => {
7880
}
7981
});
8082
}
83+
84+
if (
85+
scullyConfig.ignoreResourceTypes &&
86+
scullyConfig.ignoreResourceTypes.length > 0
87+
) {
88+
await page.setRequestInterception(true);
89+
page.on('request', checkIfRequestShouldBeIgnored);
90+
91+
// eslint-disable-next-line no-inner-declarations
92+
function checkIfRequestShouldBeIgnored(request) {
93+
if (scullyConfig.ignoreResourceTypes.includes(request.resourceType())) {
94+
request.abort();
95+
} else {
96+
request.continue();
97+
}
98+
}
99+
}
100+
81101
/** this will be called from the browser, but runs in node */
82102
await page.exposeFunction('onCustomEvent', () => {
83103
resolve();

libs/scully/src/lib/utils/interfacesandenums.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { LaunchOptions } from 'puppeteer';
1+
import { LaunchOptions, ResourceType } from 'puppeteer';
22

33
export enum RouteTypes {
44
json = 'json',
@@ -49,6 +49,8 @@ export interface ScullyConfig {
4949
guessParserOptions?: GuessParserOptions;
5050
/** the maximum of concurrent puppeteer tabs open. defaults to the available amounts of cores */
5151
maxRenderThreads?: number;
52+
/** the resource types to ignore when generating pages via Puppeteer */
53+
ignoreResourceTypes?: ResourceType[];
5254
}
5355

5456
interface RouteConfig {

0 commit comments

Comments
 (0)