@@ -12,9 +12,17 @@ InvalidUrlError.prototype = Error.prototype
12
12
/**
13
13
* @param {string } url URL to get CSS from
14
14
* @param {string } waitUntil https://github.com/puppeteer/puppeteer/blob/master/docs/api.md#pagegotourl-options
15
+ * @param {string } timeout https://github.com/puppeteer/puppeteer/blob/master/docs/api.md#pagegotourl-options
16
+ * @param {string } origins Can either be 'include' or 'exlude'
17
+ * @param {string } inlineStyles Can either be 'include' or 'exlude'
15
18
* @returns {string } All CSS that was found
16
19
*/
17
- module . exports = async ( url , { waitUntil = 'networkidle0' , origins = 'exclude' , inlineStyles = 'include' } = { } ) => {
20
+ module . exports = async ( url , {
21
+ waitUntil = 'networkidle0' ,
22
+ timeout = 10000 ,
23
+ origins = 'exclude' ,
24
+ inlineStyles = 'include'
25
+ } = { } ) => {
18
26
// Setup a browser instance
19
27
const browser = await puppeteer . launch ( )
20
28
@@ -26,29 +34,40 @@ module.exports = async (url, {waitUntil = 'networkidle0', origins = 'exclude', i
26
34
await page . setUserAgent ( 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.16; rv:85.0) Gecko/20100101 Firefox/85.0' )
27
35
await page . coverage . startCSSCoverage ( )
28
36
url = normalizeUrl ( url , { stripWWW : false } )
29
- const response = await page . goto ( url , { waitUntil} )
37
+
38
+ let response
39
+
40
+ // Explicit try-catch for when pages timeout
41
+ try {
42
+ response = await page . goto ( url , {
43
+ waitUntil,
44
+ timeout
45
+ } )
46
+ } catch ( error ) {
47
+ // In case of timeouts
48
+ await browser . close ( )
49
+
50
+ throw error
51
+ }
30
52
31
53
// Make sure that we only try to extract CSS from valid pages.
32
54
// Bail out if the response is an invalid request (400, 500)
33
55
if ( response . status ( ) >= 400 ) {
34
56
await browser . close ( ) // Don't leave any resources behind
35
57
36
- return Promise . reject (
37
- new InvalidUrlError ( {
38
- url,
39
- statusCode : response . status ( ) ,
40
- statusText : response . statusText ( )
41
- } )
42
- )
58
+ throw new InvalidUrlError ( {
59
+ url,
60
+ statusCode : response . status ( ) ,
61
+ statusText : response . statusText ( )
62
+ } )
43
63
}
44
64
45
65
// If the response is a CSS file, return that file
46
66
// instead of running our complicated setup
47
67
const headers = response . headers ( )
48
68
49
69
if ( headers [ 'content-type' ] . includes ( 'text/css' ) ) {
50
- const css = await response . text ( )
51
- return Promise . resolve ( css )
70
+ return response . text ( )
52
71
}
53
72
54
73
const coverage = await page . coverage . stopCSSCoverage ( )
@@ -95,6 +114,8 @@ module.exports = async (url, {waitUntil = 'networkidle0', origins = 'exclude', i
95
114
. map ( css => ( { type : 'inline' , href : url , css} ) )
96
115
}
97
116
117
+ await browser . close ( )
118
+
98
119
const links = coverage
99
120
// Filter out the <style> tags that were found in the coverage
100
121
// report since we've conducted our own search for them.
@@ -107,21 +128,17 @@ module.exports = async (url, {waitUntil = 'networkidle0', origins = 'exclude', i
107
128
type : 'link-or-import'
108
129
} ) )
109
130
110
- await browser . close ( )
111
-
112
131
const css = links
113
132
. concat ( styleSheetsApiCss )
114
133
. concat ( inlineStyles === 'exclude' ? [ ] : inlineCss )
115
134
116
135
// Return the complete structure ...
117
136
if ( origins === 'include' ) {
118
- return Promise . resolve ( css )
137
+ return css
119
138
}
120
139
121
140
// ... or return all CSS as a single String
122
- return Promise . resolve (
123
- css
124
- . map ( ( { css} ) => css )
125
- . join ( '\n' )
126
- )
141
+ return css
142
+ . map ( ( { css} ) => css )
143
+ . join ( '\n' )
127
144
}
0 commit comments