@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.
- package/dist/astro/component.ts +1 -2
- package/dist/astro/index.astro +25 -10
- package/dist/index.d.mts +12 -10
- package/dist/index.d.ts +12 -10
- package/dist/index.js +58 -31
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +58 -31
- package/dist/index.mjs.map +1 -1
- package/dist/next/index.d.mts +5 -5
- package/dist/next/index.d.ts +5 -5
- package/dist/next/index.js +84 -38
- package/dist/next/index.js.map +1 -1
- package/dist/next/index.mjs +84 -38
- package/dist/next/index.mjs.map +1 -1
- package/dist/nuxt/index.d.mts +5 -2
- package/dist/nuxt/index.d.ts +5 -2
- package/dist/nuxt/index.js +30 -182
- package/dist/nuxt/index.js.map +1 -1
- package/dist/nuxt/index.mjs +26 -171
- package/dist/nuxt/index.mjs.map +1 -1
- package/dist/nuxt/runtime/index.d.mts +40 -0
- package/dist/nuxt/runtime/index.d.ts +40 -0
- package/dist/nuxt/runtime/index.js +278 -0
- package/dist/nuxt/runtime/index.js.map +1 -0
- package/dist/nuxt/runtime/index.mjs +250 -0
- package/dist/nuxt/runtime/index.mjs.map +1 -0
- package/dist/react/index.d.mts +6 -5
- package/dist/react/index.d.ts +6 -5
- package/dist/react/index.js +76 -37
- package/dist/react/index.js.map +1 -1
- package/dist/react/index.mjs +76 -37
- package/dist/react/index.mjs.map +1 -1
- package/dist/remix/index.d.mts +5 -5
- package/dist/remix/index.d.ts +5 -5
- package/dist/remix/index.js +86 -41
- package/dist/remix/index.js.map +1 -1
- package/dist/remix/index.mjs +86 -41
- package/dist/remix/index.mjs.map +1 -1
- package/dist/sveltekit/index.d.mts +6 -6
- package/dist/sveltekit/index.d.ts +6 -6
- package/dist/sveltekit/index.js +77 -43
- package/dist/sveltekit/index.js.map +1 -1
- package/dist/sveltekit/index.mjs +77 -43
- package/dist/sveltekit/index.mjs.map +1 -1
- package/dist/vue/index.d.mts +36 -1
- package/dist/vue/index.d.ts +36 -1
- package/dist/vue/index.js +75 -36
- package/dist/vue/index.js.map +1 -1
- package/dist/vue/index.mjs +75 -36
- package/dist/vue/index.mjs.map +1 -1
- package/package.json +16 -6
- package/tsup.config.js +17 -2
- package/.eslintrc.cjs +0 -7
package/dist/nuxt/index.d.mts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import * as _nuxt_schema from '@nuxt/schema';
|
|
2
|
+
import { NuxtModule } from '@nuxt/schema';
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
declare const _default: NuxtModule<_nuxt_schema.ModuleOptions, Partial<_nuxt_schema.ModuleOptions>, false>;
|
|
5
|
+
|
|
6
|
+
export { _default as default };
|
package/dist/nuxt/index.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import * as _nuxt_schema from '@nuxt/schema';
|
|
2
|
+
import { NuxtModule } from '@nuxt/schema';
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
declare const _default: NuxtModule<_nuxt_schema.ModuleOptions, Partial<_nuxt_schema.ModuleOptions>, false>;
|
|
5
|
+
|
|
6
|
+
export { _default as default };
|
package/dist/nuxt/index.js
CHANGED
|
@@ -4,8 +4,8 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
6
|
var __export = (target, all) => {
|
|
7
|
-
for (var
|
|
8
|
-
__defProp(target,
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
9
|
};
|
|
10
10
|
var __copyProps = (to, from, except, desc) => {
|
|
11
11
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
@@ -17,187 +17,35 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
17
|
};
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
|
|
20
|
-
// src/nuxt/
|
|
21
|
-
var
|
|
22
|
-
__export(
|
|
23
|
-
|
|
20
|
+
// src/nuxt/module.ts
|
|
21
|
+
var module_exports = {};
|
|
22
|
+
__export(module_exports, {
|
|
23
|
+
default: () => module_default
|
|
24
24
|
});
|
|
25
|
-
module.exports = __toCommonJS(
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const env = process.env.NODE_ENV;
|
|
50
|
-
if (env === "development" || env === "test") {
|
|
51
|
-
return "development";
|
|
52
|
-
}
|
|
53
|
-
} catch (e) {
|
|
54
|
-
}
|
|
55
|
-
return "production";
|
|
56
|
-
}
|
|
57
|
-
function isDevelopment() {
|
|
58
|
-
return detectEnvironment() === "development";
|
|
59
|
-
}
|
|
60
|
-
function computeRoute(pathname, pathParams) {
|
|
61
|
-
if (!pathname || !pathParams) {
|
|
62
|
-
return pathname;
|
|
63
|
-
}
|
|
64
|
-
let result = pathname;
|
|
65
|
-
try {
|
|
66
|
-
const entries = Object.entries(pathParams);
|
|
67
|
-
for (const [key, value] of entries) {
|
|
68
|
-
if (!Array.isArray(value)) {
|
|
69
|
-
const matcher = turnValueToRegExp(value);
|
|
70
|
-
if (matcher.test(result)) {
|
|
71
|
-
result = result.replace(matcher, `/[${key}]`);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
for (const [key, value] of entries) {
|
|
76
|
-
if (Array.isArray(value)) {
|
|
77
|
-
const matcher = turnValueToRegExp(value.join("/"));
|
|
78
|
-
if (matcher.test(result)) {
|
|
79
|
-
result = result.replace(matcher, `/[...${key}]`);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
return result;
|
|
84
|
-
} catch (e) {
|
|
85
|
-
return pathname;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
function turnValueToRegExp(value) {
|
|
89
|
-
return new RegExp(`/${escapeRegExp(value)}(?=[/?#]|$)`);
|
|
90
|
-
}
|
|
91
|
-
function escapeRegExp(string) {
|
|
92
|
-
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
93
|
-
}
|
|
94
|
-
function getScriptSrc(props) {
|
|
95
|
-
if (props.scriptSrc) {
|
|
96
|
-
return props.scriptSrc;
|
|
97
|
-
}
|
|
98
|
-
if (isDevelopment()) {
|
|
99
|
-
return "https://va.vercel-scripts.com/v1/speed-insights/script.debug.js";
|
|
100
|
-
}
|
|
101
|
-
if (props.dsn) {
|
|
102
|
-
return "https://va.vercel-scripts.com/v1/speed-insights/script.js";
|
|
103
|
-
}
|
|
104
|
-
if (props.basePath) {
|
|
105
|
-
return `${props.basePath}/speed-insights/script.js`;
|
|
25
|
+
module.exports = __toCommonJS(module_exports);
|
|
26
|
+
var import_kit = require("@nuxt/kit");
|
|
27
|
+
var module_default = (0, import_kit.defineNuxtModule)({
|
|
28
|
+
meta: {
|
|
29
|
+
name: "@vercel/speed-insights",
|
|
30
|
+
configKey: "speedInsights",
|
|
31
|
+
docs: "https://vercel.com/docs/speed-insights/quickstart"
|
|
32
|
+
},
|
|
33
|
+
setup() {
|
|
34
|
+
const template = (0, import_kit.addTemplate)({
|
|
35
|
+
filename: "vercel-speed-insights.client.ts",
|
|
36
|
+
getContents: () => `
|
|
37
|
+
import { injectSpeedInsights } from '@vercel/speed-insights/nuxt/runtime'
|
|
38
|
+
import { defineNuxtPlugin } from '#imports'
|
|
39
|
+
|
|
40
|
+
export default defineNuxtPlugin(() => {
|
|
41
|
+
injectSpeedInsights()
|
|
42
|
+
})
|
|
43
|
+
`
|
|
44
|
+
});
|
|
45
|
+
(0, import_kit.addPlugin)({
|
|
46
|
+
src: template.dst,
|
|
47
|
+
mode: "client"
|
|
48
|
+
});
|
|
106
49
|
}
|
|
107
|
-
return "/_vercel/speed-insights/script.js";
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// src/generic.ts
|
|
111
|
-
function injectSpeedInsights(props = {}) {
|
|
112
|
-
var _a;
|
|
113
|
-
if (!isBrowser() || props.route === null) return null;
|
|
114
|
-
initQueue();
|
|
115
|
-
const src = getScriptSrc(props);
|
|
116
|
-
if (document.head.querySelector(`script[src*="${src}"]`)) return null;
|
|
117
|
-
if (props.beforeSend) {
|
|
118
|
-
(_a = window.si) == null ? void 0 : _a.call(window, "beforeSend", props.beforeSend);
|
|
119
|
-
}
|
|
120
|
-
const script = document.createElement("script");
|
|
121
|
-
script.src = src;
|
|
122
|
-
script.defer = true;
|
|
123
|
-
script.dataset.sdkn = name + (props.framework ? `/${props.framework}` : "");
|
|
124
|
-
script.dataset.sdkv = version;
|
|
125
|
-
if (props.sampleRate) {
|
|
126
|
-
script.dataset.sampleRate = props.sampleRate.toString();
|
|
127
|
-
}
|
|
128
|
-
if (props.route) {
|
|
129
|
-
script.dataset.route = props.route;
|
|
130
|
-
}
|
|
131
|
-
if (props.endpoint) {
|
|
132
|
-
script.dataset.endpoint = props.endpoint;
|
|
133
|
-
} else if (props.basePath) {
|
|
134
|
-
script.dataset.endpoint = `${props.basePath}/speed-insights/vitals`;
|
|
135
|
-
}
|
|
136
|
-
if (props.dsn) {
|
|
137
|
-
script.dataset.dsn = props.dsn;
|
|
138
|
-
}
|
|
139
|
-
if (isDevelopment() && props.debug === false) {
|
|
140
|
-
script.dataset.debug = "false";
|
|
141
|
-
}
|
|
142
|
-
script.onerror = () => {
|
|
143
|
-
console.log(
|
|
144
|
-
`[Vercel Speed Insights] Failed to load script from ${src}. Please check if any content blockers are enabled and try again.`
|
|
145
|
-
);
|
|
146
|
-
};
|
|
147
|
-
document.head.appendChild(script);
|
|
148
|
-
return {
|
|
149
|
-
setRoute: (route) => {
|
|
150
|
-
script.dataset.route = route ?? void 0;
|
|
151
|
-
}
|
|
152
|
-
};
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
// src/vue/utils.ts
|
|
156
|
-
var import_meta = {};
|
|
157
|
-
function getBasePath() {
|
|
158
|
-
try {
|
|
159
|
-
return import_meta.env.VITE_VERCEL_OBSERVABILITY_BASEPATH;
|
|
160
|
-
} catch {
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
// src/vue/create-component.ts
|
|
165
|
-
function createComponent(framework = "vue") {
|
|
166
|
-
return (0, import_vue.defineComponent)({
|
|
167
|
-
props: [
|
|
168
|
-
"dsn",
|
|
169
|
-
"sampleRate",
|
|
170
|
-
"beforeSend",
|
|
171
|
-
"debug",
|
|
172
|
-
"scriptSrc",
|
|
173
|
-
"endpoint"
|
|
174
|
-
],
|
|
175
|
-
setup(props) {
|
|
176
|
-
const route = (0, import_vue_router.useRoute)();
|
|
177
|
-
const configure = injectSpeedInsights({
|
|
178
|
-
...props,
|
|
179
|
-
framework,
|
|
180
|
-
basePath: getBasePath()
|
|
181
|
-
});
|
|
182
|
-
if (route && configure) {
|
|
183
|
-
const changeRoute = () => {
|
|
184
|
-
configure.setRoute(computeRoute(route.path, route.params));
|
|
185
|
-
};
|
|
186
|
-
changeRoute();
|
|
187
|
-
(0, import_vue.watch)(route, changeRoute);
|
|
188
|
-
}
|
|
189
|
-
},
|
|
190
|
-
// Vue component must have a render function, or a template.
|
|
191
|
-
render() {
|
|
192
|
-
return null;
|
|
193
|
-
}
|
|
194
|
-
});
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
// src/nuxt/index.ts
|
|
198
|
-
var SpeedInsights = createComponent("nuxt");
|
|
199
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
200
|
-
0 && (module.exports = {
|
|
201
|
-
SpeedInsights
|
|
202
50
|
});
|
|
203
51
|
//# sourceMappingURL=index.js.map
|
package/dist/nuxt/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/nuxt/index.ts","../../src/vue/create-component.ts","../../package.json","../../src/queue.ts","../../src/utils.ts","../../src/generic.ts","../../src/vue/utils.ts"],"sourcesContent":["import { createComponent } from '../vue/create-component';\n\n// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- vue's defineComponent return type is any\nexport const SpeedInsights = createComponent('nuxt');\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';\nimport { getBasePath } from './utils';\n\nexport function createComponent(\n framework = 'vue',\n): ReturnType<typeof defineComponent> {\n return defineComponent({\n props: [\n 'dsn',\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({\n ...props,\n framework,\n basePath: getBasePath(),\n });\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\": \"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;AAAA;AAAA;;;ACAA,iBAAuC;AAEvC,wBAAyB;;;ACDvB,WAAQ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AAEA,SAAO,UAAU,MAAY;AAE3B,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;;;AC1EA;AAAO,SAAS,cAAkC;AAIhD,MAAI;AACF,WAAO,YAAY,IAAI;AAAA,EAGzB,QAAQ;AAAA,EAER;AACF;;;ALJO,SAAS,gBACd,YAAY,OACwB;AACpC,aAAO,4BAAgB;AAAA,IACrB,OAAO;AAAA,MACL;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,IACA,MAAM,OAA8C;AAClD,YAAM,YAAQ,4BAAS;AACvB,YAAM,YAAY,oBAAoB;AAAA,QACpC,GAAG;AAAA,QACH;AAAA,QACA,UAAU,YAAY;AAAA,MACxB,CAAC;AAED,UAAI,SAAS,WAAW;AACtB,cAAM,cAAc,MAAY;AAC9B,oBAAU,SAAS,aAAa,MAAM,MAAM,MAAM,MAAM,CAAC;AAAA,QAC3D;AAEA,oBAAY;AACZ,8BAAM,OAAO,WAAW;AAAA,MAC1B;AAAA,IACF;AAAA;AAAA,IAEA,SAAS;AACP,aAAO;AAAA,IACT;AAAA,EACF,CAAC;AACH;;;ADtCO,IAAM,gBAAgB,gBAAgB,MAAM;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/nuxt/module.ts"],"sourcesContent":["import { addPlugin, addTemplate, defineNuxtModule } from '@nuxt/kit';\nimport type { NuxtModule } from '@nuxt/schema';\n\nexport default defineNuxtModule({\n meta: {\n name: '@vercel/speed-insights',\n configKey: 'speedInsights',\n docs: 'https://vercel.com/docs/speed-insights/quickstart',\n },\n setup() {\n const template = addTemplate({\n filename: 'vercel-speed-insights.client.ts',\n getContents: () => `\nimport { injectSpeedInsights } from '@vercel/speed-insights/nuxt/runtime'\nimport { defineNuxtPlugin } from '#imports'\n\nexport default defineNuxtPlugin(() => {\n injectSpeedInsights()\n})\n`,\n });\n\n addPlugin({\n src: template.dst,\n mode: 'client',\n });\n },\n}) as NuxtModule;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,iBAAyD;AAGzD,IAAO,qBAAQ,6BAAiB;AAAA,EAC9B,MAAM;AAAA,IACJ,MAAM;AAAA,IACN,WAAW;AAAA,IACX,MAAM;AAAA,EACR;AAAA,EACA,QAAQ;AACN,UAAM,eAAW,wBAAY;AAAA,MAC3B,UAAU;AAAA,MACV,aAAa,MAAM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAQrB,CAAC;AAED,8BAAU;AAAA,MACR,KAAK,SAAS;AAAA,MACd,MAAM;AAAA,IACR,CAAC;AAAA,EACH;AACF,CAAC;","names":[]}
|
package/dist/nuxt/index.mjs
CHANGED
|
@@ -1,175 +1,30 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
// src/nuxt/module.ts
|
|
2
|
+
import { addPlugin, addTemplate, defineNuxtModule } from "@nuxt/kit";
|
|
3
|
+
var module_default = defineNuxtModule({
|
|
4
|
+
meta: {
|
|
5
|
+
name: "@vercel/speed-insights",
|
|
6
|
+
configKey: "speedInsights",
|
|
7
|
+
docs: "https://vercel.com/docs/speed-insights/quickstart"
|
|
8
|
+
},
|
|
9
|
+
setup() {
|
|
10
|
+
const template = addTemplate({
|
|
11
|
+
filename: "vercel-speed-insights.client.ts",
|
|
12
|
+
getContents: () => `
|
|
13
|
+
import { injectSpeedInsights } from '@vercel/speed-insights/nuxt/runtime'
|
|
14
|
+
import { defineNuxtPlugin } from '#imports'
|
|
4
15
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
// src/utils.ts
|
|
18
|
-
function isBrowser() {
|
|
19
|
-
return typeof window !== "undefined";
|
|
20
|
-
}
|
|
21
|
-
function detectEnvironment() {
|
|
22
|
-
try {
|
|
23
|
-
const env = process.env.NODE_ENV;
|
|
24
|
-
if (env === "development" || env === "test") {
|
|
25
|
-
return "development";
|
|
26
|
-
}
|
|
27
|
-
} catch (e) {
|
|
28
|
-
}
|
|
29
|
-
return "production";
|
|
30
|
-
}
|
|
31
|
-
function isDevelopment() {
|
|
32
|
-
return detectEnvironment() === "development";
|
|
33
|
-
}
|
|
34
|
-
function computeRoute(pathname, pathParams) {
|
|
35
|
-
if (!pathname || !pathParams) {
|
|
36
|
-
return pathname;
|
|
37
|
-
}
|
|
38
|
-
let result = pathname;
|
|
39
|
-
try {
|
|
40
|
-
const entries = Object.entries(pathParams);
|
|
41
|
-
for (const [key, value] of entries) {
|
|
42
|
-
if (!Array.isArray(value)) {
|
|
43
|
-
const matcher = turnValueToRegExp(value);
|
|
44
|
-
if (matcher.test(result)) {
|
|
45
|
-
result = result.replace(matcher, `/[${key}]`);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
for (const [key, value] of entries) {
|
|
50
|
-
if (Array.isArray(value)) {
|
|
51
|
-
const matcher = turnValueToRegExp(value.join("/"));
|
|
52
|
-
if (matcher.test(result)) {
|
|
53
|
-
result = result.replace(matcher, `/[...${key}]`);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
return result;
|
|
58
|
-
} catch (e) {
|
|
59
|
-
return pathname;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
function turnValueToRegExp(value) {
|
|
63
|
-
return new RegExp(`/${escapeRegExp(value)}(?=[/?#]|$)`);
|
|
64
|
-
}
|
|
65
|
-
function escapeRegExp(string) {
|
|
66
|
-
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
67
|
-
}
|
|
68
|
-
function getScriptSrc(props) {
|
|
69
|
-
if (props.scriptSrc) {
|
|
70
|
-
return props.scriptSrc;
|
|
71
|
-
}
|
|
72
|
-
if (isDevelopment()) {
|
|
73
|
-
return "https://va.vercel-scripts.com/v1/speed-insights/script.debug.js";
|
|
74
|
-
}
|
|
75
|
-
if (props.dsn) {
|
|
76
|
-
return "https://va.vercel-scripts.com/v1/speed-insights/script.js";
|
|
77
|
-
}
|
|
78
|
-
if (props.basePath) {
|
|
79
|
-
return `${props.basePath}/speed-insights/script.js`;
|
|
80
|
-
}
|
|
81
|
-
return "/_vercel/speed-insights/script.js";
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// src/generic.ts
|
|
85
|
-
function injectSpeedInsights(props = {}) {
|
|
86
|
-
var _a;
|
|
87
|
-
if (!isBrowser() || props.route === null) return null;
|
|
88
|
-
initQueue();
|
|
89
|
-
const src = getScriptSrc(props);
|
|
90
|
-
if (document.head.querySelector(`script[src*="${src}"]`)) return null;
|
|
91
|
-
if (props.beforeSend) {
|
|
92
|
-
(_a = window.si) == null ? void 0 : _a.call(window, "beforeSend", props.beforeSend);
|
|
93
|
-
}
|
|
94
|
-
const script = document.createElement("script");
|
|
95
|
-
script.src = src;
|
|
96
|
-
script.defer = true;
|
|
97
|
-
script.dataset.sdkn = name + (props.framework ? `/${props.framework}` : "");
|
|
98
|
-
script.dataset.sdkv = version;
|
|
99
|
-
if (props.sampleRate) {
|
|
100
|
-
script.dataset.sampleRate = props.sampleRate.toString();
|
|
101
|
-
}
|
|
102
|
-
if (props.route) {
|
|
103
|
-
script.dataset.route = props.route;
|
|
104
|
-
}
|
|
105
|
-
if (props.endpoint) {
|
|
106
|
-
script.dataset.endpoint = props.endpoint;
|
|
107
|
-
} else if (props.basePath) {
|
|
108
|
-
script.dataset.endpoint = `${props.basePath}/speed-insights/vitals`;
|
|
109
|
-
}
|
|
110
|
-
if (props.dsn) {
|
|
111
|
-
script.dataset.dsn = props.dsn;
|
|
112
|
-
}
|
|
113
|
-
if (isDevelopment() && props.debug === false) {
|
|
114
|
-
script.dataset.debug = "false";
|
|
115
|
-
}
|
|
116
|
-
script.onerror = () => {
|
|
117
|
-
console.log(
|
|
118
|
-
`[Vercel Speed Insights] Failed to load script from ${src}. Please check if any content blockers are enabled and try again.`
|
|
119
|
-
);
|
|
120
|
-
};
|
|
121
|
-
document.head.appendChild(script);
|
|
122
|
-
return {
|
|
123
|
-
setRoute: (route) => {
|
|
124
|
-
script.dataset.route = route ?? void 0;
|
|
125
|
-
}
|
|
126
|
-
};
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
// src/vue/utils.ts
|
|
130
|
-
function getBasePath() {
|
|
131
|
-
try {
|
|
132
|
-
return import.meta.env.VITE_VERCEL_OBSERVABILITY_BASEPATH;
|
|
133
|
-
} catch {
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
// src/vue/create-component.ts
|
|
138
|
-
function createComponent(framework = "vue") {
|
|
139
|
-
return defineComponent({
|
|
140
|
-
props: [
|
|
141
|
-
"dsn",
|
|
142
|
-
"sampleRate",
|
|
143
|
-
"beforeSend",
|
|
144
|
-
"debug",
|
|
145
|
-
"scriptSrc",
|
|
146
|
-
"endpoint"
|
|
147
|
-
],
|
|
148
|
-
setup(props) {
|
|
149
|
-
const route = useRoute();
|
|
150
|
-
const configure = injectSpeedInsights({
|
|
151
|
-
...props,
|
|
152
|
-
framework,
|
|
153
|
-
basePath: getBasePath()
|
|
154
|
-
});
|
|
155
|
-
if (route && configure) {
|
|
156
|
-
const changeRoute = () => {
|
|
157
|
-
configure.setRoute(computeRoute(route.path, route.params));
|
|
158
|
-
};
|
|
159
|
-
changeRoute();
|
|
160
|
-
watch(route, changeRoute);
|
|
161
|
-
}
|
|
162
|
-
},
|
|
163
|
-
// Vue component must have a render function, or a template.
|
|
164
|
-
render() {
|
|
165
|
-
return null;
|
|
166
|
-
}
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
// src/nuxt/index.ts
|
|
171
|
-
var SpeedInsights = createComponent("nuxt");
|
|
16
|
+
export default defineNuxtPlugin(() => {
|
|
17
|
+
injectSpeedInsights()
|
|
18
|
+
})
|
|
19
|
+
`
|
|
20
|
+
});
|
|
21
|
+
addPlugin({
|
|
22
|
+
src: template.dst,
|
|
23
|
+
mode: "client"
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
});
|
|
172
27
|
export {
|
|
173
|
-
|
|
28
|
+
module_default as default
|
|
174
29
|
};
|
|
175
30
|
//# sourceMappingURL=index.mjs.map
|
package/dist/nuxt/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/vue/create-component.ts","../../package.json","../../src/queue.ts","../../src/utils.ts","../../src/generic.ts","../../src/vue/utils.ts","../../src/nuxt/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';\nimport { getBasePath } from './utils';\n\nexport function createComponent(\n framework = 'vue',\n): ReturnType<typeof defineComponent> {\n return defineComponent({\n props: [\n 'dsn',\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({\n ...props,\n framework,\n basePath: getBasePath(),\n });\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\": \"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","import { createComponent } from '../vue/create-component';\n\n// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- vue's defineComponent return type is any\nexport const SpeedInsights = createComponent('nuxt');\n"],"mappings":";AAAA,SAAS,iBAAiB,aAAa;AAEvC,SAAS,gBAAgB;;;ACDvB,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;AAEO,SAAS,aACd,UACA,YACe;AACf,MAAI,CAAC,YAAY,CAAC,YAAY;AAC5B,WAAO;AAAA,EACT;AAEA,MAAI,SAAS;AACb,MAAI;AACF,UAAM,UAAU,OAAO,QAAQ,UAAU;AAEzC,eAAW,CAAC,KAAK,KAAK,KAAK,SAAS;AAClC,UAAI,CAAC,MAAM,QAAQ,KAAK,GAAG;AACzB,cAAM,UAAU,kBAAkB,KAAK;AACvC,YAAI,QAAQ,KAAK,MAAM,GAAG;AACxB,mBAAS,OAAO,QAAQ,SAAS,KAAK,GAAG,GAAG;AAAA,QAC9C;AAAA,MACF;AAAA,IACF;AAEA,eAAW,CAAC,KAAK,KAAK,KAAK,SAAS;AAClC,UAAI,MAAM,QAAQ,KAAK,GAAG;AACxB,cA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"sourcesContent":["import { addPlugin, addTemplate, defineNuxtModule } from '@nuxt/kit';\nimport type { NuxtModule } from '@nuxt/schema';\n\nexport default defineNuxtModule({\n meta: {\n name: '@vercel/speed-insights',\n configKey: 'speedInsights',\n docs: 'https://vercel.com/docs/speed-insights/quickstart',\n },\n setup() {\n const template = addTemplate({\n filename: 'vercel-speed-insights.client.ts',\n getContents: () => `\nimport { injectSpeedInsights } from '@vercel/speed-insights/nuxt/runtime'\nimport { defineNuxtPlugin } from '#imports'\n\nexport default defineNuxtPlugin(() => {\n injectSpeedInsights()\n})\n`,\n });\n\n addPlugin({\n src: template.dst,\n mode: 'client',\n });\n },\n}) as NuxtModule;\n"],"mappings":";AAAA,SAAS,WAAW,aAAa,wBAAwB;AAGzD,IAAO,iBAAQ,iBAAiB;AAAA,EAC9B,MAAM;AAAA,IACJ,MAAM;AAAA,IACN,WAAW;AAAA,IACX,MAAM;AAAA,EACR;AAAA,EACA,QAAQ;AACN,UAAM,WAAW,YAAY;AAAA,MAC3B,UAAU;AAAA,MACV,aAAa,MAAM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAQrB,CAAC;AAED,cAAU;AAAA,MACR,KAAK,SAAS;AAAA,MACd,MAAM;AAAA,IACR,CAAC;AAAA,EACH;AACF,CAAC;","names":[]}
|
|
@@ -0,0 +1,40 @@
|
|
|
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
|
+
|
|
36
|
+
declare const SpeedInsights: any;
|
|
37
|
+
|
|
38
|
+
declare function injectSpeedInsights(props?: Omit<SpeedInsightsProps, 'framework'>): void;
|
|
39
|
+
|
|
40
|
+
export { type BeforeSend as BeforeSendMiddleware, SpeedInsights, type SpeedInsightsProps, injectSpeedInsights };
|
|
@@ -0,0 +1,40 @@
|
|
|
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
|
+
|
|
36
|
+
declare const SpeedInsights: any;
|
|
37
|
+
|
|
38
|
+
declare function injectSpeedInsights(props?: Omit<SpeedInsightsProps, 'framework'>): void;
|
|
39
|
+
|
|
40
|
+
export { type BeforeSend as BeforeSendMiddleware, SpeedInsights, type SpeedInsightsProps, injectSpeedInsights };
|