@spectosh/browser 0.2.2 → 0.2.3

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/README.md CHANGED
@@ -13,9 +13,9 @@ npm i @spectosh/browser
13
13
  ```js
14
14
  import { startRecording, identify, track } from '@spectosh/browser'
15
15
 
16
+ // in your entry file, before the app renders
16
17
  const rec = startRecording({
17
- key: 'pk_your_key',
18
- endpoint: 'https://specto.sh/api/ingest', // or your self-hosted instance
18
+ key: 'pk_your_key', // pk_…@eu.specto.sh / pk_…@your-host: the Install page shows yours
19
19
  privacy: 'strict', // optional: mask ALL text, not just inputs
20
20
  sampleRate: 0.5, // optional: record half of all sessions
21
21
  consent: 'required', // optional: record nothing until grantConsent()
@@ -29,6 +29,47 @@ rec.sessionUrl() // link to this replay in Specto (handy for support tools)
29
29
  rec.stop()
30
30
  ```
31
31
 
32
+ **The key is the whole install.** A bare `pk_…` belongs to the hosted
33
+ service; a project in another region or on a self-hosted instance has a key
34
+ that carries its home — `pk_…@eu.specto.sh`, `pk_…@replay.example.com`. The
35
+ SDK asks that host for the project's settings before sending anything, so a
36
+ key it doesn't know records nothing (and says why in the console) instead of
37
+ sending data to the wrong place. `host` overrides the key's — e.g. your own
38
+ domain proxying to Specto.
39
+
40
+ **Recording starts at once.** Errors, console output, requests and the DOM
41
+ are captured from the first moment and held in memory while the project's
42
+ settings load; then they're filtered by those settings and sent — or dropped.
43
+ Start it as early as you can and the first requests and errors of a page load
44
+ are in the replay.
45
+
46
+ ## React
47
+
48
+ ```js
49
+ import { createRoot } from 'react-dom/client'
50
+ import { SpectoProvider, SpectoErrorBoundary, spectoErrorHandler, useSpecto } from '@spectosh/browser/react'
51
+
52
+ createRoot(document.getElementById('root'), {
53
+ onCaughtError: spectoErrorHandler(), // React 19: errors React catches → Issues
54
+ onUncaughtError: spectoErrorHandler(),
55
+ }).render(
56
+ <SpectoProvider apiKey="pk_your_key" user={user}>
57
+ <App />
58
+ </SpectoProvider>
59
+ )
60
+
61
+ const { identify, track, captureError, sessionUrl } = useSpecto()
62
+ ```
63
+
64
+ `SpectoProvider` starts recording during its first render — before any
65
+ component's effects run — once per page (StrictMode, remounts and hot reloads
66
+ reuse it; unmounting never stops it), and does nothing on the server, so it
67
+ works as-is in a Next.js App Router `'use client'` provider. `user` is
68
+ identified whenever it changes; every other prop is a `startRecording`
69
+ option. On React 18, `SpectoErrorBoundary` reports render errors with their
70
+ component stack. React is an optional peer dependency (17+). TypeScript
71
+ types ship with the package.
72
+
32
73
  ## Sessions
33
74
 
34
75
  A session is one visitor's continuous activity. It survives page loads (it
@@ -60,5 +101,6 @@ never ruins the rest of a replay.
60
101
  Full docs live at `/docs` on your Specto instance.
61
102
 
62
103
  Build from the monorepo: `pnpm run build:npm` (outputs `packages/browser/dist`,
63
- ESM + CJS, worker inlined). Releases go out via `pnpm run release` locally or
104
+ ESM + CJS for both entries, worker inlined); `pnpm --filter @spectosh/browser
105
+ test` runs the unit tests and checks the types. Releases go out via `pnpm run release` locally or
64
106
  the tag-triggered workflow (`.github/workflows/release.yml`).
