Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
fix(metadata): stricter detection of user agent (#3184)
Browse files Browse the repository at this point in the history
* fix(metadata): stricter detection of user agent

1. react native has window, but no navigator
2. in potentially other environments document might be undefined

* add test
  • Loading branch information
Haroenv committed Oct 27, 2021
1 parent aa2eb9b commit 994c8ae
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
11 changes: 11 additions & 0 deletions packages/react-instantsearch-core/src/core/__tests__/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ describe('isMetadataEnabled', () => {
expect(isMetadataEnabled()).toBe(false);
});

it("does not enable when there's no navigator (react native)", () => {
setUserAgent(algoliaUserAgent);

// @ts-expect-error
delete global.window;
// @ts-expect-error
global.window = {};

expect(isMetadataEnabled()).toBe(false);
});

it('metadata enabled returns true', () => {
setUserAgent(algoliaUserAgent);

Expand Down
7 changes: 5 additions & 2 deletions packages/react-instantsearch-core/src/core/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import type { Widget } from './createWidgetsManager';

export function isMetadataEnabled() {
return (
typeof window !== 'undefined' &&
window.navigator.userAgent.includes('Algolia Crawler')
typeof window === 'object' &&
typeof window.navigator === 'object' &&
typeof window.navigator.userAgent === 'string' &&
window.navigator.userAgent.includes('Algolia Crawler') &&
typeof window.document === 'object'
);
}

Expand Down

0 comments on commit 994c8ae

Please sign in to comment.