document.querySelector is null

Created on 24 June 2024, 6 months ago

Problem/Motivation

After upgrading to drupal core 10.1.x that is many errors in the console like

Uncaught TypeError: document.querySelector(...) is null
and

    Uncaught TypeError: Drupal.toolbar is undefined
    <anonymous> http://ah-vetportal.docker.amazee.io/core/modules/toolbar/js/models/MenuModel.js?v=10.1.8:14
    <anonymous> http://ah-vetportal.docker.amazee.io/core/modules/toolbar/js/models/MenuModel.js?v=10.1.8:29

And this makes the toolbar misplaced.

It seems that inside the Toolbar.js file, there is a missing class 'toolbar-oriented' in the toolbar:

if (isOriented) {
document
.querySelector('#toolbar-administration')
.classList.add('toolbar-oriented');
}

I could reproduce this issue using landing pages builder with layout builder.
But also document.querySelector('#toolbar-administration').classList.add('toolbar-oriented') is giving null using Claro admin theme.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

๐Ÿ› Bug report
Status

Active

Version

10.1 โœจ

Component
Toolbarย  โ†’

Last updated about 1 month ago

  • Maintained by
  • ๐Ÿ‡ซ๐Ÿ‡ทFrance @nod_
Created by

๐Ÿ‡ฆ๐Ÿ‡ทArgentina andreadruiz Buenos Aires

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

Comments & Activities

  • Issue created by @andreadruiz
  • ๐Ÿ‡ฆ๐Ÿ‡ทArgentina andreadruiz Buenos Aires
  • ๐Ÿ‡ฆ๐Ÿ‡ทArgentina andreadruiz Buenos Aires
  • ๐Ÿ‡ฆ๐Ÿ‡ทArgentina andreadruiz Buenos Aires
  • Can you please update the issue summary with instructions to build a site that exhibits this bug in Drupal 10.3?

    Could you include the complete JavaScript stack trace too?

  • Status changed to Needs review 6 months ago
  • ๐Ÿ‡ฆ๐Ÿ‡ทArgentina andreadruiz Buenos Aires
  • ๐Ÿ‡ฆ๐Ÿ‡ทArgentina andreadruiz Buenos Aires
  • Status changed to Needs work 6 months ago
  • Please create a merge request.

  • ๐Ÿ‡ป๐Ÿ‡ชVenezuela zahord

    I've seen a similar error in toolbar.js file where `document.querySelector` is not defined, so I added a validation to avoid the error.

  • Status changed to Needs review 4 months ago
  • ๐Ÿ‡ป๐Ÿ‡ชVenezuela zahord
  • Status changed to Needs work 4 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Issue summary appears incomplete and missing steps.

    Also all fixes should be in an MR against 11.x please

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany ammaletu Bonn, Germany

    I just ran into this on Drupal 10.2.8. It seems to happen always once you logged in as a user who can see the toolbar and then log out again. After comparing this with a clean Drupal 10.2 installation with Claro theme, I found the source of the problem: A colleague required the "toolbar/toolbar" library as a dependency in our theme, unconditionally. This way, the toolbar.js file was always loaded, even when no toolbar was being displayed.

    I added steps to reproduce in the issue summary. This is more or less a user error, but maybe it helps somebody else in the future. Of course, it would be nice if there was a way to clear this session storage value when switching sessions. Maybe add something after the "Add anti-flicker functionality." block, starting in toolbar.js on line 241. If "#toolbar-administration" is not a part of the page, can we then delete the session storage value? This would help in cases where this library is loaded withput a toolbar.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia mdsohaib4242

    To fix this issue, update Toolbar.js to first check if #toolbar-administration actually exists before trying to apply any classes to it. This way, the script wonโ€™t attempt to modify elements that arenโ€™t there, something like this

    const toolbarElement = document.querySelector('#toolbar-administration');
    if (toolbarElement && isOriented) {
      toolbarElement.classList.add('toolbar-oriented');
    }
    
Production build 0.71.5 2024