@ogygia/insights 0.0.0 → 0.15.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.
@@ -0,0 +1,168 @@
1
+ // src/third-party.ts
2
+ /** host suffix → what that kind of service is (labels only; detection does not need them) */
3
+ const KNOWN = [
4
+ ["googletagmanager.com", "tag manager"],
5
+ ["tiqcdn.com", "tag manager"],
6
+ ["tealiumiq.com", "tag manager"],
7
+ ["adobedtm.com", "tag manager"],
8
+ ["segment.com", "tag manager"],
9
+ ["google-analytics.com", "analytics"],
10
+ ["analytics.google.com", "analytics"],
11
+ ["plausible.io", "analytics"],
12
+ ["mxpnl.com", "analytics"],
13
+ ["amplitude.com", "analytics"],
14
+ ["heapanalytics.com", "analytics"],
15
+ ["hotjar.com", "session replay"],
16
+ ["fullstory.com", "session replay"],
17
+ ["clarity.ms", "session replay"],
18
+ ["mouseflow.com", "session replay"],
19
+ ["doubleclick.net", "ads"],
20
+ ["googlesyndication.com", "ads"],
21
+ ["googleadservices.com", "ads"],
22
+ ["facebook.net", "ads"],
23
+ ["licdn.com", "ads"],
24
+ ["bing.com", "ads"],
25
+ ["ads-twitter.com", "ads"],
26
+ ["intercom.io", "chat"],
27
+ ["intercomcdn.com", "chat"],
28
+ ["zdassets.com", "chat"],
29
+ ["crisp.chat", "chat"],
30
+ ["optimizely.com", "a/b testing"],
31
+ ["visualwebsiteoptimizer.com", "a/b testing"],
32
+ ["abtasty.com", "a/b testing"],
33
+ ["cookielaw.org", "consent"],
34
+ ["onetrust.com", "consent"],
35
+ ["cookiebot.com", "consent"],
36
+ ["usercentrics.eu", "consent"],
37
+ ["fonts.googleapis.com", "fonts"],
38
+ ["fonts.gstatic.com", "fonts"],
39
+ ["typekit.net", "fonts"],
40
+ ["jsdelivr.net", "library cdn"],
41
+ ["unpkg.com", "library cdn"],
42
+ ["cdnjs.cloudflare.com", "library cdn"],
43
+ ["esm.sh", "library cdn"]
44
+ ];
45
+ function kind_of_host(host) {
46
+ for (const [suffix, kind] of KNOWN) if (host === suffix || host.endsWith(`.${suffix}`)) return kind;
47
+ return "other";
48
+ }
49
+ function host_of(url) {
50
+ try {
51
+ return new URL(url).host;
52
+ } catch {
53
+ return "";
54
+ }
55
+ }
56
+ /** `named`: URLs the server's HTML references (its scripts, preloads, their imports) — another
57
+ * origin's script not among them came at runtime. Without it (the live page already holds the
58
+ * injected script elements), `parsed`: when the HTML was parsed (domInteractive) — a script that
59
+ * starts after that and after another origin's script ran was loaded by a script. `in_page`: the
60
+ * URLs the live document holds — a script no element names came by an import or a fetch (a
61
+ * self-loading library's parts), whenever it started. */
62
+ function third_party(resources, page_origin, cpu_by_host, named, parsed, in_page) {
63
+ const page_host = host_of(page_origin);
64
+ const by = /* @__PURE__ */ new Map();
65
+ let first_third_script_end = Infinity;
66
+ let runtime_loaded = 0;
67
+ let weighed = 0;
68
+ const sorted = [...resources].sort((a, b) => a.start - b.start);
69
+ for (const r of sorted) {
70
+ const host = host_of(r.url);
71
+ if (!host || host === page_host) continue;
72
+ const o = by.get(host) ?? {
73
+ host,
74
+ kind: kind_of_host(host),
75
+ files: 0,
76
+ scripts: 0,
77
+ script_bytes: 0,
78
+ unsized: 0,
79
+ wire: 0,
80
+ blocking: 0,
81
+ first: r.start,
82
+ cpu_ms: cpu_by_host ? cpu_by_host.get(host) ?? 0 : null
83
+ };
84
+ o.files++;
85
+ o.wire += r.transfer ?? 0;
86
+ if (r.blocking) o.blocking++;
87
+ if (r.type === "script") {
88
+ o.scripts++;
89
+ o.script_bytes += r.size ?? 0;
90
+ if (!r.size) o.unsized++;
91
+ else if (r.weighed) weighed++;
92
+ if (r.start >= first_third_script_end && (named ? !named.has(r.url) : in_page !== void 0 && !in_page.has(r.url) || parsed !== void 0 && r.start > parsed)) runtime_loaded++;
93
+ first_third_script_end = Math.min(first_third_script_end, r.end);
94
+ }
95
+ by.set(host, o);
96
+ }
97
+ if (!by.size) return null;
98
+ const origins = [...by.values()].sort((a, b) => (b.cpu_ms ?? 0) - (a.cpu_ms ?? 0) || b.script_bytes - a.script_bytes || b.wire - a.wire);
99
+ const sum = (k) => origins.reduce((s, o) => s + o[k], 0);
100
+ return {
101
+ origins,
102
+ script_bytes: sum("script_bytes"),
103
+ unsized: sum("unsized"),
104
+ weighed,
105
+ wire: sum("wire"),
106
+ blocking: sum("blocking"),
107
+ cpu_ms: cpu_by_host ? origins.reduce((s, o) => s + (o.cpu_ms ?? 0), 0) : null,
108
+ runtime_loaded
109
+ };
110
+ }
111
+ /** What to do about each kind, in one line (Partytown only where it tends to work). */
112
+ const ADVICE = {
113
+ "tag manager": "audit its tags (each one is a script it adds at runtime), fire them after the page is interactive or on consent, or move them to server-side tagging",
114
+ analytics: "load it after the page is interactive (idle or first interaction); a pure beacon tag is the kind that runs well off the main thread in a worker (Partytown), worth trying there",
115
+ ads: "reserve the slots at their final size and load the ad code after the main content; conversion pixels can wait for idle",
116
+ "session replay": "start it after the first interaction, or sample it (record a share of visits): it watches every DOM change",
117
+ chat: "show a plain button that looks like the widget and load the widget on its first click (a facade)",
118
+ "a/b testing": "it edits the page before or as islands wake: run the experiment on the server (render the variant) so islands hydrate clean",
119
+ consent: "keep it small and async, and let it gate the other tags rather than load beside them",
120
+ fonts: "self-host the fonts (one origin fewer on the critical path) and preload the one the first screen uses",
121
+ "library cdn": "bundle the library with the app (one origin fewer, cached with your code, no runtime fetches of its parts)",
122
+ other: "load it async, after the first paint, or where it is used"
123
+ };
124
+ const kb = (n) => {
125
+ if (n >= 1048576) return `${(n / 1048576).toFixed(1)} MB`;
126
+ return n > 0 && n < 1024 ? `${n} B` : `${Math.round(n / 1024)} KB`;
127
+ };
128
+ /** `edited`: islands that did not wake from the server's markup on this visit (changed, recovered,
129
+ * healed, failed), with when they woke (ms; Infinity when unknown or never). */
130
+ function third_party_findings(tp, edited = []) {
131
+ if (!tp) return [];
132
+ const out = [];
133
+ const MANY = 5;
134
+ const heavy = tp.origins.filter((o) => (o.cpu_ms ?? 0) >= 50 || o.script_bytes >= 51200 || o.unsized >= MANY || o.blocking);
135
+ const size = (bytes, scripts, unsized) => {
136
+ if (!unsized) return kb(bytes);
137
+ return unsized === scripts ? `${scripts} script${scripts === 1 ? "" : "s"}, sizes hidden` : `at least ${kb(bytes)}`;
138
+ };
139
+ const label = (o) => `${o.host}${o.kind !== "other" ? ` (${o.kind})` : ""} ${size(o.script_bytes, o.scripts, o.unsized)}${o.cpu_ms ? `, ${Math.round(o.cpu_ms)} ms CPU` : ""}${o.blocking ? `, ${o.blocking} blocking` : ""}`;
140
+ const scripts = tp.origins.reduce((s, o) => s + o.scripts, 0);
141
+ let hid = `${tp.weighed} of them`;
142
+ if (tp.weighed === scripts) hid = scripts === 1 ? "it" : "each";
143
+ const weighed = tp.weighed ? ` (this browser hid the size of ${hid}; weighed from the files themselves)` : "";
144
+ const served = (tp.unsized ? null : `${kb(tp.script_bytes)} of JS${weighed}`) ?? (tp.unsized === scripts ? `${scripts} script${scripts === 1 ? "" : "s"} (this browser hides other origins' file sizes; a Chromium visit shows them)` : `at least ${kb(tp.script_bytes)} of JS (${tp.unsized} of the ${scripts} scripts had their size hidden by this browser${tp.weighed ? `; ${tp.weighed} more it hid were weighed from the files themselves` : ""})`);
145
+ if (tp.script_bytes >= 30720 || tp.unsized >= MANY || (tp.cpu_ms ?? 0) >= 50 || tp.blocking > 0) out.push({
146
+ code: "third-party",
147
+ severity: (tp.cpu_ms ?? 0) >= 200 || tp.blocking > 0 || tp.script_bytes >= 204800 ? "warn" : "info",
148
+ message: `${tp.origins.length} other origin${tp.origins.length === 1 ? "" : "s"} served ${served}${tp.cpu_ms !== null ? ` and ran ${Math.round(tp.cpu_ms)} ms on the main thread` : ""}${tp.runtime_loaded ? `; ${tp.runtime_loaded} of their scripts the page never names, loaded by other scripts (how a tag manager or a self-loading library adds code)` : ""}. The heaviest: ${(heavy.length ? heavy : tp.origins).slice(0, 3).map(label).join("; ")}.`,
149
+ fix: [...new Set((heavy.length ? heavy : tp.origins).slice(0, 3).map((o) => `${o.kind === "other" ? o.host : o.kind}: ${ADVICE[o.kind]}`))].join(" · ")
150
+ });
151
+ const blocking = tp.origins.filter((o) => o.blocking);
152
+ if (blocking.length) out.push({
153
+ code: "third-party-blocking",
154
+ severity: "warn",
155
+ message: `${blocking.map((o) => o.host).join(", ")} held the first paint: the page waited on another origin's server before showing anything.`,
156
+ fix: "Load its scripts with async or defer, self-host a stylesheet or font it serves, or at least preconnect to it."
157
+ });
158
+ const last_woke = edited.reduce((m, e) => Math.max(m, e.done), -Infinity);
159
+ const editors = tp.origins.filter((o) => o.scripts > 0 && o.first < last_woke && o.kind !== "fonts" && o.kind !== "library cdn");
160
+ if (edited.length && editors.length) out.push({
161
+ code: "third-party-edits",
162
+ severity: "warn",
163
+ message: `${[...new Set(edited.map((e) => e.name))].slice(0, 3).join(", ")} did not wake from the server's markup, and ${editors.slice(0, 3).map((o) => `${o.host}${o.kind !== "other" ? ` (${o.kind})` : ""}`).join(", ")} ran scripts before ${edited.length === 1 ? "it" : "they"} woke: a script from another origin that edits the DOM is the usual cause.`,
164
+ fix: "Keep third-party edits out of island subtrees (target the markup around them), or render the variant on the server so the island hydrates what it was sent."
165
+ });
166
+ return out;
167
+ }
168
+ export { kind_of_host, third_party, third_party_findings };
package/package.json CHANGED
@@ -1,13 +1,59 @@
1
1
  {
2
- "name": "@ogygia/insights",
3
- "version": "0.0.0",
4
- "description": "Placeholder that reserves the name for ogygia. The first real release is 0.14.",
5
- "homepage": "https://ogygiajs.dev",
6
- "license": "MIT",
7
- "repository": {
8
- "type": "git",
9
- "url": "git+https://github.com/ogygiajs/ogygia.git",
10
- "directory": "packages/insights"
11
- },
12
- "files": ["README.md"]
13
- }
2
+ "name": "@ogygia/insights",
3
+ "version": "0.15.0",
4
+ "description": "The analysis engine for ogygia: what the browser measured on a page visit or a recorded session, turned into findings for the devtools dock, the profiler's report and @ogygia/testing",
5
+ "keywords": [
6
+ "devtools",
7
+ "ogygia",
8
+ "performance",
9
+ "sveltekit",
10
+ "web-vitals"
11
+ ],
12
+ "homepage": "https://ogygiajs.dev",
13
+ "license": "MIT",
14
+ "author": "Puru Vijayvargia <devpuruvj@gmail.com>",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/ogygiajs/ogygia.git",
18
+ "directory": "packages/insights"
19
+ },
20
+ "files": [
21
+ "dist",
22
+ "README.md"
23
+ ],
24
+ "type": "module",
25
+ "sideEffects": false,
26
+ "exports": {
27
+ ".": {
28
+ "types": "./dist/index.d.ts",
29
+ "default": "./dist/index.js"
30
+ }
31
+ },
32
+ "publishConfig": {
33
+ "access": "public"
34
+ },
35
+ "dependencies": {
36
+ "@ogygia/machine": "^0.14.0",
37
+ "devalue": "^5.9.3"
38
+ },
39
+ "devDependencies": {
40
+ "@ogygia/config": "0.0.0",
41
+ "@ogygia/core": "0.15.0",
42
+ "@sveltejs/vite-plugin-svelte": "^7.2.0",
43
+ "@types/node": "^26.1.2",
44
+ "@typescript/native": "npm:typescript@^7.0.2",
45
+ "svelte": "5.57.3",
46
+ "tsdown": "^0.23.4",
47
+ "typescript": "^6.0.3",
48
+ "vite": "^8.3.5",
49
+ "vitest": "^4.1.12"
50
+ },
51
+ "peerDependencies": {
52
+ "@ogygia/core": "^0.15.0"
53
+ },
54
+ "scripts": {
55
+ "build": "tsdown",
56
+ "check": "tsc -p . && tsc -p test && vitest run",
57
+ "test": "vitest run"
58
+ }
59
+ }