verbb / navigation

A Craft CMS plugin to create navigation menus for your site.
Other
90 stars 23 forks source link

getActiveNode does not work properly in production. dump-ing its value crashes CraftCMS. node.active value does not work in production either. #279

Closed andreimoment closed 2 years ago

andreimoment commented 2 years ago

Description

This is my code. Works perfectly well in my nitro environment.

Multi-site config. The site1TopNav is nav specific to the current, default site.

Once I move to production (official domain), the highlight no longer shows.

If I uncomment the dump(active_node) line, production crashes.

Any idea on how to debug this or if there's a setting I've missed?

{% set nodes = craft.navigation.nodes()
    .handle('site1TopNav')
    .level(1)
    .all() %}

{% set active_node = craft.navigation.getActiveNode({ handle: 'site1TopNav' }) %}

{# <!-- {{ dump(active_node) }} --> #}

<nav class="top-nav" data-controller="topnav">
  <div class="top-nav__container">

    <ul class="top-nav__menu" data-topnav-target="receiver">

      {% nav node in nodes %}

        {# highlight the current menu item  #}
        {% if active_node is not null and active_node.url == node.url %}
          {% set active_class = "-active" %}
        {% else %}
          {% set active_class = "" %}
        {% endif %}

        <li class="top-nav__item {{active_class}}">
          <a href="{{node.url}}">{{node.title}}</a>
        </li>

      {% endnav %}
    </ul>

  </div>
</nav>

Steps to reproduce

  1. Please see code above

Additional info

Additional context

andreimoment commented 2 years ago

I updated my code to the following, and the current node still does not highlight in production - while working perfectly in dev.

{% nav node in nodes %}

        {% if node.active %}
          {% set active_class = "-active" %}
        {% else %}
          {% set active_class = "" %}
        {% endif %}

        <li class="top-nav__item {{active_class}}">
          <a href="{{node.url}}">{{node.title}}</a>
        </li>

      {% endnav %}
andreimoment commented 2 years ago

The issue with this was the site being set as sitename.com in craftcms and the host always redirecting to www.sitename.com. I added the "www." in craftcms and the menu now highlights properly. Closing.

engram-design commented 2 years ago

Glad you got it sorted. I'm not sure what the "production crashes" error was (ensure you've got Show full exception views when Dev Mode is disabled enabled in your user profile on production so you can always see a full stack trace error. But I have a feeling it's due to dump() not being available on any environment that doesn't have devMode enabled - so the cause of the fatal error on production.

And yep, it'll need to be exact URLs for matching, so that makes sense.