Skip to content

SDK-2871 Optimize Bundle Size by Using Simple UserAgent Parsing#1342

Merged
fadi-george merged 12 commits intomainfrom
fadi/sdk-2871-use-simpler-user-agent-helpers
Aug 6, 2025
Merged

SDK-2871 Optimize Bundle Size by Using Simple UserAgent Parsing#1342
fadi-george merged 12 commits intomainfrom
fadi/sdk-2871-use-simpler-user-agent-helpers

Conversation

@fadi-george
Copy link
Copy Markdown
Contributor

@fadi-george fadi-george commented Jul 29, 2025

Description

1 Line Summary

  • remove bowser library in favor of simple user agent parsing

Details

  • removes bowser library and bowserCastle helpers
  • creates user agent shared helpers for determining browser name, version, is a mobile browser, is a tablet browser, etc.
  • removes mockUserAgent test helper by updating setup tests to set a default user agent
  • moves some helpers from utils/utils to helpers/general, helpers/dom for better organization
  • split out event helpers logic to shared/listeners that are more tree-shake-able
# Previous Build fe3613
  build/releases/OneSignalSDK.page.js
  Size limit: 631 B
  Size:       631 B gzipped
  
  build/releases/OneSignalSDK.page.es6.js
  Size limit: 63.75 kB
  Size:       63.68 kB gzipped
  
  build/releases/OneSignalSDK.sw.js
  Size limit: 34 kB
  Size:       33.96 kB gzipped
  
  build/releases/OneSignalSDK.page.styles.css
  Size limit: 8.81 kB
  Size:       8.8 kB  gzipped
# Current Branch
  build/releases/OneSignalSDK.page.js
  Size limit: 631 B
  Size:       631 B gzipped
  
  build/releases/OneSignalSDK.page.es6.js
  Size limit: 60.6 kB
  Size:       60.53 kB gzipped
  
  build/releases/OneSignalSDK.sw.js
  Size limit: 25.25 kB
  Size:       25.19 kB gzipped
  
  build/releases/OneSignalSDK.page.styles.css
  Size limit: 8.81 kB
  Size:       8.8 kB  gzipped

Screenshots

iPad - Safari Desktop Browsing

Screenshot 2025-08-05 at 12 36 19 PM

iPad- Request Mobile Site

Screenshot 2025-08-05 at 12 35 56 PM

iPhone Safari:

Screenshot 2025-08-05 at 12 28 47 PM

macOS Safari:

Screenshot 2025-08-05 at 12 38 32 PM

Firefox:

Screenshot 2025-08-05 at 12 40 53 PM

Opera:

Screenshot 2025-08-05 at 12 45 53 PM

Edge:

Screenshot 2025-08-05 at 12 46 19 PM

Chrome:

Screenshot 2025-08-05 at 12 47 16 PM Screenshot 2025-08-05 at 12 47 39 PM

Systems Affected

  • WebSDK
  • Backend
  • Dashboard

Validation

Tests

Info

Checklist

  • All the automated tests pass or I explained why that is not possible
  • I have personally tested this on my machine or explained why that is not possible
  • I have included test coverage for these changes or explained why they are not needed

Programming Checklist
Interfaces:

  • Don't use default export
  • New interfaces are in model files

Functions:

  • Don't use default export
  • All function signatures have return types
  • Helpers should not access any data but rather be given the data to operate on.

Typescript:

  • No Typescript warnings
  • Avoid silencing null/undefined warnings with the exclamation point

Other:

  • Iteration: refrain from using elem of array syntax. Prefer forEach or use map
  • Avoid using global OneSignal accessor for context if possible. Instead, we can pass it to function/constructor so that we don't call OneSignal.context

Info

Checklist

  • I have included screenshots/recordings of the intended results or explained why they are not needed

Related Tickets



This change is Reviewable

@fadi-george fadi-george marked this pull request as draft July 30, 2025 08:22
@fadi-george fadi-george force-pushed the fadi/sdk-2871-use-simpler-user-agent-helpers branch from f6603b1 to 8e4f09c Compare July 30, 2025 18:38
@fadi-george fadi-george marked this pull request as ready for review July 31, 2025 01:48
@fadi-george fadi-george force-pushed the fadi/sdk-2871-use-simpler-user-agent-helpers branch from 29eb2c7 to 2c2c9e8 Compare July 31, 2025 23:28
@fadi-george fadi-george force-pushed the fadi/sdk-2871-use-simpler-user-agent-helpers branch from 2c2c9e8 to b7667b0 Compare August 1, 2025 22:00
@fadi-george fadi-george changed the title Optimize Bundle Size by Using Simple UserAgent Parsing SDK-2871 Optimize Bundle Size by Using Simple UserAgent Parsing Aug 4, 2025
Copy link
Copy Markdown
Contributor

