Skip to content

Block 253 of 297

Live Metric

One number that refreshes in the visitor's browser from a public JSON endpoint, with a server-rendered fallback.

Puck key
LiveMetricBlock
Export
liveMetricBlock
React component
<LiveMetric />
Files
components/puck/live-metric-block.tsx
components/puck/live-metric.client.tsx
Dependencies
None
License
MIT

About this block

When to use it, how to write for it, and what to change in the code.

What it's for

One number that refreshes in the visitor's browser by polling a public JSON endpoint, with a Live, Paused or Offline status and an "Updated 30s ago" note. The initial value renders on the server, so the card shows a number with JavaScript off. Polling pauses when the card is off screen or the tab is hidden.

  • Visitors online or dollars raised during a giving day or livestream
  • Current wait time or open spots that change through the day
  • Any public counter where a page reload would be too slow
Writing the content
  • Live data URL must be a public endpoint that allows cross-origin requests from your site and needs no API key, since every visitor can see it.
  • Set Value path with dots, like data.visitors_now or items.0.count.
  • Bind Initial value with a {{ source.field }} token so the first number is current; an unbound token in the URL is ignored.
  • Pick the slowest Refresh every that still feels current. The minimum is 15 seconds, and failures double the wait up to five minutes.
Accessibility
  • A polite live region announces the new value at most once a minute, so screen readers aren't interrupted on every poll.
  • Live, Paused and Offline are written out as text next to their indicators, so status does not rely on color.
  • The pulsing Live dot stops animating for visitors who prefer reduced motion.
Customizing the code
  • Requires the companion live-metric.client.tsx file, a client component that handles polling; copy both files together.
  • Timing constants are at the top of the client file: TIMEOUT (10 seconds), MAX_DELAY (5 minutes) and ANNOUNCE_EVERY (1 minute).

Editor fields

What content editors see in the P1 sidebar when they select this block.

  • Metric nametext
  • Heading levelselect
  • Subtitletext
  • Initial valuetext
  • Prefixtext
  • Suffixtext
  • Live data URLtext
  • Value pathtext
  • Refresh everyselect
  • Footnotetext