@vercel/speed-insights 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,168 @@
1
+ "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/vue/index.ts
21
+ var vue_exports = {};
22
+ __export(vue_exports, {
23
+ SpeedInsights: () => SpeedInsights
24
+ });
25
+ module.exports = __toCommonJS(vue_exports);
26
+
27
+ // src/vue/create-component.ts
28
+ var import_vue = require("vue");
29
+ var import_vue_router = require("vue-router");
30
+
31
+ // package.json
32
+ var name = "@vercel/speed-insights";
33
+ var version = "0.0.6";
34
+
35
+ // src/queue.ts
36
+ var initQueue = () => {
37
+ if (window.si)
38
+ return;
39
+ window.si = function a(...params) {
40
+ (window.siq = window.siq || []).push(params);
41
+ };
42
+ };
43
+
44
+ // src/utils.ts
45
+ function isBrowser() {
46
+ return typeof window !== "undefined";
47
+ }
48
+ function detectEnvironment() {
49
+ try {
50
+ const env = process.env.NODE_ENV;
51
+ if (env === "development" || env === "test") {
52
+ return "development";
53
+ }
54
+ } catch (e) {
55
+ }
56
+ return "production";
57
+ }
58
+ function isDevelopment() {
59
+ return detectEnvironment() === "development";
60
+ }
61
+ function computeRoute(pathname, pathParams) {
62
+ if (!pathname || !pathParams) {
63
+ return pathname;
64
+ }
65
+ let result = pathname;
66
+ try {
67
+ for (const [key, valueOrArray] of Object.entries(pathParams)) {
68
+ const isValueArray = Array.isArray(valueOrArray);
69
+ const value = isValueArray ? valueOrArray.join("/") : valueOrArray;
70
+ const expr = isValueArray ? `...${key}` : key;
71
+ const matcher = new RegExp(`/${escapeRegExp(value)}(?=[/?#]|$)`);
72
+ if (matcher.test(result)) {
73
+ result = result.replace(matcher, `/[${expr}]`);
74
+ }
75
+ }
76
+ return result;
77
+ } catch (e) {
78
+ return pathname;
79
+ }
80
+ }
81
+ function escapeRegExp(string) {
82
+ return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
83
+ }
84
+
85
+ // src/generic.ts
86
+ var DEV_SCRIPT_URL = `https://va.vercel-scripts.com/v1/speed-insights/script.debug.js`;
87
+ var SCRIPT_URL = `/_vercel/speed-insights/script.js`;
88
+ function injectSpeedInsights(props) {
89
+ var _a;
90
+ if (!isBrowser() || props.route === null)
91
+ return null;
92
+ initQueue();
93
+ const src = props.scriptSrc || (isDevelopment() ? DEV_SCRIPT_URL : SCRIPT_URL);
94
+ if (document.head.querySelector(`script[src*="${src}"]`))
95
+ return null;
96
+ if (props.beforeSend) {
97
+ (_a = window.si) == null ? void 0 : _a.call(window, "beforeSend", props.beforeSend);
98
+ }
99
+ const script = document.createElement("script");
100
+ script.src = src;
101
+ script.defer = true;
102
+ script.dataset.sdkn = name + (props.framework ? `/${props.framework}` : "");
103
+ script.dataset.sdkv = version;
104
+ if (props.sampleRate) {
105
+ script.dataset.sampleRate = props.sampleRate.toString();
106
+ }
107
+ if (props.route) {
108
+ script.dataset.route = props.route;
109
+ }
110
+ if (props.endpoint) {
111
+ script.dataset.endpoint = props.endpoint;
112
+ }
113
+ if (props.token) {
114
+ script.dataset.token = props.token;
115
+ }
116
+ if (isDevelopment() && props.debug === false) {
117
+ script.dataset.debug = "false";
118
+ }
119
+ script.onerror = () => {
120
+ 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.";
121
+ console.log(
122
+ `[Vercel Speed Insights] Failed to load script from ${src}. ${errorMessage}`
123
+ );
124
+ };
125
+ document.head.appendChild(script);
126
+ return {
127
+ setRoute: (route) => {
128
+ script.dataset.route = route ?? void 0;
129
+ }
130
+ };
131
+ }
132
+
133
+ // src/vue/create-component.ts
134
+ function createComponent(framework = "vue") {
135
+ return (0, import_vue.defineComponent)({
136
+ props: [
137
+ "token",
138
+ "sampleRate",
139
+ "beforeSend",
140
+ "debug",
141
+ "scriptSrc",
142
+ "endpoint"
143
+ ],
144
+ setup(props) {
145
+ const route = (0, import_vue_router.useRoute)();
146
+ const configure = injectSpeedInsights({ ...props, framework });
147
+ if (route && configure) {
148
+ const changeRoute = () => {
149
+ configure.setRoute(computeRoute(route.path, route.params));
150
+ };
151
+ changeRoute();
152
+ (0, import_vue.watch)(route, changeRoute);
153
+ }
154
+ },
155
+ // Vue component must have a render function, or a template.
156
+ render() {
157
+ return null;
158
+ }
159
+ });
160
+ }
161
+
162
+ // src/vue/index.ts
163
+ var SpeedInsights = createComponent();
164
+ // Annotate the CommonJS export names for ESM import in node:
165
+ 0 && (module.exports = {
166
+ SpeedInsights
167
+ });
168
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/vue/index.ts","../../src/vue/create-component.ts","../../package.json","../../src/queue.ts","../../src/utils.ts","../../src/generic.ts"],"sourcesContent":["import { createComponent } from './create-component';\n\n// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- vue's defineComponent return type is any\nexport const SpeedInsights = createComponent();\n","import { defineComponent, watch } from 'vue';\n// for barebone vue project, vite will issue a warning since 'vue-router' import can't be resolved,\nimport { useRoute } from 'vue-router';\nimport { injectSpeedInsights, type SpeedInsightsProps } from '../generic';\nimport { computeRoute } from '../utils';\n\nexport function createComponent(\n framework = 'vue',\n): ReturnType<typeof defineComponent> {\n return defineComponent({\n props: [\n 'token',\n 'sampleRate',\n 'beforeSend',\n 'debug',\n 'scriptSrc',\n 'endpoint',\n ],\n setup(props: Omit<SpeedInsightsProps, 'framework'>) {\n const route = useRoute();\n const configure = injectSpeedInsights({ ...props, framework });\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- route is undefined for barebone vue project\n if (route && configure) {\n const changeRoute = (): void => {\n configure.setRoute(computeRoute(route.path, route.params));\n };\n\n changeRoute();\n watch(route, changeRoute);\n }\n },\n // Vue component must have a render function, or a template.\n render() {\n return null;\n },\n });\n}\n","{\n \"name\": \"@vercel/speed-insights\",\n \"version\": \"0.0.6\",\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.mjs\",\n \"import\": \"./dist/index.mjs\",\n \"require\": \"./dist/index.js\"\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 \"./remix\": {\n \"browser\": \"./dist/remix/index.mjs\",\n \"import\": \"./dist/remix/index.mjs\",\n \"require\": \"./dist/remix/index.js\"\n },\n \"./sveltekit\": {\n \"svelte\": \"./dist/sveltekit/index.mjs\",\n \"types\": \"./dist/sveltekit/index.d.ts\"\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 \",\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 \"vue\": \"^3.3.4\",\n \"vue-router\": \"^4.2.5\"\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 try {\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(`/${escapeRegExp(value)}(?=[/?#]|$)`);\n if (matcher.test(result)) {\n result = result.replace(matcher, `/[${expr}]`);\n }\n }\n\n return result;\n } catch (e) {\n return pathname;\n }\n}\n\nfunction escapeRegExp(string: string): string {\n return string.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\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 injectSpeedInsights(\n props: SpeedInsightsProps & {\n framework?: 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 =\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 | null): void => {\n script.dataset.route = route ?? undefined;\n },\n };\n}\n\nexport { injectSpeedInsights };\nexport type { SpeedInsightsProps };\n\n// eslint-disable-next-line import/no-default-export -- Allow default export\nexport default {\n injectSpeedInsights,\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,iBAAuC;AAEvC,wBAAyB;;;ACDvB,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;A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 import { useRoute } from "vue-router";
4
+
5
+ // package.json
6
+ var name = "@vercel/speed-insights";
7
+ var version = "0.0.6";
8
+
9
+ // src/queue.ts
10
+ var initQueue = () => {
11
+ if (window.si)
12
+ return;
13
+ window.si = function a(...params) {
14
+ (window.siq = window.siq || []).push(params);
15
+ };
16
+ };
17
+
18
+ // src/utils.ts
19
+ function isBrowser() {
20
+ return typeof window !== "undefined";
21
+ }
22
+ function detectEnvironment() {
23
+ try {
24
+ const env = process.env.NODE_ENV;
25
+ if (env === "development" || env === "test") {
26
+ return "development";
27
+ }
28
+ } catch (e) {
29
+ }
30
+ return "production";
31
+ }
32
+ function isDevelopment() {
33
+ return detectEnvironment() === "development";
34
+ }
35
+ function computeRoute(pathname, pathParams) {
36
+ if (!pathname || !pathParams) {
37
+ return pathname;
38
+ }
39
+ let result = pathname;
40
+ try {
41
+ for (const [key, valueOrArray] of Object.entries(pathParams)) {
42
+ const isValueArray = Array.isArray(valueOrArray);
43
+ const value = isValueArray ? valueOrArray.join("/") : valueOrArray;
44
+ const expr = isValueArray ? `...${key}` : key;
45
+ const matcher = new RegExp(`/${escapeRegExp(value)}(?=[/?#]|$)`);
46
+ if (matcher.test(result)) {
47
+ result = result.replace(matcher, `/[${expr}]`);
48
+ }
49
+ }
50
+ return result;
51
+ } catch (e) {
52
+ return pathname;
53
+ }
54
+ }
55
+ function escapeRegExp(string) {
56
+ return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
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 injectSpeedInsights(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 ?? void 0;
103
+ }
104
+ };
105
+ }
106
+
107
+ // src/vue/create-component.ts
108
+ function createComponent(framework = "vue") {
109
+ return defineComponent({
110
+ props: [
111
+ "token",
112
+ "sampleRate",
113
+ "beforeSend",
114
+ "debug",
115
+ "scriptSrc",
116
+ "endpoint"
117
+ ],
118
+ setup(props) {
119
+ const route = useRoute();
120
+ const configure = injectSpeedInsights({ ...props, framework });
121
+ if (route && configure) {
122
+ const changeRoute = () => {
123
+ configure.setRoute(computeRoute(route.path, route.params));
124
+ };
125
+ changeRoute();
126
+ watch(route, changeRoute);
127
+ }
128
+ },
129
+ // Vue component must have a render function, or a template.
130
+ render() {
131
+ return null;
132
+ }
133
+ });
134
+ }
135
+
136
+ // src/vue/index.ts
137
+ var SpeedInsights = createComponent();
138
+ export {
139
+ SpeedInsights
140
+ };
141
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/vue/create-component.ts","../../package.json","../../src/queue.ts","../../src/utils.ts","../../src/generic.ts","../../src/vue/index.ts"],"sourcesContent":["import { defineComponent, watch } from 'vue';\n// for barebone vue project, vite will issue a warning since 'vue-router' import can't be resolved,\nimport { useRoute } from 'vue-router';\nimport { injectSpeedInsights, type SpeedInsightsProps } from '../generic';\nimport { computeRoute } from '../utils';\n\nexport function createComponent(\n framework = 'vue',\n): ReturnType<typeof defineComponent> {\n return defineComponent({\n props: [\n 'token',\n 'sampleRate',\n 'beforeSend',\n 'debug',\n 'scriptSrc',\n 'endpoint',\n ],\n setup(props: Omit<SpeedInsightsProps, 'framework'>) {\n const route = useRoute();\n const configure = injectSpeedInsights({ ...props, framework });\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- route is undefined for barebone vue project\n if (route && configure) {\n const changeRoute = (): void => {\n configure.setRoute(computeRoute(route.path, route.params));\n };\n\n changeRoute();\n watch(route, changeRoute);\n }\n },\n // Vue component must have a render function, or a template.\n render() {\n return null;\n },\n });\n}\n","{\n \"name\": \"@vercel/speed-insights\",\n \"version\": \"0.0.6\",\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.mjs\",\n \"import\": \"./dist/index.mjs\",\n \"require\": \"./dist/index.js\"\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 \"./remix\": {\n \"browser\": \"./dist/remix/index.mjs\",\n \"import\": \"./dist/remix/index.mjs\",\n \"require\": \"./dist/remix/index.js\"\n },\n \"./sveltekit\": {\n \"svelte\": \"./dist/sveltekit/index.mjs\",\n \"types\": \"./dist/sveltekit/index.d.ts\"\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 \",\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 \"vue\": \"^3.3.4\",\n \"vue-router\": \"^4.2.5\"\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 try {\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(`/${escapeRegExp(value)}(?=[/?#]|$)`);\n if (matcher.test(result)) {\n result = result.replace(matcher, `/[${expr}]`);\n }\n }\n\n return result;\n } catch (e) {\n return pathname;\n }\n}\n\nfunction escapeRegExp(string: string): string {\n return string.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\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 injectSpeedInsights(\n props: SpeedInsightsProps & {\n framework?: 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 =\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 | null): void => {\n script.dataset.route = route ?? undefined;\n },\n };\n}\n\nexport { injectSpeedInsights };\nexport type { SpeedInsightsProps };\n\n// eslint-disable-next-line import/no-default-export -- Allow default export\nexport default {\n injectSpeedInsights,\n};\n","import { createComponent } from './create-component';\n\n// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- vue's defineComponent return type is any\nexport const SpeedInsights = createComponent();\n"],"mappings":";AAAA,SAAS,iBAAiB,aAAa;AAEvC,SAAS,gBAAgB;;;ACDvB,WAAQ;AACR,cAAW;;;ACFN,IAAM,YAAY,MAAY;AAEnC,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AAY;AAC9B,oBAAU,SAAS,aAAa,MAAM,MAAM,MAAM,MAAM,CAAC;AAAA,QAC3D;AAEA,oBAAY;AACZ,cAAM,OAAO,WAAW;AAAA,MAC1B;AAAA,IACF;AAAA;AAAA,IAEA,SAAS;AACP,aAAO;AAAA,IACT;AAAA,EACF,CAAC;AACH;;;AKjCO,IAAM,gBAAgB,gBAAgB;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vercel/speed-insights",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "description": "Speed Insights is a tool for measuring web performance and providing suggestions for improvement.",
5
5
  "keywords": [
6
6
  "speed-insights",
@@ -14,23 +14,33 @@
14
14
  "exports": {
15
15
  "./package.json": "./package.json",
16
16
  ".": {
17
- "browser": "./dist/index.js",
18
- "import": "./dist/index.js",
19
- "require": "./dist/index.cjs"
17
+ "browser": "./dist/index.mjs",
18
+ "import": "./dist/index.mjs",
19
+ "require": "./dist/index.js"
20
20
  },
21
21
  "./next": {
22
- "browser": "./dist/next/index.js",
23
- "import": "./dist/next/index.js",
24
- "require": "./dist/next/index.cjs"
22
+ "browser": "./dist/next/index.mjs",
23
+ "import": "./dist/next/index.mjs",
24
+ "require": "./dist/next/index.js"
25
+ },
26
+ "./nuxt": {
27
+ "browser": "./dist/nuxt/index.mjs",
28
+ "import": "./dist/nuxt/index.mjs",
29
+ "require": "./dist/nuxt/index.js"
25
30
  },
26
31
  "./remix": {
27
- "browser": "./dist/remix/index.js",
28
- "import": "./dist/remix/index.js",
29
- "require": "./dist/remix/index.cjs"
32
+ "browser": "./dist/remix/index.mjs",
33
+ "import": "./dist/remix/index.mjs",
34
+ "require": "./dist/remix/index.js"
30
35
  },
31
36
  "./sveltekit": {
32
37
  "svelte": "./dist/sveltekit/index.mjs",
33
38
  "types": "./dist/sveltekit/index.d.ts"
39
+ },
40
+ "./vue": {
41
+ "browser": "./dist/vue/index.mjs",
42
+ "import": "./dist/vue/index.mjs",
43
+ "require": "./dist/vue/index.js"
34
44
  }
35
45
  },
36
46
  "main": "dist/index.js",
@@ -46,11 +56,17 @@
46
56
  "next": [
47
57
  "dist/next/index.d.ts"
48
58
  ],
59
+ "nuxt": [
60
+ "dist/nuxt/index.d.ts"
61
+ ],
49
62
  "remix": [
50
63
  "dist/remix/index.d.ts"
51
64
  ],
52
65
  "sveltekit": [
53
66
  "dist/sveltekit/index.d.ts"
67
+ ],
68
+ "vue": [
69
+ "dist/vue/index.d.ts"
54
70
  ]
55
71
  }
