Optimizing Ubermenu/Mega Menu for CWV

Here is a quick snippet on how to optimise the mega menu for CWV and improve on TBT.

Assuming your code is like this

 

Make the changes so that it looks something like,

 

What I have changed is

  1. Wrapped the wp.menu shortcode within template tag,
  2. Closed the primary_menu div
  3. Added a class “js-primary-ubermenu” to the div, you can add any class as long as it is unique on the page. This class is used as the selector in the template tag
  4. I have added an inline style to set the height of the DIV so that CLS is not impacted.

When spa.js loads and executes, it retrieves the HTML within the template tag and inserts it into the correct location.

In case your mega menu needs to be opened on click instead of hover, put the following code

A 300 ms delay is added to ensure that user experience, page speed, or CWV is not impacted.

Updated on Apr 21, 2025