Make WordPress Core

#63613 closed defect (bug) (fixed)

Twenty Twenty-One: menu toggle is not responsive until after "load" event

Reported by: gernberg Owned by: westonruter
Priority: normal Milestone: 6.8.2
Component: Bundled Theme Version: 5.6
Severity: minor Keywords: fixed-major dev-reviewed
Cc: Focuses: javascript

Description

The bundled theme "Twenty Twenty-One" primary navigation / menu toggle is not functional until the load event triggers on mobile.

This means that the menu might be unusable for multiple seconds.

Can be reproduced by:

  1. Pick a page with this theme and some heavier resources / images.
  2. Simulate a mobile browser + slow connection speed.
  3. Attempt to open the primary navigation between the DOMContentLoaded (DCL) and "load" events.

Reading the code for primary-navigation.js, it looks to me like the only dependency for this script would be that the DOM element for the menu exists.

As of #59316, the script is loading as defer, meaning that the required DOM elements for the menu will always be ready by the time primary-navigation.js executes.

I would suggest to improve page responsiveness by removing the event listener completely and initializing the menu immediately when primary-navigation.js executes.

One alternative considered would be to change from "load" to "DOMContentLoaded", but I don't think that brings any benefits compared to just getting rid of the event listener logic completely.

Attachments (2)

63613.diff (544 bytes ) - added by gernberg 14 months ago.
Suggested patch
src-base.diff (566 bytes ) - added by gernberg 14 months ago.
Suggested fix with src/ folder included.

Download all attachments as: .zip

Change History (13)

@gernberg
14 months ago

Suggested patch

@gernberg
14 months ago

Suggested fix with src/ folder included.

#1 @sabernhardt
14 months ago

  • Focuses javascript added
  • Summary Bundled theme "twentytwentyone" menu toggle is not responsive until after "load" eventTwenty Twenty-One: menu toggle is not responsive until after "load" event
  • Version trunk5.6

#2 @westonruter
14 months ago

I'd love to see the mobile menu reimplemented with dialog and Invoker Commands to eliminate the JavaScript dependency altogether, at some point. See also: https://github.com/WordPress/gutenberg/pull/69720#issuecomment-2795480829

#3 @westonruter
14 months ago

  • Keywords has-patch added
  • Milestone Awaiting Review6.9
  • Owner set to westonruter
  • Status newaccepted

I agree there is no reason to wait for the load event to initialize the nav menu.

@gernberg thanks for reporting this great issue and providing a patch!

#4 @westonruter
14 months ago

  • Milestone 6.96.8.2

Since there is another ticket (#60196) for this theme milestoned for the next minor release, I'll milestone this ticket likewise.

#5 @westonruter
14 months ago

I'm iterating on the patch to opt for the DOMContentLoaded event. Even though defer is used, it is possible (although unlikely) that another dependent script could be registered which does not have the defer strategy, in which case this primary-navigation.js script would fall back to the blocking loading strategy as well. Since the script is printed in the head this would result in the nav menu not working at all since the script would be executed before the body is constructed in the DOM. So using the DOMContentLoaded will ensure the nav menu is constructed after the the DOM is completely constructed, regardless of the loading strategy.

#6 @westonruter
14 months ago

  • Resolutionfixed
  • Status acceptedclosed

In 60352:

Twenty Twenty-One: Add nav menu toggle behaviors at DOMContentLoaded instead of at the load event.

The load event (on window) fires after all resources on a page have been loaded, including images. This means that
on a slow connection, the mobile nav menu button may be rendered yet unresponsive to taps between when it first appears
and when the page has fully loaded. This is rectified by switching to the DOMContentLoaded event so that the nav menu
behaviors are attached as soon as the DOM has fully loaded.

Props gernberg, westonruter, sabernhardt.
Fixes #63613.

#7 @westonruter
14 months ago

  • Keywords fixed-major added
  • Resolution fixed
  • Status closedreopened

Re-opening for 6.8.2 consideration.

This ticket was mentioned in Slack in #core by audrasjb. View the logs.


14 months ago

#9 @audrasjb
14 months ago

  • Keywords dev-reviewed added; has-patch removed

We ran into this ticket during today's 6.8.2 bug scrub.
The changeset is pretty straightforward and it makes sense to include such a small change given we already have another issue milestoned to 6.8.2 for TT1.

Marking this as dev-reviewed (double committer sign-off) so it is ready for a 6.8 backport.

This ticket was mentioned in Slack in #core-themes by sabernhardt. View the logs.


13 months ago

#11 @audrasjb
13 months ago

  • Resolutionfixed
  • Status reopenedclosed

In 60423:

Twenty Twenty-One: Add nav menu toggle behaviors at DOMContentLoaded instead of at the load event.

The load event (on window) fires after all resources on a page have been loaded, including images. This means that
on a slow connection, the mobile nav menu button may be rendered yet unresponsive to taps between when it first appears
and when the page has fully loaded. This is rectified by switching to the DOMContentLoaded event so that the nav menu
behaviors are attached as soon as the DOM has fully loaded.

Reviewed by audrasjb.
Merges [60352] to the 6.8 branch.
Props gernberg, westonruter, sabernhardt.
Fixes #63613.

Note: See TracTickets for help on using tickets.