inpspy 1.0.0-test → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/LICENSE +21 -0
- package/README.md +262 -2
- package/dist/index.cjs +980 -0
- package/dist/index.d.cts +198 -0
- package/dist/index.d.ts +198 -0
- package/dist/index.js +941 -0
- package/dist/react.cjs +56 -0
- package/dist/react.d.cts +14 -0
- package/dist/react.d.ts +14 -0
- package/dist/react.js +31 -0
- package/package.json +97 -5
- package/index.js +0 -1
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 1.0.0
|
|
4
|
+
|
|
5
|
+
First release.
|
|
6
|
+
|
|
7
|
+
- `start(options)` measures every click, tap and key press with the Event Timing API (`durationThreshold` 40 ms by default, `buffered: true`), groups entries by `interactionId` and keeps the longest one per interaction.
|
|
8
|
+
- Splits each interaction into input delay, processing and presentation delay. Handlers of pointerdown, pointerup and click painted in the same frame all count as processing, and the three phases always add up to the duration.
|
|
9
|
+
- Script attribution from the Long Animation Frames API (Chromium 123+): the scripts that ran during the interaction, longest first, with URL, function name, invoker (`click handler`, `setTimeout callback`, …), forced layout time and a third-party flag (different origin than the page). Degrades to "attribution unavailable" where the API is missing.
|
|
10
|
+
- A readable target selector (`button#buy`, `section#cart button.remove`, `[data-testid]`) plus a short text hint. Still named when a handler removed the element (Event Timing then reports `target: null`). Never reads an input's value.
|
|
11
|
+
- Page INP estimate as web-vitals computes it (longest interaction; one skipped per 50 interactions).
|
|
12
|
+
- Overlay in a shadow root: a toast per slow interaction (default 200 ms and up) with the phase bar and the top 3 scripts, and a panel with the page INP estimate, the last `maxEntries` interactions, "Copy report" (Markdown) and "Clear". Four corner positions, light and dark, `prefers-reduced-motion`, keyboard accessible, never moves focus, ignores clicks on itself.
|
|
13
|
+
- A grouped console report per slow interaction (with the element, clickable in DevTools), `onInteraction` callback, `getInteractions()`, `getReport("markdown" | "json")`, `clear()`, `getSupport()` and `isRunning()`.
|
|
14
|
+
- `inpspy/react`: `<InpSpy />` and `useInpSpy()`, Strict Mode safe, `"use client"` for the Next.js App Router.
|
|
15
|
+
- SSR safe: importing on the server never throws and `start()` there is a no-op. Zero runtime dependencies.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 CK Giri
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,5 +1,265 @@
|
|
|
1
1
|
# inpspy
|
|
2
2
|
|
|
3
|
-
Find what makes clicks slow
|
|
3
|
+
**Find what makes clicks slow.** inpspy is a development overlay for INP (Interaction to Next Paint). Click around your app and every slow click, tap or key press pops up with its time, the element, where the time went (input delay, processing, presentation) and the scripts that ran, third-party ones flagged.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
[](https://www.npmjs.com/package/inpspy)
|
|
6
|
+
[](https://bundlephobia.com/package/inpspy)
|
|
7
|
+
[](./LICENSE)
|
|
8
|
+
|
|
9
|
+

|
|
10
|
+
|
|
11
|
+
<sub>A real run in Microsoft Edge: the "Add to cart" handler of a fake third-party widget busy-waits 280 ms. Every number in the picture is measured.</sub>
|
|
12
|
+
|
|
13
|
+
## Quick start
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
npm i -D inpspy
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
if (process.env.NODE_ENV !== "production") {
|
|
21
|
+
import("inpspy").then(({ start }) => start());
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Then use your app. Interactions of 200 ms or more show a toast and a grouped report in the console; the **INP** pill in the corner opens the panel with all of them.
|
|
26
|
+
|
|
27
|
+
## Why
|
|
28
|
+
|
|
29
|
+
- ⏱️ **INP is a Core Web Vital.** It replaced First Input Delay (FID) in March 2024. It measures how long a page takes to visually respond to clicks, taps and key presses.
|
|
30
|
+
- 🎯 **Google's thresholds:** good ≤ 200 ms, needs improvement ≤ 500 ms, poor above 500 ms, assessed at the 75th percentile of page loads.
|
|
31
|
+
- 🤷 **Field tools tell you the number, not the cause.** Search Console, CrUX and RUM dashboards report "INP 420 ms" but rarely which element, which handler or which script made it slow.
|
|
32
|
+
- 🔍 **inpspy shows the cause live, while you click.** The element, the phase that took the time and the scripts that ran, with third-party scripts flagged. No extension, no profiler session to dig through.
|
|
33
|
+
- 🪶 **Zero dependencies,** 9 KB min+gzip, SSR safe, and meant to be loaded in development only.
|
|
34
|
+
|
|
35
|
+
## What you see
|
|
36
|
+
|
|
37
|
+
A toast for each slow interaction:
|
|
38
|
+
|
|
39
|
+
```
|
|
40
|
+
328 ms click · needs improvement
|
|
41
|
+
button#add-to-cart "Add to cart"
|
|
42
|
+
[███████████████████████████████████████████]
|
|
43
|
+
Input delay 1 ms Processing 325 ms Presentation 2 ms
|
|
44
|
+
280 ms · vendor-widget.js third-party · click handler
|
|
45
|
+
45 ms · app.js · addToCart() · click handler
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
The bar splits the interaction into the three phases INP is made of. They always add up to the total.
|
|
49
|
+
|
|
50
|
+
| Phase | From → to | Usually long because |
|
|
51
|
+
| --- | --- | --- |
|
|
52
|
+
| **Input delay** | the input → the first event handler starts | the main thread was busy with other work: a long task, a timer, hydration, a third-party script |
|
|
53
|
+
| **Processing** | first handler starts → last handler ends | your event handlers (pointerdown, pointerup, click, keydown…) do too much work |
|
|
54
|
+
| **Presentation delay** | handlers end → next frame painted | rendering: style recalculation, layout, paint of a large or complex DOM |
|
|
55
|
+
|
|
56
|
+
The script lines come from the [Long Animation Frames API](https://developer.chrome.com/docs/web-platform/long-animation-frames): each script that ran 5 ms or more during the interaction, longest first, with what called it (`click handler`, `setTimeout callback`, `script evaluation`, …). Scripts from another origin than the page are marked **third-party**. Function names are shown when the browser reports them (it usually doesn't for cross-origin scripts loaded without CORS).
|
|
57
|
+
|
|
58
|
+
The **panel** (click the INP pill) shows the page INP estimate, the last 50 interactions with the same details, **Copy report** (Markdown, ready for an issue or PR) and **Clear**. Each slow interaction is also logged to the console as a collapsed group with the element itself, so you can click it to reveal it in the Elements panel.
|
|
59
|
+
|
|
60
|
+
## Recipes
|
|
61
|
+
|
|
62
|
+
### Plain JavaScript (development only)
|
|
63
|
+
|
|
64
|
+
```js
|
|
65
|
+
// main.js
|
|
66
|
+
if (process.env.NODE_ENV !== "production") {
|
|
67
|
+
import("inpspy").then(({ start }) => start({ threshold: 150, position: "bottom-left" }));
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
The dynamic `import()` keeps inpspy out of your production bundle: bundlers drop the dead branch.
|
|
72
|
+
|
|
73
|
+
### React
|
|
74
|
+
|
|
75
|
+
```jsx
|
|
76
|
+
import { lazy, Suspense } from "react";
|
|
77
|
+
|
|
78
|
+
const InpSpy =
|
|
79
|
+
process.env.NODE_ENV !== "production" ? lazy(() => import("inpspy/react").then((m) => ({ default: m.InpSpy }))) : () => null;
|
|
80
|
+
|
|
81
|
+
export function App() {
|
|
82
|
+
return (
|
|
83
|
+
<>
|
|
84
|
+
<Routes />
|
|
85
|
+
<Suspense fallback={null}>
|
|
86
|
+
<InpSpy threshold={150} />
|
|
87
|
+
</Suspense>
|
|
88
|
+
</>
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
`<InpSpy />` renders nothing; it starts inpspy on mount and stops it on unmount. It is Strict Mode safe: the extra mount and unmount of development leave exactly one running session. The hook form is `useInpSpy(options)`. Both take the [options](#api-reference) of `start()`.
|
|
94
|
+
|
|
95
|
+
### Next.js (App Router)
|
|
96
|
+
|
|
97
|
+
```tsx
|
|
98
|
+
// app/inp-spy.tsx
|
|
99
|
+
"use client";
|
|
100
|
+
|
|
101
|
+
import dynamic from "next/dynamic";
|
|
102
|
+
|
|
103
|
+
const InpSpy = dynamic(() => import("inpspy/react").then((m) => m.InpSpy), { ssr: false });
|
|
104
|
+
|
|
105
|
+
export function DevInpSpy() {
|
|
106
|
+
return process.env.NODE_ENV === "development" ? <InpSpy /> : null;
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
```tsx
|
|
111
|
+
// app/layout.tsx
|
|
112
|
+
import { DevInpSpy } from "./inp-spy";
|
|
113
|
+
|
|
114
|
+
export default function RootLayout({ children }: { children: React.ReactNode }) {
|
|
115
|
+
return (
|
|
116
|
+
<html lang="en">
|
|
117
|
+
<body>
|
|
118
|
+
{children}
|
|
119
|
+
<DevInpSpy />
|
|
120
|
+
</body>
|
|
121
|
+
</html>
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
`inpspy/react` starts with `"use client"`, and importing `inpspy` on the server never throws (`start()` there does nothing), so it is safe in server components too.
|
|
127
|
+
|
|
128
|
+
### Vite
|
|
129
|
+
|
|
130
|
+
```js
|
|
131
|
+
// src/main.js
|
|
132
|
+
if (import.meta.env.DEV) {
|
|
133
|
+
import("inpspy").then(({ start }) => start());
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### Paste into the DevTools console (no install)
|
|
138
|
+
|
|
139
|
+
```js
|
|
140
|
+
import("https://esm.sh/inpspy").then((m) => m.start());
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Works on any page that allows loading scripts from a CDN (a strict Content Security Policy blocks it). It also sees earlier interactions the browser buffered (those of 104 ms or more), but the attribution of those may be incomplete.
|
|
144
|
+
|
|
145
|
+
### Send interactions to your own logging
|
|
146
|
+
|
|
147
|
+
```js
|
|
148
|
+
import { start } from "inpspy";
|
|
149
|
+
|
|
150
|
+
start({
|
|
151
|
+
overlay: false,
|
|
152
|
+
console: false,
|
|
153
|
+
onInteraction(interaction) {
|
|
154
|
+
if (interaction.rating === "good") return;
|
|
155
|
+
navigator.sendBeacon("/dev-metrics", JSON.stringify(interaction));
|
|
156
|
+
},
|
|
157
|
+
});
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
`onInteraction` is called for every recorded interaction (at or above `durationThreshold`, 40 ms by default), slow or not. The object is plain JSON: duration, rating, phases, target selector, scripts. `getReport("json")` gives you everything at once, for example from an end-to-end test.
|
|
161
|
+
|
|
162
|
+
## How to fix common causes
|
|
163
|
+
|
|
164
|
+
- **Long processing: break up the work in your handlers.** Do the visible update first, then yield to the main thread (`await scheduler.yield()` where supported, or `await new Promise((r) => setTimeout(r, 0))`) before analytics, storage or other non-urgent work.
|
|
165
|
+
- **Third-party scripts in your handlers or input delay: defer them.** Load tag managers, chat widgets and A/B testing scripts with `async`/`defer` or after the page is interactive, and send their tracking calls after the next paint instead of inside the click handler.
|
|
166
|
+
- **Long input delay: shorten long tasks.** Timers, hydration and big script evaluation that block the main thread delay every input that arrives meanwhile. Split them up and yield between chunks.
|
|
167
|
+
- **Long presentation delay or forced layout: avoid layout thrashing.** Don't read layout (`offsetHeight`, `getBoundingClientRect()`) right after writing styles in a loop; batch reads before writes. inpspy shows forced style and layout time per script in the report.
|
|
168
|
+
- **Long presentation delay: reduce DOM size and rendering work.** Large DOMs make every style and layout pass slower. Virtualize long lists, use `content-visibility: auto` for off-screen sections and avoid re-rendering large trees on every click.
|
|
169
|
+
|
|
170
|
+
## API reference
|
|
171
|
+
|
|
172
|
+
```js
|
|
173
|
+
import { start, getInteractions, getReport, clear } from "inpspy";
|
|
174
|
+
|
|
175
|
+
const stop = start({ threshold: 200 });
|
|
176
|
+
// …click around…
|
|
177
|
+
console.log(getReport()); // Markdown
|
|
178
|
+
stop();
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
<details>
|
|
182
|
+
<summary><b><code>start(options?)</code> → <code>stop()</code></b></summary>
|
|
183
|
+
|
|
184
|
+
Starts measuring and returns a function that stops it (removes the overlay, disconnects the observers). Calling `start()` again replaces the running session. On the server it does nothing and returns a no-op.
|
|
185
|
+
|
|
186
|
+
| Option | Default | Description |
|
|
187
|
+
| --- | --- | --- |
|
|
188
|
+
| `threshold` | `200` | Interactions at or above this many ms show a toast and a console report |
|
|
189
|
+
| `durationThreshold` | `40` | Event Timing `durationThreshold`: shorter interactions are not recorded at all (Chromium minimum: 16) |
|
|
190
|
+
| `overlay` | `true` | Show the on-page overlay |
|
|
191
|
+
| `console` | `true` | Log a grouped console report for each slow interaction |
|
|
192
|
+
| `maxEntries` | `50` | How many interactions the panel and `getInteractions()` keep |
|
|
193
|
+
| `position` | `"bottom-right"` | `"bottom-right"`, `"bottom-left"`, `"top-right"` or `"top-left"` |
|
|
194
|
+
| `toastDuration` | `8000` | How long a toast stays, in ms (paused while hovered or focused); `0` keeps toasts until closed |
|
|
195
|
+
| `onInteraction` | none | `(interaction) => void`, called for every recorded interaction |
|
|
196
|
+
|
|
197
|
+
</details>
|
|
198
|
+
|
|
199
|
+
<details>
|
|
200
|
+
<summary><b><code>InpInteraction</code></b></summary>
|
|
201
|
+
|
|
202
|
+
```ts
|
|
203
|
+
{
|
|
204
|
+
id: number; // the browser's interactionId
|
|
205
|
+
type: "pointer" | "keyboard";
|
|
206
|
+
events: string[]; // ["pointerdown", "pointerup", "click"]
|
|
207
|
+
startTime: number; // ms since navigation start
|
|
208
|
+
duration: number; // ms from the input to the next paint
|
|
209
|
+
rating: "good" | "needs-improvement" | "poor";
|
|
210
|
+
phases: { inputDelay: number; processing: number; presentationDelay: number };
|
|
211
|
+
target: string; // "button#add-to-cart", "section#cart button.remove", "(removed element)"
|
|
212
|
+
targetText: string; // "Add to cart" (aria-label, title, alt, placeholder or text; never an input's value)
|
|
213
|
+
scripts: InpScript[]; // longest first
|
|
214
|
+
attribution: "loaf" | "none" | "unsupported";
|
|
215
|
+
url: string;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
InpScript = {
|
|
219
|
+
url: string; // "" when the browser doesn't report one
|
|
220
|
+
functionName: string; // often "" for cross-origin scripts
|
|
221
|
+
invoker: string; // "BUTTON#add-to-cart.onclick", "TimerHandler:setTimeout"
|
|
222
|
+
invokerType: string; // "event-listener", "user-callback", "classic-script", …
|
|
223
|
+
duration: number;
|
|
224
|
+
forcedStyleAndLayoutDuration: number;
|
|
225
|
+
startTime: number;
|
|
226
|
+
thirdParty: boolean; // different origin than the page
|
|
227
|
+
}
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
`attribution` is `"none"` when no long animation frame (50 ms or more) overlapped the interaction, and `"unsupported"` when the browser has no Long Animation Frames API.
|
|
231
|
+
|
|
232
|
+
</details>
|
|
233
|
+
|
|
234
|
+
<details>
|
|
235
|
+
<summary><b>Other exports</b></summary>
|
|
236
|
+
|
|
237
|
+
- `getInteractions()`: the recorded interactions, oldest first (a copy). Kept after `stop()`, reset by the next `start()`.
|
|
238
|
+
- `getReport(format?)`: `"markdown"` (default) or `"json"`, with the page INP estimate.
|
|
239
|
+
- `clear()`: forget the recorded interactions and remove the toasts.
|
|
240
|
+
- `getSupport()`: `{ eventTiming, longAnimationFrames }` for the current browser.
|
|
241
|
+
- `isRunning()`: whether a session is running.
|
|
242
|
+
- The pure helpers the overlay uses, for your own tooling: `rate(ms)`, `estimateInp(durations, interactionCount)`, `summarize(entries)`, `attributeScripts(start, end, loafEntries, pageOrigin)`, `describeElement(element)`, `isThirdParty(url, pageOrigin)`, `formatReport(interactions, context, format)`.
|
|
243
|
+
- `inpspy/react`: `<InpSpy {...options} />` and `useInpSpy(options)`.
|
|
244
|
+
|
|
245
|
+
The page INP estimate follows web-vitals: the longest interaction, or with 50 or more interactions, roughly the 98th percentile (one interaction skipped per 50). The interaction count comes from `performance.interactionCount` where the browser has it.
|
|
246
|
+
|
|
247
|
+
</details>
|
|
248
|
+
|
|
249
|
+
## Browser support
|
|
250
|
+
|
|
251
|
+
- **Chromium browsers** (Chrome, Edge, Opera, Brave): everything, including script attribution, which needs the Long Animation Frames API (**Chromium 123+**). The test suite runs in Microsoft Edge.
|
|
252
|
+
- **Firefox and Safari:** support for the Event Timing API (and its `interactionId`) varies by version; neither has Long Animation Frames. Where Event Timing is available inpspy measures interactions and shows "attribution unavailable"; where it isn't, the panel says so and nothing is recorded. See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/PerformanceEventTiming#browser_compatibility) for the current state.
|
|
253
|
+
- **Server / Node:** importing is safe and `start()` is a no-op.
|
|
254
|
+
|
|
255
|
+
## Good to know
|
|
256
|
+
|
|
257
|
+
- **A development tool, not a replacement for field data.** Your INP is what real users experience (CrUX, Search Console, your RUM). inpspy helps you find and fix the cause of slow interactions on your machine.
|
|
258
|
+
- **Your numbers are not your users' numbers.** A fast laptop hides problems a mid-range phone has; DevTools CPU throttling helps but isn't exact. Open DevTools and the overlay itself add a little overhead. Look at which phase and which script dominate, not at the exact milliseconds.
|
|
259
|
+
- **Event Timing rounds durations to 8 ms** and only reports interactions of `durationThreshold` (40 ms) or more, so fast interactions are not in the list.
|
|
260
|
+
- **Attribution is per frame.** Scripts are matched by time to the long animation frames that overlap the interaction; a script running in the same frame for another reason (a timer, for example) is listed too, which is usually what you want for input delay.
|
|
261
|
+
- **inpspy ignores clicks on its own overlay,** lives in a shadow root (page CSS can't break it and it can't break the page) and never moves focus.
|
|
262
|
+
|
|
263
|
+
## License
|
|
264
|
+
|
|
265
|
+
MIT © CK Giri
|