Uncaught TypeError: once is not a function

Created on 28 September 2022, about 2 years ago
Updated 27 June 2023, over 1 year ago

Problem/Motivation

I have just update the website from latest D8 to D9.4.7 and now I am getting bunch of errors in console. Website loads fine but it looks like jquery once library is not loaded at all.

As far as I understand, jquery.once was removed in 9.3, but also it looks like bunch of core files depend on this library.

Uncaught TypeError: once is not a function
    at Object.attach (contextual.js?v=9.4.7:102:29)
    at drupal.js?v=9.4.7:27:24
    at Array.forEach (<anonymous>)
    at Drupal.attachBehaviors (drupal.js?v=9.4.7:24:34)
    at drupal.init.js?v=9.4.7:29:12
    at HTMLDocument.listener (drupal.init.js?v=9.4.7:17:7)
Uncaught TypeError: once is not a function
    at Drupal.ajax.bindAjaxLinks (ajax.js?v=9.4.7:142:5)
    at Object.attach (ajax.js?v=9.4.7:42:19)
    at drupal.js?v=9.4.7:27:24
    at Array.forEach (<anonymous>)
    at Drupal.attachBehaviors (drupal.js?v=9.4.7:24:34)
    at drupal.init.js?v=9.4.7:29:12
    at HTMLDocument.listener (drupal.init.js?v=9.4.7:17:7)
Uncaught TypeError: once is not a function
    at Object.attach (toolbar.js?v=9.4.7:27:7)
    at drupal.js?v=9.4.7:27:24
    at Array.forEach (<anonymous>)
    at Drupal.attachBehaviors (drupal.js?v=9.4.7:24:34)
    at drupal.init.js?v=9.4.7:29:12
    at HTMLDocument.listener (drupal.init.js?v=9.4.7:17:7)

... etc.

The same errors goes for: tour.js?v=9.4.7:12:7, contextual.toolbar.js?v=9.4.7:37:11, escapeAdmin.js?v=9.4.7:19:27, big_pipe.js?v=9.4.7:50:5

When I click on those files, all are located in in core/modules and all are using syntax like once(something). I am not sure whether this is due to my upgrade or not.

I tried adding core/once as library dependency on my theme file but it had no effect, I guess theme files are loaded way after core modules but also I do not know how to solve that. I tried also adding this code to my module:

/**
 * Implements hook_library_info_alter().
 */
function MYMODULE_library_info_alter(&$libraries, $extension) {
  foreach ($libraries as &$library) {
    if (!in_array('core/jquery.once', $library['dependencies'] ?? [])) {
      continue;
    }
    $dependencies = array_filter($library['dependencies'] ?? [], function ($item) {
      return $item != 'core/jquery.once';
    });
    $dependencies[] = 'core/once';
    $dependencies[] = 'core/jquery.once.bc';
    $library['dependencies'] = $dependencies;
  }
}

But it unfortunately also had no effect. It looks like apart from that upgrade all went smooth so this is the only thing bothering me now.

πŸ’¬ Support request
Status

Closed: works as designed

Version

9.4

Component
JavascriptΒ  β†’

Last updated 2 days ago

Created by

πŸ‡ΈπŸ‡°Slovakia lubwn

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States loopy1492

    As we go from d9 to d10, modules are starting to remove the jquery.once dependency en masse. When a site has finally upgraded all of their modules, the dependency will be gone and any custom code that was using .once will no longer work. Anyone searching for this issue should refer to https://www.drupal.org/node/3158256 β†’ where it shows us that it's not enough to just swap out the libraries reference, but also to change how the function is used in the code itself.

  • adding jquery.once to the dependencies works for me on drupal 9.5x

Production build 0.71.5 2024