#Active Nav Link not working

5 messages · Page 1 of 1 (latest)

dim linden
#

Navlink has been working in my localhost but when deployed it won't work. Attached is the screenshot of my code

dreamy flame
#

This is probably due to how your hoster handles trailing slashes. In my nav I just remove all trailing slashes to keep it consistent with a simple pathname.replace(/\/$/, "") https://github.com/Trombach/astro-homepage/blob/21896486c062fe754ee9b580f35bf257f14fdff2/src/components/Nav/NavLink.astro#L10

GitHub

My homepage written in Astro and Svelte. Contribute to Trombach/astro-homepage development by creating an account on GitHub.

#

if that doesn't work, I would start console logging the value of pathname to see why it breaks

vivid thunder
swift remnant
#
---
const {href, class: className, ...props} = Astro.props;

const {pathname} = Astro.url;
const isActive = href === pathname || href === pathname.replace(/\/$/, "");
---

<a href={href} class:list={[className, {active: isActive}]} {...props}>
    <slot />
</a>

This is what I have done for NavLink like you have.