56
72
  },
@@ -71,7 +87,9 @@
71
87
  "react": "^18.2.0",
72
88
  "react-dom": "^18.2.0",
73
89
  "svelte": "^4.0.5",
74
- "tsup": "7.2.0"
90
+ "tsup": "7.2.0",
91
+ "vue": "^3.3.4",
92
+ "vue-router": "^4.2.5"
75
93
  },
76
94
  "scripts": {
77
95
  "build": "tsup ",
package/tsup.config.js CHANGED
@@ -31,6 +31,14 @@ export default defineConfig([
31
31
  };
32
32
  },
33
33
  },
34
+ {
35
+ ...cfg,
36
+ entry: {
37
+ index: 'src/nuxt/index.ts',
38
+ },
39
+ external: ['vue', 'vue-router'],
40
+ outDir: 'dist/nuxt',
41
+ },
34
42
  {
35
43
  ...cfg,
36
44
  entry: {
@@ -47,4 +55,12 @@ export default defineConfig([
47
55
  external: ['svelte', '@sveltejs/kit', '$app'],
48
56
  outDir: 'dist/sveltekit',
49
57
  },
58
+ {
59
+ ...cfg,
60
+ entry: {
61
+ index: 'src/vue/index.ts',
62
+ },
63
+ external: ['vue', 'vue-router'],
64
+ outDir: 'dist/vue',
65
+ },
50
66
  ]);