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:

    The backend renders different HTML for mobile vs desktop visitors (open=true vs open=false in the embed script).

  • Varnish caches only one variant per URL, so mobile users may receive the desktop version (which auto-opens the chat bubble), or vice versa.
  • 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

  1. 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.

  2. JavaScript

    • oswald.js now handles viewport detection using window.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.yml updated to include dependency core/drupalSettings.

  3. Caching

    • Backend HTML is now identical for all devices → Varnish and Drupal page cache remain valid.

    • Only frontend JavaScript decides on behavior per viewport.

Comments

marco van bemmel created an issue. See original summary.

marco van bemmel’s picture

Component: User interface » Code
marco van bemmel’s picture

marco van bemmel’s picture

marco van bemmel’s picture

marco van bemmel’s picture

Title: Move “Prevent automatic opening on mobile devices” logic to JavaScript to avoid cache issues + add configurable mobile breakpoint » Move mobile detection from PHP to JS to avoid cache issues
bramdriesen’s picture

Version: 4.0.0-alpha11 » 4.0.x-dev
Assigned: marco van bemmel » Unassigned
Status: Active » Needs review
Issue tags: +Needs merge request

  • wouters_f committed 5df91719 on 4.0.x
    Issue #3557364: Move mobile detection from PHP to JS to avoid cache...
wouters_f’s picture

Status: Needs review » Fixed

Applied 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

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

  • wouters_f committed 5df91719 on 3601380-d12-hooks
    Issue #3557364: Move mobile detection from PHP to JS to avoid cache...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.