diff --git a/core/lib/lantern/lantern.js b/core/lib/lantern/lantern.js index f2535058b3c0..f4694e598271 100644 --- a/core/lib/lantern/lantern.js +++ b/core/lib/lantern/lantern.js @@ -43,18 +43,10 @@ function createProcessedNavigation(traceEngineResult) { throw new Error('missing metric scores for main frame'); } - // Grab the latest navigation with scores. - /** @type {Map=} */ - let scores; - for (const navigation of Meta.mainFrameNavigations.reverse()) { - const navigationId = navigation.args.data?.navigationId; - if (!navigationId) continue; - - scores = scoresByNav.get(navigationId); - if (scores) break; - } + const lastNavigationId = Meta.mainFrameNavigations.at(-1)?.args.data?.navigationId ?? ''; + const scores = scoresByNav.get(lastNavigationId); if (!scores) { - throw new Error('no metric scores found for main frame navigation'); + throw new Error('missing metric scores for specified navigation'); } /** @param {MetricName} metric */ diff --git a/core/test/audits/redirects-test.js b/core/test/audits/redirects-test.js index 8db1c8865c01..7c33488c8c5c 100644 --- a/core/test/audits/redirects-test.js +++ b/core/test/audits/redirects-test.js @@ -160,6 +160,9 @@ describe('Performance: Redirects audit', () => { const traceEvents = artifacts.traces.defaultPass.traceEvents; const navStart = traceEvents.find(e => e.name === 'navigationStart'); + const fcp = traceEvents.find(e => e.name === 'firstContentfulPaint'); + const lcp = traceEvents.find(e => e.name === 'largestContentfulPaint::Candidate'); + const secondNavStart = JSON.parse(JSON.stringify(navStart)); traceEvents.push(secondNavStart); navStart.args.data.isLoadingMainFrame = true; @@ -169,6 +172,16 @@ describe('Performance: Redirects audit', () => { secondNavStart.args.data.documentLoaderURL = 'https://www.lisairish.com/'; secondNavStart.args.data.navigationId = '2'; + const secondFcp = JSON.parse(JSON.stringify(fcp)); + traceEvents.push(secondFcp); + secondFcp.args.data.navigationId = '2'; + secondFcp.ts += 2; + + const secondLcp = JSON.parse(JSON.stringify(lcp)); + traceEvents.push(secondLcp); + secondLcp.args.data.navigationId = '2'; + secondFcp.ts += 2; + const output = await RedirectsAudit.audit(artifacts, context); expect(output.details.items).toHaveLength(3); expect(Math.round(output.score * 100) / 100).toMatchInlineSnapshot(`0`); @@ -255,15 +268,33 @@ describe('Performance: Redirects audit', () => { const traceEvents = artifacts.traces.defaultPass.traceEvents; const navStart = traceEvents.find(e => e.name === 'navigationStart'); + const fcp = traceEvents.find(e => e.name === 'firstContentfulPaint'); + const lcp = traceEvents.find(e => e.name === 'largestContentfulPaint::Candidate'); const secondNavStart = JSON.parse(JSON.stringify(navStart)); traceEvents.push(secondNavStart); secondNavStart.args.data.navigationId = '2'; + const secondFcp = JSON.parse(JSON.stringify(fcp)); + traceEvents.push(secondFcp); + secondFcp.args.data.navigationId = '2'; + + const secondLcp = JSON.parse(JSON.stringify(lcp)); + traceEvents.push(secondLcp); + secondLcp.args.data.navigationId = '2'; + const thirdNavStart = JSON.parse(JSON.stringify(navStart)); traceEvents.push(thirdNavStart); thirdNavStart.args.data.navigationId = '3'; + const thirdFcp = JSON.parse(JSON.stringify(fcp)); + traceEvents.push(thirdFcp); + thirdFcp.args.data.navigationId = '3'; + + const thirdLcp = JSON.parse(JSON.stringify(lcp)); + traceEvents.push(thirdLcp); + thirdLcp.args.data.navigationId = '3'; + const output = await RedirectsAudit.audit(artifacts, context); expect(output).toMatchObject({ score: 0,