diff --git a/packages/integrations/vercel/src/serverless/adapter.ts b/packages/integrations/vercel/src/serverless/adapter.ts index 93fd90c49..21d4486b1 100644 --- a/packages/integrations/vercel/src/serverless/adapter.ts +++ b/packages/integrations/vercel/src/serverless/adapter.ts @@ -60,6 +60,10 @@ function getAdapter({ } export interface VercelServerlessConfig { + /** + * @deprecated + */ + analytics?: boolean; webAnalytics?: VercelWebAnalyticsConfig; speedInsights?: VercelSpeedInsightsConfig; includeFiles?: string[]; @@ -71,6 +75,7 @@ export interface VercelServerlessConfig { } export default function vercelServerless({ + analytics, webAnalytics, speedInsights, includeFiles, @@ -117,12 +122,18 @@ export default function vercelServerless({ name: PACKAGE_NAME, hooks: { 'astro:config:setup': async ({ command, config, updateConfig, injectScript, logger }) => { - if (webAnalytics?.enabled) { + if (webAnalytics?.enabled || analytics) { + if (analytics) { + logger.warn( + `@astrojs/vercel: the \`analytics\` property is deprecated. Please use the new \`webAnalytics\` and \`speedInsights\` properties instead.` + ); + } + injectScript( 'page', await getInjectableWebAnalyticsContent({ config: { - ...webAnalytics.config, + ...(webAnalytics?.config || {}), mode: command === 'dev' ? 'development' : 'production', }, astro: { @@ -132,7 +143,7 @@ export default function vercelServerless({ }) ); } - if (command === 'build' && speedInsights?.enabled) { + if (command === 'build' && (speedInsights?.enabled || analytics)) { injectScript('page', 'import "@astrojs/vercel/speed-insights"'); } const outDir = getVercelOutput(config.root); diff --git a/packages/integrations/vercel/src/speed-insights.ts b/packages/integrations/vercel/src/speed-insights.ts index d212df89a..cd2ae7fe8 100644 --- a/packages/integrations/vercel/src/speed-insights.ts +++ b/packages/integrations/vercel/src/speed-insights.ts @@ -39,11 +39,14 @@ const sendToSpeedInsights = (metric: Metric, options: Options) => { function collectWebVitals() { const analyticsId = (import.meta as any).env.PUBLIC_VERCEL_ANALYTICS_ID; + if (!analyticsId) { console.error('[Speed Insights] VERCEL_ANALYTICS_ID not found'); return; } + const options: Options = { path: window.location.pathname, analyticsId }; + try { onFID((metric) => sendToSpeedInsights(metric, options)); onTTFB((metric) => sendToSpeedInsights(metric, options)); diff --git a/packages/integrations/vercel/src/static/adapter.ts b/packages/integrations/vercel/src/static/adapter.ts index c5d1d4cef..cd86225ea 100644 --- a/packages/integrations/vercel/src/static/adapter.ts +++ b/packages/integrations/vercel/src/static/adapter.ts @@ -20,6 +20,10 @@ function getAdapter(): AstroAdapter { } export interface VercelStaticConfig { + /** + * @deprecated + */ + analytics?: boolean; webAnalytics?: VercelWebAnalyticsConfig; speedInsights?: VercelSpeedInsightsConfig; imageService?: boolean; @@ -27,6 +31,7 @@ export interface VercelStaticConfig { } export default function vercelStatic({ + analytics, webAnalytics, speedInsights, imageService, @@ -38,12 +43,18 @@ export default function vercelStatic({ name: '@astrojs/vercel', hooks: { 'astro:config:setup': async ({ command, config, injectScript, updateConfig, logger }) => { - if (webAnalytics?.enabled) { + if (webAnalytics?.enabled || analytics) { + if (analytics) { + logger.warn( + `@astrojs/vercel: the \`analytics\` property is deprecated. Please use the new \`webAnalytics\` and \`speedInsights\` properties instead.` + ); + } + injectScript( 'page', await getInjectableWebAnalyticsContent({ config: { - ...webAnalytics.config, + ...(webAnalytics?.config || {}), mode: command === 'dev' ? 'development' : 'production', }, astro: { @@ -53,7 +64,7 @@ export default function vercelStatic({ }) ); } - if (command === 'build' && speedInsights?.enabled) { + if (command === 'build' && (speedInsights?.enabled || analytics)) { injectScript('page', 'import "@astrojs/vercel/speed-insights"'); } const outDir = new URL('./static/', getVercelOutput(config.root));