The site has a tree menu that contains three steps: Category A ... n -> Subcategory 1 ... n -> Post 1 ... n At the same time, since there can be a dozen two subcategories in one category, each of which has more than ten posts, then building a whole menu as a snake takes quite a lot of resources, if done on the fly, when the page loads. At the same time, the menu is displayed both on the pages of categories and on the pages of posts.

How best to get rid of building a menu on the fly?

Menus can change only if subcategories or posts are added / removed. At the same time, the menu options themselves are as many as the Categories A (which have no parent categories).

It makes sense to generate / change the menu at one point in time, and then store it in the database or somehow cache it?

  • If the menu changes rarely, then use the cache to store it. - Visman

1 answer 1

In this case, data loading for the menu via AJAX will help (menu items and links are taken from the database). Another question is, how much volume code you have, it is better to load 100 lines directly at the starting assembly of the DOM tree, rather than screwing up the loading.

  • one
    The code is quite voluminous. But why load with AJAX if you can compile the menu in advance and take it from the cache, for example? - SurenderMeg
  • @SurenderMeg there is a question in the scope of this menu (now and in the future). if it’s about 200-300 points, then it’s easier to do it right in HTML, if there are thousands of points there that change from time to time - better than AJAX - Dmitry Maslennikov