|
| 1 | +interface BrowserProperties { |
| 2 | + viewport?: string | null; |
| 3 | + documentSize?: string | null; |
| 4 | + resolution?: string | null; |
| 5 | + colorDepth: number; |
| 6 | + devicePixelRatio: number; |
| 7 | + cookiesEnabled: boolean; |
| 8 | + online: boolean; |
| 9 | + browserLanguage: string; |
| 10 | + documentLanguage: string; |
| 11 | + webdriver: boolean; |
| 12 | + deviceMemory?: number; // Optional because it's not supported in all browsers |
| 13 | + hardwareConcurrency?: number; |
| 14 | +} |
| 15 | + |
| 16 | +function useResizeObserver(): boolean { |
| 17 | + return 'ResizeObserver' in window; |
| 18 | +} |
| 19 | + |
| 20 | +let resizeObserverInitialized = false; |
| 21 | +function initializeResizeObserver() { |
| 22 | + if (resizeObserverInitialized) { |
| 23 | + return; |
| 24 | + } |
| 25 | + resizeObserverInitialized = true; |
| 26 | + |
| 27 | + const resizeObserver = new ResizeObserver((entries) => { |
| 28 | + for (let entry of entries) { |
| 29 | + if (entry.target === document.body || entry.target === document.documentElement) { |
| 30 | + cachedProperties = readBrowserProperties(); |
| 31 | + } |
| 32 | + } |
| 33 | + }); |
| 34 | + resizeObserver.observe(document.body); |
| 35 | + resizeObserver.observe(document.documentElement); |
| 36 | +} |
| 37 | + |
| 38 | +let cachedProperties: BrowserProperties; |
| 39 | + |
1 | 40 | /* Separator used for dimension values e.g. widthxheight */ |
2 | 41 | const DIMENSION_SEPARATOR = 'x'; |
3 | 42 |
|
| 43 | +/** |
| 44 | + * Gets various browser properties (that are expensive to read!) |
| 45 | + * - Will use a "ResizeObserver" approach in modern browsers to update cached properties only on change |
| 46 | + * - Will fallback to a direct read approach without cache in old browsers |
| 47 | + * |
| 48 | + * @returns BrowserProperties |
| 49 | + */ |
4 | 50 | export function getBrowserProperties() { |
| 51 | + if (!useResizeObserver()) { |
| 52 | + return readBrowserProperties(); |
| 53 | + } |
| 54 | + |
| 55 | + if (!cachedProperties) { |
| 56 | + cachedProperties = readBrowserProperties(); |
| 57 | + } |
| 58 | + initializeResizeObserver(); |
| 59 | + return cachedProperties; |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Reads the browser properties - expensive call! |
| 64 | + * |
| 65 | + * @returns BrowserProperties |
| 66 | + */ |
| 67 | +function readBrowserProperties(): BrowserProperties { |
5 | 68 | return { |
6 | 69 | viewport: floorDimensionFields(detectViewport()), |
7 | 70 | documentSize: floorDimensionFields(detectDocumentSize()), |
|
0 commit comments