Skip to content

Commit

Permalink
flush metrics on each collect
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Sep 11, 2021
1 parent 0e711e8 commit 556e70c
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 27 deletions.
4 changes: 2 additions & 2 deletions packages/next/client/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import PageLoader, { StyleSheetTuple } from './page-loader'
import measureWebVitals from './performance-relayer'
import { RouteAnnouncer } from './route-announcer'
import { createRouter, makePublicRouterInstance } from './router'
import { flushWebVitalsCallbacks } from '../vitals/vitals'
import { trackWebVitalMetric } from '../vitals/vitals'

/// <reference types="react-dom/experimental" />

Expand Down Expand Up @@ -310,7 +310,7 @@ export async function initNext(opts: { webpackHMR?: any } = {}) {
: 'web-vital',
}
exportedReportWebVitals?.(webVitals)
flushWebVitalsCallbacks(webVitals)
trackWebVitalMetric(webVitals)
}

const pageEntrypoint =
Expand Down
13 changes: 6 additions & 7 deletions packages/next/vitals/vitals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@ import { NextWebVitalsMetric } from '../pages/_app'

type ReportWebVitalsCallback = (webVitals: NextWebVitalsMetric) => any
export const webVitalsCallbacks = new Set<ReportWebVitalsCallback>()
let metric: NextWebVitalsMetric
const metrics: NextWebVitalsMetric[] = []

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

export function useWebVitalsReport(callback: ReportWebVitalsCallback) {
useEffect(() => {
if (metric) {
callback(metric)
return
if (metrics.length) {
metrics.forEach((metric) => callback(metric))
}

webVitalsCallbacks.add(callback)
return () => {
webVitalsCallbacks.delete(callback)
}
}, [callback])
}, [callback, metrics.length])
}
3 changes: 3 additions & 0 deletions test/integration/relay-analytics/pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ export default class MyApp extends App {}
/*
Method is experimental and will eventually be handled in a Next.js plugin
*/

///* reportWebVitals
export function reportWebVitals(data) {
localStorage.setItem(
data.name || data.entryType,
data.value !== undefined ? data.value : data.startTime
)
}
// reportWebVitals */
9 changes: 9 additions & 0 deletions test/integration/relay-analytics/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/* global localStorage */
import { useWebVitalsReport } from 'next/vitals'

if (typeof navigator !== 'undefined') {
window.__BEACONS = window.__BEACONS || []

Expand All @@ -16,6 +19,12 @@ if (typeof navigator !== 'undefined') {
}

export default () => {
useWebVitalsReport((data) => {
localStorage.setItem(
data.name || data.entryType,
data.value !== undefined ? data.value : data.startTime
)
})
return (
<div>
<h1>Foo!</h1>
Expand Down
12 changes: 0 additions & 12 deletions test/integration/relay-analytics/test/hook-impl-app.js

This file was deleted.

9 changes: 3 additions & 6 deletions test/integration/relay-analytics/test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-env jest */

import { join } from 'path'
import fs from 'fs-extra'
import webdriver from 'next-webdriver'
import {
File,
Expand All @@ -14,10 +13,7 @@ import {

const appDir = join(__dirname, '../')
const customApp = new File(join(appDir, 'pages/_app.js'))
const hookImplCustomAppContent = fs.readFileSync(
join(__dirname, 'hook-impl-app.js'),
{ encoding: 'utf-8' }
)

let appPort
let server
let stdout
Expand All @@ -44,7 +40,8 @@ describe('Analytics relayer', () => {

describe('Hook based analytics', () => {
beforeAll(async () => {
customApp.write(hookImplCustomAppContent)
customApp.replace('///* reportWebVitals', '/*')
customApp.replace('// reportWebVitals */', '*/')
await buildApp()
})

Expand Down

0 comments on commit 556e70c

Please sign in to comment.