Skip to content

Commit

Permalink
[cli] Ignore VERCEL_ANALYTICS_ID if @vercel/speed-insights is present…
Browse files Browse the repository at this point in the history
… on the project (#11305)

In the past, we used the `VERCEL_ANALYTICS_ID` environment variable with the previous Speed Insights feature on Vercel to activate specific logic in Next.js, Nuxt and Gatsby for collecting and sending web vitals data.

With the new Speed Insights, that's not required anymore.

We no longer want to set the environment variable when detecting the new `@vercel/speed-insights` package. 
This PR confirms that the variable is not set if the new package is detected.
  • Loading branch information
chriswdmr committed Apr 18, 2024
1 parent 0b05981 commit 1e6323e
Show file tree
Hide file tree
Showing 41 changed files with 2,621 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .changeset/friendly-points-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vercel': minor
---

Don't propagate legacy env VERCEL_ANALYTICS_ID if @vercel/speed-insights package is detected
22 changes: 22 additions & 0 deletions packages/build-utils/src/get-installed-package-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import debug from './debug';

export async function getInstalledPackageVersion(
packageName: string,
path?: string | string[]
): Promise<string | undefined> {
try {
const resolved = require.resolve(`${packageName}/package.json`, {
paths: path ? (Array.isArray(path) ? path : [path]) : [process.cwd()],
});

const version: string = require(resolved).version;

return version;
} catch (err) {
debug(
`Could not resolve package "${packageName}". Package is not installed.`,
err
);
return undefined;
}
}
2 changes: 2 additions & 0 deletions packages/build-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,5 @@ export * from './types';
export * from './errors';

export { NODE_VERSIONS } from './fs/node-version';

export { getInstalledPackageVersion } from './get-installed-package-version';
33 changes: 8 additions & 25 deletions packages/cli/src/commands/build/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs, { readJSON } from 'fs-extra';
import fs from 'fs-extra';
import chalk from 'chalk';
import dotenv from 'dotenv';
import semver from 'semver';
Expand All @@ -22,6 +22,7 @@ import {
Cron,
validateNpmrc,
type FlagDefinitions,
getInstalledPackageVersion,
} from '@vercel/build-utils';
import {
detectBuilders,
Expand Down Expand Up @@ -71,7 +72,6 @@ import { setMonorepoDefaultSettings } from '../../util/build/monorepo';
import { help } from '../help';
import { buildCommand } from './command';
import { scrubArgv } from '../../util/build/scrub-argv';
import { cwd } from 'process';

type BuildResult = BuildResultV2 | BuildResultV3;

Expand Down Expand Up @@ -253,8 +253,11 @@ export default async function main(client: Client): Promise<number> {
output.debug(`Loaded environment variables from "${envPath}"`);
}

// For Vercel Legacy speed Insights support
// For legacy Speed Insights
if (project.settings.analyticsId) {
// we pass the env down to the builder
// inside the builder we decide if we want to keep it or not

envToUnset.add('VERCEL_ANALYTICS_ID');
process.env.VERCEL_ANALYTICS_ID = project.settings.analyticsId;
}
Expand Down Expand Up @@ -584,8 +587,7 @@ async function doBuild(
}

let needBuildsJsonOverride = false;
const speedInsightsVersion = await readInstalledVersion(
client,
const speedInsightsVersion = await getInstalledPackageVersion(
'@vercel/speed-insights'
);
if (speedInsightsVersion) {
Expand All @@ -595,8 +597,7 @@ async function doBuild(
};
needBuildsJsonOverride = true;
}
const webAnalyticsVersion = await readInstalledVersion(
client,
const webAnalyticsVersion = await getInstalledPackageVersion(
'@vercel/analytics'
);
if (webAnalyticsVersion) {
Expand Down Expand Up @@ -859,21 +860,3 @@ async function writeFlagsJSON(
async function writeBuildJson(buildsJson: BuildsManifest, outputDir: string) {
await fs.writeJSON(join(outputDir, 'builds.json'), buildsJson, { spaces: 2 });
}

export async function readInstalledVersion(
{ output }: Client,
pkgName: string
): Promise<string | undefined> {
try {
const descriptorPath = require.resolve(`${pkgName}/package.json`, {
paths: [cwd()],
});
const descriptor = await readJSON(descriptorPath);
return descriptor?.version;
} catch (err) {
output.debug(
`Package ${pkgName} is not installed (failed to read its package.json: ${err})`
);
}
return;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
!.vercel
public/env.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"orgId": ".",
"projectId": ".",
"settings": {
"analyticsId": "12345"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const fs = require('fs');
fs.mkdirSync('public', { recursive: true });
fs.writeFileSync('public/env.json', JSON.stringify(process.env));

module.exports = {};

0 comments on commit 1e6323e

Please sign in to comment.