package/dist/react.cjs ADDED
@@ -0,0 +1 @@
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./src-VtKEKQLX.cjs");let t=require("react");var n={identify:e.r,track:e.d,captureError:e.t,sessionUrl:e.s,stop:e.u,grantConsent:e.n,revokeConsent:e.o,optOut:e.a,optIn:e.i},r=(0,t.createContext)(n);function i(t){if(typeof window>`u`)return null;try{return e.l(t)}catch(e){try{console.error(`[specto] ${e?.message||e}`)}catch{}return null}}function a({apiKey:a,user:o,children:s,...c}){(0,t.useState)(()=>i({key:a,...c}));let l=o?.id??null,u=o?JSON.stringify(Object.fromEntries(Object.entries(o).filter(([e])=>e!==`id`))):``;return(0,t.useEffect)(()=>{l!=null&&l!==``&&e.r(l,u?JSON.parse(u):void 0)},[l,u]),(0,t.createElement)(r.Provider,{value:n},s)}var o=()=>(0,t.useContext)(r),s=class extends t.Component{constructor(e){super(e),this.state={error:null},this.reset=()=>this.setState({error:null})}static getDerivedStateFromError(e){return{error:e}}componentDidCatch(t,n){e.t(t,{componentStack:n?.componentStack,source:`react-boundary`}),this.props.onError?.(t,n)}render(){let{error:e}=this.state;if(!e)return this.props.children;let{fallback:t}=this.props;return typeof t==`function`?t({error:e,reset:this.reset}):t??null}};function c(t){return(n,r)=>{if(e.t(n,{componentStack:r?.componentStack,source:`react-root`}),t)t(n,r);else try{console.error(n)}catch{}}}exports.SpectoErrorBoundary=s,exports.SpectoProvider=a,exports.captureError=e.t,exports.spectoErrorHandler=c,exports.useSpecto=o;
package/dist/react.js ADDED
@@ -0,0 +1,71 @@
1
+ import { a as e, d as t, i as n, l as r, n as i, o as a, r as o, s, t as c, u as l } from "./src-KEdhVklE.js";
2
+ import { Component as u, createContext as d, createElement as f, useContext as p, useEffect as m, useState as h } from "react";
3
+ //#region src/react.js
4
+ var g = {
5
+ identify: o,
6
+ track: t,
7
+ captureError: c,
8
+ sessionUrl: s,
9
+ stop: l,
10
+ grantConsent: i,
11
+ revokeConsent: a,
12
+ optOut: e,
13
+ optIn: n
14
+ }, _ = d(g);
15
+ function v(e) {
16
+ if (typeof window > "u") return null;
17
+ try {
18
+ return r(e);
19
+ } catch (e) {
20
+ try {
21
+ console.error(`[specto] ${e?.message || e}`);
22
+ } catch {}
23
+ return null;
24
+ }
25
+ }
26
+ function y({ apiKey: e, user: t, children: n, ...r }) {
27
+ h(() => v({
28
+ key: e,
29
+ ...r
30
+ }));
31
+ let i = t?.id ?? null, a = t ? JSON.stringify(Object.fromEntries(Object.entries(t).filter(([e]) => e !== "id"))) : "";
32
+ return m(() => {
33
+ i != null && i !== "" && o(i, a ? JSON.parse(a) : void 0);
34
+ }, [i, a]), f(_.Provider, { value: g }, n);
35
+ }
36
+ var b = () => p(_), x = class extends u {
37
+ constructor(e) {
38
+ super(e), this.state = { error: null }, this.reset = () => this.setState({ error: null });
39
+ }
40
+ static getDerivedStateFromError(e) {
41
+ return { error: e };
42
+ }
43
+ componentDidCatch(e, t) {
44
+ c(e, {
45
+ componentStack: t?.componentStack,
46
+ source: "react-boundary"
47
+ }), this.props.onError?.(e, t);
48
+ }
49
+ render() {
50
+ let { error: e } = this.state;
51
+ if (!e) return this.props.children;
52
+ let { fallback: t } = this.props;
53
+ return typeof t == "function" ? t({
54
+ error: e,
55
+ reset: this.reset
56
+ }) : t ?? null;
57
+ }
58
+ };
59
+ function S(e) {
60
+ return (t, n) => {
61
+ if (c(t, {
62
+ componentStack: n?.componentStack,
63
+ source: "react-root"
64
+ }), e) e(t, n);
65
+ else try {
66
+ console.error(t);
67
+ } catch {}
68
+ };
69
+ }
70
+ //#endregion
71
+ export { x as SpectoErrorBoundary, y as SpectoProvider, c as captureError, S as spectoErrorHandler, b as useSpecto };