diff --git a/packages/docusaurus-plugin-vercel-analytics/package.json b/packages/docusaurus-plugin-vercel-analytics/package.json index 271be8d36325..e4b8ef4f780b 100644 --- a/packages/docusaurus-plugin-vercel-analytics/package.json +++ b/packages/docusaurus-plugin-vercel-analytics/package.json @@ -19,6 +19,7 @@ "license": "MIT", "dependencies": { "@docusaurus/core": "3.0.0", + "@docusaurus/logger": "3.0.0", "@docusaurus/types": "3.0.0", "@docusaurus/utils-validation": "3.0.0", "@docusaurus/utils": "3.0.0", diff --git a/packages/docusaurus-plugin-vercel-analytics/src/__tests__/options.test.ts b/packages/docusaurus-plugin-vercel-analytics/src/__tests__/options.test.ts index 14dd087b7404..41af7e32d0b7 100644 --- a/packages/docusaurus-plugin-vercel-analytics/src/__tests__/options.test.ts +++ b/packages/docusaurus-plugin-vercel-analytics/src/__tests__/options.test.ts @@ -29,13 +29,14 @@ describe('validateOptions', () => { expect(testValidateOptions(undefined)).toEqual(validationResult(undefined)); }); - // TODO: fix this test - // it('throws for custom id', () => { - // const config: Options = {id: 'custom', mode: 'auto', debug: false}; - // expect(() => - // testValidateOptions(config), - // ).toThrowErrorMatchingInlineSnapshot(`""value" must be of type object"`); - // }); + it('throws for custom id', () => { + const config: Options = {id: 'custom', mode: 'auto', debug: false}; + expect(() => testValidateOptions(config)) + .toThrowErrorMatchingInlineSnapshot(` + "You site uses the Vercel Analytics plugin with a custom plugin id (custom). + But this plugin is only supposed to be used at most once per site. Therefore providing a custom plugin id is unsupported." + `); + }); it('accept for default id', () => { const config: Options = {id: 'default', mode: 'auto', debug: false}; diff --git a/packages/docusaurus-plugin-vercel-analytics/src/index.ts b/packages/docusaurus-plugin-vercel-analytics/src/index.ts index 50040afdf82b..c05d1cefe83a 100644 --- a/packages/docusaurus-plugin-vercel-analytics/src/index.ts +++ b/packages/docusaurus-plugin-vercel-analytics/src/index.ts @@ -5,7 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -import {DEFAULT_PLUGIN_ID} from '@docusaurus/utils'; import type {LoadContext, Plugin} from '@docusaurus/types'; import type {PluginOptions, Options} from './options'; @@ -13,12 +12,6 @@ export default function pluginVercelAnalytics( context: LoadContext, options: PluginOptions, ): Plugin { - if (options.id !== DEFAULT_PLUGIN_ID) { - throw new Error( - `You cannot use a custom plugin id option with the Vercel Analytics plugin`, - ); - } - const isProd = process.env.NODE_ENV === 'production'; return { diff --git a/packages/docusaurus-plugin-vercel-analytics/src/options.ts b/packages/docusaurus-plugin-vercel-analytics/src/options.ts index 253aca21dbac..754866ab0c2d 100644 --- a/packages/docusaurus-plugin-vercel-analytics/src/options.ts +++ b/packages/docusaurus-plugin-vercel-analytics/src/options.ts @@ -5,6 +5,7 @@ * LICENSE file in the root directory of this source tree. */ import {DEFAULT_PLUGIN_ID} from '@docusaurus/utils'; +import logger from '@docusaurus/logger'; import {Joi} from '@docusaurus/utils-validation'; import type {OptionValidationContext} from '@docusaurus/types'; @@ -17,14 +18,25 @@ export type PluginOptions = { export type Options = Partial; const pluginOptionsSchema = Joi.object({ - id: Joi.string().valid(DEFAULT_PLUGIN_ID).default(DEFAULT_PLUGIN_ID), mode: Joi.string().valid('auto', 'production', 'development').optional(), debug: Joi.boolean().optional(), }); +// We can't validate this through the schema +// Docusaurus core auto registers the id field to the schema already +function ensureNoMultiInstance(options: Options) { + if (options?.id && options.id !== DEFAULT_PLUGIN_ID) { + throw new Error( + logger.interpolate`You site uses the Vercel Analytics plugin with a custom plugin id (name=${options.id}). + But this plugin is only supposed to be used at most once per site. Therefore providing a custom plugin id is unsupported.`, + ); + } +} + export function validateOptions({ validate, options, }: OptionValidationContext): PluginOptions { + ensureNoMultiInstance(options); return validate(pluginOptionsSchema, options); }