Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flush buffered vitals metrics on page mount #33867

Merged
merged 5 commits into from Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 11 additions & 11 deletions packages/next/client/vitals.ts
Expand Up @@ -5,15 +5,19 @@ type ReportWebVitalsCallback = (webVitals: NextWebVitalsMetric) => any
export const webVitalsCallbacks = new Set<ReportWebVitalsCallback>()

let flushed = false
export const bufferedVitalsMetrics: NextWebVitalsMetric[] = []
const metrics: NextWebVitalsMetric[] = []

export function getBufferedVitalsMetrics() {
return metrics
}
huozhi marked this conversation as resolved.
Show resolved Hide resolved

export function flushBufferedVitalsMetrics() {
flushed = true
bufferedVitalsMetrics.length = 0
metrics.length = 0
}

export function trackWebVitalMetric(metric: NextWebVitalsMetric) {
bufferedVitalsMetrics.push(metric)
metrics.push(metric)
webVitalsCallbacks.forEach((callback) => callback(metric))
}

Expand All @@ -23,7 +27,7 @@ export function useWebVitalsReport(callback: ReportWebVitalsCallback) {
if (process.env.NODE_ENV === 'development') {
if (flushed) {
console.error(
`Web vitals reporting callback is attached too late, please attach it before page is mounted.`
'The `useWebVitalsReport` hook was called too late -- did you use it inside of a <Suspense> boundary?'
)
}
}
Expand All @@ -32,14 +36,10 @@ export function useWebVitalsReport(callback: ReportWebVitalsCallback) {
// Flush calculated metrics
const reportMetric = (metric: NextWebVitalsMetric) => {
callback(metric)
metricIndexRef.current = bufferedVitalsMetrics.length
metricIndexRef.current = metrics.length
}
for (
let i = metricIndexRef.current;
i < bufferedVitalsMetrics.length;
i++
) {
reportMetric(bufferedVitalsMetrics[i])
for (let i = metricIndexRef.current; i < metrics.length; i++) {
reportMetric(metrics[i])
}

webVitalsCallbacks.add(reportMetric)
Expand Down
4 changes: 2 additions & 2 deletions test/integration/relay-analytics-disabled/pages/index.js
@@ -1,4 +1,4 @@
import { bufferedVitalsMetrics } from 'next/dist/client/vitals'
import { getBufferedVitalsMetrics } from 'next/dist/client/vitals'

if (typeof navigator !== 'undefined') {
window.__BEACONS = window.__BEACONS || []
Expand All @@ -22,7 +22,7 @@ export default () => {
<div>
<h1>Foo!</h1>
<h2>bar!</h2>
<p>{`buffered metrics: ${bufferedVitalsMetrics.length}`}</p>
<p>{`buffered metrics: ${getBufferedVitalsMetrics().length}`}</p>
</div>
)
}
4 changes: 2 additions & 2 deletions test/integration/relay-analytics/pages/index.js
@@ -1,6 +1,6 @@
/* global localStorage */
import { unstable_useWebVitalsReport } from 'next/vitals'
import { bufferedVitalsMetrics } from 'next/dist/client/vitals'
import { getBufferedVitalsMetrics } from 'next/dist/client/vitals'

if (typeof navigator !== 'undefined') {
window.__BEACONS = window.__BEACONS || []
Expand Down Expand Up @@ -38,7 +38,7 @@ export default () => {
<div>
<h1>Foo!</h1>
<h2>bar!</h2>
<p>{`buffered metrics: ${bufferedVitalsMetrics.length}`}</p>
<p>{`buffered metrics: ${getBufferedVitalsMetrics().length}`}</p>
</div>
)
}