@vercel/speed-insights 1.3.1 → 2.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.
Files changed (53) hide show
  1. package/dist/astro/component.ts +1 -2
  2. package/dist/astro/index.astro +25 -10
  3. package/dist/index.d.mts +12 -10
  4. package/dist/index.d.ts +12 -10
  5. package/dist/index.js +58 -31
  6. package/dist/index.js.map +1 -1
  7. package/dist/index.mjs +58 -31
  8. package/dist/index.mjs.map +1 -1
  9. package/dist/next/index.d.mts +5 -5
  10. package/dist/next/index.d.ts +5 -5
  11. package/dist/next/index.js +84 -38
  12. package/dist/next/index.js.map +1 -1
  13. package/dist/next/index.mjs +84 -38
  14. package/dist/next/index.mjs.map +1 -1
  15. package/dist/nuxt/index.d.mts +5 -2
  16. package/dist/nuxt/index.d.ts +5 -2
  17. package/dist/nuxt/index.js +30 -182
  18. package/dist/nuxt/index.js.map +1 -1
  19. package/dist/nuxt/index.mjs +26 -171
  20. package/dist/nuxt/index.mjs.map +1 -1
  21. package/dist/nuxt/runtime/index.d.mts +40 -0
  22. package/dist/nuxt/runtime/index.d.ts +40 -0
  23. package/dist/nuxt/runtime/index.js +278 -0
  24. package/dist/nuxt/runtime/index.js.map +1 -0
  25. package/dist/nuxt/runtime/index.mjs +250 -0
  26. package/dist/nuxt/runtime/index.mjs.map +1 -0
  27. package/dist/react/index.d.mts +6 -5
  28. package/dist/react/index.d.ts +6 -5
  29. package/dist/react/index.js +76 -37
  30. package/dist/react/index.js.map +1 -1
  31. package/dist/react/index.mjs +76 -37
  32. package/dist/react/index.mjs.map +1 -1
  33. package/dist/remix/index.d.mts +5 -5
  34. package/dist/remix/index.d.ts +5 -5
  35. package/dist/remix/index.js +86 -41
  36. package/dist/remix/index.js.map +1 -1
  37. package/dist/remix/index.mjs +86 -41
  38. package/dist/remix/index.mjs.map +1 -1
  39. package/dist/sveltekit/index.d.mts +6 -6
  40. package/dist/sveltekit/index.d.ts +6 -6
  41. package/dist/sveltekit/index.js +77 -43
  42. package/dist/sveltekit/index.js.map +1 -1
  43. package/dist/sveltekit/index.mjs +77 -43
  44. package/dist/sveltekit/index.mjs.map +1 -1
  45. package/dist/vue/index.d.mts +36 -1
  46. package/dist/vue/index.d.ts +36 -1
  47. package/dist/vue/index.js +75 -36
  48. package/dist/vue/index.js.map +1 -1
  49. package/dist/vue/index.mjs +75 -36
  50. package/dist/vue/index.mjs.map +1 -1
  51. package/package.json +16 -6
  52. package/tsup.config.js +17 -2
  53. package/.eslintrc.cjs +0 -7
@@ -2,20 +2,20 @@ interface SpeedInsightsProps {
2
2
  dsn?: string;
3
3
  sampleRate?: number;
4
4
  route?: string | null;
5
- beforeSend?: BeforeSendMiddleware;
5
+ beforeSend?: BeforeSend;
6
6
  debug?: boolean;
7
7
  scriptSrc?: string;
8
8
  endpoint?: string;
9
9
  }
10
10
  type EventTypes = 'vital';
