88const Audit = require ( './audit.js' ) ;
99const URL = require ( '../lib/url-shim.js' ) ;
1010const NetworkRecords = require ( '../computed/network-records.js' ) ;
11+ const MainResource = require ( '../computed/main-resource.js' ) ;
1112
1213class NetworkRequests extends Audit {
1314 /**
@@ -19,7 +20,7 @@ class NetworkRequests extends Audit {
1920 scoreDisplayMode : Audit . SCORING_MODES . INFORMATIVE ,
2021 title : 'Network Requests' ,
2122 description : 'Lists the network requests that were made during page load.' ,
22- requiredArtifacts : [ 'devtoolsLogs' ] ,
23+ requiredArtifacts : [ 'devtoolsLogs' , 'URL' , 'GatherContext' ] ,
2324 } ;
2425 }
2526
@@ -28,75 +29,91 @@ class NetworkRequests extends Audit {
2829 * @param {LH.Audit.Context } context
2930 * @return {Promise<LH.Audit.Product> }
3031 */
31- static audit ( artifacts , context ) {
32+ static async audit ( artifacts , context ) {
3233 const devtoolsLog = artifacts . devtoolsLogs [ Audit . DEFAULT_PASS ] ;
33- return NetworkRecords . request ( devtoolsLog , context ) . then ( records => {
34- const earliestStartTime = records . reduce (
35- ( min , record ) => Math . min ( min , record . startTime ) ,
36- Infinity
37- ) ;
34+ const records = await NetworkRecords . request ( devtoolsLog , context ) ;
35+ const earliestStartTime = records . reduce (
36+ ( min , record ) => Math . min ( min , record . startTime ) ,
37+ Infinity
38+ ) ;
3839
39- /** @param {number } time */
40- const timeToMs = time => time < earliestStartTime || ! Number . isFinite ( time ) ?
41- undefined : ( time - earliestStartTime ) * 1000 ;
40+ // Optional mainFrameId check because the main resource is only available for
41+ // navigations. TODO: https://github.com/GoogleChrome/lighthouse/issues/14157
42+ // for the general solution to this.
43+ /** @type {string|undefined } */
44+ let mainFrameId ;
45+ if ( artifacts . GatherContext . gatherMode === 'navigation' ) {
46+ const mainResource = await MainResource . request ( { devtoolsLog, URL : artifacts . URL } , context ) ;
47+ mainFrameId = mainResource . frameId ;
48+ }
4249
43- const results = records . map ( record => {
44- const endTimeDeltaMs = record . lrStatistics ?. endTimeDeltaMs ;
45- const TCPMs = record . lrStatistics ?. TCPMs ;
46- const requestMs = record . lrStatistics ?. requestMs ;
47- const responseMs = record . lrStatistics ?. responseMs ;
50+ /** @param {number } time */
51+ const timeToMs = time => time < earliestStartTime || ! Number . isFinite ( time ) ?
52+ undefined : ( time - earliestStartTime ) * 1000 ;
4853
49- return {
50- url : URL . elideDataURI ( record . url ) ,
51- protocol : record . protocol ,
52- startTime : timeToMs ( record . startTime ) ,
53- endTime : timeToMs ( record . endTime ) ,
54- finished : record . finished ,
55- transferSize : record . transferSize ,
56- resourceSize : record . resourceSize ,
57- statusCode : record . statusCode ,
58- mimeType : record . mimeType ,
59- resourceType : record . resourceType ,
60- lrEndTimeDeltaMs : endTimeDeltaMs , // Only exists on Lightrider runs
61- lrTCPMs : TCPMs , // Only exists on Lightrider runs
62- lrRequestMs : requestMs , // Only exists on Lightrider runs
63- lrResponseMs : responseMs , // Only exists on Lightrider runs
64- } ;
65- } ) ;
66-
67- // NOTE(i18n): this audit is only for debug info in the LHR and does not appear in the report.
68- /** @type {LH.Audit.Details.Table['headings'] } */
69- const headings = [
70- { key : 'url' , itemType : 'url' , text : 'URL' } ,
71- { key : 'protocol' , itemType : 'text' , text : 'Protocol' } ,
72- { key : 'startTime' , itemType : 'ms' , granularity : 1 , text : 'Start Time' } ,
73- { key : 'endTime' , itemType : 'ms' , granularity : 1 , text : 'End Time' } ,
74- {
75- key : 'transferSize' ,
76- itemType : 'bytes' ,
77- displayUnit : 'kb' ,
78- granularity : 1 ,
79- text : 'Transfer Size' ,
80- } ,
81- {
82- key : 'resourceSize' ,
83- itemType : 'bytes' ,
84- displayUnit : 'kb' ,
85- granularity : 1 ,
86- text : 'Resource Size' ,
87- } ,
88- { key : 'statusCode' , itemType : 'text' , text : 'Status Code' } ,
89- { key : 'mimeType' , itemType : 'text' , text : 'MIME Type' } ,
90- { key : 'resourceType' , itemType : 'text' , text : 'Resource Type' } ,
91- ] ;
92-
93- const tableDetails = Audit . makeTableDetails ( headings , results ) ;
54+ const results = records . map ( record => {
55+ const endTimeDeltaMs = record . lrStatistics ?. endTimeDeltaMs ;
56+ const TCPMs = record . lrStatistics ?. TCPMs ;
57+ const requestMs = record . lrStatistics ?. requestMs ;
58+ const responseMs = record . lrStatistics ?. responseMs ;
59+ // Default these to undefined so omitted from JSON in the negative case.
60+ const isLinkPreload = record . isLinkPreload || undefined ;
61+ const experimentalFromMainFrame = mainFrameId ?
62+ ( ( record . frameId === mainFrameId ) || undefined ) :
63+ undefined ;
9464
9565 return {
96- score : 1 ,
97- details : tableDetails ,
66+ url : URL . elideDataURI ( record . url ) ,
67+ protocol : record . protocol ,
68+ startTime : timeToMs ( record . startTime ) ,
69+ endTime : timeToMs ( record . endTime ) ,
70+ finished : record . finished ,
71+ transferSize : record . transferSize ,
72+ resourceSize : record . resourceSize ,
73+ statusCode : record . statusCode ,
74+ mimeType : record . mimeType ,
75+ resourceType : record . resourceType ,
76+ isLinkPreload,
77+ experimentalFromMainFrame,
78+ lrEndTimeDeltaMs : endTimeDeltaMs , // Only exists on Lightrider runs
79+ lrTCPMs : TCPMs , // Only exists on Lightrider runs
80+ lrRequestMs : requestMs , // Only exists on Lightrider runs
81+ lrResponseMs : responseMs , // Only exists on Lightrider runs
9882 } ;
9983 } ) ;
84+
85+ // NOTE(i18n): this audit is only for debug info in the LHR and does not appear in the report.
86+ /** @type {LH.Audit.Details.Table['headings'] } */
87+ const headings = [
88+ { key : 'url' , itemType : 'url' , text : 'URL' } ,
89+ { key : 'protocol' , itemType : 'text' , text : 'Protocol' } ,
90+ { key : 'startTime' , itemType : 'ms' , granularity : 1 , text : 'Start Time' } ,
91+ { key : 'endTime' , itemType : 'ms' , granularity : 1 , text : 'End Time' } ,
92+ {
93+ key : 'transferSize' ,
94+ itemType : 'bytes' ,
95+ displayUnit : 'kb' ,
96+ granularity : 1 ,
97+ text : 'Transfer Size' ,
98+ } ,
99+ {
100+ key : 'resourceSize' ,
101+ itemType : 'bytes' ,
102+ displayUnit : 'kb' ,
103+ granularity : 1 ,
104+ text : 'Resource Size' ,
105+ } ,
106+ { key : 'statusCode' , itemType : 'text' , text : 'Status Code' } ,
107+ { key : 'mimeType' , itemType : 'text' , text : 'MIME Type' } ,
108+ { key : 'resourceType' , itemType : 'text' , text : 'Resource Type' } ,
109+ ] ;
110+
111+ const tableDetails = Audit . makeTableDetails ( headings , results ) ;
112+
113+ return {
114+ score : 1 ,
115+ details : tableDetails ,
116+ } ;
100117 }
101118}
102119
0 commit comments