Next.js routing can sometimes be unpredictable, especially when navigating from a nested route

I recently set up a directory named dashboard within the pages folder, and it contains the following files:

  1. index.tsx
  2. customers.tsx
  3. invoice.tsx
  4. items.tsx

When a user navigates to http://localhost:3000/dashboard, the index.tsx page is displayed. However, when I try to access the invoices or customers pages by clicking on buttons, I encounter a 404 error. Upon inspecting the URL, I noticed that it directs to http://localhost:3000/customers and http://localhost:3000/invoices instead of

http://localhost:3000/dashboard/customers
and
http://localhost:3000/dashboard/invoices

Despite this, including the full route in the href for customers seems to work after refreshing the page:

//This snippet works after page refresh
<Link href='dashboard/customers'>
  <a>Customers</a>
</Link>

However, when attempting to navigate from customers to any nested page, such as invoices, using the following code results in a 404 error:

<Link href='dashboard/invoices'>
  <a>Customers</a>
</Link>
// I consistently get a 404 page

Upon investigation, I discovered that the URL was being modified to

http://localhost:3000/dashboard/dashboard/invoices
rather than
http://localhost:3000/dashboard/invoices

Here's the code for the buttons:

<Link href='/customers'>
  <a>Customers</a>
</Link>

<Link href='/invoices'>
  <a>Invoices</a>
</Link>

Unfortunately, I am unsure of what mistake I might be making. Any guidance would be greatly appreciated.

Answer №1

<Link href='dashboard/invoices' />
is an example of a relative route - href='dashboard/invoices' instructs the browser to navigate to
/yourCurrentLocation/dashboard/invoices
.

To create an absolute route, you need to include a leading /. This ensures that the route is always based on the root directory. Therefore, your corrected code would look like this:

<Link href='/dashboard/invoices' />
.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Declare a new variable with a specific data type in TypeScript

I'm working on creating a variable in my component of a specific type, as shown below. myrequest.model.ts export class MyRequest { public endValue: string; public yearEnd: string; } When importing the above into my component, I do the follow ...

Change a nested for-loop into an Observable that has been transformed using RxJS

Currently, the following function is operational, but I consider it a temporary solution as I'm extracting .value from a BehaviorSubject instead of maintaining it as an observable. Existing Code Snippet get ActiveBikeFilters(): any { const upd ...

Common problems encountered post Typescript compilation

I encountered the same problem. Below is my tsconfig settings: "compilerOptions": { "module": "commonjs", "moduleResolution": "node", "newLine": "LF", &q ...

Is there a way to position the Image component from next/image using absolute positioning?

Is it possible to use an element Image from 'next/image' with the CSS style { position: absolute; left: 50% }? It appears that it is not being applied. For example: import React from 'react' import Image from 'next/image' imp ...

Function exported as default in Typescript

My current version of TypeScript is 1.6.2 and we compile it to ECMA 5. I am a beginner in TypeScript, so please bear with me. These are the imported library typings. The contents of redux-thunk.d.ts: declare module "redux-thunk" { import { Middle ...

having difficulties adjusting the size of the SVG element

I have been struggling to resize an SVG element that I imported into my React project. No matter what methods I try, I can't seem to get it right. First, I import the SVG file: import SVG from "../icon.svg" Then, I attempt to use it as an ...

Trouble configuring a React TypeScript router has arisen

Recently, I have successfully set up multiple routers in TypeScript. However, I am facing challenges in implementing the same in a new project for some unknown reason. import React from 'react'; import Container from './Components/Containers ...

Unable to retrieve HTTP call response during debugging, although it is visible in the browser

When I send an HTTP request to create a record, I am able to see the added record id in the Network section of browsers like Chrome and Firefox. However, when I try to debug the code and retrieve the same id value, I encounter difficulties. I have tried us ...

The Next JS build process is failing to generate certain paths

Issue with Anime Database App Deployment A problem arose when I developed an anime database app using Nextjs and deployed it on Vercel. Although the build was successful and the initial page rendered properly, only a few dynamic routes displayed correctly ...

Exploring NuxtJS Vuex Module namespaces and mutation enumerations

My NuxtJS website has a Vuex store with a root state and a module located at store/shop/cart/state.ts. Within the module, there is a file called store/shop/cart/mutations.ts. import { MutationTree } from 'vuex'; import { CartState } from './ ...

Error message in Typescript: "Property cannot be assigned to because it is immutable, despite not being designated as read-only"

Here is the code snippet I am working with: type SetupProps = { defaults: string; } export class Setup extends React.Component<SetupProps, SetupState> { constructor(props: any) { super(props); this.props.defaults = "Whatever ...

Protractor Browser Instance Failure

We have encountered an issue with our UI suite failing in Chrome during the login process. Initially, we thought it might be due to upgrading to Chrome 79, as the problems arose simultaneously. Interestingly, the login functionality still works smoothly in ...

Include TypeScript in a single component within an already established Vue project

I've been working on a large Vue project and I want to integrate TypeScript into it. However, every time I try to do so, I run into a lot of errors that take weeks to fix. Instead of going through that, I'd like to find a way to add TypeScript to ...

Use Ramda to convert an array of objects into nested objects

As a beginner, please forgive me for asking what may be considered a naive question. I currently have an array of objects const arr = [{id: 1, name: 'Pete'}, {id: 5, name: 'John'}, {id: 3, name: 'Peter'}] and I am looking to ...

Using TypeScript and React: Implementing interfaces based on props conditions

I am currently designing a component that must either receive a prop named text or children, but not both or neither. ✓ Allow <NotificationBar text="Demo"/> <NotificationBar>Demo</NotificationBar> ✗ Disallow <NotificationBar/&g ...

Docker containers do not allow environment variables to be overridden

I am relatively new to Docker and I have a Nextjs application running inside a docker container. This application utilizes certain environment variables for communication with the server. An issue arises where, despite passing the environment variables whe ...

Sending an onclick event to a child class through React and TypeScript

I'm currently working through the Facebook React tutorial with Typescript for the first time. I need to pass an onClick event to the 'Square' component, which is implemented using Typescript and interfaces for state and props. How can I mod ...

API Routes - xxxx has been resolved by the API without sending back a response, potentially causing delays in request processing

While working on my frontend project, I encountered an issue with a simple API call using API routes. Whenever I try to complete the call, I face the following error which prevents my redirect from working: API resolved without sending a response for /ap ...

How to Avoid 404 Errors When Using Next/Link in the App Directory

Here is a snippet of my NavBar() {/* Desktop Nav */} <div className="hidden md:flex items-center space-x-6"> <Link href={item1Href}> <div className="text-lg hover:text-gray-400">{i ...

What are the steps for changing this JavaScript file into TypeScript?

I'm currently in the process of converting this JavaScript file to TypeScript. However, I've encountered an error with the onClick function as shown below: import React from 'react'; import { Popover } from 'antd'; import A fr ...