Problem/Motivation
When the "Prevent automatic opening on mobile devices" option is enabled, the Oswald Chatbot module currently determines whether a visitor is using a mobile device server-side in PHP (via MobileDetect and isMobileRequest()).
This leads to Varnish and page cache inconsistencies because:
- Varnish caches only one variant per URL, so mobile users may receive the desktop version (which auto-opens the chat bubble), or vice versa.
The backend renders different HTML for mobile vs desktop visitors (open=true vs open=false in the embed script).
As a result, the chatbot may open automatically on mobile screens, even when “prevent auto open on mobile” is enabled.
Proposed resolution
Move the entire device-specific decision (“open or not on mobile”) to the JavaScript layer, and make the backend deliver a single, cache-safe variant.
Key changes
-
PHP backend
-
OswaldChatbot::getAutoOpen()no longer performs device detection. -
OswaldChatbot::getCacheTags()no longer includes device-dependent tags. -
In
OswaldChatbotController::attachAndInitialize():-
The embed script always uses
?open=false(the Oswald script never auto-opens by itself). -
Configuration values are passed via
drupalSettings.oswald:[ 'autoOpen' => TRUE/FALSE, 'preventAutoOpenMobile' => TRUE/FALSE, 'mobileBreakpoint' => (int) $chatbot->get('mobile_breakpoint')->value ?: 768, ]
-
-
Added a new base field
mobile_breakpoint(integer, default 768) so site builders can define what screen width counts as “mobile”. -
Added update hook
oswald_update_91004()to install this new field for existing entities.
-
-
JavaScript
-
oswald.jsnow handles viewport detection usingwindow.matchMedia('(max-width: Xpx)'). -
The decision logic runs client-side:
var shouldOpen = shouldOpenByUrl || (autoOpen && (!isMobile || !preventAutoOpenMobile)); -
This ensures:
-
On desktop: chat opens automatically if enabled.
-
On mobile: chat stays closed when “prevent auto open on mobile” is checked.
-
With
?openOswaldWidget=true: chat always opens (manual override).
-
-
oswald.libraries.ymlupdated to include dependencycore/drupalSettings.
-
-
Caching
-
Backend HTML is now identical for all devices → Varnish and Drupal page cache remain valid.
-
Only frontend JavaScript decides on behavior per viewport.
-
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | 3557364-oswald-auto-open-js-mobile-breakpoint.patch | 11.81 KB | marco van bemmel |
Comments
Comment #2
marco van bemmel commentedComment #3
marco van bemmel commentedComment #4
marco van bemmel commentedComment #5
marco van bemmel commentedComment #6
marco van bemmel commentedComment #7
bramdriesenComment #9
wouters_f commentedApplied the patch from the issue (3557364-oswald-auto-open-js-mobile-breakpoint_0.patch). Resolved a minor conflict in the event listener block (used the patch version which adds once() to prevent duplicate listeners). Merged into 4.0.x via MR !17: https://git.drupalcode.org/project/oswald/-/merge_requests/17