From 26c3f7bb2d6585f6e6d4dba62dbc4762ad9a6266 Mon Sep 17 00:00:00 2001 From: Sam Vloeberghs Date: Wed, 27 May 2020 17:37:28 +0200 Subject: [PATCH] feat(scully): added the option to ignore a list of ResourceTypes --- docs/scully-configuration.md | 8 ++++++++ .../renderPlugins/puppeteerRenderPlugin.ts | 20 +++++++++++++++++++ .../src/lib/utils/interfacesandenums.ts | 4 +++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/docs/scully-configuration.md b/docs/scully-configuration.md index b6369d2c1..3b2fd8736 100644 --- a/docs/scully-configuration.md +++ b/docs/scully-configuration.md @@ -79,6 +79,8 @@ export interface ScullyConfig { guessParserOptions?: GuessParserOptions; /** the maximum of concurrent puppeteer tabs open. defaults to the available amounts of cores */ maxRenderThreads?: number; + /** the resource types to ignore when generating pages via Puppeteer */ + ignoreResourceTypes?: ResourceType[]; } ``` @@ -198,3 +200,9 @@ Connects your application to a different host. This is useful when using your ow The`guessParserOptions` that get passed to the `guess-parser` library. Currently, the only supported property is `excludedFiles`, and it excludes files from the `guess-parser` route discovery process. + +### ignoreResourceTypes + +The `ignoreResourceTypes` array that get passed to the `puppeteerRenderPlugin`. +Any `ResourceType` that is listed here will be ignored by the Puppeteer instance rendering the requested page. +For example if you add `image` and `font` all requests to images and fonts loaded on your pages will be ignored. diff --git a/libs/scully/src/lib/renderPlugins/puppeteerRenderPlugin.ts b/libs/scully/src/lib/renderPlugins/puppeteerRenderPlugin.ts index 13244ca09..b1fb23aa4 100644 --- a/libs/scully/src/lib/renderPlugins/puppeteerRenderPlugin.ts +++ b/libs/scully/src/lib/renderPlugins/puppeteerRenderPlugin.ts @@ -53,11 +53,13 @@ export const puppeteerRender = async (route: HandledRoute): Promise => { page.on('requestfailed', unRegisterRequest); const requests = new Set(); + // eslint-disable-next-line no-inner-declarations function registerRequest(request) { request.continue(); requests.add(requests); } + // eslint-disable-next-line no-inner-declarations function unRegisterRequest(request) { // request.continue(); @@ -78,6 +80,24 @@ export const puppeteerRender = async (route: HandledRoute): Promise => { } }); } + + if ( + scullyConfig.ignoreResourceTypes && + scullyConfig.ignoreResourceTypes.length > 0 + ) { + await page.setRequestInterception(true); + page.on('request', checkIfRequestShouldBeIgnored); + + // eslint-disable-next-line no-inner-declarations + function checkIfRequestShouldBeIgnored(request) { + if (scullyConfig.ignoreResourceTypes.includes(request.resourceType())) { + request.abort(); + } else { + request.continue(); + } + } + } + /** this will be called from the browser, but runs in node */ await page.exposeFunction('onCustomEvent', () => { resolve(); diff --git a/libs/scully/src/lib/utils/interfacesandenums.ts b/libs/scully/src/lib/utils/interfacesandenums.ts index d61c6de22..202e169c9 100644 --- a/libs/scully/src/lib/utils/interfacesandenums.ts +++ b/libs/scully/src/lib/utils/interfacesandenums.ts @@ -1,4 +1,4 @@ -import { LaunchOptions } from 'puppeteer'; +import { LaunchOptions, ResourceType } from 'puppeteer'; export enum RouteTypes { json = 'json', @@ -49,6 +49,8 @@ export interface ScullyConfig { guessParserOptions?: GuessParserOptions; /** the maximum of concurrent puppeteer tabs open. defaults to the available amounts of cores */ maxRenderThreads?: number; + /** the resource types to ignore when generating pages via Puppeteer */ + ignoreResourceTypes?: ResourceType[]; } interface RouteConfig {