Skip to content

Commit

Permalink
feat: Upgrade to web-vitals v4 (#1193)
Browse files Browse the repository at this point in the history
Co-authored-by: Chunwai Li <[email protected]>
  • Loading branch information
ptang-nr and cwli24 authored Sep 23, 2024
1 parent a21b939 commit 81349b8
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 17 deletions.
9 changes: 4 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@
"dependencies": {
"fflate": "0.7.4",
"rrweb": "2.0.0-alpha.12",
"web-vitals": "3.5.2"
"web-vitals": "4.2.3"
},
"devDependencies": {
"@babel/cli": "^7.23.4",
Expand Down
12 changes: 9 additions & 3 deletions src/common/vitals/interaction-to-next-paint.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ if (isBrowserScope) {
onINP(({ value, attribution, id }) => {
const attrs = {
metricId: id,
eventTarget: attribution.eventTarget,
eventType: attribution.eventType,
eventTime: attribution.eventTime,
eventTarget: attribution.interactionTarget, // event* attrs deprecated in v4, kept for NR backwards compatibility
eventTime: attribution.interactionTime, // event* attrs deprecated in v4, kept for NR backwards compatibility
interactionTarget: attribution.interactionTarget,
interactionTime: attribution.interactionTime,
interactionType: attribution.interactionType,
inputDelay: attribution.inputDelay,
nextPaintTime: attribution.nextPaintTime,
processingDuration: attribution.processingDuration,
presentationDelay: attribution.presentationDelay,
loadState: attribution.loadState
}
interactionToNextPaint.update({ value, attrs })
Expand Down
3 changes: 2 additions & 1 deletion src/common/vitals/largest-contentful-paint.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ if (isBrowserScope) {
element: attribution.element,
timeToFirstByte: attribution.timeToFirstByte,
resourceLoadDelay: attribution.resourceLoadDelay,
resourceLoadTime: attribution.resourceLoadTime,
resourceLoadDuration: attribution.resourceLoadDuration,
resourceLoadTime: attribution.resourceLoadDuration, // kept for NR backwards compatibility, deprecated in v3->v4
elementRenderDelay: attribution.elementRenderDelay
}
if (attribution.url) attrs.elUrl = cleanURL(attribution.url)
Expand Down
26 changes: 21 additions & 5 deletions tests/unit/common/vitals/interaction-to-next-paint.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ beforeEach(() => {
})

const inpAttribution = {
eventTarget: 'html',
eventType: 'keydown',
eventTime: 100,
interactionType: 'keyboard',
interactionTarget: 'html',
interactionTime: 100,
inputDelay: 0,
nextPaintTime: 200,
processingDuration: 0,
presentationDelay: 0,
loadState: 'complete'
}
const getFreshINPImport = async (codeToRun) => {
Expand All @@ -19,10 +23,22 @@ const getFreshINPImport = async (codeToRun) => {
}

describe('inp', () => {
test('reports fcp from web-vitals', (done) => {
test('reports inp from web-vitals', (done) => {
getFreshINPImport(metric => metric.subscribe(({ value, attrs }) => {
expect(value).toEqual(8)
expect(attrs).toEqual({ ...inpAttribution, metricId: 'ruhroh' })
expect(attrs).toStrictEqual({
eventTarget: inpAttribution.interactionTarget,
eventTime: inpAttribution.interactionTime,
interactionTarget: inpAttribution.interactionTarget,
interactionTime: inpAttribution.interactionTime,
interactionType: inpAttribution.interactionType,
inputDelay: inpAttribution.inputDelay,
nextPaintTime: inpAttribution.nextPaintTime,
processingDuration: inpAttribution.processingDuration,
presentationDelay: inpAttribution.presentationDelay,
loadState: inpAttribution.loadState,
metricId: 'ruhroh'
})
done()
}))
})
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/common/vitals/largest-contentful-paint.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const lcpAttribution = {
element: '#someid',
timeToFirstByte: 1,
resourceLoadDelay: 2,
resourceLoadTime: 3,
resourceLoadDuration: 3,
elementRenderDelay: 4,
url: 'http://domain.com/page?k1=v1#hash'
}
Expand All @@ -38,7 +38,8 @@ describe('lcp', () => {
element: lcpAttribution.element,
timeToFirstByte: lcpAttribution.timeToFirstByte,
resourceLoadDelay: lcpAttribution.resourceLoadDelay,
resourceLoadTime: lcpAttribution.resourceLoadTime,
resourceLoadDuration: lcpAttribution.resourceLoadDuration,
resourceLoadTime: lcpAttribution.resourceLoadDuration,
elementRenderDelay: lcpAttribution.elementRenderDelay
})
done()
Expand Down

0 comments on commit 81349b8

Please sign in to comment.