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

Commit 994c8ae

Browse files
authored
fix(metadata): stricter detection of user agent (#3184)
* 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
1 parent aa2eb9b commit 994c8ae

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

packages/react-instantsearch-core/src/core/__tests__/metadata.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ describe('isMetadataEnabled', () => {
4848
expect(isMetadataEnabled()).toBe(false);
4949
});
5050

51+
it("does not enable when there's no navigator (react native)", () => {
52+
setUserAgent(algoliaUserAgent);
53+
54+
// @ts-expect-error
55+
delete global.window;
56+
// @ts-expect-error
57+
global.window = {};
58+
59+
expect(isMetadataEnabled()).toBe(false);
60+
});
61+
5162
it('metadata enabled returns true', () => {
5263
setUserAgent(algoliaUserAgent);
5364

packages/react-instantsearch-core/src/core/metadata.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ import type { Widget } from './createWidgetsManager';
33

44
export function isMetadataEnabled() {
55
return (
6-
typeof window !== 'undefined' &&
7-
window.navigator.userAgent.includes('Algolia Crawler')
6+
typeof window === 'object' &&
7+
typeof window.navigator === 'object' &&
8+
typeof window.navigator.userAgent === 'string' &&
9+
window.navigator.userAgent.includes('Algolia Crawler') &&
10+
typeof window.document === 'object'
811
);
912
}
1013

0 commit comments

Comments
 (0)