@vercel/speed-insights 0.0.1 → 0.0.3-beta.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/README.md CHANGED
@@ -1,4 +1,4 @@
1
- ![Speed Insights](https://github.com/vercel/analytics/blob/main/.github/banner.png)
1
+ ![Speed Insights](https://github.com/vercel/speed-insights/blob/main/.github/banner.png)
2
2
 
3
3
  <div align="center"><strong>Vercel Speed Insights</strong></div>
4
4
  <div align="center">Performance insights for your website</div>
@@ -0,0 +1,49 @@
1
+ interface SpeedInsightsProps {
2
+ token?: string;
3
+ sampleRate?: number;
4
+ route?: string | null;
5
+ beforeSend?: BeforeSendMiddleware;
6
+ debug?: boolean;
7
+ scriptSrc?: string;
8
+ endpoint?: string;
9
+ }
10
+ type EventTypes = 'vital';
11
+ interface Event {
12
+ type: EventTypes;
13
+ url: string;
14
+ }
15
+ type BeforeSendMiddleware = (data: Event) => Event | null | undefined | false;
16
+ interface Functions {
17
+ beforeSend?: BeforeSendMiddleware;
18
+ }
19
+ interface SpeedInsights<T extends keyof Functions = keyof Functions> {
20
+ queue: [T, Functions[T]][];
21
+ addAction: (action: T, data: Functions[T]) => void;
22
+ }
23
+ declare global {
24
+ interface Window {
25
+ /** Base interface to track events */
26
+ si?: SpeedInsights['addAction'];
27
+ /** Queue for speed insights datapoints, before the library is loaded */
28
+ siq?: SpeedInsights['queue'];
29
+ sil?: boolean;
30
+ }
31
+ }
32
+
33
+ /**
34
+ * 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).
35
+ * @param [props] - Speed Insights options.
36
+ * @param [props.debug] - Whether to enable debug logging in development. Defaults to `true`.
37
+ * @param [props.beforeSend] - A middleware function to modify events before they are sent. Should return the event object or `null` to cancel the event.
38
+ */
39
+ declare function inject(props: SpeedInsightsProps & {
40
+ framework?: string;
41
+ }): {
42
+ setRoute: (route: string) => void;
43
+ } | null;
44
+
45
+ declare const _default: {
46
+ inject: typeof inject;
47
+ };
48
+
49
+ export { SpeedInsightsProps, _default as default, inject };
@@ -0,0 +1,49 @@
1
+ interface SpeedInsightsProps {
2
+ token?: string;
3
+ sampleRate?: number;
4
+ route?: string | null;
5
+ beforeSend?: BeforeSendMiddleware;
6
+ debug?: boolean;
7
+ scriptSrc?: string;
8
+ endpoint?: string;
9
+ }
10
+ type EventTypes = 'vital';
11
+ interface Event {
12
+ type: EventTypes;
13
+ url: string;
14
+ }
15
+ type BeforeSendMiddleware = (data: Event) => Event | null | undefined | false;
16
+ interface Functions {
17
+ beforeSend?: BeforeSendMiddleware;
18
+ }
19
+ interface SpeedInsights<T extends keyof Functions = keyof Functions> {
20
+ queue: [T, Functions[T]][];
21
+ addAction: (action: T, data: Functions[T]) => void;
22
+ }
23
+ declare global {
24
+ interface Window {
25
+ /** Base interface to track events */
26
+ si?: SpeedInsights['addAction'];
27
+ /** Queue for speed insights datapoints, before the library is loaded */
28
+ siq?: SpeedInsights['queue'];
29
+ sil?: boolean;
30
+ }
31
+ }
32
+
33
+ /**
34
+ * 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).
35
+ * @param [props] - Speed Insights options.
36
+ * @param [props.debug] - Whether to enable debug logging in development. Defaults to `true`.
37
+ * @param [props.beforeSend] - A middleware function to modify events before they are sent. Should return the event object or `null` to cancel the event.
38
+ */
39
+ declare function inject(props: SpeedInsightsProps & {
40
+ framework?: string;
41
+ }): {
42
+ setRoute: (route: string) => void;
43
+ } | null;
44
+
45
+ declare const _default: {
46
+ inject: typeof inject;
47
+ };
48
+
49
+ export { SpeedInsightsProps, _default as default, inject };
package/dist/index.js ADDED
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name2 in all)
8
+ __defProp(target, name2, { get: all[name2], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/generic.ts
21
+ var generic_exports = {};
22
+ __export(generic_exports, {
23
+ default: () => generic_default,
24
+ inject: () => inject
25
+ });
26
+ module.exports = __toCommonJS(generic_exports);
27
+
28
+ // package.json
29
+ var name = "@vercel/speed-insights";
30
+ var version = "0.0.3-beta.0";
31
+
32
+ // src/queue.ts
33
+ var initQueue = () => {
34
+ if (window.si)
35
+ return;
36
+ window.si = function a(...params) {
37
+ (window.siq = window.siq || []).push(params);
38
+ };
39
+ };
40
+
41
+ // src/utils.ts
42
+ function isBrowser() {
43
+ return typeof window !== "undefined";
44
+ }
45
+ function detectEnvironment() {
46
+ try {
47
+ const env = process.env.NODE_ENV;
48
+ if (env === "development" || env === "test") {
49
+ return "development";
50
+ }
51
+ } catch (e) {
52
+ }
53
+ return "production";
54
+ }
55
+ function isDevelopment() {
56
+ return detectEnvironment() === "development";
57
+ }
58
+
59
+ // src/generic.ts
60
+ var DEV_SCRIPT_URL = `https://va.vercel-scripts.com/v1/speed-insights/script.debug.js`;
61
+ var SCRIPT_URL = `/_vercel/speed-insights/script.js`;
62
+ function inject(props) {
63
+ var _a;
64
+ if (!isBrowser() || props.route === null)
65
+ return null;
66
+ initQueue();
67
+ const src = props.scriptSrc || (isDevelopment() ? DEV_SCRIPT_URL : SCRIPT_URL);
68
+ if (document.head.querySelector(`script[src*="${src}"]`))
69
+ return null;
70
+ if (props.beforeSend) {
71
+ (_a = window.si) == null ? void 0 : _a.call(window, "beforeSend", props.beforeSend);
72
+ }
73
+ const script = document.createElement("script");
74
+ script.src = src;
75
+ script.defer = true;
76
+ script.dataset.sdkn = name + (props.framework ? `/${props.framework}` : "");
77
+ script.dataset.sdkv = version;
78
+ if (props.sampleRate) {
79
+ script.dataset.sampleRate = props.sampleRate.toString();
80
+ }
81
+ if (props.route) {
82
+ script.dataset.route = props.route;
83
+ }
84
+ if (props.endpoint) {
85
+ script.dataset.endpoint = props.endpoint;
86
+ }
87
+ if (props.token) {
88
+ script.dataset.token = props.token;
89
+ }
90
+ if (isDevelopment() && props.debug === false) {
91
+ script.dataset.debug = "false";
92
+ }
93
+ script.onerror = () => {
94
+ const errorMessage = isDevelopment() ? "Please check if any ad blockers are enabled and try again." : "Be sure to enable Speed Insights for your project and deploy again. See https://vercel.com/docs/speed-insights for more information.";
95
+ console.log(
96
+ `[Vercel Speed Insights] Failed to load script from ${src}. ${errorMessage}`
97
+ );
98
+ };
99
+ document.head.appendChild(script);
100
+ return {
101
+ setRoute: (route) => {
102
+ script.dataset.route = route;
103
+ }
104
+ };
105
+ }
106
+ var generic_default = {
107
+ inject
108
+ };
109
+ // Annotate the CommonJS export names for ESM import in node:
110
+ 0 && (module.exports = {
111
+ inject
112
+ });
113
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/generic.ts","../package.json","../src/queue.ts","../src/utils.ts"],"sourcesContent":["import { name as packageName, version } from '../package.json';\nimport { initQueue } from './queue';\nimport type { SpeedInsightsProps } from './types';\nimport { isBrowser, isDevelopment } from './utils';\n\nconst DEV_SCRIPT_URL = `https://va.vercel-scripts.com/v1/speed-insights/script.debug.js`;\nconst SCRIPT_URL = `/_vercel/speed-insights/script.js`;\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 */\nfunction inject(\n props: SpeedInsightsProps & {\n framework?: string;\n },\n): {\n setRoute: (route: string) => 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 =\n props.scriptSrc || (isDevelopment() ? DEV_SCRIPT_URL : SCRIPT_URL);\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 }\n if (props.token) {\n script.dataset.token = props.token;\n }\n if (isDevelopment() && props.debug === false) {\n script.dataset.debug = 'false';\n }\n\n script.onerror = (): void => {\n const errorMessage = isDevelopment()\n ? 'Please check if any ad blockers are enabled and try again.'\n : 'Be sure to enable Speed Insights for your project and deploy again. See https://vercel.com/docs/speed-insights for more information.';\n\n // eslint-disable-next-line no-console -- Logging is okay here\n console.log(\n `[Vercel Speed Insights] Failed to load script from ${src}. ${errorMessage}`,\n );\n };\n\n document.head.appendChild(script);\n\n return {\n setRoute: (route: string): void => {\n script.dataset.route = route;\n },\n };\n}\n\nexport { inject };\nexport type { SpeedInsightsProps };\n\n// eslint-disable-next-line import/no-default-export -- Allow default export\nexport default {\n inject,\n};\n","{\n \"name\": \"@vercel/speed-insights\",\n \"version\": \"0.0.3-beta.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\": \"MPL-2.0\",\n \"exports\": {\n \"./package.json\": \"./package.json\",\n \".\": {\n \"browser\": \"./dist/index.js\",\n \"import\": \"./dist/index.js\",\n \"require\": \"./dist/index.cjs\"\n },\n \"./next\": {\n \"browser\": \"./dist/next/index.js\",\n \"import\": \"./dist/next/index.js\",\n \"require\": \"./dist/next/index.cjs\"\n },\n \"./remix\": {\n \"browser\": \"./dist/remix/index.js\",\n \"import\": \"./dist/remix/index.js\",\n \"require\": \"./dist/remix/index.cjs\"\n },\n \"./sveltekit\": {\n \"svelte\": \"./dist/sveltekit/index.mjs\",\n \"types\": \"./dist/sveltekit/index.d.ts\"\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 \"remix\": [\n \"dist/remix/index.d.ts\"\n ],\n \"sveltekit\": [\n \"dist/sveltekit/index.d.ts\"\n ]\n }\n },\n \"scripts\": {\n \"build\": \"tsup \",\n \"dev\": \"tsup --watch\",\n \"postinstall\": \"node scripts/postinstall.mjs\",\n \"lint\": \"eslint .\",\n \"lint-fix\": \"eslint . --fix\",\n \"test\": \"jest\",\n \"type-check\": \"tsc --noEmit\"\n },\n \"devDependencies\": {\n \"@remix-run/react\": \"^2.0.1\",\n \"@sveltejs/kit\": \"^1.20.4\",\n \"@swc/core\": \"^1.3.82\",\n \"@swc/jest\": \"^0.2.29\",\n \"@testing-library/jest-dom\": \"^6.1.2\",\n \"@testing-library/react\": \"^14.0.0\",\n \"@types/jest\": \"^29.5.4\",\n \"@types/node\": \"^20.5.9\",\n \"@types/react\": \"^18.2.21\",\n \"copyfiles\": \"^2.4.1\",\n \"jest\": \"^29.6.4\",\n \"jest-environment-jsdom\": \"^29.6.4\",\n \"next\": \"^13.4.19\",\n \"react\": \"^18.2.0\",\n \"react-dom\": \"^18.2.0\",\n \"svelte\": \"^4.0.5\",\n \"tsup\": \"7.2.0\"\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","export 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\n for (const [key, valueOrArray] of Object.entries(pathParams)) {\n const isValueArray = Array.isArray(valueOrArray);\n const value = isValueArray ? valueOrArray.join('/') : valueOrArray;\n const expr = isValueArray ? `...${key}` : key;\n\n const matcher = new RegExp(`/${value}(?=[/?#]|$)`);\n if (matcher.test(result)) {\n result = result.replace(matcher, `/[${expr}]`);\n }\n }\n\n return result;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCE,WAAQ;AACR,cAAW;;;ACFN,IAAM,YAAY,MAAY;AAEnC,MAAI,OAAO;AAAI;AAEf,SAAO,KAAK,SAAS,KAAK,QAAc;AACtC,KAAC,OAAO,MAAM,OAAO,OAAO,CAAC,GAAG,KAAK,MAAM;AAAA,EAC7C;AACF;;;ACPO,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;;;AHjBA,IAAM,iBAAiB;AACvB,IAAM,aAAa;AAQnB,SAAS,OACP,OAKO;AApBT;AAsBE,MAAI,CAAC,UAAU,KAAK,MAAM,UAAU;AAAM,WAAO;AAEjD,YAAU;AAEV,QAAM,MACJ,MAAM,cAAc,cAAc,IAAI,iBAAiB;AAEzD,MAAI,SAAS,KAAK,cAAc,gBAAgB,GAAG,IAAI;AAAG,WAAO;AAEjE,MAAI,MAAM,YAAY;AACpB,iBAAO,OAAP,gCAAY,cAAc,MAAM;AAAA,EAClC;AAEA,QAAM,SAAS,SAAS,cAAc,QAAQ;AAC9C,SAAO,MAAM;AACb,SAAO,QAAQ;AACf,SAAO,QAAQ,OACb,QAAe,MAAM,YAAY,IAAI,MAAM,SAAS,KAAK;AAC3D,SAAO,QAAQ,OAAO;AAEtB,MAAI,MAAM,YAAY;AACpB,WAAO,QAAQ,aAAa,MAAM,WAAW,SAAS;AAAA,EACxD;AACA,MAAI,MAAM,OAAO;AACf,WAAO,QAAQ,QAAQ,MAAM;AAAA,EAC/B;AACA,MAAI,MAAM,UAAU;AAClB,WAAO,QAAQ,WAAW,MAAM;AAAA,EAClC;AACA,MAAI,MAAM,OAAO;AACf,WAAO,QAAQ,QAAQ,MAAM;AAAA,EAC/B;AACA,MAAI,cAAc,KAAK,MAAM,UAAU,OAAO;AAC5C,WAAO,QAAQ,QAAQ;AAAA,EACzB;AAEA,SAAO,UAAU,MAAY;AAC3B,UAAM,eAAe,cAAc,IAC/B,+DACA;AAGJ,YAAQ;AAAA,MACN,sDAAsD,GAAG,KAAK,YAAY;AAAA,IAC5E;AAAA,EACF;AAEA,WAAS,KAAK,YAAY,MAAM;AAEhC,SAAO;AAAA,IACL,UAAU,CAAC,UAAwB;AACjC,aAAO,QAAQ,QAAQ;AAAA,IACzB;AAAA,EACF;AACF;AAMA,IAAO,kBAAQ;AAAA,EACb;AACF;","names":[]}
package/dist/index.mjs ADDED
@@ -0,0 +1,86 @@
1
+ // package.json
2
+ var name = "@vercel/speed-insights";
3
+ var version = "0.0.3-beta.0";
4
+
5
+ // src/queue.ts
6
+ var initQueue = () => {
7
+ if (window.si)
8
+ return;
9
+ window.si = function a(...params) {
10
+ (window.siq = window.siq || []).push(params);
11
+ };
12
+ };
13
+
14
+ // src/utils.ts
15
+ function isBrowser() {
16
+ return typeof window !== "undefined";
17
+ }
18
+ function detectEnvironment() {
19
+ try {
20
+ const env = process.env.NODE_ENV;
21
+ if (env === "development" || env === "test") {
22
+ return "development";
23
+ }
24
+ } catch (e) {
25
+ }
26
+ return "production";
27
+ }
28
+ function isDevelopment() {
29
+ return detectEnvironment() === "development";
30
+ }
31
+
32
+ // src/generic.ts
33
+ var DEV_SCRIPT_URL = `https://va.vercel-scripts.com/v1/speed-insights/script.debug.js`;
34
+ var SCRIPT_URL = `/_vercel/speed-insights/script.js`;
35
+ function inject(props) {
36
+ var _a;
37
+ if (!isBrowser() || props.route === null)
38
+ return null;
39
+ initQueue();
40
+ const src = props.scriptSrc || (isDevelopment() ? DEV_SCRIPT_URL : SCRIPT_URL);
41
+ if (document.head.querySelector(`script[src*="${src}"]`))
42
+ return null;
43
+ if (props.beforeSend) {
44
+ (_a = window.si) == null ? void 0 : _a.call(window, "beforeSend", props.beforeSend);
45
+ }
46
+ const script = document.createElement("script");
47
+ script.src = src;
48
+ script.defer = true;
49
+ script.dataset.sdkn = name + (props.framework ? `/${props.framework}` : "");
50
+ script.dataset.sdkv = version;
51
+ if (props.sampleRate) {
52
+ script.dataset.sampleRate = props.sampleRate.toString();
53
+ }
54
+ if (props.route) {
55
+ script.dataset.route = props.route;
56
+ }
57
+ if (props.endpoint) {
58
+ script.dataset.endpoint = props.endpoint;
59
+ }
60
+ if (props.token) {
61
+ script.dataset.token = props.token;
62
+ }
63
+ if (isDevelopment() && props.debug === false) {
64
+ script.dataset.debug = "false";
65
+ }
66
+ script.onerror = () => {
67
+ const errorMessage = isDevelopment() ? "Please check if any ad blockers are enabled and try again." : "Be sure to enable Speed Insights for your project and deploy again. See https://vercel.com/docs/speed-insights for more information.";
68
+ console.log(
69
+ `[Vercel Speed Insights] Failed to load script from ${src}. ${errorMessage}`
70
+ );
71
+ };
72
+ document.head.appendChild(script);
73
+ return {
74
+ setRoute: (route) => {
75
+ script.dataset.route = route;
76
+ }
77
+ };
78
+ }
79
+ var generic_default = {
80
+ inject
81
+ };
82
+ export {
83
+ generic_default as default,
84
+ inject
85
+ };
86
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../package.json","../src/queue.ts","../src/utils.ts","../src/generic.ts"],"sourcesContent":["{\n \"name\": \"@vercel/speed-insights\",\n \"version\": \"0.0.3-beta.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\": \"MPL-2.0\",\n \"exports\": {\n \"./package.json\": \"./package.json\",\n \".\": {\n \"browser\": \"./dist/index.js\",\n \"import\": \"./dist/index.js\",\n \"require\": \"./dist/index.cjs\"\n },\n \"./next\": {\n \"browser\": \"./dist/next/index.js\",\n \"import\": \"./dist/next/index.js\",\n \"require\": \"./dist/next/index.cjs\"\n },\n \"./remix\": {\n \"browser\": \"./dist/remix/index.js\",\n \"import\": \"./dist/remix/index.js\",\n \"require\": \"./dist/remix/index.cjs\"\n },\n \"./sveltekit\": {\n \"svelte\": \"./dist/sveltekit/index.mjs\",\n \"types\": \"./dist/sveltekit/index.d.ts\"\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 \"remix\": [\n \"dist/remix/index.d.ts\"\n ],\n \"sveltekit\": [\n \"dist/sveltekit/index.d.ts\"\n ]\n }\n },\n \"scripts\": {\n \"build\": \"tsup \",\n \"dev\": \"tsup --watch\",\n \"postinstall\": \"node scripts/postinstall.mjs\",\n \"lint\": \"eslint .\",\n \"lint-fix\": \"eslint . --fix\",\n \"test\": \"jest\",\n \"type-check\": \"tsc --noEmit\"\n },\n \"devDependencies\": {\n \"@remix-run/react\": \"^2.0.1\",\n \"@sveltejs/kit\": \"^1.20.4\",\n \"@swc/core\": \"^1.3.82\",\n \"@swc/jest\": \"^0.2.29\",\n \"@testing-library/jest-dom\": \"^6.1.2\",\n \"@testing-library/react\": \"^14.0.0\",\n \"@types/jest\": \"^29.5.4\",\n \"@types/node\": \"^20.5.9\",\n \"@types/react\": \"^18.2.21\",\n \"copyfiles\": \"^2.4.1\",\n \"jest\": \"^29.6.4\",\n \"jest-environment-jsdom\": \"^29.6.4\",\n \"next\": \"^13.4.19\",\n \"react\": \"^18.2.0\",\n \"react-dom\": \"^18.2.0\",\n \"svelte\": \"^4.0.5\",\n \"tsup\": \"7.2.0\"\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","export 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\n for (const [key, valueOrArray] of Object.entries(pathParams)) {\n const isValueArray = Array.isArray(valueOrArray);\n const value = isValueArray ? valueOrArray.join('/') : valueOrArray;\n const expr = isValueArray ? `...${key}` : key;\n\n const matcher = new RegExp(`/${value}(?=[/?#]|$)`);\n if (matcher.test(result)) {\n result = result.replace(matcher, `/[${expr}]`);\n }\n }\n\n return result;\n}\n","import { name as packageName, version } from '../package.json';\nimport { initQueue } from './queue';\nimport type { SpeedInsightsProps } from './types';\nimport { isBrowser, isDevelopment } from './utils';\n\nconst DEV_SCRIPT_URL = `https://va.vercel-scripts.com/v1/speed-insights/script.debug.js`;\nconst SCRIPT_URL = `/_vercel/speed-insights/script.js`;\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 */\nfunction inject(\n props: SpeedInsightsProps & {\n framework?: string;\n },\n): {\n setRoute: (route: string) => 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 =\n props.scriptSrc || (isDevelopment() ? DEV_SCRIPT_URL : SCRIPT_URL);\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 }\n if (props.token) {\n script.dataset.token = props.token;\n }\n if (isDevelopment() && props.debug === false) {\n script.dataset.debug = 'false';\n }\n\n script.onerror = (): void => {\n const errorMessage = isDevelopment()\n ? 'Please check if any ad blockers are enabled and try again.'\n : 'Be sure to enable Speed Insights for your project and deploy again. See https://vercel.com/docs/speed-insights for more information.';\n\n // eslint-disable-next-line no-console -- Logging is okay here\n console.log(\n `[Vercel Speed Insights] Failed to load script from ${src}. ${errorMessage}`,\n );\n };\n\n document.head.appendChild(script);\n\n return {\n setRoute: (route: string): void => {\n script.dataset.route = route;\n },\n };\n}\n\nexport { inject };\nexport type { SpeedInsightsProps };\n\n// eslint-disable-next-line import/no-default-export -- Allow default export\nexport default {\n inject,\n};\n"],"mappings":";AACE,WAAQ;AACR,cAAW;;;ACFN,IAAM,YAAY,MAAY;AAEnC,MAAI,OAAO;AAAI;AAEf,SAAO,KAAK,SAAS,KAAK,QAAc;AACtC,KAAC,OAAO,MAAM,OAAO,OAAO,CAAC,GAAG,KAAK,MAAM;AAAA,EAC7C;AACF;;;ACPO,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;;;ACjBA,IAAM,iBAAiB;AACvB,IAAM,aAAa;AAQnB,SAAS,OACP,OAKO;AApBT;AAsBE,MAAI,CAAC,UAAU,KAAK,MAAM,UAAU;AAAM,WAAO;AAEjD,YAAU;AAEV,QAAM,MACJ,MAAM,cAAc,cAAc,IAAI,iBAAiB;AAEzD,MAAI,SAAS,KAAK,cAAc,gBAAgB,GAAG,IAAI;AAAG,WAAO;AAEjE,MAAI,MAAM,YAAY;AACpB,iBAAO,OAAP,gCAAY,cAAc,MAAM;AAAA,EAClC;AAEA,QAAM,SAAS,SAAS,cAAc,QAAQ;AAC9C,SAAO,MAAM;AACb,SAAO,QAAQ;AACf,SAAO,QAAQ,OACb,QAAe,MAAM,YAAY,IAAI,MAAM,SAAS,KAAK;AAC3D,SAAO,QAAQ,OAAO;AAEtB,MAAI,MAAM,YAAY;AACpB,WAAO,QAAQ,aAAa,MAAM,WAAW,SAAS;AAAA,EACxD;AACA,MAAI,MAAM,OAAO;AACf,WAAO,QAAQ,QAAQ,MAAM;AAAA,EAC/B;AACA,MAAI,MAAM,UAAU;AAClB,WAAO,QAAQ,WAAW,MAAM;AAAA,EAClC;AACA,MAAI,MAAM,OAAO;AACf,WAAO,QAAQ,QAAQ,MAAM;AAAA,EAC/B;AACA,MAAI,cAAc,KAAK,MAAM,UAAU,OAAO;AAC5C,WAAO,QAAQ,QAAQ;AAAA,EACzB;AAEA,SAAO,UAAU,MAAY;AAC3B,UAAM,eAAe,cAAc,IAC/B,+DACA;AAGJ,YAAQ;AAAA,MACN,sDAAsD,GAAG,KAAK,YAAY;AAAA,IAC5E;AAAA,EACF;AAEA,WAAS,KAAK,YAAY,MAAM;AAEhC,SAAO;AAAA,IACL,UAAU,CAAC,UAAwB;AACjC,aAAO,QAAQ,QAAQ;AAAA,IACzB;AAAA,EACF;AACF;AAMA,IAAO,kBAAQ;AAAA,EACb;AACF;","names":[]}
@@ -0,0 +1,35 @@
1
+ interface SpeedInsightsProps {
2
+ token?: string;
3
+ sampleRate?: number;
4
+ route?: string | null;
5
+ beforeSend?: BeforeSendMiddleware;
6
+ debug?: boolean;
7
+ scriptSrc?: string;
8
+ endpoint?: string;
9
+ }
10
+ type EventTypes = 'vital';
11
+ interface Event {
12
+ type: EventTypes;
13
+ url: string;
14
+ }
15
+ type BeforeSendMiddleware = (data: Event) => Event | null | undefined | false;
16
+ interface Functions {
17
+ beforeSend?: BeforeSendMiddleware;
18
+ }
19
+ interface SpeedInsights$1<T extends keyof Functions = keyof Functions> {
20
+ queue: [T, Functions[T]][];
21
+ addAction: (action: T, data: Functions[T]) => void;
22
+ }
23
+ declare global {
24
+ interface Window {
25
+ /** Base interface to track events */
26
+ si?: SpeedInsights$1['addAction'];
27
+ /** Queue for speed insights datapoints, before the library is loaded */
28
+ siq?: SpeedInsights$1['queue'];
29
+ sil?: boolean;
30
+ }
31
+ }
32
+
33
+ declare function SpeedInsights(props: Omit<SpeedInsightsProps, 'route'>): JSX.Element;
34
+
35
+ export { SpeedInsights };
@@ -1,7 +1,35 @@
1
1
  interface SpeedInsightsProps {
2
- token: string;
2
+ token?: string;
3
3
  sampleRate?: number;
4
+ route?: string | null;
5
+ beforeSend?: BeforeSendMiddleware;
6
+ debug?: boolean;
7
+ scriptSrc?: string;
8
+ endpoint?: string;
4
9
  }
5
- declare function SpeedInsights({ token, sampleRate }: SpeedInsightsProps): null;
10
+ type EventTypes = 'vital';
11
+ interface Event {
12
+ type: EventTypes;
13
+ url: string;
14
+ }
15
+ type BeforeSendMiddleware = (data: Event) => Event | null | undefined | false;
16
+ interface Functions {
17
+ beforeSend?: BeforeSendMiddleware;
18
+ }
19
+ interface SpeedInsights$1<T extends keyof Functions = keyof Functions> {
20
+ queue: [T, Functions[T]][];
21
+ addAction: (action: T, data: Functions[T]) => void;
22
+ }
23
+ declare global {
24
+ interface Window {
25
+ /** Base interface to track events */
26
+ si?: SpeedInsights$1['addAction'];
27
+ /** Queue for speed insights datapoints, before the library is loaded */
28
+ siq?: SpeedInsights$1['queue'];
29
+ sil?: boolean;
30
+ }
31
+ }
32
+
33
+ declare function SpeedInsights(props: Omit<SpeedInsightsProps, 'route'>): JSX.Element;
6
34
 
7
35
  export { SpeedInsights };