@@ -15,15 +15,15 @@ const noop = () => {
15
15
*
16
16
* Errors that occur during this process are collated, but not thrown yet.
17
17
*
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
21
21
* @return {Array } - Two member array; CSSFileSet, and an object containing errors that occurred at each URL.
22
22
*/
23
23
async function collateCssFiles (
24
24
browserInterface : BrowserInterface ,
25
25
urls : string [ ] ,
26
- successUrlsThreshold : number
26
+ maxPages : number
27
27
) : Promise < [ CSSFileSet , { [ url : string ] : UrlError } ] > {
28
28
const cssFiles = new CSSFileSet ( browserInterface ) ;
29
29
const errors = { } ;
@@ -53,9 +53,9 @@ async function collateCssFiles(
53
53
const internalStyles = await browserInterface . getInternalStyles ( url ) ;
54
54
await cssFiles . addInternalStyles ( url , internalStyles ) ;
55
55
56
- // Abort early if we hit the threshold of success urls.
56
+ // Abort early if we hit the critical mass
57
57
successes ++ ;
58
- if ( successes >= successUrlsThreshold ) {
58
+ if ( successes >= maxPages ) {
59
59
break ;
60
60
}
61
61
} catch ( err ) {
@@ -69,13 +69,13 @@ async function collateCssFiles(
69
69
/**
70
70
* Get CSS selectors for above the fold content for the valid URLs.
71
71
*
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
79
79
*
80
80
* @return {Set<string> } - List of above the fold selectors.
81
81
*/
@@ -84,14 +84,14 @@ async function getAboveFoldSelectors( {
84
84
selectorPages,
85
85
validUrls,
86
86
viewports,
87
- successUrlsThreshold ,
87
+ maxPages ,
88
88
updateProgress,
89
89
} : {
90
90
browserInterface : BrowserInterface ;
91
91
selectorPages : { [ selector : string ] : Set < string > } ;
92
92
validUrls : string [ ] ;
93
93
viewports : Viewport [ ] ;
94
- successUrlsThreshold : number ;
94
+ maxPages : number ;
95
95
updateProgress : ( ) => void ;
96
96
} ) : Promise < Set < string > > {
97
97
// For each selector string, create a "trimmed" version with the stuff JavaScript can't handle cut out.
@@ -105,7 +105,7 @@ async function getAboveFoldSelectors( {
105
105
const aboveFoldSelectors = new Set < string > ( ) ;
106
106
const dangerousSelectors = new Set < string > ( ) ;
107
107
108
- for ( const url of validUrls . slice ( 0 , successUrlsThreshold ) ) {
108
+ for ( const url of validUrls . slice ( 0 , maxPages ) ) {
109
109
// Work out which CSS selectors match any element on this page.
110
110
const pageSelectors = await browserInterface . runInPage <
111
111
ReturnType < typeof BrowserInterface . innerFindMatchingSelectors >
@@ -143,13 +143,15 @@ export async function generateCriticalCSS( {
143
143
viewports,
144
144
filters,
145
145
successRatio = 1 ,
146
+ maxPages = 10 ,
146
147
} : {
147
148
browserInterface : BrowserInterface ;
148
149
progressCallback ?: ( step : number , total : number ) => void ;
149
150
urls : string [ ] ;
150
151
viewports : Viewport [ ] ;
151
152
filters ?: FilterSpec ;
152
153
successRatio ?: number ;
154
+ maxPages ?: number ;
153
155
} ) : Promise < [ string , Error [ ] ] > {
154
156
const successUrlsThreshold = Math . ceil ( urls . length * successRatio ) ;
155
157
@@ -160,11 +162,7 @@ export async function generateCriticalCSS( {
160
162
const updateProgress = ( ) => progressCallback ( ++ progress , progressSteps ) ;
161
163
162
164
// 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 ) ;
168
166
updateProgress ( ) ;
169
167
170
168
// Verify there are enough valid URLs to carry on with.
@@ -186,7 +184,7 @@ export async function generateCriticalCSS( {
186
184
selectorPages,
187
185
validUrls,
188
186
viewports,
189
- successUrlsThreshold ,
187
+ maxPages ,
190
188
updateProgress,
191
189
} ) ;
192
190
0 commit comments