@sherwinski sherwinski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI the following test is failing locally on this branch, it might be flaky:

src/onesignal/OneSignal.test.ts > OneSignal > Custom Events > custom event can execute before login for an existing user w/ no external id
Error: Timed out in waitUntil!

Comment on lines +49 to +51
(getBrowserName() === Browser.Chrome &&
(isTabletBrowser() || isMobileBrowser())) ||
requiresUserInteraction()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is still important today but we're missing this check:

Number(browserVersion) >= 63

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added back

Comment on lines +68 to +115
/* S T A T I C */

/**
* Get the User Model Subscription type based on browser detection.
*/
public static getSubscriptionType(): SubscriptionTypeValue {
const browserName = getBrowserName();
if (browserName === Browser.Firefox) {
return SubscriptionType.FirefoxPush;
}
if (useSafariVapidPush()) {
return SubscriptionType.SafariPush;
}
if (useSafariLegacyPush) {
return SubscriptionType.SafariLegacyPush;
}
// Other browsers, like Edge, are Chromium based so we consider them "Chrome".
return SubscriptionType.ChromePush;
}

/**
* Get the legacy player.device_type
* NOTE: Use getSubscriptionType() instead when possible.
*/
public static getDeviceType(): DeliveryPlatformKindValue {
switch (this.getSubscriptionType()) {
case SubscriptionType.FirefoxPush:
return DeliveryPlatformKind.Firefox;
case SubscriptionType.SafariLegacyPush:
return DeliveryPlatformKind.SafariLegacy;
case SubscriptionType.SafariPush:
return DeliveryPlatformKind.SafariVapid;
}
return DeliveryPlatformKind.ChromeLike;
}

public static getDeviceOS(): string | number {
const browserVersion = getBrowserVersion();
return isNaN(browserVersion) ? -1 : browserVersion;
}

public static getDeviceModel(): string {
return navigator.platform;
}

public static getSdk(): string {
return String(VERSION);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'm a little confused why this logic is being moved to this file

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not intended, removed

Comment thread src/shared/useragent/useragent.ts Outdated
Comment on lines +42 to +52
export function isTabletBrowser(): boolean {
const ua = navigator.userAgent.toLowerCase();

const isIPad = /\bipad\b/.test(ua);
const isAndroidTablet = /android/.test(ua) && !/mobile/.test(ua); // Android tablets don't include "mobile"
const isWindowsTablet =
/windows/.test(ua) && /touch/.test(ua) && !/phone/.test(ua);
const isKindleOrFire = /kindle|silk|kf[a-z]{2,}/.test(ua); // Amazon devices

return isIPad || isAndroidTablet || isWindowsTablet || isKindleOrFire;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a resource we can link to that exhaustively lists the different user agents we can expect? It would be good for future reference.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see there's one mentioned here: __test__/constants/useragent.ts. Might be good to add a comment in this file to either reference that same URL or this file.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added comment, logic was based off detect-ua package

Comment thread src/shared/listeners/listeners.ts Outdated
Comment on lines +252 to +256
/**
* NOTE: This uses the OneSignal REST API POST /notifications with
* include_player_ids. This field will be dropped by 2025 so a
* replacement will needed by then.
*/
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still a concern? Something we might have to address in a future PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see a sendConvertedAPIRequests that reports a notification as clicked but I dont see post call anymore. This seems to be a misleading comment.

if (useSafariVapidPush()) {
return SubscriptionType.SafariPush;
}
if (useSafariLegacyPush) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be useSafariLegacyPush()?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made it a basic constant. Also Ill remove these since they are in shared/environment

Comment thread src/shared/helpers/dom.ts Outdated
@@ -0,0 +1,137 @@
import Log from 'src/sw/libraries/Log';
Copy link
Copy Markdown
Contributor

@sherwinski sherwinski Aug 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the difference between this Log and the one in src/shared/libraries/Log.ts?

Copy link
Copy Markdown
Contributor Author

@fadi-george fadi-george Aug 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be src/shared/libraries/Log. In future prs ill combine them together

@fadi-george fadi-george force-pushed the fadi/sdk-2871-use-simpler-user-agent-helpers branch 6 times, most recently from 747f4a3 to 5463db9 Compare August 6, 2025 21:42
- remove duplicate code
- add version check for prompts manager
@fadi-george fadi-george force-pushed the fadi/sdk-2871-use-simpler-user-agent-helpers branch from 5463db9 to b069192 Compare August 6, 2025 22:28
@fadi-george fadi-george requested a review from sherwinski August 6, 2025 22:29
@fadi-george fadi-george merged commit 5174ae4 into main Aug 6, 2025
4 checks passed
@fadi-george fadi-george deleted the fadi/sdk-2871-use-simpler-user-agent-helpers branch August 6, 2025 22:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants