Navigating with Next.js: Trying to change routes from a dynamic slug page results in improper routing

In my scenario, I have two routes: /video and /products/[id]. This setup results in the following files within my pages folder: video.js and products/[id].js. However, I encountered an issue where using router.push or Link to navigate from /products/[id] to /video causes the URL to update to /products/video, which is not the intended behavior.

I am puzzled by this behavior. What could be causing this unexpected redirection?

Answer №1

To properly direct to the video page, make sure to include the full path as /video instead of just video. Otherwise, it will be relative to the current path you are on - in this case, /products from your products page.

router.push('/video')
<Link href="/video">...</Link>

Answer №2

Your file naming convention seems to be causing the issue.

For the /video route, you should only have a video.js file as your page.

To access the /products/[id] page, make sure to name your file [id].js and place it inside the /products folder.

Do not name the file products[id].js; just use [id].js instead.

Organize your files by putting the [id].js file in the /products folder and the video.js file in the pages folder (e.g., /pages/video.js for the /video route).

The /pages/products/[id].js file will then correspond to the /products/id route.

In summary: Ensure that your pages folder contains video.js, create a products folder, and place the [id].js file inside it.

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

Incorporate child routes within a React component

I've been attempting to include <route> in a component within a routers component, but it doesn't seem to be working. Can anyone provide some assistance? My login page currently has a form and I'd like to switch that out with routes. ...

Suggestions for customizing this comparison function for sorting tables

When sorting alphabetically, I want to prioritize anything that begins with [ or . before the rest. How can this be achieved? function ts_sort_default(a,b) { aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]); bb = ts_getInnerText(b.cells[SORT_COLUMN_ ...

npm update fails to update specific packages

After React was updated to version 0.15 to address the issue of excessive generation of tags, I made the decision to update my project.</p> <p>However, when I ran the command <code>npm update, it only updated to version 0.14.8. Running ...

The onValue event in Firebase is not triggering, and I am unable to determine the cause

I am currently working on a web3 application using Next/Js and Firebase. The concept is for users to connect with their Solana wallet, list their NFTs, and choose one to connect in a game-container. My challenge lies in retrieving information from all con ...

Encountering an error when using JSONP with a period in the callback function

I am currently facing an issue with making an ajax jsonp call. It seems that the json returned contains a dot in the callback function, as shown in the example below: ABCD.render_section({ "page": { "parameters": { "pubDate": "2013-06-05 00:00:00.0", ...

What does the error "Cannot access property 'length' of null when using 'angular'" mean?

I am currently working on counting the initial li items and encountering an issue with 'Cannot read property 'length' of null' after fetching data from the server using a unit test case. Here is my code: import { ComponentFixture, Tes ...

Retrieving JSON Data from an API with JavaScript/jQuery

Having some trouble with JSON parsing using jQuery and JavaScript. I'm trying to fetch weather information from the open weather simplest API, but for some reason it's not working. When I hardcode the JSON data into a file or my script, everythin ...

Calculating the surface area of a two-dimensional flat shape within a three-dimensional space using Javascript and Math

Is it possible to calculate the surface area of a 2D polygon with any shape, using a set of 3D vertices? For instance, what would be the surface area of the given figure? var polygon = new Polygon([new Point(0,0,0), new Point(5,8,2), new Point(11,15,7)]) ...

Updating website links in real time with the power of jquery

Is there a way to dynamically change the parameter of a link? For example: Link1 Link2 Link3 By default, their URL is ?item=text, so link1 would be href="?item=link1", etc. But when I click on link1, the URLs of link2 and link3 should change to include ...

Extract item from the collection

I am struggling with the following code: app.controller('modalController', function($scope, $http,$modalInstance, $rootScope, items){ // Retrieving information $http.post('/ajax/bundle', {'items':items}).success(functi ...

Unable to detect cookie presence in header due to reading issue

Kindly review the image provided via the following link. https://i.sstatic.net/innYb.png The front-end code initiates a call to the back-end API to log in a user. The back-end is built using Expressjs 4 and the cookie store utilizes redis. I am puzzled ...

Adding a class to a TD element only when there is text in that TD as well as in other TD elements

I am facing a challenge where I need to add a new class to a td element, specifically the first one in the code snippet below. However, this should only happen if that td contains a certain number (e.g., "2") and if the next td contains some specific text ...

Is it possible to implement microservices on the front-end using JavaScript?

My current project involves multiple developers working on different components/modules for an app simultaneously. The catch is that each component could be developed in a different framework or library. For example, URI/app1 could be a search component u ...

Experimenting with the useDebounceCallback hook through Jest testing

Currently, I am experimenting with testing debounce functionality using Jest in a component that utilizes URL parameters for search. The function handleSearch makes use of the useDebounceCallback hook from usehooks-ts (which internally uses lodash debounce ...

The issue of Vue.js template errors within inline code

I am experimenting with using inline-template in combination with vue.js 2.0. This usage is within the context of Laravel blade 5.4: <users inline-template> <tbody> @foreach($users as $user) <tr> ...

JavaScript: Perform multiplication and addition on two arrays

I am looking for a way to multiply pairs of values in two arrays that have the same length, and then sum up the results based on their indexes. As an example, var arr1 = [2,3,4,5]; var arr2 = [4,3,3,1]; The output would be 34 (4*2+3*3+4*3+5*1). What is ...

Retrieve live JSON data using Node.js

I am currently running an express server with the following code: server.js const express = require('express'); const app = express(); var json = require("./sample.js") app.use("/", (req, res) => { console.log("----------->", JSON.strin ...

Combining Components in NextJs Without Using Functional Components

I created a module, then split it into components and now I'm trying to piece it back together. The individual components are functioning correctly. Some components are nested inside the div of another component. I attempted to group them within a d ...

Converting an HTML page into a base 64 string: Step-by-step guide

Currently, I am utilizing Angular 6 for my project. On the index page, I am binding data and then submitting it to the database. My objective is to submit the entire HTML page with the bound data as a string in the database. Is there a way to convert an H ...

Trouble with top attribute functionality within animate function

Why does the top attribute in the animate function of JQuery not seem to work, while the opacity attribute functions correctly in the code snippet below? $(function() { $(window).on('scroll', function() { ...