Skip to content

Commit

Permalink
faster fetch of loadingExperience using new url:/origin: prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
alekseykulikov committed Sep 11, 2019
1 parent eb1d17d commit 781a81d
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/audits/field-fcp-origin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { Audit } = require('lighthouse')
const {
getCruxData,
getLoadingExperience,
createNotApplicableResult,
createValueResult,
createErrorResult,
Expand Down Expand Up @@ -39,7 +39,7 @@ class FieldFcpOriginAudit extends Audit {
*/
static async audit(artifacts, context) {
try {
const { originLoadingExperience: ole } = await getCruxData(artifacts, context)
const ole = await getLoadingExperience(artifacts, context, false)
if (!isResultsInField(ole)) return createNotApplicableResult(FieldFcpOriginAudit.meta.title)
return createValueResult(ole.metrics.FIRST_CONTENTFUL_PAINT_MS, 's', FieldFcpOriginAudit.defaultOptions)
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions src/audits/field-fcp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { Audit } = require('lighthouse')
const {
getCruxData,
getLoadingExperience,
createNotApplicableResult,
createValueResult,
createErrorResult,
Expand Down Expand Up @@ -39,7 +39,7 @@ class FieldFcpAudit extends Audit {
*/
static async audit(artifacts, context) {
try {
const { loadingExperience: le } = await getCruxData(artifacts, context)
const le = await getLoadingExperience(artifacts, context)
if (!isResultsInField(le)) return createNotApplicableResult(FieldFcpAudit.meta.title)
return createValueResult(le.metrics.FIRST_CONTENTFUL_PAINT_MS, 's', FieldFcpAudit.defaultOptions)
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions src/audits/field-fid-origin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { Audit } = require('lighthouse')
const {
getCruxData,
getLoadingExperience,
createNotApplicableResult,
createValueResult,
createErrorResult,
Expand Down Expand Up @@ -39,7 +39,7 @@ class FieldFidOriginAudit extends Audit {
*/
static async audit(artifacts, context) {
try {
const { originLoadingExperience: ole } = await getCruxData(artifacts, context)
const ole = await getLoadingExperience(artifacts, context, false)
if (!isResultsInField(ole)) return createNotApplicableResult(FieldFidOriginAudit.meta.title)
return createValueResult(ole.metrics.FIRST_INPUT_DELAY_MS, 'ms', FieldFidOriginAudit.defaultOptions)
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions src/audits/field-fid.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { Audit } = require('lighthouse')
const {
getCruxData,
getLoadingExperience,
createNotApplicableResult,
createValueResult,
createErrorResult,
Expand Down Expand Up @@ -39,7 +39,7 @@ class FieldFidAudit extends Audit {
*/
static async audit(artifacts, context) {
try {
const { loadingExperience: le } = await getCruxData(artifacts, context)
const le = await getLoadingExperience(artifacts, context)
if (!isResultsInField(le)) return createNotApplicableResult(FieldFidAudit.meta.title)
return createValueResult(le.metrics.FIRST_INPUT_DELAY_MS, 'ms', FieldFidAudit.defaultOptions)
} catch (err) {
Expand Down
10 changes: 6 additions & 4 deletions src/utils/audit-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,24 @@ const requests = new Map()
*
* @param {Object} artifacts
* @param {Object} context
* @return {Promise<{ loadingExperience: LoadingExperience, originLoadingExperience: LoadingExperience }>}
* @param {boolean} [isUrl]
* @return {Promise<LoadingExperience>}
*/

exports.getCruxData = async (artifacts, context) => {
exports.getLoadingExperience = async (artifacts, context, isUrl = true) => {
const { URL, settings } = artifacts
// @ts-ignore
const psiToken = context.settings.psiToken || null
const strategy = settings.emulatedFormFactor === 'desktop' ? 'desktop' : 'mobile'
const url = URL.finalUrl
const prefix = isUrl ? 'url' : 'origin'
const url = `${prefix}:${URL.finalUrl}`
const key = url + strategy
if (!requests.has(key)) {
requests.set(key, runPsi({ url, strategy, psiToken }))
}
const json = await requests.get(key)
if (json.error) throw new Error(JSON.stringify(json.error))
return { loadingExperience: json.loadingExperience, originLoadingExperience: json.originLoadingExperience }
return json.loadingExperience
}

/**
Expand Down

0 comments on commit 781a81d

Please sign in to comment.