1.0.7 • Published 12 months ago

@paretojs/monitor v1.0.7

Weekly downloads
-
License
ISC
Repository
-
Last release
12 months ago

@paretojs/monitor

中文版

A Visual Performance Panel

Usage

Server

Setup

import { addMonitorMiddleware } from '@paretojs/monitor'
import express from 'express'

const app = express()

addMonitorMiddleware(app)({
  // control whether show monitor ui in frontend
  showMonitor: true,
})

Mark

req.monitor.mark('renderTopChunk')

Inject to window

<script
  id="MONITOR"
  dangerouslySetInnerHTML={{ __html: res.locals.monitorInfos }}
/>

Client

Usage

import { report, FirstScreen } from '@paretojs/monitor'
import { useEffect } from 'react'

const App = () => {
  useEffect(() => {
    report().then(console.log)
  }, [])

  return (
    <>
      <div>app</div>
      <FirstScreen />
    </>
  )
}

Report

@paretojs/monitor collects two types of data. One is returned by the report() function mentioned above, and the other is collected using the web-vitals package. As the data collected by web-vitals is recommended to be reported during visibilitychange and pagehide events (refer to https://github.com/GoogleChrome/web-vitals/blob/main/README.md#batch-multiple-reports-together), we store the data collected by web-vitals in window['WEB_VITALS'].

Pitfall

The Time to Interactive (TTI) collected by the web-vitals package is much longer than what we collect ourselves. This is because it waits for the DOM to stabilize, which conflicts with the characteristics of stream rendering.

Report Example

const App = () => {
  useEffect(() => {
    report().then(data => {
      // your report function
      sendToAnalytics(data)
    })

    addEventListener('visibilitychange', () => {
      if (document.visibilityState === 'hidden') {
        sendToAnalytics(window['__WEB_VITALS__'])
      }
    })

    // NOTE: Safari does not reliably fire the `visibilitychange` event when the
    // page is being unloaded. If Safari support is needed, you should also flush
    // the queue in the `pagehide` event.
    addEventListener('pagehide', () => {
      sendToAnalytics(window['__WEB_VITALS__'])
    })

    return () => {
      // remove listeners....
    }
  }, [])

  return (
    <>
      <div>app</div>
      <FirstScreen />
    </>
  )
}

Metrics Description

Custom

ParameterExplanation
psPage Start - The timestamp from the first script in the head tag
fpFirst Paint - The time when the first paint happens on the screen.
drDOM Ready - The time when the DOM (Document Object Model) is ready.
ldLoad - The time when the full page has loaded.
fsnFirst Screen No Images - The end time of the first screen render (regardless of whether the images have finished loading)
frafFirst RequestAnimationFrame - The time when the first RequestAnimationFrame API is called.
ttiTime to Interactive - The time it takes for the page to become fully interactive.
fpcFirst Paint - The time recorded By performance.getEntriesByName("first-paint")[0], Can be compared with the fp value we recorded ourselves
fcpFirst Content Paint

Performance

The recorded values in Performance correspond to the times in PerformanceNavigationTiming.

Node

ParameterExplanation
Load First Screen Datawhen SSR request ready
On Shell Readywhen onShellReady function called
Render Top Chunkwhen head tags send
On All Readywhen onAllReady function called
Pipe Endwhen stream end

Resource

ParameterExplanation
scriptStartEarliest script request time
scriptEndThe latest script end time
styleStartEarliest style request time
styleEndThe latest style end time
imageStartEarliest image request time in first screen
imageEndThe latest image end time in first screen
1.0.7

12 months ago

1.0.6

12 months ago

1.0.5

12 months ago

1.0.4

12 months ago

1.0.3

12 months ago

1.0.2

12 months ago

1.0.1

12 months ago

1.0.0

12 months ago