11
- interface Event {
11
+ interface BeforeSendEvent {
12
12
  type: EventTypes;
13
13
  url: string;
14
14
  route?: string;
15
15
  }
16
- type BeforeSendMiddleware = (data: Event) => Event | null | undefined | false;
16
+ type BeforeSend = (event: BeforeSendEvent) => BeforeSendEvent | null | undefined | false;
17
17
  interface Functions {
18
- beforeSend?: BeforeSendMiddleware;
18
+ beforeSend?: BeforeSend;
19
19
  }
20
20
  interface SpeedInsights<T extends keyof Functions = keyof Functions> {
21
21
  queue: [T, Functions[T]][];
@@ -29,10 +29,10 @@ declare global {
29
29
  siq?: SpeedInsights['queue'];
30
30
  sil?: boolean;
31
31
  /** used by Astro component only */
32
- speedInsightsBeforeSend?: BeforeSendMiddleware;
32
+ speedInsightsBeforeSend?: BeforeSend;
33
33
  }
34
34
  }
35
35
 
36
36
  declare function injectSpeedInsights(props?: Omit<SpeedInsightsProps, 'framework'>): void;
37
37
 
38
- export { injectSpeedInsights };
38
+ export { type BeforeSend, type BeforeSendEvent, type SpeedInsightsProps, injectSpeedInsights };
@@ -24,19 +24,22 @@ __export(sveltekit_exports, {
24
24
  });
25
25
  module.exports = __toCommonJS(sveltekit_exports);
26
26
  var import_store = require("svelte/store");
27
-
28
- // package.json
29
- var name = "@vercel/speed-insights";
30
- var version = "1.3.1";
27
+ var import_environment = require("$app/environment");
28
+ var import_stores = require("$app/stores");
31
29
 
32
30
  // src/queue.ts
33
31
  var initQueue = () => {
34
32
  if (window.si) return;
35
33
  window.si = function a(...params) {
36
- (window.siq = window.siq || []).push(params);
34
+ window.siq = window.siq || [];
35
+ window.siq.push(params);
37
36
  };
38
37
  };
39
38
 
39
+ // package.json
40
+ var name = "@vercel/speed-insights";
41
+ var version = "2.0.0";
42
+
40
43
  // src/utils.ts
41
44
  function isBrowser() {
42
45
  return typeof window !== "undefined";
@@ -47,7 +50,7 @@ function detectEnvironment() {
47
50
  if (env === "development" || env === "test") {
48
51
  return "development";
49
52
  }
50
- } catch (e) {
53
+ } catch {
51
54
  }
52
55
  return "production";
53
56
  }
@@ -56,7 +59,7 @@ function isDevelopment() {
56
59
  }
57
60
  function getScriptSrc(props) {
58
61
  if (props.scriptSrc) {
59
- return props.scriptSrc;
62
+ return makeAbsolute(props.scriptSrc);
60
63
  }
61
64
  if (isDevelopment()) {
62
65
  return "https://va.vercel-scripts.com/v1/speed-insights/script.debug.js";
@@ -65,42 +68,68 @@ function getScriptSrc(props) {
65
68
  return "https://va.vercel-scripts.com/v1/speed-insights/script.js";
66
69
  }
67
70
  if (props.basePath) {
68
- return `${props.basePath}/speed-insights/script.js`;
71
+ return makeAbsolute(`${props.basePath}/speed-insights/script.js`);
69
72
  }
70
73
  return "/_vercel/speed-insights/script.js";
71
74
  }
72
-
73
- // src/generic.ts
74
- function injectSpeedInsights(props = {}) {
75
+ function loadProps(explicitProps, confString) {
75
76
  var _a;
76
- if (!isBrowser() || props.route === null) return null;
77
- initQueue();
78
- const src = getScriptSrc(props);
79
- if (document.head.querySelector(`script[src*="${src}"]`)) return null;
80
- if (props.beforeSend) {
81
- (_a = window.si) == null ? void 0 : _a.call(window, "beforeSend", props.beforeSend);
77
+ let props = explicitProps;
78
+ if (confString) {
79
+ try {
80
+ props = {
81
+ ...(_a = JSON.parse(confString)) == null ? void 0 : _a.speedInsights,
82
+ ...explicitProps
83
+ };
84
+ } catch {
85
+ }
82
86
  }
83
- const script = document.createElement("script");
84
- script.src = src;
85
- script.defer = true;
86
- script.dataset.sdkn = name + (props.framework ? `/${props.framework}` : "");
87
- script.dataset.sdkv = version;
87
+ const dataset = {
88
+ sdkn: name + (props.framework ? `/${props.framework}` : ""),
89
+ sdkv: version
90
+ };
88
91
  if (props.sampleRate) {
89
- script.dataset.sampleRate = props.sampleRate.toString();
92
+ dataset.sampleRate = props.sampleRate.toString();
90
93
  }
91
94
  if (props.route) {
92
- script.dataset.route = props.route;
95
+ dataset.route = props.route;
96
+ }
97
+ if (isDevelopment() && props.debug === false) {
98
+ dataset.debug = "false";
99
+ }
100
+ if (props.dsn) {
101
+ dataset.dsn = props.dsn;
93
102
  }
94
103
  if (props.endpoint) {
95
- script.dataset.endpoint = props.endpoint;
104
+ dataset.endpoint = makeAbsolute(props.endpoint);
96
105
  } else if (props.basePath) {
97
- script.dataset.endpoint = `${props.basePath}/speed-insights/vitals`;
106
+ dataset.endpoint = makeAbsolute(`${props.basePath}/speed-insights/vitals`);
98
107
  }
99
- if (props.dsn) {
100
- script.dataset.dsn = props.dsn;
108
+ return {
109
+ src: getScriptSrc(props),
110
+ beforeSend: props.beforeSend,
111
+ dataset
112
+ };
113
+ }
114
+ function makeAbsolute(url) {
115
+ return url.startsWith("http://") || url.startsWith("https://") || url.startsWith("/") ? url : `/${url}`;
116
+ }
117
+
118
+ // src/generic.ts
119
+ function injectSpeedInsights(props = {}, confString) {
120
+ var _a;
121
+ if (!isBrowser() || props.route === null) return null;
122
+ initQueue();
123
+ const { beforeSend, src, dataset } = loadProps(props, confString);
124
+ if (document.head.querySelector(`script[src*="${src}"]`)) return null;
125
+ if (beforeSend) {
126
+ (_a = window.si) == null ? void 0 : _a.call(window, "beforeSend", beforeSend);
101
127
  }
102
- if (isDevelopment() && props.debug === false) {
103
- script.dataset.debug = "false";
128
+ const script = document.createElement("script");
129
+ script.src = src;
130
+ script.defer = true;
131
+ for (const [key, value] of Object.entries(dataset)) {
132
+ script.dataset[key] = value;
104
133
  }
105
134
  script.onerror = () => {
106
135
  console.log(
@@ -123,25 +152,30 @@ function getBasePath() {
123
152
  } catch {
124
153
  }
125
154
  }
155
+ function getConfigString() {
156
+ try {
157
+ return import_meta.env.VITE_VERCEL_OBSERVABILITY_CLIENT_CONFIG;
158
+ } catch {
159
+ }
160
+ }
126
161
 
127
162
  // src/sveltekit/index.ts
128
- var import_stores = require("$app/stores");
129
- var import_environment = require("$app/environment");
130
163
  function injectSpeedInsights2(props = {}) {
131
164
  var _a;
132
165
  if (import_environment.browser) {
133
- const speedInsights = injectSpeedInsights({
134
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- route could be undefined in layout.js file
135
- route: (_a = (0, import_store.get)(import_stores.page).route) == null ? void 0 : _a.id,
136
- ...props,
137
- framework: "sveltekit",
138
- basePath: getBasePath()
139
- });
166
+ const speedInsights = injectSpeedInsights(
167
+ {
168
+ route: (_a = (0, import_store.get)(import_stores.page).route) == null ? void 0 : _a.id,
169
+ ...props,
170
+ framework: "sveltekit",
171
+ basePath: getBasePath()
172
+ },
173
+ getConfigString()
174
+ );
140
175
  if (speedInsights) {
141
- import_stores.page.subscribe((value) => {
142
- var _a2;
143
- if ((_a2 = value.route) == null ? void 0 : _a2.id) {
144
- speedInsights.setRoute(value.route.id);
176
+ import_stores.page.subscribe(({ route }) => {
177
+ if (route == null ? void 0 : route.id) {
178
+ speedInsights.setRoute(route.id);
145
179
  }
146
180
  });
147
181
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/sveltekit/index.ts","../../package.json","../../src/queue.ts","../../src/utils.ts","../../src/generic.ts","../../src/sveltekit/utils.ts"],"sourcesContent":["import { get } from 'svelte/store';\nimport {\n injectSpeedInsights as genericInject,\n type SpeedInsightsProps,\n} from '../generic';\nimport { getBasePath } from './utils';\nimport { page } from '$app/stores';\nimport { browser } from '$app/environment';\nimport type {} from '@sveltejs/kit'; // don't remove, ensures ambient types for $app/* are loaded\n\nexport function injectSpeedInsights(\n props: Omit<SpeedInsightsProps, 'framework'> = {},\n): void {\n if (browser) {\n const speedInsights = genericInject({\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- route could be undefined in layout.js file\n route: get(page).route?.id,\n ...props,\n framework: 'sveltekit',\n basePath: getBasePath(),\n });\n\n if (speedInsights) {\n page.subscribe((value) => {\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- route could be undefined in layout.js file\n if (value.route?.id) {\n speedInsights.setRoute(value.route.id);\n }\n });\n }\n }\n}\n","{\n \"name\": \"@vercel/speed-insights\",\n \"version\": \"1.3.1\",\n \"description\": \"Speed Insights is a tool for measuring web performance and providing suggestions for improvement.\",\n \"keywords\": [\n \"speed-insights\",\n \"vercel\"\n ],\n \"repository\": {\n \"url\": \"github:vercel/speed-insights\",\n \"directory\": \"packages/web\"\n },\n \"license\": \"Apache-2.0\",\n \"exports\": {\n \"./package.json\": \"./package.json\",\n \".\": {\n \"browser\": \"./dist/index.mjs\",\n \"import\": \"./dist/index.mjs\",\n \"require\": \"./dist/index.js\"\n },\n \"./astro\": {\n \"import\": \"./dist/astro/component.ts\"\n },\n \"./next\": {\n \"browser\": \"./dist/next/index.mjs\",\n \"import\": \"./dist/next/index.mjs\",\n \"require\": \"./dist/next/index.js\"\n },\n \"./nuxt\": {\n \"browser\": \"./dist/nuxt/index.mjs\",\n \"import\": \"./dist/nuxt/index.mjs\",\n \"require\": \"./dist/nuxt/index.js\"\n },\n \"./react\": {\n \"browser\": \"./dist/react/index.mjs\",\n \"import\": \"./dist/react/index.mjs\",\n \"require\": \"./dist/react/index.js\"\n },\n \"./remix\": {\n \"browser\": \"./dist/remix/index.mjs\",\n \"import\": \"./dist/remix/index.mjs\",\n \"require\": \"./dist/remix/index.js\"\n },\n \"./sveltekit\": {\n \"types\": \"./dist/sveltekit/index.d.ts\",\n \"svelte\": \"./dist/sveltekit/index.mjs\"\n },\n \"./vue\": {\n \"browser\": \"./dist/vue/index.mjs\",\n \"import\": \"./dist/vue/index.mjs\",\n \"require\": \"./dist/vue/index.js\"\n }\n },\n \"main\": \"./dist/index.js\",\n \"types\": \"./dist/index.d.ts\",\n \"typesVersions\": {\n \"*\": {\n \"*\": [\n \"dist/index.d.ts\"\n ],\n \"react\": [\n \"dist/react/index.d.ts\"\n ],\n \"next\": [\n \"dist/next/index.d.ts\"\n ],\n \"nuxt\": [\n \"dist/nuxt/index.d.ts\"\n ],\n \"remix\": [\n \"dist/remix/index.d.ts\"\n ],\n \"sveltekit\": [\n \"dist/sveltekit/index.d.ts\"\n ],\n \"vue\": [\n \"dist/vue/index.d.ts\"\n ]\n }\n },\n \"scripts\": {\n \"build\": \"tsup && pnpm copy-astro\",\n \"copy-astro\": \"cp -R src/astro dist/\",\n \"dev\": \"pnpm copy-astro && tsup --watch\",\n \"lint\": \"eslint .\",\n \"lint-fix\": \"eslint . --fix\",\n \"test\": \"vitest\",\n \"type-check\": \"tsc --noEmit\"\n },\n \"devDependencies\": {\n \"@remix-run/react\": \"^2.14.0\",\n \"@sveltejs/kit\": \"^2.8.1\",\n \"@swc/core\": \"^1.9.2\",\n \"@testing-library/jest-dom\": \"^6.6.3\",\n \"@testing-library/react\": \"^16.0.1\",\n \"@types/node\": \"^22.9.1\",\n \"@types/react\": \"^18.3.12\",\n \"copyfiles\": \"^2.4.1\",\n \"jsdom\": \"^25.0.1\",\n \"next\": \"^14.0.4\",\n \"react\": \"^18.3.1\",\n \"react-dom\": \"^18.3.1\",\n \"svelte\": \"^5.2.7\",\n \"tsup\": \"8.3.5\",\n \"vitest\": \"^2.1.5\",\n \"vue\": \"^3.5.13\",\n \"vue-router\": \"^4.4.5\"\n },\n \"peerDependencies\": {\n \"@sveltejs/kit\": \"^1 || ^2\",\n \"next\": \">= 13\",\n \"react\": \"^18 || ^19 || ^19.0.0-rc\",\n \"svelte\": \">= 4\",\n \"vue\": \"^3\",\n \"vue-router\": \"^4\"\n },\n \"peerDependenciesMeta\": {\n \"@sveltejs/kit\": {\n \"optional\": true\n },\n \"next\": {\n \"optional\": true\n },\n \"react\": {\n \"optional\": true\n },\n \"svelte\": {\n \"optional\": true\n },\n \"vue\": {\n \"optional\": true\n },\n \"vue-router\": {\n \"optional\": true\n }\n }\n}\n","export const initQueue = (): void => {\n // initialize va until script is loaded\n if (window.si) return;\n\n window.si = function a(...params): void {\n (window.siq = window.siq || []).push(params);\n };\n};\n","import type { SpeedInsightsProps } from './types';\n\nexport function isBrowser(): boolean {\n return typeof window !== 'undefined';\n}\n\nfunction detectEnvironment(): 'development' | 'production' {\n try {\n const env = process.env.NODE_ENV;\n if (env === 'development' || env === 'test') {\n return 'development';\n }\n } catch (e) {\n // do nothing, this is okay\n }\n return 'production';\n}\n\nexport function isProduction(): boolean {\n return detectEnvironment() === 'production';\n}\n\nexport function isDevelopment(): boolean {\n return detectEnvironment() === 'development';\n}\n\nexport function computeRoute(\n pathname: string | null,\n pathParams: Record<string, string | string[]> | null,\n): string | null {\n if (!pathname || !pathParams) {\n return pathname;\n }\n\n let result = pathname;\n try {\n const entries = Object.entries(pathParams);\n // simple keys must be handled first\n for (const [key, value] of entries) {\n if (!Array.isArray(value)) {\n const matcher = turnValueToRegExp(value);\n if (matcher.test(result)) {\n result = result.replace(matcher, `/[${key}]`);\n }\n }\n }\n // array values next\n for (const [key, value] of entries) {\n if (Array.isArray(value)) {\n const matcher = turnValueToRegExp(value.join('/'));\n if (matcher.test(result)) {\n result = result.replace(matcher, `/[...${key}]`);\n }\n }\n }\n return result;\n } catch (e) {\n return pathname;\n }\n}\n\nfunction turnValueToRegExp(value: string): RegExp {\n return new RegExp(`/${escapeRegExp(value)}(?=[/?#]|$)`);\n}\n\nfunction escapeRegExp(string: string): string {\n return string.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n}\n\nexport function getScriptSrc(\n props: SpeedInsightsProps & { basePath?: string },\n): string {\n if (props.scriptSrc) {\n return props.scriptSrc;\n }\n if (isDevelopment()) {\n return 'https://va.vercel-scripts.com/v1/speed-insights/script.debug.js';\n }\n if (props.dsn) {\n return 'https://va.vercel-scripts.com/v1/speed-insights/script.js';\n }\n if (props.basePath) {\n return `${props.basePath}/speed-insights/script.js`;\n }\n return '/_vercel/speed-insights/script.js';\n}\n","import { name as packageName, version } from '../package.json';\nimport { initQueue } from './queue';\nimport type { SpeedInsightsProps, BeforeSendMiddleware } from './types';\nimport { computeRoute, getScriptSrc, isBrowser, isDevelopment } from './utils';\n\n/**\n * Injects the Vercel Speed Insights script into the page head and starts tracking page views. Read more in our [documentation](https://vercel.com/docs/speed-insights).\n * @param [props] - Speed Insights options.\n * @param [props.debug] - Whether to enable debug logging in development. Defaults to `true`.\n * @param [props.beforeSend] - A middleware function to modify events before they are sent. Should return the event object or `null` to cancel the event.\n * @param [props.sampleRate] - When setting to 0.5, 50% of the events will be sent to Vercel Speed Insights. Defaults to `1`.\n * @param [props.route] - The dynamic route of the page.\n * @param [props.dsn] - The DSN of the project to send events to. Only required when self-hosting.\n */\nfunction injectSpeedInsights(\n props: SpeedInsightsProps & {\n framework?: string;\n basePath?: string;\n } = {},\n): {\n setRoute: (route: string | null) => void;\n} | null {\n // When route is null, it means that pages router is not ready yet. Will resolve soon\n if (!isBrowser() || props.route === null) return null;\n\n initQueue();\n\n const src = getScriptSrc(props);\n\n if (document.head.querySelector(`script[src*=\"${src}\"]`)) return null;\n\n if (props.beforeSend) {\n window.si?.('beforeSend', props.beforeSend);\n }\n\n const script = document.createElement('script');\n script.src = src;\n script.defer = true;\n script.dataset.sdkn =\n packageName + (props.framework ? `/${props.framework}` : '');\n script.dataset.sdkv = version;\n\n if (props.sampleRate) {\n script.dataset.sampleRate = props.sampleRate.toString();\n }\n if (props.route) {\n script.dataset.route = props.route;\n }\n if (props.endpoint) {\n script.dataset.endpoint = props.endpoint;\n } else if (props.basePath) {\n script.dataset.endpoint = `${props.basePath}/speed-insights/vitals`;\n }\n if (props.dsn) {\n script.dataset.dsn = props.dsn;\n }\n if (isDevelopment() && props.debug === false) {\n script.dataset.debug = 'false';\n }\n\n script.onerror = (): void => {\n // eslint-disable-next-line no-console -- Logging is okay here\n console.log(\n `[Vercel Speed Insights] Failed to load script from ${src}. Please check if any content blockers are enabled and try again.`,\n );\n };\n\n document.head.appendChild(script);\n\n return {\n setRoute: (route: string | null): void => {\n script.dataset.route = route ?? undefined;\n },\n };\n}\n\nexport { injectSpeedInsights, computeRoute };\nexport type { SpeedInsightsProps, BeforeSendMiddleware };\n\n// eslint-disable-next-line import/no-default-export -- Allow default export\nexport default {\n injectSpeedInsights,\n computeRoute,\n};\n","export function getBasePath(): string | undefined {\n // !! important !!\n // do not access env variables using import.meta.env[varname]\n // some bundles won't replace the value at build time.\n try {\n return import.meta.env.VITE_VERCEL_OBSERVABILITY_BASEPATH as\n | string\n | undefined;\n } catch {\n // do nothing\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,6BAAAA;AAAA;AAAA;AAAA,mBAAoB;;;ACClB,WAAQ;AACR,cAAW;;;ACFN,IAAM,YAAY,MAAY;AAEnC,MAAI,OAAO,GAAI;AAEf,SAAO,KAAK,SAAS,KAAK,QAAc;AACtC,KAAC,OAAO,MAAM,OAAO,OAAO,CAAC,GAAG,KAAK,MAAM;AAAA,EAC7C;AACF;;;ACLO,SAAS,YAAqB;AACnC,SAAO,OAAO,WAAW;AAC3B;AAEA,SAAS,oBAAkD;AACzD,MAAI;AACF,UAAM,MAAM,QAAQ,IAAI;AACxB,QAAI,QAAQ,iBAAiB,QAAQ,QAAQ;AAC3C,aAAO;AAAA,IACT;AAAA,EACF,SAAS,GAAG;AAAA,EAEZ;AACA,SAAO;AACT;AAMO,SAAS,gBAAyB;AACvC,SAAO,kBAAkB,MAAM;AACjC;AA6CO,SAAS,aACd,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AM;AAEhC,SAAO;AAAA,IACL,UAAU,CAAC,UAA+B;AACxC,aAAO,QAAQ,QAAQ,SAAS;AAAA,IAClC;AAAA,EACF;AACF;;;AC1EA;AAAO,SAAS,cAAkC;AAIhD,MAAI;AACF,WAAO,YAAY,IAAI;AAAA,EAGzB,QAAQ;AAAA,EAER;AACF;;;ALLA,oBAAqB;AACrB,yBAAwB;AAGjB,SAASC,qBACd,QAA+C,CAAC,GAC1C;AAZR;AAaE,MAAI,4BAAS;AACX,UAAM,gBAAgB,oBAAc;AAAA;AAAA,MAElC,QAAO,2BAAI,kBAAI,EAAE,UAAV,mBAAiB;AAAA,MACxB,GAAG;AAAA,MACH,WAAW;AAAA,MACX,UAAU,YAAY;AAAA,IACxB,CAAC;AAED,QAAI,eAAe;AACjB,yBAAK,UAAU,CAAC,UAAU;AAvBhC,YAAAC;AAyBQ,aAAIA,MAAA,MAAM,UAAN,gBAAAA,IAAa,IAAI;AACnB,wBAAc,SAAS,MAAM,MAAM,EAAE;AAAA,QACvC;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AACF;","names":["injectSpeedInsights","injectSpeedInsights","_a"]}
1
+ {"version":3,"sources":["../../src/sveltekit/index.ts","../../src/queue.ts","../../package.json","../../src/utils.ts","../../src/generic.ts","../../src/sveltekit/utils.ts"],"sourcesContent":["import type {} from '@sveltejs/kit'; // don't remove, ensures ambient types for $app/* are loaded\nimport { get } from 'svelte/store';\nimport { browser } from '$app/environment';\nimport { page } from '$app/stores';\nimport { injectSpeedInsights as inject } from '../generic';\nimport type { BeforeSend, BeforeSendEvent, SpeedInsightsProps } from '../types';\nimport { getBasePath, getConfigString } from './utils';\n\nexport function injectSpeedInsights(\n props: Omit<SpeedInsightsProps, 'framework'> = {},\n): void {\n if (browser) {\n const speedInsights = inject(\n {\n route: get(page).route?.id,\n ...props,\n framework: 'sveltekit',\n basePath: getBasePath(),\n },\n getConfigString(),\n );\n\n if (speedInsights) {\n page.subscribe(({ route }) => {\n if (route?.id) {\n speedInsights.setRoute(route.id);\n }\n });\n }\n }\n}\nexport type { SpeedInsightsProps, BeforeSend, BeforeSendEvent };\n","export const initQueue = (): void => {\n // initialize va until script is loaded\n if (window.si) return;\n\n window.si = function a(...params): void {\n window.siq = window.siq || [];\n window.siq.push(params);\n };\n};\n","{\n \"name\": \"@vercel/speed-insights\",\n \"version\": \"2.0.0\",\n \"description\": \"Speed Insights is a tool for measuring web performance and providing suggestions for improvement.\",\n \"keywords\": [\n \"speed-insights\",\n \"vercel\"\n ],\n \"repository\": {\n \"url\": \"github:vercel/speed-insights\",\n \"directory\": \"packages/web\"\n },\n \"license\": \"Apache-2.0\",\n \"exports\": {\n \"./package.json\": \"./package.json\",\n \".\": {\n \"browser\": \"./dist/index.mjs\",\n \"import\": \"./dist/index.mjs\",\n \"require\": \"./dist/index.js\"\n },\n \"./astro\": {\n \"import\": \"./dist/astro/component.ts\"\n },\n \"./next\": {\n \"browser\": \"./dist/next/index.mjs\",\n \"import\": \"./dist/next/index.mjs\",\n \"require\": \"./dist/next/index.js\"\n },\n \"./nuxt\": {\n \"import\": \"./dist/nuxt/index.mjs\",\n \"require\": \"./dist/nuxt/index.js\"\n },\n \"./nuxt/runtime\": {\n \"browser\": \"./dist/nuxt/runtime/index.mjs\",\n \"import\": \"./dist/nuxt/runtime/index.mjs\",\n \"require\": \"./dist/nuxt/runtime/index.js\"\n },\n \"./react\": {\n \"browser\": \"./dist/react/index.mjs\",\n \"import\": \"./dist/react/index.mjs\",\n \"require\": \"./dist/react/index.js\"\n },\n \"./remix\": {\n \"browser\": \"./dist/remix/index.mjs\",\n \"import\": \"./dist/remix/index.mjs\",\n \"require\": \"./dist/remix/index.js\"\n },\n \"./sveltekit\": {\n \"types\": \"./dist/sveltekit/index.d.ts\",\n \"svelte\": \"./dist/sveltekit/index.mjs\"\n },\n \"./vue\": {\n \"browser\": \"./dist/vue/index.mjs\",\n \"import\": \"./dist/vue/index.mjs\",\n \"require\": \"./dist/vue/index.js\"\n }\n },\n \"main\": \"./dist/index.js\",\n \"types\": \"./dist/index.d.ts\",\n \"typesVersions\": {\n \"*\": {\n \"*\": [\n \"dist/index.d.ts\"\n ],\n \"react\": [\n \"dist/react/index.d.ts\"\n ],\n \"next\": [\n \"dist/next/index.d.ts\"\n ],\n \"nuxt\": [\n \"dist/nuxt/index.d.ts\"\n ],\n \"nuxt/runtime\": [\n \"dist/nuxt/runtime/index.d.ts\"\n ],\n \"remix\": [\n \"dist/remix/index.d.ts\"\n ],\n \"sveltekit\": [\n \"dist/sveltekit/index.d.ts\"\n ],\n \"vue\": [\n \"dist/vue/index.d.ts\"\n ]\n }\n },\n \"scripts\": {\n \"build\": \"tsup && pnpm copy-astro\",\n \"copy-astro\": \"cp -R src/astro dist/\",\n \"dev\": \"pnpm copy-astro && tsup --watch\",\n \"test\": \"vitest\"\n },\n \"devDependencies\": {\n \"@nuxt/kit\": \"^4.2.0\",\n \"@nuxt/schema\": \"^4.2.0\",\n \"@remix-run/react\": \"^2.14.0\",\n \"@sveltejs/kit\": \"^2.8.1\",\n \"@swc/core\": \"^1.9.2\",\n \"@testing-library/jest-dom\": \"^6.6.3\",\n \"@testing-library/react\": \"^16.0.1\",\n \"@types/node\": \"^22.9.1\",\n \"@types/react\": \"^18.3.12\",\n \"copyfiles\": \"^2.4.1\",\n \"jsdom\": \"^25.0.1\",\n \"next\": \"^14.0.4\",\n \"react\": \"^18.3.1\",\n \"react-dom\": \"^18.3.1\",\n \"svelte\": \"^5.2.7\",\n \"tsup\": \"8.3.5\",\n \"vitest\": \"^2.1.5\",\n \"vue\": \"^3.5.13\",\n \"vue-router\": \"^4.4.5\"\n },\n \"peerDependencies\": {\n \"@sveltejs/kit\": \"^1 || ^2\",\n \"next\": \">= 13\",\n \"nuxt\": \">= 3\",\n \"react\": \"^18 || ^19 || ^19.0.0-rc\",\n \"svelte\": \">= 4\",\n \"vue\": \"^3\",\n \"vue-router\": \"^4\"\n },\n \"peerDependenciesMeta\": {\n \"@sveltejs/kit\": {\n \"optional\": true\n },\n \"next\": {\n \"optional\": true\n },\n \"nuxt\": {\n \"optional\": true\n },\n \"react\": {\n \"optional\": true\n },\n \"svelte\": {\n \"optional\": true\n },\n \"vue\": {\n \"optional\": true\n },\n \"vue-router\": {\n \"optional\": true\n }\n }\n}\n","import { name as packageName, version } from '../package.json';\nimport type {\n BeforeSend,\n InjectSpeedInsightsProps,\n SpeedInsightsProps,\n} from './types';\n\nexport function isBrowser(): boolean {\n return typeof window !== 'undefined';\n}\n\nfunction detectEnvironment(): 'development' | 'production' {\n try {\n const env = process.env.NODE_ENV;\n if (env === 'development' || env === 'test') {\n return 'development';\n }\n } catch {\n // do nothing, this is okay\n }\n return 'production';\n}\n\nexport function isProduction(): boolean {\n return detectEnvironment() === 'production';\n}\n\nexport function isDevelopment(): boolean {\n return detectEnvironment() === 'development';\n}\n\nexport function computeRoute(\n pathname: string | null,\n pathParams: Record<string, string | string[]> | null,\n): string | null {\n if (!pathname || !pathParams) {\n return pathname;\n }\n\n let result = pathname;\n try {\n const entries = Object.entries(pathParams);\n // simple keys must be handled first\n for (const [key, value] of entries) {\n if (!Array.isArray(value)) {\n const matcher = turnValueToRegExp(value);\n if (matcher.test(result)) {\n result = result.replace(matcher, `/[${key}]`);\n }\n }\n }\n // array values next\n for (const [key, value] of entries) {\n if (Array.isArray(value)) {\n const matcher = turnValueToRegExp(value.join('/'));\n if (matcher.test(result)) {\n result = result.replace(matcher, `/[...${key}]`);\n }\n }\n }\n return result;\n } catch {\n return pathname;\n }\n}\n\nfunction turnValueToRegExp(value: string): RegExp {\n return new RegExp(`/${escapeRegExp(value)}(?=[/?#]|$)`);\n}\n\nfunction escapeRegExp(string: string): string {\n return string.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n}\n\nfunction getScriptSrc(\n props: SpeedInsightsProps & { basePath?: string },\n): string {\n if (props.scriptSrc) {\n return makeAbsolute(props.scriptSrc);\n }\n if (isDevelopment()) {\n return 'https://va.vercel-scripts.com/v1/speed-insights/script.debug.js';\n }\n if (props.dsn) {\n return 'https://va.vercel-scripts.com/v1/speed-insights/script.js';\n }\n if (props.basePath) {\n return makeAbsolute(`${props.basePath}/speed-insights/script.js`);\n }\n return '/_vercel/speed-insights/script.js';\n}\n\nexport function loadProps(\n explicitProps: InjectSpeedInsightsProps,\n confString?: string,\n): {\n src: string;\n beforeSend?: BeforeSend;\n dataset: Record<string, string>;\n} {\n let props = explicitProps;\n if (confString) {\n try {\n props = {\n ...(JSON.parse(confString)\n ?.speedInsights as Partial<SpeedInsightsProps>),\n ...explicitProps,\n };\n } catch {\n // Invalid JSON, use only explicit props\n }\n }\n\n const dataset: Record<string, string> = {\n sdkn: packageName + (props.framework ? `/${props.framework}` : ''),\n sdkv: version,\n };\n\n if (props.sampleRate) {\n dataset.sampleRate = props.sampleRate.toString();\n }\n if (props.route) {\n dataset.route = props.route;\n }\n if (isDevelopment() && props.debug === false) {\n dataset.debug = 'false';\n }\n if (props.dsn) {\n dataset.dsn = props.dsn;\n }\n\n if (props.endpoint) {\n dataset.endpoint = makeAbsolute(props.endpoint);\n } else if (props.basePath) {\n // backward compatibility\n dataset.endpoint = makeAbsolute(`${props.basePath}/speed-insights/vitals`);\n }\n\n return {\n src: getScriptSrc(props),\n beforeSend: props.beforeSend,\n dataset,\n };\n}\n\nfunction makeAbsolute(url: string): string {\n return url.startsWith('http://') ||\n url.startsWith('https://') ||\n url.startsWith('/')\n ? url\n : `/${url}`;\n}\n","import { initQueue } from './queue';\nimport type {\n BeforeSend,\n InjectSpeedInsightsProps,\n SpeedInsightsProps,\n} from './types';\nimport { computeRoute, isBrowser, loadProps } from './utils';\n\n/**\n * Injects the Vercel Speed Insights script into the page head and starts tracking page views. Read more in our [documentation](https://vercel.com/docs/speed-insights).\n * @param [props] - Speed Insights options.\n * @param [props.debug] - Whether to enable debug logging in development. Defaults to `true`.\n * @param [props.beforeSend] - A middleware function to modify events before they are sent. Should return the event object or `null` to cancel the event.\n * @param [props.sampleRate] - When setting to 0.5, 50% of the events will be sent to Vercel Speed Insights. Defaults to `1`.\n * @param [props.route] - The dynamic route of the page.\n * @param [props.dsn] - The DSN of the project to send events to. Only required when self-hosting.\n * @param [confString] - an optional JSON string (InjectSpeedInsightsProps) containing the default configuration. Explicit props will take over any provided default.\n */\nfunction injectSpeedInsights(\n props: InjectSpeedInsightsProps = {},\n confString?: string,\n): {\n setRoute: (route: string | null) => void;\n} | null {\n // When route is null, it means that pages router is not ready yet. Will resolve soon\n if (!isBrowser() || props.route === null) return null;\n\n initQueue();\n\n const { beforeSend, src, dataset } = loadProps(props, confString);\n\n if (document.head.querySelector(`script[src*=\"${src}\"]`)) return null;\n\n if (beforeSend) {\n window.si?.('beforeSend', beforeSend);\n }\n\n const script = document.createElement('script');\n script.src = src;\n script.defer = true;\n\n // Apply all dataset attributes from loadProps\n for (const [key, value] of Object.entries(dataset)) {\n script.dataset[key] = value;\n }\n\n script.onerror = (): void => {\n console.log(\n `[Vercel Speed Insights] Failed to load script from ${src}. Please check if any content blockers are enabled and try again.`,\n );\n };\n\n document.head.appendChild(script);\n\n return {\n setRoute: (route: string | null): void => {\n script.dataset.route = route ?? undefined;\n },\n };\n}\n\nexport { injectSpeedInsights, computeRoute };\nexport type { SpeedInsightsProps, BeforeSend as BeforeSendMiddleware };\n\nexport default {\n injectSpeedInsights,\n computeRoute,\n};\n","// !! important !!\n// do not access env variables using import.meta.env[varname]\n// some bundlers won't replace the value at build time.\n\nexport function getBasePath(): string | undefined {\n try {\n return import.meta.env.VITE_VERCEL_OBSERVABILITY_BASEPATH as\n | string\n | undefined;\n } catch {\n // do nothing\n }\n}\n\nexport function getConfigString(): string | undefined {\n try {\n return import.meta.env.VITE_VERCEL_OBSERVABILITY_CLIENT_CONFIG as\n | string\n | undefined;\n } catch {\n // do nothing\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,6BAAAA;AAAA;AAAA;AACA,mBAAoB;AACpB,yBAAwB;AACxB,oBA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,cAAkC;AAChD,MAAI;AACF,WAAO,YAAY,IAAI;AAAA,EAGzB,QAAQ;AAAA,EAER;AACF;AAEO,SAAS,kBAAsC;AACpD,MAAI;AACF,WAAO,YAAY,IAAI;AAAA,EAGzB,QAAQ;AAAA,EAER;AACF;;;ALdO,SAASC,qBACd,QAA+C,CAAC,GAC1C;AAVR;AAWE,MAAI,4BAAS;AACX,UAAM,gBAAgB;AAAA,MACpB;AAAA,QACE,QAAO,2BAAI,kBAAI,EAAE,UAAV,mBAAiB;AAAA,QACxB,GAAG;AAAA,QACH,WAAW;AAAA,QACX,UAAU,YAAY;AAAA,MACxB;AAAA,MACA,gBAAgB;AAAA,IAClB;AAEA,QAAI,eAAe;AACjB,yBAAK,UAAU,CAAC,EAAE,MAAM,MAAM;AAC5B,YAAI,+BAAO,IAAI;AACb,wBAAc,SAAS,MAAM,EAAE;AAAA,QACjC;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AACF;","names":["injectSpeedInsights","injectSpeedInsights"]}
@@ -1,18 +1,21 @@
1
1
  // src/sveltekit/index.ts
2
2
  import { get } from "svelte/store";
3
-
4
- // package.json
5
- var name = "@vercel/speed-insights";
6
- var version = "1.3.1";
3
+ import { browser } from "$app/environment";
4
+ import { page } from "$app/stores";
7
5
 
8
6
  // src/queue.ts
9
7
  var initQueue = () => {
10
8
  if (window.si) return;
11
9
  window.si = function a(...params) {
12
- (window.siq = window.siq || []).push(params);
10
+ window.siq = window.siq || [];
11
+ window.siq.push(params);
13
12
  };
14
13
  };
15
14
 
15
+ // package.json
16
+ var name = "@vercel/speed-insights";
17
+ var version = "2.0.0";
18
+
16
19
  // src/utils.ts
17
20
  function isBrowser() {
18
21
  return typeof window !== "undefined";
@@ -23,7 +26,7 @@ function detectEnvironment() {
23
26
  if (env === "development" || env === "test") {
24
27
  return "development";
25
28
  }
26
- } catch (e) {
29
+ } catch {
27
30
  }
28
31
  return "production";
29
32
  }
@@ -32,7 +35,7 @@ function isDevelopment() {
32
35
  }
33
36
  function getScriptSrc(props) {
34
37
  if (props.scriptSrc) {
35
- return props.scriptSrc;
38
+ return makeAbsolute(props.scriptSrc);
36
39
  }
37
40
  if (isDevelopment()) {
38
41
  return "https://va.vercel-scripts.com/v1/speed-insights/script.debug.js";
@@ -41,42 +44,68 @@ function getScriptSrc(props) {
41
44
  return "https://va.vercel-scripts.com/v1/speed-insights/script.js";
42
45
  }
43
46
  if (props.basePath) {
44
- return `${props.basePath}/speed-insights/script.js`;
47
+ return makeAbsolute(`${props.basePath}/speed-insights/script.js`);
45
48
  }
46
49
  return "/_vercel/speed-insights/script.js";
47
50
  }
48
-
49
- // src/generic.ts
50
- function injectSpeedInsights(props = {}) {
51
+ function loadProps(explicitProps, confString) {
51
52
  var _a;
52
- if (!isBrowser() || props.route === null) return null;
53
- initQueue();
54
- const src = getScriptSrc(props);
55
- if (document.head.querySelector(`script[src*="${src}"]`)) return null;
56
- if (props.beforeSend) {
57
- (_a = window.si) == null ? void 0 : _a.call(window, "beforeSend", props.beforeSend);
53
+ let props = explicitProps;
54
+ if (confString) {
55
+ try {
56
+ props = {
57
+ ...(_a = JSON.parse(confString)) == null ? void 0 : _a.speedInsights,
58
+ ...explicitProps
59
+ };
60
+ } catch {
61
+ }
58
62
  }
59
- const script = document.createElement("script");
60
- script.src = src;
61
- script.defer = true;
62
- script.dataset.sdkn = name + (props.framework ? `/${props.framework}` : "");
63
- script.dataset.sdkv = version;
63
+ const dataset = {
64
+ sdkn: name + (props.framework ? `/${props.framework}` : ""),
65
+ sdkv: version
66
+ };
64
67
  if (props.sampleRate) {
65
- script.dataset.sampleRate = props.sampleRate.toString();
68
+ dataset.sampleRate = props.sampleRate.toString();
66
69
  }
67
70
  if (props.route) {
68
- script.dataset.route = props.route;
71
+ dataset.route = props.route;
72
+ }
73
+ if (isDevelopment() && props.debug === false) {
74
+ dataset.debug = "false";
75
+ }
76
+ if (props.dsn) {
77
+ dataset.dsn = props.dsn;
69
78
  }
70
79
  if (props.endpoint) {
71
- script.dataset.endpoint = props.endpoint;
80
+ dataset.endpoint = makeAbsolute(props.endpoint);
72
81
  } else if (props.basePath) {
73
- script.dataset.endpoint = `${props.basePath}/speed-insights/vitals`;
82
+ dataset.endpoint = makeAbsolute(`${props.basePath}/speed-insights/vitals`);
74
83
  }
75
- if (props.dsn) {
76
- script.dataset.dsn = props.dsn;
84
+ return {
85
+ src: getScriptSrc(props),
86
+ beforeSend: props.beforeSend,
87
+ dataset
88
+ };
89
+ }
90
+ function makeAbsolute(url) {
91
+ return url.startsWith("http://") || url.startsWith("https://") || url.startsWith("/") ? url : `/${url}`;
92
+ }
93
+
94
+ // src/generic.ts
95
+ function injectSpeedInsights(props = {}, confString) {
96
+ var _a;
97
+ if (!isBrowser() || props.route === null) return null;
98
+ initQueue();
99
+ const { beforeSend, src, dataset } = loadProps(props, confString);
100
+ if (document.head.querySelector(`script[src*="${src}"]`)) return null;
101
+ if (beforeSend) {
102
+ (_a = window.si) == null ? void 0 : _a.call(window, "beforeSend", beforeSend);
77
103
  }
78
- if (isDevelopment() && props.debug === false) {
79
- script.dataset.debug = "false";
104
+ const script = document.createElement("script");
105
+ script.src = src;
106
+ script.defer = true;
107
+ for (const [key, value] of Object.entries(dataset)) {
108
+ script.dataset[key] = value;
80
109
  }
81
110
  script.onerror = () => {
82
111
  console.log(
@@ -98,25 +127,30 @@ function getBasePath() {
98
127
  } catch {
99
128
  }
100
129
  }
130
+ function getConfigString() {
131
+ try {
132
+ return import.meta.env.VITE_VERCEL_OBSERVABILITY_CLIENT_CONFIG;
133
+ } catch {
134
+ }
135
+ }
101
136
 
102
137
  // src/sveltekit/index.ts
103
- import { page } from "$app/stores";
104
- import { browser } from "$app/environment";
105
138
  function injectSpeedInsights2(props = {}) {
106
139
  var _a;
107
140
  if (browser) {
108
- const speedInsights = injectSpeedInsights({
109
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- route could be undefined in layout.js file
110
- route: (_a = get(page).route) == null ? void 0 : _a.id,
111
- ...props,
112
- framework: "sveltekit",
113
- basePath: getBasePath()
114
- });
141
+ const speedInsights = injectSpeedInsights(
142
+ {
143
+ route: (_a = get(page).route) == null ? void 0 : _a.id,
144
+ ...props,
145
+ framework: "sveltekit",
146
+ basePath: getBasePath()
147
+ },
148
+ getConfigString()
149
+ );
115
150
  if (speedInsights) {
116
- page.subscribe((value) => {
117
- var _a2;
118
- if ((_a2 = value.route) == null ? void 0 : _a2.id) {
119
- speedInsights.setRoute(value.route.id);
151
+ page.subscribe(({ route }) => {
152
+ if (route == null ? void 0 : route.id) {
153
+ speedInsights.setRoute(route.id);
120
154
  }
121
155
  });
122
156
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/sveltekit/index.ts","../../package.json","../../src/queue.ts","../../src/utils.ts","../../src/generic.ts","../../src/sveltekit/utils.ts"],"sourcesContent":["import { get } from 'svelte/store';\nimport {\n injectSpeedInsights as genericInject,\n type SpeedInsightsProps,\n} from '../generic';\nimport { getBasePath } from './utils';\nimport { page } from '$app/stores';\nimport { browser } from '$app/environment';\nimport type {} from '@sveltejs/kit'; // don't remove, ensures ambient types for $app/* are loaded\n\nexport function injectSpeedInsights(\n props: Omit<SpeedInsightsProps, 'framework'> = {},\n): void {\n if (browser) {\n const speedInsights = genericInject({\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- route could be undefined in layout.js file\n route: get(page).route?.id,\n ...props,\n framework: 'sveltekit',\n basePath: getBasePath(),\n });\n\n if (speedInsights) {\n page.subscribe((value) => {\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- route could be undefined in layout.js file\n if (value.route?.id) {\n speedInsights.setRoute(value.route.id);\n }\n });\n }\n }\n}\n","{\n \"name\": \"@vercel/speed-insights\",\n \"version\": \"1.3.1\",\n \"description\": \"Speed Insights is a tool for measuring web performance and providing suggestions for improvement.\",\n \"keywords\": [\n \"speed-insights\",\n \"vercel\"\n ],\n \"repository\": {\n \"url\": \"github:vercel/speed-insights\",\n \"directory\": \"packages/web\"\n },\n \"license\": \"Apache-2.0\",\n \"exports\": {\n \"./package.json\": \"./package.json\",\n \".\": {\n \"browser\": \"./dist/index.mjs\",\n \"import\": \"./dist/index.mjs\",\n \"require\": \"./dist/index.js\"\n },\n \"./astro\": {\n \"import\": \"./dist/astro/component.ts\"\n },\n \"./next\": {\n \"browser\": \"./dist/next/index.mjs\",\n \"import\": \"./dist/next/index.mjs\",\n \"require\": \"./dist/next/index.js\"\n },\n \"./nuxt\": {\n \"browser\": \"./dist/nuxt/index.mjs\",\n \"import\": \"./dist/nuxt/index.mjs\",\n \"require\": \"./dist/nuxt/index.js\"\n },\n \"./react\": {\n \"browser\": \"./dist/react/index.mjs\",\n \"import\": \"./dist/react/index.mjs\",\n \"require\": \"./dist/react/index.js\"\n },\n \"./remix\": {\n \"browser\": \"./dist/remix/index.mjs\",\n \"import\": \"./dist/remix/index.mjs\",\n \"require\": \"./dist/remix/index.js\"\n },\n \"./sveltekit\": {\n \"types\": \"./dist/sveltekit/index.d.ts\",\n \"svelte\": \"./dist/sveltekit/index.mjs\"\n },\n \"./vue\": {\n \"browser\": \"./dist/vue/index.mjs\",\n \"import\": \"./dist/vue/index.mjs\",\n \"require\": \"./dist/vue/index.js\"\n }\n },\n \"main\": \"./dist/index.js\",\n \"types\": \"./dist/index.d.ts\",\n \"typesVersions\": {\n \"*\": {\n \"*\": [\n \"dist/index.d.ts\"\n ],\n \"react\": [\n \"dist/react/index.d.ts\"\n ],\n \"next\": [\n \"dist/next/index.d.ts\"\n ],\n \"nuxt\": [\n \"dist/nuxt/index.d.ts\"\n ],\n \"remix\": [\n \"dist/remix/index.d.ts\"\n ],\n \"sveltekit\": [\n \"dist/sveltekit/index.d.ts\"\n ],\n \"vue\": [\n \"dist/vue/index.d.ts\"\n ]\n }\n },\n \"scripts\": {\n \"build\": \"tsup && pnpm copy-astro\",\n \"copy-astro\": \"cp -R src/astro dist/\",\n \"dev\": \"pnpm copy-astro && tsup --watch\",\n \"lint\": \"eslint .\",\n \"lint-fix\": \"eslint . --fix\",\n \"test\": \"vitest\",\n \"type-check\": \"tsc --noEmit\"\n },\n \"devDependencies\": {\n \"@remix-run/react\": \"^2.14.0\",\n \"@sveltejs/kit\": \"^2.8.1\",\n \"@swc/core\": \"^1.9.2\",\n \"@testing-library/jest-dom\": \"^6.6.3\",\n \"@testing-library/react\": \"^16.0.1\",\n \"@types/node\": \"^22.9.1\",\n \"@types/react\": \"^18.3.12\",\n \"copyfiles\": \"^2.4.1\",\n \"jsdom\": \"^25.0.1\",\n \"next\": \"^14.0.4\",\n \"react\": \"^18.3.1\",\n \"react-dom\": \"^18.3.1\",\n \"svelte\": \"^5.2.7\",\n \"tsup\": \"8.3.5\",\n \"vitest\": \"^2.1.5\",\n \"vue\": \"^3.5.13\",\n \"vue-router\": \"^4.4.5\"\n },\n \"peerDependencies\": {\n \"@sveltejs/kit\": \"^1 || ^2\",\n \"next\": \">= 13\",\n \"react\": \"^18 || ^19 || ^19.0.0-rc\",\n \"svelte\": \">= 4\",\n \"vue\": \"^3\",\n \"vue-router\": \"^4\"\n },\n \"peerDependenciesMeta\": {\n \"@sveltejs/kit\": {\n \"optional\": true\n },\n \"next\": {\n \"optional\": true\n },\n \"react\": {\n \"optional\": true\n },\n \"svelte\": {\n \"optional\": true\n },\n \"vue\": {\n \"optional\": true\n },\n \"vue-router\": {\n \"optional\": true\n }\n }\n}\n","export const initQueue = (): void => {\n // initialize va until script is loaded\n if (window.si) return;\n\n window.si = function a(...params): void {\n (window.siq = window.siq || []).push(params);\n };\n};\n","import type { SpeedInsightsProps } from './types';\n\nexport function isBrowser(): boolean {\n return typeof window !== 'undefined';\n}\n\nfunction detectEnvironment(): 'development' | 'production' {\n try {\n const env = process.env.NODE_ENV;\n if (env === 'development' || env === 'test') {\n return 'development';\n }\n } catch (e) {\n // do nothing, this is okay\n }\n return 'production';\n}\n\nexport function isProduction(): boolean {\n return detectEnvironment() === 'production';\n}\n\nexport function isDevelopment(): boolean {\n return detectEnvironment() === 'development';\n}\n\nexport function computeRoute(\n pathname: string | null,\n pathParams: Record<string, string | string[]> | null,\n): string | null {\n if (!pathname || !pathParams) {\n return pathname;\n }\n\n let result = pathname;\n try {\n const entries = Object.entries(pathParams);\n // simple keys must be handled first\n for (const [key, value] of entries) {\n if (!Array.isArray(value)) {\n const matcher = turnValueToRegExp(value);\n if (matcher.test(result)) {\n result = result.replace(matcher, `/[${key}]`);\n }\n }\n }\n // array values next\n for (const [key, value] of entries) {\n if (Array.isArray(value)) {\n const matcher = turnValueToRegExp(value.join('/'));\n if (matcher.test(result)) {\n result = result.replace(matcher, `/[...${key}]`);\n }\n }\n }\n return result;\n } catch (e) {\n return pathname;\n }\n}\n\nfunction turnValueToRegExp(value: string): RegExp {\n return new RegExp(`/${escapeRegExp(value)}(?=[/?#]|$)`);\n}\n\nfunction escapeRegExp(string: string): string {\n return string.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n}\n\nexport function getScriptSrc(\n props: SpeedInsightsProps & { basePath?: string },\n): string {\n if (props.scriptSrc) {\n return props.scriptSrc;\n }\n if (isDevelopment()) {\n return 'https://va.vercel-scripts.com/v1/speed-insights/script.debug.js';\n }\n if (props.dsn) {\n return 'https://va.vercel-scripts.com/v1/speed-insights/script.js';\n }\n if (props.basePath) {\n return `${props.basePath}/speed-insights/script.js`;\n }\n return '/_vercel/speed-insights/script.js';\n}\n","import { name as packageName, version } from '../package.json';\nimport { initQueue } from './queue';\nimport type { SpeedInsightsProps, BeforeSendMiddleware } from './types';\nimport { computeRoute, getScriptSrc, isBrowser, isDevelopment } from './utils';\n\n/**\n * Injects the Vercel Speed Insights script into the page head and starts tracking page views. Read more in our [documentation](https://vercel.com/docs/speed-insights).\n * @param [props] - Speed Insights options.\n * @param [props.debug] - Whether to enable debug logging in development. Defaults to `true`.\n * @param [props.beforeSend] - A middleware function to modify events before they are sent. Should return the event object or `null` to cancel the event.\n * @param [props.sampleRate] - When setting to 0.5, 50% of the events will be sent to Vercel Speed Insights. Defaults to `1`.\n * @param [props.route] - The dynamic route of the page.\n * @param [props.dsn] - The DSN of the project to send events to. Only required when self-hosting.\n */\nfunction injectSpeedInsights(\n props: SpeedInsightsProps & {\n framework?: string;\n basePath?: string;\n } = {},\n): {\n setRoute: (route: string | null) => void;\n} | null {\n // When route is null, it means that pages router is not ready yet. Will resolve soon\n if (!isBrowser() || props.route === null) return null;\n\n initQueue();\n\n const src = getScriptSrc(props);\n\n if (document.head.querySelector(`script[src*=\"${src}\"]`)) return null;\n\n if (props.beforeSend) {\n window.si?.('beforeSend', props.beforeSend);\n }\n\n const script = document.createElement('script');\n script.src = src;\n script.defer = true;\n script.dataset.sdkn =\n packageName + (props.framework ? `/${props.framework}` : '');\n script.dataset.sdkv = version;\n\n if (props.sampleRate) {\n script.dataset.sampleRate = props.sampleRate.toString();\n }\n if (props.route) {\n script.dataset.route = props.route;\n }\n if (props.endpoint) {\n script.dataset.endpoint = props.endpoint;\n } else if (props.basePath) {\n script.dataset.endpoint = `${props.basePath}/speed-insights/vitals`;\n }\n if (props.dsn) {\n script.dataset.dsn = props.dsn;\n }\n if (isDevelopment() && props.debug === false) {\n script.dataset.debug = 'false';\n }\n\n script.onerror = (): void => {\n // eslint-disable-next-line no-console -- Logging is okay here\n console.log(\n `[Vercel Speed Insights] Failed to load script from ${src}. Please check if any content blockers are enabled and try again.`,\n );\n };\n\n document.head.appendChild(script);\n\n return {\n setRoute: (route: string | null): void => {\n script.dataset.route = route ?? undefined;\n },\n };\n}\n\nexport { injectSpeedInsights, computeRoute };\nexport type { SpeedInsightsProps, BeforeSendMiddleware };\n\n// eslint-disable-next-line import/no-default-export -- Allow default export\nexport default {\n injectSpeedInsights,\n computeRoute,\n};\n","export function getBasePath(): string | undefined {\n // !! important !!\n // do not access env variables using import.meta.env[varname]\n // some bundles won't replace the value at build time.\n try {\n return import.meta.env.VITE_VERCEL_OBSERVABILITY_BASEPATH as\n | string\n | undefined;\n } catch {\n // do nothing\n }\n}\n"],"mappings":";AAA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veltekit/index.ts","../../src/queue.ts","../../package.json","../../src/utils.ts","../../src/generic.ts","../../src/sveltekit/utils.ts"],"sourcesContent":["import type {} from '@sveltejs/kit'; // don't remove, ensures ambient types for $app/* are loaded\nimport { get } from 'svelte/store';\nimport { browser } from '$app/environment';\nimport { page } from '$app/stores';\nimport { injectSpeedInsights as inject } from '../generic';\nimport type { BeforeSend, BeforeSendEvent, SpeedInsightsProps } from '../types';\nimport { getBasePath, getConfigString } from './utils';\n\nexport function injectSpeedInsights(\n props: Omit<SpeedInsightsProps, 'framework'> = {},\n): void {\n if (browser) {\n const speedInsights = inject(\n {\n route: get(page).route?.id,\n ...props,\n framework: 'sveltekit',\n basePath: getBasePath(),\n },\n getConfigString(),\n );\n\n if (speedInsights) {\n page.subscribe(({ route }) => {\n if (route?.id) {\n speedInsights.setRoute(route.id);\n }\n });\n }\n }\n}\nexport type { SpeedInsightsProps, BeforeSend, BeforeSendEvent };\n","export const initQueue = (): void => {\n // initialize va until script is loaded\n if (window.si) return;\n\n window.si = function a(...params): void {\n window.siq = window.siq || [];\n window.siq.push(params);\n };\n};\n","{\n \"name\": \"@vercel/speed-insights\",\n \"version\": \"2.0.0\",\n \"description\": \"Speed Insights is a tool for measuring web performance and providing suggestions for improvement.\",\n \"keywords\": [\n \"speed-insights\",\n \"vercel\"\n ],\n \"repository\": {\n \"url\": \"github:vercel/speed-insights\",\n \"directory\": \"packages/web\"\n },\n \"license\": \"Apache-2.0\",\n \"exports\": {\n \"./package.json\": \"./package.json\",\n \".\": {\n \"browser\": \"./dist/index.mjs\",\n \"import\": \"./dist/index.mjs\",\n \"require\": \"./dist/index.js\"\n },\n \"./astro\": {\n \"import\": \"./dist/astro/component.ts\"\n },\n \"./next\": {\n \"browser\": \"./dist/next/index.mjs\",\n \"import\": \"./dist/next/index.mjs\",\n \"require\": \"./dist/next/index.js\"\n },\n \"./nuxt\": {\n \"import\": \"./dist/nuxt/index.mjs\",\n \"require\": \"./dist/nuxt/index.js\"\n },\n \"./nuxt/runtime\": {\n \"browser\": \"./dist/nuxt/runtime/index.mjs\",\n \"import\": \"./dist/nuxt/runtime/index.mjs\",\n \"require\": \"./dist/nuxt/runtime/index.js\"\n },\n \"./react\": {\n \"browser\": \"./dist/react/index.mjs\",\n \"import\": \"./dist/react/index.mjs\",\n \"require\": \"./dist/react/index.js\"\n },\n \"./remix\": {\n \"browser\": \"./dist/remix/index.mjs\",\n \"import\": \"./dist/remix/index.mjs\",\n \"require\": \"./dist/remix/index.js\"\n },\n \"./sveltekit\": {\n \"types\": \"./dist/sveltekit/index.d.ts\",\n \"svelte\": \"./dist/sveltekit/index.mjs\"\n },\n \"./vue\": {\n \"browser\": \"./dist/vue/index.mjs\",\n \"import\": \"./dist/vue/index.mjs\",\n \"require\": \"./dist/vue/index.js\"\n }\n },\n \"main\": \"./dist/index.js\",\n \"types\": \"./dist/index.d.ts\",\n \"typesVersions\": {\n \"*\": {\n \"*\": [\n \"dist/index.d.ts\"\n ],\n \"react\": [\n \"dist/react/index.d.ts\"\n ],\n \"next\": [\n \"dist/next/index.d.ts\"\n ],\n \"nuxt\": [\n \"dist/nuxt/index.d.ts\"\n ],\n \"nuxt/runtime\": [\n \"dist/nuxt/runtime/index.d.ts\"\n ],\n \"remix\": [\n \"dist/remix/index.d.ts\"\n ],\n \"sveltekit\": [\n \"dist/sveltekit/index.d.ts\"\n ],\n \"vue\": [\n \"dist/vue/index.d.ts\"\n ]\n }\n },\n \"scripts\": {\n \"build\": \"tsup && pnpm copy-astro\",\n \"copy-astro\": \"cp -R src/astro dist/\",\n \"dev\": \"pnpm copy-astro && tsup --watch\",\n \"test\": \"vitest\"\n },\n \"devDependencies\": {\n \"@nuxt/kit\": \"^4.2.0\",\n \"@nuxt/schema\": \"^4.2.0\",\n \"@remix-run/react\": \"^2.14.0\",\n \"@sveltejs/kit\": \"^2.8.1\",\n \"@swc/core\": \"^1.9.2\",\n \"@testing-library/jest-dom\": \"^6.6.3\",\n \"@testing-library/react\": \"^16.0.1\",\n \"@types/node\": \"^22.9.1\",\n \"@types/react\": \"^18.3.12\",\n \"copyfiles\": \"^2.4.1\",\n \"jsdom\": \"^25.0.1\",\n \"next\": \"^14.0.4\",\n \"react\": \"^18.3.1\",\n \"react-dom\": \"^18.3.1\",\n \"svelte\": \"^5.2.7\",\n \"tsup\": \"8.3.5\",\n \"vitest\": \"^2.1.5\",\n \"vue\": \"^3.5.13\",\n \"vue-router\": \"^4.4.5\"\n },\n \"peerDependencies\": {\n \"@sveltejs/kit\": \"^1 || ^2\",\n \"next\": \">= 13\",\n \"nuxt\": \">= 3\",\n \"react\": \"^18 || ^19 || ^19.0.0-rc\",\n \"svelte\": \">= 4\",\n \"vue\": \"^3\",\n \"vue-router\": \"^4\"\n },\n \"peerDependenciesMeta\": {\n \"@sveltejs/kit\": {\n \"optional\": true\n },\n \"next\": {\n \"optional\": true\n },\n \"nuxt\": {\n \"optional\": true\n },\n \"react\": {\n \"optional\": true\n },\n \"svelte\": {\n \"optional\": true\n },\n \"vue\": {\n \"optional\": true\n },\n \"vue-router\": {\n \"optional\": true\n }\n }\n}\n","import { name as packageName, version } from '../package.json';\nimport type {\n BeforeSend,\n InjectSpeedInsightsProps,\n SpeedInsightsProps,\n} from './types';\n\nexport function isBrowser(): boolean {\n return typeof window !== 'undefined';\n}\n\nfunction detectEnvironment(): 'development' | 'production' {\n try {\n const env = process.env.NODE_ENV;\n if (env === 'development' || env === 'test') {\n return 'development';\n }\n } catch {\n // do nothing, this is okay\n }\n return 'production';\n}\n\nexport function isProduction(): boolean {\n return detectEnvironment() === 'production';\n}\n\nexport function isDevelopment(): boolean {\n return detectEnvironment() === 'development';\n}\n\nexport function computeRoute(\n pathname: string | null,\n pathParams: Record<string, string | string[]> | null,\n): string | null {\n if (!pathname || !pathParams) {\n return pathname;\n }\n\n let result = pathname;\n try {\n const entries = Object.entries(pathParams);\n // simple keys must be handled first\n for (const [key, value] of entries) {\n if (!Array.isArray(value)) {\n const matcher = turnValueToRegExp(value);\n if (matcher.test(result)) {\n result = result.replace(matcher, `/[${key}]`);\n }\n }\n }\n // array values next\n for (const [key, value] of entries) {\n if (Array.isArray(value)) {\n const matcher = turnValueToRegExp(value.join('/'));\n if (matcher.test(result)) {\n result = result.replace(matcher, `/[...${key}]`);\n }\n }\n }\n return result;\n } catch {\n return pathname;\n }\n}\n\nfunction turnValueToRegExp(value: string): RegExp {\n return new RegExp(`/${escapeRegExp(value)}(?=[/?#]|$)`);\n}\n\nfunction escapeRegExp(string: string): string {\n return string.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n}\n\nfunction getScriptSrc(\n props: SpeedInsightsProps & { basePath?: string },\n): string {\n if (props.scriptSrc) {\n return makeAbsolute(props.scriptSrc);\n }\n if (isDevelopment()) {\n return 'https://va.vercel-scripts.com/v1/speed-insights/script.debug.js';\n }\n if (props.dsn) {\n return 'https://va.vercel-scripts.com/v1/speed-insights/script.js';\n }\n if (props.basePath) {\n return makeAbsolute(`${props.basePath}/speed-insights/script.js`);\n }\n return '/_vercel/speed-insights/script.js';\n}\n\nexport function loadProps(\n explicitProps: InjectSpeedInsightsProps,\n confString?: string,\n): {\n src: string;\n beforeSend?: BeforeSend;\n dataset: Record<string, string>;\n} {\n let props = explicitProps;\n if (confString) {\n try {\n props = {\n ...(JSON.parse(confString)\n ?.speedInsights as Partial<SpeedInsightsProps>),\n ...explicitProps,\n };\n } catch {\n // Invalid JSON, use only explicit props\n }\n }\n\n const dataset: Record<string, string> = {\n sdkn: packageName + (props.framework ? `/${props.framework}` : ''),\n sdkv: version,\n };\n\n if (props.sampleRate) {\n dataset.sampleRate = props.sampleRate.toString();\n }\n if (props.route) {\n dataset.route = props.route;\n }\n if (isDevelopment() && props.debug === false) {\n dataset.debug = 'false';\n }\n if (props.dsn) {\n dataset.dsn = props.dsn;\n }\n\n if (props.endpoint) {\n dataset.endpoint = makeAbsolute(props.endpoint);\n } else if (props.basePath) {\n // backward compatibility\n dataset.endpoint = makeAbsolute(`${props.basePath}/speed-insights/vitals`);\n }\n\n return {\n src: getScriptSrc(props),\n beforeSend: props.beforeSend,\n dataset,\n };\n}\n\nfunction makeAbsolute(url: string): string {\n return url.startsWith('http://') ||\n url.startsWith('https://') ||\n url.startsWith('/')\n ? url\n : `/${url}`;\n}\n","import { initQueue } from './queue';\nimport type {\n BeforeSend,\n InjectSpeedInsightsProps,\n SpeedInsightsProps,\n} from './types';\nimport { computeRoute, isBrowser, loadProps } from './utils';\n\n/**\n * Injects the Vercel Speed Insights script into the page head and starts tracking page views. Read more in our [documentation](https://vercel.com/docs/speed-insights).\n * @param [props] - Speed Insights options.\n * @param [props.debug] - Whether to enable debug logging in development. Defaults to `true`.\n * @param [props.beforeSend] - A middleware function to modify events before they are sent. Should return the event object or `null` to cancel the event.\n * @param [props.sampleRate] - When setting to 0.5, 50% of the events will be sent to Vercel Speed Insights. Defaults to `1`.\n * @param [props.route] - The dynamic route of the page.\n * @param [props.dsn] - The DSN of the project to send events to. Only required when self-hosting.\n * @param [confString] - an optional JSON string (InjectSpeedInsightsProps) containing the default configuration. Explicit props will take over any provided default.\n */\nfunction injectSpeedInsights(\n props: InjectSpeedInsightsProps = {},\n confString?: string,\n): {\n setRoute: (route: string | null) => void;\n} | null {\n // When route is null, it means that pages router is not ready yet. Will resolve soon\n if (!isBrowser() || props.route === null) return null;\n\n initQueue();\n\n const { beforeSend, src, dataset } = loadProps(props, confString);\n\n if (document.head.querySelector(`script[src*=\"${src}\"]`)) return null;\n\n if (beforeSend) {\n window.si?.('beforeSend', beforeSend);\n }\n\n const script = document.createElement('script');\n script.src = src;\n script.defer = true;\n\n // Apply all dataset attributes from loadProps\n for (const [key, value] of Object.entries(dataset)) {\n script.dataset[key] = value;\n }\n\n script.onerror = (): void => {\n console.log(\n `[Vercel Speed Insights] Failed to load script from ${src}. Please check if any content blockers are enabled and try again.`,\n );\n };\n\n document.head.appendChild(script);\n\n return {\n setRoute: (route: string | null): void => {\n script.dataset.route = route ?? undefined;\n },\n };\n}\n\nexport { injectSpeedInsights, computeRoute };\nexport type { SpeedInsightsProps, BeforeSend as BeforeSendMiddleware };\n\nexport default {\n injectSpeedInsights,\n computeRoute,\n};\n","// !! important !!\n// do not access env variables using import.meta.env[varname]\n// some bundlers won't replace the value at build time.\n\nexport function getBasePath(): string | undefined {\n try {\n return import.meta.env.VITE_VERCEL_OBSERVABILITY_BASEPATH as\n | string\n | undefined;\n } catch {\n // do nothing\n }\n}\n\nexport function getConfigString(): string | undefined {\n try {\n return import.meta.env.VITE_VERCEL_OBSERVABILITY_CLIENT_CONFIG as\n | string\n | undefined;\n } catch {\n // do nothing\n }\n}\n"],"mappings":";AACA,SAAS,WAAW;AACpB,SAAS,eAAe;AACxB,SAAS,YAAY;;;ACHd,IAAM,YAAY,MAAY;AAEnC,MAAI,OAAO,GAAI;AAEf,SAAO,KAAK,SAAS,KAAK,QAAc;AACtC,WAAO,MAAM,OAAO,OAAO,CAAC;AAC5B,WAAO,IAAI,KAAK,MAAM;AAAA,EACxB;AACF;;;ACPE,WAAQ;AACR,cAAW;;;ACKN,SAAS,YAAqB;AACnC,SAAO,OAAO,WAAW;AAC3B;AAEA,SAAS,oBAAkD;AACzD,MAAI;AACF,UAAM,MAAM,QAAQ,IAAI;AACxB,QAAI,QAAQ,iBAAiB,QAAQ,QAAQ;AAC3C,aAAO;AAAA,IACT;AAAA,EACF,QAAQ;AAAA,EAER;AACA,SAAO;AACT;AAMO,SAAS,gBAAyB;AACvC,SAAO,kBAAkB,MAAM;AACjC;AA6CA,SAAS,aACP,OACQ;AACR,MAAI,MAAM,WAAW;AACnB,WAAO,aAAa,MAAM,SAAS;AAAA,EACrC;AACA,MAAI,cAAc,GAAG;AACnB,WAAO;AAAA,EACT;AACA,MAAI,MAAM,KAAK;AACb,WAAO;AAAA,EACT;AACA,MAAI,MAAM,UAAU;AAClB,WAAO,aAAa,GAAG,MAAM,QAAQ,2BAA2B;AAAA,EAClE;AACA,SAAO;AACT;AAEO,SAAS,UACd,eACA,YAKA;A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AAY,cAAc;AAAA,EAC5B;AAEA,QAAM,SAAS,SAAS,cAAc,QAAQ;AAC9C,SAAO,MAAM;AACb,SAAO,QAAQ;AAGf,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,OAAO,GAAG;AAClD,WAAO,QAAQ,GAAG,IAAI;AAAA,EACxB;AAEA,SAAO,UAAU,MAAY;AAC3B,YAAQ;AAAA,MACN,sDAAsD,GAAG;AAAA,IAC3D;AAAA,EACF;AAEA,WAAS,KAAK,YAAY,MAAM;AAEhC,SAAO;AAAA,IACL,UAAU,CAAC,UAA+B;AACxC,aAAO,QAAQ,QAAQ,SAAS;AAAA,IAClC;AAAA,EACF;AACF;;;ACvDO,SAAS,cAAkC;AAChD,MAAI;AACF,WAAO,YAAY,IAAI;AAAA,EAGzB,QAAQ;AAAA,EAER;AACF;AAEO,SAAS,kBAAsC;AACpD,MAAI;AACF,WAAO,YAAY,IAAI;AAAA,EAGzB,QAAQ;AAAA,EAER;AACF;;;ALdO,SAASA,qBACd,QAA+C,CAAC,GAC1C;AAVR;AAWE,MAAI,SAAS;AACX,UAAM,gBAAgB;AAAA,MACpB;AAAA,QACE,QAAO,SAAI,IAAI,EAAE,UAAV,mBAAiB;AAAA,QACxB,GAAG;AAAA,QACH,WAAW;AAAA,QACX,UAAU,YAAY;AAAA,MACxB;AAAA,MACA,gBAAgB;AAAA,IAClB;AAEA,QAAI,eAAe;AACjB,WAAK,UAAU,CAAC,EAAE,MAAM,MAAM;AAC5B,YAAI,+BAAO,IAAI;AACb,wBAAc,SAAS,MAAM,EAAE;AAAA,QACjC;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AACF;","names":["injectSpeedInsights"]}
@@ -1,3 +1,38 @@
1
+ interface SpeedInsightsProps {
2
+ dsn?: string;
3
+ sampleRate?: number;
4
+ route?: string | null;
5
+ beforeSend?: BeforeSend;
6
+ debug?: boolean;
7
+ scriptSrc?: string;
8
+ endpoint?: string;
9
+ }
10
+ type EventTypes = 'vital';
11
+ interface BeforeSendEvent {
12
+ type: EventTypes;
13
+ url: string;
14
+ route?: string;
15
+ }
16
+ type BeforeSend = (event: BeforeSendEvent) => BeforeSendEvent | null | undefined | false;
17
+ interface Functions {
18
+ beforeSend?: BeforeSend;
19
+ }
20
+ interface SpeedInsights$1<T extends keyof Functions = keyof Functions> {
21
+ queue: [T, Functions[T]][];
22
+ addAction: (action: T, data: Functions[T]) => void;
23
+ }
24
+ declare global {
25
+ interface Window {
26
+ /** Base interface to track events */
27
+ si?: SpeedInsights$1['addAction'];
28
+ /** Queue for speed insights datapoints, before the library is loaded */
29
+ siq?: SpeedInsights$1['queue'];
30
+ sil?: boolean;
31
+ /** used by Astro component only */
32
+ speedInsightsBeforeSend?: BeforeSend;
33
+ }
34
+ }
35
+
1
36
  declare const SpeedInsights: any;
2
37
 
3
- export { SpeedInsights };
38
+ export { type BeforeSend, type BeforeSendEvent, SpeedInsights, type SpeedInsightsProps };
@@ -1,3 +1,38 @@
1
+ interface SpeedInsightsProps {
2
+ dsn?: string;
3
+ sampleRate?: number;
4
+ route?: string | null;
5
+ beforeSend?: BeforeSend;
6
+ debug?: boolean;
7
+ scriptSrc?: string;
8
+ endpoint?: string;
9
+ }
10
+ type EventTypes = 'vital';
11
+ interface BeforeSendEvent {
12
+ type: EventTypes;
13
+ url: string;
14
+ route?: string;
15
+ }
16
+ type BeforeSend = (event: BeforeSendEvent) => BeforeSendEvent | null | undefined | false;
17
+ interface Functions {
18
+ beforeSend?: BeforeSend;
19
+ }
20
+ interface SpeedInsights$1<T extends keyof Functions = keyof Functions> {
21
+ queue: [T, Functions[T]][];
22
+ addAction: (action: T, data: Functions[T]) => void;
23
+ }
24
+ declare global {
25
+ interface Window {
26
+ /** Base interface to track events */
27
+ si?: SpeedInsights$1['addAction'];
28
+ /** Queue for speed insights datapoints, before the library is loaded */
29
+ siq?: SpeedInsights$1['queue'];
30
+ sil?: boolean;
31
+ /** used by Astro component only */
32
+ speedInsightsBeforeSend?: BeforeSend;
33
+ }
34
+ }
35
+
1
36
  declare const SpeedInsights: any;
2
37
 
3
- export { SpeedInsights };
38
+ export { type BeforeSend, type BeforeSendEvent, SpeedInsights, type SpeedInsightsProps };