Skip to content

Commit 66dd5aa

Browse files
committed
Rename successUrlsThreshold to more accurate maxPages
1 parent 7b1008d commit 66dd5aa

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

src/generate-critical-css.ts

+20-22
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ const noop = () => {
1515
*
1616
* Errors that occur during this process are collated, but not thrown yet.
1717
*
18-
* @param {BrowserInterface} browserInterface - interface to access pages
19-
* @param {string[]} urls - list of URLs to scan for CSS files
20-
* @param {number} successUrlsThreshold - success urls amount threshold
18+
* @param {BrowserInterface} browserInterface - interface to access pages
19+
* @param {string[]} urls - list of URLs to scan for CSS files
20+
* @param {number} maxPages - number of pages to process at most
2121
* @return {Array} - Two member array; CSSFileSet, and an object containing errors that occurred at each URL.
2222
*/
2323
async function collateCssFiles(
2424
browserInterface: BrowserInterface,
2525
urls: string[],
26-
successUrlsThreshold: number
26+
maxPages: number
2727
): Promise< [ CSSFileSet, { [ url: string ]: UrlError } ] > {
2828
const cssFiles = new CSSFileSet( browserInterface );
2929
const errors = {};
@@ -53,9 +53,9 @@ async function collateCssFiles(
5353
const internalStyles = await browserInterface.getInternalStyles( url );
5454
await cssFiles.addInternalStyles( url, internalStyles );
5555

56-
// Abort early if we hit the threshold of success urls.
56+
// Abort early if we hit the critical mass
5757
successes++;
58-
if ( successes >= successUrlsThreshold ) {
58+
if ( successes >= maxPages ) {
5959
break;
6060
}
6161
} catch ( err ) {
@@ -69,13 +69,13 @@ async function collateCssFiles(
6969
/**
7070
* Get CSS selectors for above the fold content for the valid URLs.
7171
*
72-
* @param {Object} param - All the parameters as object.
73-
* @param {BrowserInterface} param.browserInterface - Interface to access pages
74-
* @param {Object} param.selectorPages - All the CSS selectors to URLs map object
75-
* @param {string[]} param.validUrls - List of all the valid URLs
76-
* @param {Array} param.viewports - Browser viewports
77-
* @param {number} param.successUrlsThreshold - Success URLs amount threshold
78-
* @param {Function} param.updateProgress - Update progress callback function
72+
* @param {Object} param - All the parameters as object.
73+
* @param {BrowserInterface} param.browserInterface - Interface to access pages
74+
* @param {Object} param.selectorPages - All the CSS selectors to URLs map object
75+
* @param {string[]} param.validUrls - List of all the valid URLs
76+
* @param {Array} param.viewports - Browser viewports
77+
* @param {number} param.maxPages - Maximum number of pages to process
78+
* @param {Function} param.updateProgress - Update progress callback function
7979
*
8080
* @return {Set<string>} - List of above the fold selectors.
8181
*/
@@ -84,14 +84,14 @@ async function getAboveFoldSelectors( {
8484
selectorPages,
8585
validUrls,
8686
viewports,
87-
successUrlsThreshold,
87+
maxPages,
8888
updateProgress,
8989
}: {
9090
browserInterface: BrowserInterface;
9191
selectorPages: { [ selector: string ]: Set< string > };
9292
validUrls: string[];
9393
viewports: Viewport[];
94-
successUrlsThreshold: number;
94+
maxPages: number;
9595
updateProgress: () => void;
9696
} ): Promise< Set< string > > {
9797
// For each selector string, create a "trimmed" version with the stuff JavaScript can't handle cut out.
@@ -105,7 +105,7 @@ async function getAboveFoldSelectors( {
105105
const aboveFoldSelectors = new Set< string >();
106106
const dangerousSelectors = new Set< string >();
107107

108-
for ( const url of validUrls.slice( 0, successUrlsThreshold ) ) {
108+
for ( const url of validUrls.slice( 0, maxPages ) ) {
109109
// Work out which CSS selectors match any element on this page.
110110
const pageSelectors = await browserInterface.runInPage<
111111
ReturnType< typeof BrowserInterface.innerFindMatchingSelectors >
@@ -143,13 +143,15 @@ export async function generateCriticalCSS( {
143143
viewports,
144144
filters,
145145
successRatio = 1,
146+
maxPages = 10,
146147
}: {
147148
browserInterface: BrowserInterface;
148149
progressCallback?: ( step: number, total: number ) => void;
149150
urls: string[];
150151
viewports: Viewport[];
151152
filters?: FilterSpec;
152153
successRatio?: number;
154+
maxPages?: number;
153155
} ): Promise< [ string, Error[] ] > {
154156
const successUrlsThreshold = Math.ceil( urls.length * successRatio );
155157

@@ -160,11 +162,7 @@ export async function generateCriticalCSS( {
160162
const updateProgress = () => progressCallback( ++progress, progressSteps );
161163

162164
// Collate all CSS Files used by all valid URLs.
163-
const [ cssFiles, cssFileErrors ] = await collateCssFiles(
164-
browserInterface,
165-
urls,
166-
successUrlsThreshold
167-
);
165+
const [ cssFiles, cssFileErrors ] = await collateCssFiles( browserInterface, urls, maxPages );
168166
updateProgress();
169167

170168
// Verify there are enough valid URLs to carry on with.
@@ -186,7 +184,7 @@ export async function generateCriticalCSS( {
186184
selectorPages,
187185
validUrls,
188186
viewports,
189-
successUrlsThreshold,
187+
maxPages,
190188
updateProgress,
191189
} );
192190

0 commit comments

Comments
 (0)