1

I have a multi-level structure in Craft that is being used for navigation. As part of this I have a few different entry types, but each entry that links to a page has a entry-field of singlePage. At the moment the load times are stupid and I am trying to optimise the whole navigation.

Below I have pasted the code to the full navigation, but I think the part that is blowing up is the loop over the pages/single entries.

{% nav entry in entry.descendants %}
{% set page = entry.singleEntry[0] ?? null %}
    <li class="mb-2">
        {% if page is not null %}<a href="{{ page.url() }}" class="text-sm text-white hover:text-lime">{{ entry.title }}</a></li>{% endif %}
        {% ifchildren %}
            <ul class="ml-4 my-2 list-reset">
                {% children %}
            </ul>
        {% endifchildren %}
    </li>
{% endnav %}

I have tried to eager load this like so;

{% set navigation = craft.entries({
    section: 'navigation',
    limit: null,
    with: [
      'children',
      'descendants',
      'singleEntry'
    ]
}) %}

Whilst this is coming back as;

(index):2754 Profiling Summary Report
13:25:22.385 (index):2755 Time:   14.12897s
13:25:22.386 (index):2756 Memory: 58,940Kb
13:25:22.386 (index):2757 Total Queries: 178

Load times are nearly mostly between 12-14 seconds. This is a reduced page with just the navigation on it too.

How can I optimise further for better results? What are the issues in my code? It must be poorly written, but having re-written it twice I am no better to solving this...

{% cache globally using key "navMega" for 1 year %}
<div class="relative hidden lg:block w-full z-50" data-controller="mega-menu">
  <div class="sitecontainer">
    <nav class="pb-4">
    {% for entry in navigation.level(1) %}
      {% if loop.first %}
        <ul class="list-reset flex justify-between">
        <li class="pr-6 // border-r border-white-25">
          <a href="{{ siteUrl() }}" class="hover:text-lime {% if craft.request.firstSegment == '' %}text-lime{% else %}text-white{% endif %}" data-action="mouseover->mega-menu#close">
            <div class="w-5 // h-5 // fill-current // mr-2">
              {% include '_includes/svg/icon-home.svg' %}
              <span class="alt">Home</span>
            </div>
          </a>
        </li>
      {% endif %}
        <li class="pl-4 pr-8 // {% if not loop.last %}border-r border-white-25{% endif %}">
          <a href="#" class="text-white hover:text-lime flex items-center font-semibold"
            data-action="click->mega-menu#open"
            data-next="{{ entry.title|kebab }}">
              {{ entry.title }}
            {%- if (entry.hasDescendants or entry.type == 'contact' or 'directories') -%}
            <div class="ml-1 w-3 h-3 fill-current /// mt-1 flex flex-col justify-center">
              {% include '_includes/svg/icon-chevron.svg' %}
            </div>
            {%- endif -%}
          </a>
        </li>
        {% if loop.last %}</ul>{% endif %}
    {% endfor %}


{% for entry in navigation.level(1) %}
    {% switch entry.type %}

      {% case 'contact' %}
      <div id="{{ entry.title|kebab }}" class="mt-10 hidden absolute pin-x bg-petrol p-4 z-10" data-target="mega-menu.menu">
        <div class="max-w-4xl flex flex-wrap pt-6 px-8 mx-auto">
          <div class="flex flex-col pr-12 mb-12 flex-1">
            <h3 class="mb-2 text-lg border-b border-petrol-light pb-2 font-semibold text-grey-lightest mb-6">
                {{ entry.title }}
            </h3>

          <div class="flex flex-wrap justify-between">
            <div class="w-1/2 pr-12 text-white leading-normal">
               <a href="mailto:{{ companyInformation.email }}" class="text-lime hover:text-white text-lg mb-6 inline-block">{{ companyInformation.email }}</a>

              {% if entry.body|length %}
                <div class="article">
                {{ entry.body }}
                </div>
              {% endif %}

              <p class="text-xs">
                ‘{{ siteName }}’ is a trading division of Judd and Judd Ltd, a company registered in England and Wales, registered number 8082403. Registered office: Suite 2, Douglas House, 32-34 Simpson Road, Fenny Stratford, Buckinghamshire, MK1 1BA, United Kingdom.
              </p>
            </div>

            <div class="pl-12">
              <div class="text-white mb-6">
                {% include '_includes/social/follow' %}
              </div>

