-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds first input delay performance metric (#8884)
* measures fid * updates typings, fixes logic, updates per review comments * update to es5 * separate clearMeasures * use relayer * creates fid polyfll render helper + simplifies measure * switch to dynamic import * creates fid experimental flag * removes unecessary time-to-first-input metric * removes hydration measure removes * default flag to false Co-authored-by: Joe Haddad <joe.haddad@zeit.co>
- Loading branch information
1 parent
0ccce72
commit 04ceba4
Showing
6 changed files
with
187 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* global hydrationMetrics */ | ||
|
||
export default onPerfEntry => { | ||
hydrationMetrics.onInputDelay((delay, event) => { | ||
const hydrationMeasures = performance.getEntriesByName( | ||
'Next.js-hydration', | ||
'measure' | ||
) | ||
|
||
if (hydrationMeasures.length > 0) { | ||
const { startTime, duration } = hydrationMeasures[0] | ||
const hydrateEnd = startTime + duration | ||
|
||
if (event.timeStamp > hydrateEnd) { | ||
onPerfEntry({ | ||
name: 'first-input-delay-after-hydration', | ||
startTime: event.timeStamp, | ||
duration: delay, | ||
}) | ||
} else { | ||
onPerfEntry({ | ||
name: 'first-input-delay-before-hydration', | ||
startTime: event.timeStamp, | ||
duration: delay, | ||
}) | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/** | ||
* This is a modified version of the First Input Delay polyfill | ||
* https://github.com/GoogleChromeLabs/first-input-delay | ||
* | ||
* It checks for a first input before and after hydration | ||
*/ | ||
|
||
type DelayCallback = (delay: number, event: Event) => void | ||
type addEventListener = ( | ||
type: string, | ||
listener: EventListener, | ||
listenerOpts: EventListenerOptions | ||
) => void | ||
type removeEventListener = addEventListener | ||
|
||
function fidPolyfill( | ||
addEventListener: addEventListener, | ||
removeEventListener: removeEventListener | ||
) { | ||
var firstInputEvent: Event | ||
var firstInputDelay: number | ||
var firstInputTimeStamp: number | ||
|
||
var callbacks: DelayCallback[] = [] | ||
|
||
var listenerOpts = { passive: true, capture: true } | ||
var startTimeStamp = +new Date() | ||
|
||
var pointerup = 'pointerup' | ||
var pointercancel = 'pointercancel' | ||
|
||
function onInputDelay(callback: DelayCallback) { | ||
callbacks.push(callback) | ||
reportInputDelayIfRecordedAndValid() | ||
} | ||
|
||
function recordInputDelay(delay: number, evt: Event) { | ||
firstInputEvent = evt | ||
firstInputDelay = delay | ||
firstInputTimeStamp = +new Date() | ||
|
||
reportInputDelayIfRecordedAndValid() | ||
} | ||
|
||
function reportInputDelayIfRecordedAndValid() { | ||
var hydrationMeasures = performance.getEntriesByName( | ||
'Next.js-hydration', | ||
'measure' | ||
) | ||
var firstInputStart = firstInputTimeStamp - startTimeStamp | ||
|
||
if ( | ||
firstInputDelay >= 0 && | ||
firstInputDelay < firstInputStart && | ||
(hydrationMeasures.length === 0 || | ||
hydrationMeasures[0].startTime < firstInputStart) | ||
) { | ||
callbacks.forEach(function(callback) { | ||
callback(firstInputDelay, firstInputEvent) | ||
}) | ||
|
||
// If the app is already hydrated, that means the first "post-hydration" input | ||
// has been measured and listeners can be removed | ||
if (hydrationMeasures.length > 0) { | ||
eachEventType(removeEventListener) | ||
callbacks = [] | ||
} | ||
} | ||
} | ||
|
||
function onPointerDown(delay: number, evt: Event) { | ||
function onPointerUp() { | ||
recordInputDelay(delay, evt) | ||
} | ||
|
||
function onPointerCancel() { | ||
removePointerEventListeners() | ||
} | ||
|
||
function removePointerEventListeners() { | ||
removeEventListener(pointerup, onPointerUp, listenerOpts) | ||
removeEventListener(pointercancel, onPointerCancel, listenerOpts) | ||
} | ||
|
||
addEventListener(pointerup, onPointerUp, listenerOpts) | ||
addEventListener(pointercancel, onPointerCancel, listenerOpts) | ||
} | ||
|
||
function onInput(evt: Event) { | ||
if (evt.cancelable) { | ||
var isEpochTime = evt.timeStamp > 1e12 | ||
var now = isEpochTime ? +new Date() : performance.now() | ||
|
||
var delay = now - evt.timeStamp | ||
|
||
if (evt.type === 'pointerdown') { | ||
onPointerDown(delay, evt) | ||
} else { | ||
recordInputDelay(delay, evt) | ||
} | ||
} | ||
} | ||
|
||
function eachEventType(callback: addEventListener | removeEventListener) { | ||
var eventTypes = [ | ||
'click', | ||
'mousedown', | ||
'keydown', | ||
'touchstart', | ||
'pointerdown', | ||
] | ||
eventTypes.forEach(function(eventType) { | ||
callback(eventType, onInput, listenerOpts) | ||
}) | ||
} | ||
|
||
eachEventType(addEventListener) | ||
|
||
var context = self as any | ||
context['hydrationMetrics'] = context['hydrationMetrics'] || {} | ||
context['hydrationMetrics']['onInputDelay'] = onInputDelay | ||
} | ||
|
||
export default fidPolyfill |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters