Documentation
Feedback
Guides
Learning Center

Learning Center
Learning Center
Submenu optimization
View in Portuguese
This content was migrated from the VTEX Learning Center and is no longer being actively maintained. For the most accurate and up-to-date information, please check the official documentation.

In the previous step, we learned how to optimize menu items, making them menu properties instead of child blocks. Some menus, however, have other submenus, such as that of Store Theme:

{"base64":"  ","img":{"width":2076,"height":893,"type":"png","mime":"image/png","wUnits":"px","hUnits":"px","length":506090,"url":"https://user-images.githubusercontent.com/18701182/93831521-5d848700-fc49-11ea-9773-c2d727013f95.png"}}

These behave like a layout, so there are no optimizations that can be done other than using the children behavior. For these cases, the ideal is to go for a hybrid solution, where the parent menus are used with children and the leaf menus (last in the menu tree) can be used as props.

However, as the user will not see the contents of the submenus when the page is loaded, we can postpone the loading of its content.

Activity

  1. To understand how we can optimize submenus, let's modify our example a little to be more similar to what we have in the Store Theme, start by editing the name of the block we worked on in the previous step ("vtex.menu@2.x: menu#category-menu ") to "vtex.menu@2.x: menu#categories":


    _10
    // store/blocks/header/category-menu.jsonc
    _10
    {
    _10
    ...
    _10
    - "vtex.menu@2.x:menu#category-menu": {
    _10
    + "vtex.menu@2.x:menu#categories": {
    _10
    ...
    _10
    }

  2. Define a menu and a sub-menu now in place of the old #category-menu, in it we will put all the menu that we had already built:


    _28
    {
    _28
    + "vtex.menu@2.x:menu#category-menu": {
    _28
    + "children": [
    _28
    + "menu-item#categories"
    _28
    + ]
    _28
    + },
    _28
    + "menu-item#categories": {
    _28
    + "props": {
    _28
    + "id": "menu-item-custom-categories",
    _28
    + "type": "custom",
    _28
    + "iconId": null,
    _28
    + "highlight": false,
    _28
    + "itemProps": {
    _28
    + "type": "internal",
    _28
    + "href": "#",
    _28
    + "noFollow": true,
    _28
    + "tagTitle": "Categories",
    _28
    + "text": "Categories"
    _28
    + }
    _28
    + },
    _28
    + "blocks": [ "vtex.menu@2.x:submenu#categories" ]
    _28
    + },
    _28
    + "vtex.menu@2.x:submenu#categories": {
    _28
    + "children": ["vtex.menu@2.x:menu#categories"]
    _28
    + },
    _28
    "vtex.menu@2.x:menu#categories": {
    _28
    ...
    _28
    }

    What we are doing here is to create a level above the menu that we had already defined to then change the navigation to a submenu format, the result should be:

    {"base64":"  ","img":{"width":2284,"height":986,"type":"png","mime":"image/png","wUnits":"px","hUnits":"px","length":421546,"url":"https://user-images.githubusercontent.com/18701182/93835843-fa015600-fc56-11ea-9b0e-b30a281b2d2b.png"}}

  3. In the browser, before your workspace URL, add a view-source: and search for title = "Major Appliances", you will see two references in the code, one for header and one for footer. This means that when we load the HTML we are bringing these menus together, even if they are not being consumed at the first moment:

    {"base64":"  ","img":{"width":2060,"height":766,"type":"png","mime":"image/png","wUnits":"px","hUnits":"px","length":438379,"url":"https://user-images.githubusercontent.com/18701182/93836918-a7299d80-fc5a-11ea-8804-0b2722742e17.png"}}

  4. With the level menu now defined, we can add a new prop to the parent menu, in order to prevent the submenus from loading until the user interacts with the categories:


    _11
    {
    _11
    "vtex.menu@2.x:menu#category-menu": {
    _11
    + "props": {
    _11
    + "experimentalOptimizeRendering": true
    _11
    + },
    _11
    "children": [
    _11
    "menu-item#categories"
    _11
    ]
    _11
    },
    _11
    ...
    _11
    }

Go back to the source code, refresh the page and search again for title = "Major Appliances"

{"base64":"  ","img":{"width":1898,"height":1124,"type":"png","mime":"image/png","wUnits":"px","hUnits":"px","length":409531,"url":"https://user-images.githubusercontent.com/18701182/93837006-f5d73780-fc5a-11ea-84c8-18542756e5a7.png"}}

See that no reference is found, but that if you browse, the behavior remains the same. This is because the submenus are loaded only after the initial load.

Answer sheet

See answer sheet for category-menu.jsonc
category-menu.jsonc

_140
// /store/blocks/header/category-menu.json
_140
{
_140
"vtex.menu@2.x:menu#category-menu": {
_140
"props": {
_140
"experimentalOptimizeRendering": true
_140
},
_140
"children": ["menu-item#categories"]
_140
},
_140
"menu-item#categories": {
_140
"props": {
_140
"id": "menu-item-custom-categories",
_140
"type": "custom",
_140
"iconId": null,
_140
"highlight": false,
_140
"itemProps": {
_140
"type": "internal",
_140
"href": "#",
_140
"noFollow": true,
_140
"tagTitle": "Categories",
_140
"text": "Categories"
_140
}
_140
},
_140
"blocks": ["vtex.menu@2.x:submenu#categories"]
_140
},
_140
"vtex.menu@2.x:submenu#categories": {
_140
"children": ["vtex.menu@2.x:menu#categories"]
_140
},
_140
"vtex.menu@2.x:menu#categories": {
_140
"props": {
_140
"items": [
_140
{
_140
"id": "menu-item-category-electronics",
_140
"type": "custom",
_140
"iconId": null,
_140
"highlight": false,
_140
"itemProps": {
_140
"categoryId": 153,
_140
"type": "internal",
_140
"href": "/electronics/",
_140
"noFollow": true,
_140
"tagTitle": "Electronics",
_140
"text": "Electronics"
_140
}
_140
},
_140
{
_140
"id": "menu-item-category-major-appliances",
_140
"type": "custom",
_140
"iconId": null,
_140
"highlight": false,
_140
"itemProps": {
_140
"categoryId": "151",
_140
"type": "internal",
_140
"href": "/major-appliances/",
_140
"noFollow": true,
_140
"tagTitle": "Major Appliances",
_140
"text": "Major Appliances"
_140
}
_140
},
_140
{
_140
"id": "menu-item-category-small-appliances",
_140
"type": "custom",
_140
"iconId": null,
_140
"highlight": false,
_140
"itemProps": {
_140
"categoryId": 152,
_140
"type": "internal",
_140
"href": "/small-appliances",
_140
"noFollow": true,
_140
"tagTitle": "Small appliances",
_140
"text": "Small appliances"
_140
}
_140
}
_140
]
_140
}
_140
},
_140
"menu-item#category-electronics": {
_140
"props": {
_140
"id": "menu-item-category-electronics",
_140
"type": "custom",
_140
"iconId": null,
_140
"highlight": false,
_140
"itemProps": {
_140
"categoryId": 153,
_140
"type": "internal",
_140
"href": "/electronics/",
_140
"noFollow": true,
_140
"tagTitle": "Electronics",
_140
"text": "Electronics"
_140
}
_140
}
_140
},
_140
_140
"menu-item#category-major-appliances": {
_140
"props": {
_140
"id": "menu-item-category-major-appliances",
_140
"type": "custom",
_140
"iconId": null,
_140
"highlight": false,
_140
"itemProps": {
_140
"categoryId": "151",
_140
"type": "internal",
_140
"href": "/major-appliances/",
_140
"noFollow": true,
_140
"tagTitle": "Major Appliances",
_140
"text": "Major Appliances"
_140
}
_140
}
_140
},
_140
"menu-item#category-small-appliances": {
_140
"props": {
_140
"id": "menu-item-category-small-appliances",
_140
"type": "custom",
_140
"iconId": null,
_140
"highlight": false,
_140
"itemProps": {
_140
"categoryId": 152,
_140
"type": "internal",
_140
"href": "/small-appliances",
_140
"noFollow": true,
_140
"tagTitle": "Small appliances",
_140
"text": "Small appliances"
_140
}
_140
}
_140
},
_140
"menu-item#login": {
_140
"props": {
_140
"id": "menu-item-custom-login",
_140
"type": "custom",
_140
"iconId": null,
_140
"highlight": false,
_140
"itemProps": {
_140
"type": "internal",
_140
"href": "/account",
_140
"noFollow": true,
_140
"tagTitle": "My account",
_140
"text": "My account"
_140
}
_140
}
_140
}
_140
}

On this page