Next.js Discord

Discord Forum

Dynamic routing creating undefined routes

Unanswered
Red wasp posted this in #help-forum
Open in Discord
Red waspOP
My current route takes the id param and navigates to the page ex. /services/1. When I navigate to a regular page, it creates a new route such as /services/about-us with nothing on it but the page layout for the dynamic [id] route

How do I go about fixing this? I only want to have dynamic routes for the id params
// Dynamic route component
import React from "react";
import Link from "next/link";

const ServiceCard = ({ id, icon, title, details }) => {
  return (
    <>
      <Link href={`/services/${id}`} className="w-full px-4 md:w-1/2 lg:w-1/3">
        <div className="mb-9 rounded-[20px] bg-white p-10 shadow-2 hover:shadow-lg md:px-7 xl:px-10">
          <div className="mb-8 flex h-[70px] w-[70px] items-center justify-center rounded-2xl bg-primary">
            {icon}
          </div>
          <h4 className="mb-[14px] text-2xl font-semibold text-dark ">
            {title}
          </h4>
          <p className="text-body-color">{details}</p>
        </div>
      </Link>
    </>
  );
};

export default ServiceCard;

The snippet of navbar that this issue occurs from
<li role="none" className="flex items-stretch">
                <Link href="/services">
                  <div
                    role="menuitem"
                    aria-haspopup="false"
                    onClick={() => setIsToggleOpen(false)} // Close the menu when a link is clicked
                    className="."
                  >
                    <span>service</span>
                  </div>
                </Link>
              </li>
              <li role="none" className="flex items-stretch">
                <Link href="/about-us">
                  <div
                    role="menuitem"
                    aria-current="page"
                    onClick={() => setIsToggleOpen(false)}
                    aria-haspopup="false"
                    className="...."
                  >
                    <span>about us</span>
                  </div>
                </Link>
              </li> 

18 Replies

Palomino
If you don't want /services/about-us to to exist, you can use the notFound api
Asian paper wasp
I'm more interested on this part:
When I navigate to a regular page, it creates a new route such as /services/about-us
By "navigate", do you mean you entered /services/about-us in the browser address bar directly and pressed enter? Or you some how reach this "page" by pressing the link shown in the code snippet?
@Palomino If you don't want `/services/about-us to` to exist, you can use the `notFound` api
Red waspOP
now it makes it 404 not found, which is good
the main issue now is that when I click on the /about-us link when i'm on services/[id], It makes that new route services/about-us instead of navigating to /about-us
this is an example:
current on services/7
i click on the about us nav link
however, when im not on [id], it navigates to /about-us
And for some reason its only happening for about-us and not the other nav links
such as home and Faq
Palomino
For your Link component that wraps around About us, you want your href attribute to be /about-us
Red waspOP
wow good eye
that was the fix