Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/scully-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
}
```

Expand Down Expand Up @@ -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.
20 changes: 20 additions & 0 deletions libs/scully/src/lib/renderPlugins/puppeteerRenderPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ export const puppeteerRender = async (route: HandledRoute): Promise<string> => {
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();
Expand All @@ -78,6 +80,24 @@ export const puppeteerRender = async (route: HandledRoute): Promise<string> => {
}
});
}

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();
Expand Down
4 changes: 3 additions & 1 deletion libs/scully/src/lib/utils/interfacesandenums.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LaunchOptions } from 'puppeteer';
import { LaunchOptions, ResourceType } from 'puppeteer';

export enum RouteTypes {
json = 'json',
Expand Down Expand Up @@ -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 {
Expand Down