Skip to content

Commit

Permalink
update to feedbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Feb 1, 2022
1 parent 21c5799 commit 265381e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
22 changes: 11 additions & 11 deletions packages/next/client/vitals.ts
Original file line number Diff line number Diff line change
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
}

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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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>
)
}

0 comments on commit 265381e

Please sign in to comment.