Add back the analytics property and add deprecation warning when used

This commit is contained in:
Chris 2023-08-29 17:24:50 +02:00
parent 6d91119b93
commit 99b07d4602
3 changed files with 31 additions and 6 deletions

View file

@ -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);

View file

@ -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));

View file

@ -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));