diff --git a/packages/autocomplete-preset-algolia/src/results.ts b/packages/autocomplete-preset-algolia/src/results.ts index 8e6beacc8..697bd69d2 100644 --- a/packages/autocomplete-preset-algolia/src/results.ts +++ b/packages/autocomplete-preset-algolia/src/results.ts @@ -18,9 +18,7 @@ interface GetAlgoliaSourceParams { function getAlgoliaSource({ searchClient, queries }: GetAlgoliaSourceParams) { if (typeof (searchClient as Client).addAlgoliaAgent === 'function') { - (searchClient as Client).addAlgoliaAgent( - `autocomplete-core (${__VERSION__})` - ); + (searchClient as Client).addAlgoliaAgent('autocomplete-core', __VERSION__); } return searchClient.search( diff --git a/packages/autocomplete-react/src/getAlgoliaHits.ts b/packages/autocomplete-react/src/getAlgoliaHits.ts index 170dd5954..c8069d933 100644 --- a/packages/autocomplete-react/src/getAlgoliaHits.ts +++ b/packages/autocomplete-react/src/getAlgoliaHits.ts @@ -10,7 +10,7 @@ interface GetAlgoliaSourceParams { export function getAlgoliaHits(params: GetAlgoliaSourceParams) { if (typeof params.searchClient.addAlgoliaAgent === 'function') { - params.searchClient.addAlgoliaAgent(`autocomplete-react (${__VERSION__})`); + params.searchClient.addAlgoliaAgent('autocomplete-react', __VERSION__); } return originalGetAlgoliaHits(params); diff --git a/packages/autocomplete-react/src/getAlgoliaResults.ts b/packages/autocomplete-react/src/getAlgoliaResults.ts index e929fe4bc..75a3c44bc 100644 --- a/packages/autocomplete-react/src/getAlgoliaResults.ts +++ b/packages/autocomplete-react/src/getAlgoliaResults.ts @@ -10,7 +10,7 @@ interface GetAlgoliaSourceParams { export function getAlgoliaResults(params: GetAlgoliaSourceParams) { if (typeof params.searchClient.addAlgoliaAgent === 'function') { - params.searchClient.addAlgoliaAgent(`autocomplete-react (${__VERSION__})`); + params.searchClient.addAlgoliaAgent('autocomplete-react', __VERSION__); } return originalGetAlgoliaResults(params); diff --git a/packages/docsearch-js/src/docsearch.tsx b/packages/docsearch-js/src/docsearch.tsx index 24225d3a6..902dc44c7 100644 --- a/packages/docsearch-js/src/docsearch.tsx +++ b/packages/docsearch-js/src/docsearch.tsx @@ -18,7 +18,7 @@ export function docsearch(props) { { - searchClient.addAlgoliaAgent(`docsearch.js (${version})`); + searchClient.addAlgoliaAgent('docsearch.js', version); return props.transformSearchClient ? props.transformSearchClient(searchClient) diff --git a/packages/docsearch-react/src/useSearchClient.ts b/packages/docsearch-react/src/useSearchClient.ts index 015833708..afa9b2447 100644 --- a/packages/docsearch-react/src/useSearchClient.ts +++ b/packages/docsearch-react/src/useSearchClient.ts @@ -11,7 +11,18 @@ export function useSearchClient( ): SearchClient { const searchClient = React.useMemo(() => { const client = algoliasearch(appId, apiKey); - client.addAlgoliaAgent(`docsearch (${version})`); + client.addAlgoliaAgent('docsearch', version); + + // Since DocSearch.js relies on DocSearch React with an alias to Preact, + // we cannot add the `docsearch-react` user agent by default, otherwise + // it would also be sent on a DocSearch.js integration. + // We therefore only add the `docsearch-react` user agent if `docsearch.js` + // is not present. + if ( + /docsearch.js \(.*\)/.test(client.transporter.userAgent.value) === false + ) { + client.addAlgoliaAgent('docsearch-react', version); + } return transformSearchClient(client); }, [appId, apiKey, transformSearchClient]); diff --git a/packages/website/src/theme/SearchBar/index.js b/packages/website/src/theme/SearchBar/index.js index 243b522cd..068a196e5 100644 --- a/packages/website/src/theme/SearchBar/index.js +++ b/packages/website/src/theme/SearchBar/index.js @@ -6,11 +6,7 @@ import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; import { useHistory } from '@docusaurus/router'; import Link from '@docusaurus/Link'; import Head from '@docusaurus/Head'; -import { - DocSearchButton, - useDocSearchKeyboardEvents, - version as docSearchVersion, -} from '@docsearch/react'; +import { DocSearchButton, useDocSearchKeyboardEvents } from '@docsearch/react'; let DocSearchModal = null; @@ -42,7 +38,8 @@ function transformItems(items) { } function transformSearchClient(searchClient) { - searchClient.addAlgoliaAgent(`docsearch-docusaurus (${docSearchVersion})`); + // @TODO: attach Docusaurus version to user agent + searchClient.addAlgoliaAgent('docusaurus'); return searchClient; }