{#             {% if entry.relatedContent|length %}
              {% for entry in entry.relatedContent %}
              {% if loop.first %}<ul class="list-reset">{% endif %}
                  <li class="text-white mb-2">
                    <a href="{{ entry.url }}" class="text-white hover:text-lime">{{ entry.title }}</a>
                  </li>
              {% if loop.last %}</ul>{% endif %}
              {% endfor %}
            {% endif %} #}
            </div>
          </div>
        </div>
      </div>

      {% case "directories" %}
      <div id="{{ entry.title|kebab }}" class="mt-10 hidden absolute pin-x bg-petrol p-4 z-10" data-target="mega-menu.menu">
        <div class="flex flex-wrap pt-6 px-8 mx-auto">
            <div class="flex flex-col pr-12 mb-12 flex-1">
              <h3 class="mb-2 text-lg border-b border-petrol-light pb-2 font-semibold text-grey-lightest mb-6">
                  {{ entry.title }}
              </h3>

            <div class="flex flex-wrap justify-between">
              <div class="pr-12 text-white leading-normal">
                {% if entry.body|length %}
                  {{ entry.body }}
                {% endif %}

{#                 {% if entry.relatedContent|length %}
                  {% for entry in entry.relatedContent %}
                  {% if loop.first %}<ul class="list-reset">{% endif %}
                      <li class="text-white mb-2 {% if loop.last %}mt-6 pt-4 border-t border-petrol-light{% endif %}">
                        <a href="{{ entry.url }}" class="text-white hover:text-lime text-sm">{{ entry.title }}</a>
                      </li>
                  {% if loop.last %}</ul>{% endif %}
                  {% endfor %}
                {% endif %} #}
              </div>

              <div class="pl-6 flex-1 flex flex-wrap">
                <h3 class="mb-2 text-lg text-white mb-6 w-full">Latest Updates</h3>
                {% for entry in directories %}
                  {% set logo = entry.logo[0] ?? null %}
                  <article class="flex /// w-1/2 px-6 mb-6">
                  <div class="flex items-center justify-center w-32 bg-white p-2">
                    {% if logo|length %}
                    <a href="{{ entry.url }}">
                      <div class="text-center">
                        {% include '_includes/partials/logo' with { classes: 'p-2 bg-white w-full' } %}
                      </div>
                    </a>
                    {% else %}
                      {% include '_includes/directories/articleImage' %}
                    {% endif %}
                  </div>
                  <div class="pl-4 /// flex-1 flex flex-col justify-center">
                    <h3 class="mb-2"><a href="{{ entry.url }}" class="text-lime hover:text-blue text-lg font-semibold">{{ entry.title }}</a></h3>
                    <p class="text-sm mb-0 text-petrol-light">{{ entry.section }}</p>
                  </div>
                </article>
                {% endfor %}
              </div>
            </div>
          </div>
        </div>
      </div>

      {% default %}
      <div id="{{ entry.title|kebab }}" class="mt-10 hidden absolute pin-x bg-petrol p-4 z-10" data-target="mega-menu.menu">
        <div class="flex flex-wrap pt-6 px-8 mx-auto">
          {% for entry in entry.children %}
            <div class="flex flex-col pr-12 mb-12 flex-1">
                <h3 class="mb-2 text-lg border-b border-petrol-light pb-2 font-semibold text-grey-lightest mb-6">
                    {{ entry.title }}
                </h3>
                <ul class="ml-4 list-reset">
                {% nav entry in entry.descendants %}
                {% set page = entry.singleEntry[0] ?? null %}
                    <li class="mb-2">
                        {% if page is not null %}<a href="{{ page.url() }}" class="text-sm text-white hover:text-lime">{{ entry.title }}</a></li>{% endif %}
                        {% ifchildren %}
                            <ul class="ml-4 my-2 list-reset">
                                {% children %}
                            </ul>
                        {% endifchildren %}
                    </li>
                {% endnav %}
                </ul>
            </div>
           {% endfor %}
        </div>
      </div>
      {% endswitch %}
    {% endfor %}
    </nav>

  </div>
</div>
{% endcache %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.