Module breaks or blocks other javascript

Created on 9 November 2023, about 1 year ago
Updated 9 September 2024, 2 months ago

Problem/Motivation

On a Drupal 10.1 site, running Drupal Commerce, my add to cart form JavaScript is not firing when this module is enabled. However, once a session is established (login or cart session), the cart JavaScript works fine.

Steps to reproduce

Drupal 10, enable this module and sub-module, visit a commerce form with ajax like the add to cart form. Change a form element, see no ajax fire.

Proposed resolution

This appears to be related to the libraries `head: true` property as removing this fixes the issue.

Remaining tasks

Confirm, review by others, test...

πŸ› Bug report
Status

Needs review

Version

2.0

Component

Code (General)

Created by

πŸ‡ΊπŸ‡ΈUnited States andyg5000 North Carolina, USA

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @andyg5000
  • Status changed to Needs review about 1 year ago
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.1.4 + Environment: PHP 8.1 & MySQL 5.7
    last update about 1 year ago
    Patch Failed to Apply
  • πŸ‡ΊπŸ‡ΈUnited States andyg5000 North Carolina, USA

    Here's a patch that's fixing the issue for me.

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.1.4 + Environment: PHP 8.1 & MySQL 5.7
    last update about 1 year ago
    23 pass
  • πŸ‡ΊπŸ‡ΈUnited States andyg5000 North Carolina, USA
  • πŸ‡ΊπŸ‡ΈUnited States loze Los Angeles

    Im getting the following error when adding a product to the cart with ajax, both with and without this patch.

    If i disable the module it goes away.

    I'm using Drupal 9.5.11

    "
    An AJAX HTTP error occurred.
    HTTP Result Code: 200
    Debugging information follows.
    Path: /event/365052?ajax_form=1
    StatusText: OK
    ResponseText: TypeError: Drupal\commerce\Context::__construct(): Argument #1 ($customer) must be of type Drupal\Core\Session\AccountInterface, null given, called in /Users/XXXXX/web/modules/contrib/commerce/modules/cart/src/Form/AddToCartForm.php on line 248 in Drupal\commerce\Context->__construct() (line 58 of /Users/XXXXX/web/modules/contrib/commerce/src/Context.php)."

  • Status changed to Needs work 3 months ago
  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

    @andyg5000 could you please turn this into a MR?

    BTW should this eventually even use async? Is there any documentation from facebook available for best practice?

  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica
  • πŸ‡ΊπŸ‡ΈUnited States rhovland Oregon

    Meta's best practices guide says to place it in the HEAD
    https://www.facebook.com/business/help/218844828315224?id=1205376682832142

    This is to ensure that an ad conversion is counted. However it will also slow down page load which IMO is far more important to conversion than the metrics catching users that close the page while it's still loading.

  • Pipeline finished with Success
    2 months ago
    Total: 160s
    #275182
  • Pipeline finished with Success
    2 months ago
    Total: 146s
    #275187
  • Status changed to Needs review 2 months ago
  • πŸ‡ΊπŸ‡ΈUnited States rhovland Oregon
  • I'm working on this issue, Friday, September 6, to Update the Issue Summary. This is my first contribution, Feel free to correct me if I did wrong on this. Thank you.

  • Hey, we want to test this issue, but we're concerned that it will take a lot of time. Also, setting it up on the commerce site seems really complicated. Do you have any suggestions to make the process of creating a commerce site faster?

  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

    Based on #7 it should be a setting IMHO. What should be the default?

  • πŸ‡ΊπŸ‡ΈUnited States rhovland Oregon

    After examining the code closer it appears that the tracking code is already async. It however is not deferred. My best understanding of this is that the assets will load in parallel with the rest of the page but still be parsed as the page is loading. If it was deferred then the browser would wait until the page is loaded to request and parse the tracking script.

    I examined several other tracking modules and almost all of them attach their code in the HEAD as well. So the source of the bug is still unknown.

    Also there's some information missing here as commerce does not use ajax to submit the add to cart form. It only uses ajax when selecting variations. We need to know the name of the module that ajaxifies the add to cart button to properly debug this.

  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

    Thanks for your investigations @rhovland!
    Anyway we could still make this a setting defaulting to the suggestion from facebook to put it into the header and link the documentation.

    I'm unsure, but looks like generally this works as designed.

  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

    @andyg5000 @rhovland so focus on ✨ Add option to defer loading pixel Needs work ?

  • πŸ‡ΊπŸ‡ΈUnited States rhovland Oregon

    Since we don't know the cause of the original issue there's no way to know if deferring loading would work the same as moving it to the footer. There could be an issue in the module's code itself instead of the facebook pixel library. There could be a name collision or something similar going on. There could be problem with the way window is used.

    I don't have the needed javascript knowledge to examine the code and tell what's done correctly and what isn't.

    The libraries.yml entry encompasses the entire module's javascript code including the configuration checks. Deferring loading can either be done at the libraries.yml level (not really needed) or in the request for the facebook pixel code (an external resource that could slow down page loads).

Production build 0.71.5 2024