Skip to content

Commit

Permalink
fix: make dfts-helper browser info utility SSR compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
Dafnik committed Oct 10, 2024
1 parent ecab41f commit b6462c9
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions libs/dfts-helper/src/lib/helper/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { st_isAvailable } from './storage/common';

export type BrowserInfo = ReturnType<typeof i_complete>;

export const i_screenSize = `${screen.width ?? ''} x ${screen.height ?? ''}`;
export function i_screenSize(): string {
if (typeof window === 'undefined') {
return '';
}
return `${window?.screen?.width ?? ''} x ${window?.screen?.height ?? ''}`;
}

export const i_cookieEnabled = () => {
let cookieEnabled = navigator.cookieEnabled;
Expand All @@ -14,7 +19,12 @@ export const i_cookieEnabled = () => {
return cookieEnabled;
};

export const i_mobile = /Mobile|mini|Fennec|Android|iP(ad|od|hone)/.test(navigator.appVersion);
export function i_mobile(): boolean {
if (typeof window === 'undefined') {
return false;
}
return /Mobile|mini|Fennec|Android|iP(ad|od|hone)/.test(navigator.appVersion);
}

/**
* @return {screenSize: string, name: string, version: string, majorVersion: number, mobile: boolean, os: string, osVersion: string, cookies: boolean, localStorage: boolean}
Expand All @@ -36,7 +46,7 @@ export const i_complete = (): {
const nAgt = navigator.userAgent;
let browser = 'Netscape';
let version = '4.0';
let majorVersion = 4;
let majorVersion: number;
let nameOffset, verOffset, ix;

// Opera
Expand Down Expand Up @@ -177,11 +187,11 @@ export const i_complete = (): {
}

return {
screenSize: i_screenSize,
screenSize: i_screenSize(),
name: browser,
version: version,
majorVersion: majorVersion,
mobile: i_mobile,
mobile: i_mobile(),
os: os,
osVersion: osVersion,
cookies: i_cookieEnabled(),
Expand Down

0 comments on commit b6462c9

Please sign in to comment.