Troubleshooting a strange anomaly with superior param in Nextjs routing

I'm currently working on a project with nextjs v12 and encountering an interesting issue. There are 2 routes that seem to be overlapping in an unexpected way.

  1. /:academy/:course
  2. /questions/:id

When I navigate to /questions/1, everything works as intended. Similarly, when I go to /mit/math, it also functions correctly.

The problem:

Here's where things get tricky - when I redirect from /questions/1 to /questions/2, instead of loading the new route, it actually loads the other route (/:academy/:course). To make matters more confusing, if I refresh the page after the redirect, it then properly loads the /questions/:id route!

My attempted solutions:

  • Checked for any misspellings in the code
  • Tried changing /questions/:id to /aquestions/:id

Do you have any insights or suggestions on how to resolve this challenging issue?

Thank you for your help.

Issue Resolved

The root cause was actually due to having both routes named /q/:id, so I renamed one to /q-lesson/:id. Additionally, because it involves server-side rendering (SSR), I had to clear the cache and restart the project to see the changes take effect.

Answer №1

It is important to note that predefined routes take precedence over dynamic routes, and dynamic routes take precedence over catch-all routes according to the official nextJS documentation.

This should not happen

Check out the nextJS docs for more information on dynamic routes

In this particular situation, it seems like we are attempting to use 2 partial dynamic paths which is causing confusion for nextJS in determining the correct path. Consider adding a rewrite rule to consistently route /questions/* paths to /questions/:id.

Learn more about rewrite rules in next.config.js here

If you could provide the code snippet of how you are implementing redirection, it would be helpful in providing a solution tailored to your specific scenario.

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

Creating a new row does not result in the creation of a new span displaying the character count message

Every description field should have its own character counter, with different SpanIDs displayed in respective SpanIds for each new row. How can this be achieved? <div class="row"> <div class="col-s ...

Encountering this issue: Unable to access the property 'length' of an undefined variable

I'm currently developing an app using nuxt, vuetify 2, and typescript. Within the app, I have radio buttons (referred to as b1 and b2) and text fields (referred to as t1, t2, t3). When a user clicks on b1, it displays t1 and t3. On the other hand, w ...

Exploring the process of using querySelector() to locate specific class and div IDs in JavaScript code

My objective is to make the id="myDropdownTop" trigger the corresponding drop-down menu. The problem lies in my script code, particularly the final line: ("div.dropdown-content").classList.toggle("show"); does not seem to be functioning correctly. <!D ...

Angular 2 Error: Unable to access the property 'value' as it is undefined

Whenever I click on the Submit button, this error pops up in the browser console. https://i.sstatic.net/57a5N.jpg In my application, I am attempting to retrieve information about the code uploaded by a Student. I am at a loss as to why this error is bein ...

Is it possible for a submission of a form to modify the content length header, resulting in the request failing?

Issue Description: After binding a submit event to an AJAX post request in order to send a predetermined key-value pair to a PHP script, the expected message indicating successful communication is not received. Despite the fact that the submit event trig ...

I attempted to combine AngularJS and three.js, but unfortunately, the geometries are not appearing in the render

(I found this example online, but it's not working as expected). I am attempting to incorporate AngularJS for implementing an MVC architecture for three.js files. Despite not encountering any errors during debugging, I am unable to see the rendered ge ...

Prevent the hiding of a select element with jQuery Chosen Select

I am facing difficulty hiding a select element with the chosen-select class if it does not have any elements present. I attempted: $("#Category2").hide(); and also tried removing the chosen-select class before hiding the element: $("#Category2").re ...

What is the fastest way to invoke the driver.quit() method in Selenium?

Is there a way to force close the browser immediately instead of waiting for it to complete loading before calling driver.quit()? ...

What are the benefits of adding member functions to the data structures of React.js store?

Using React.js and Typescript, I store plain Javascript objects in the React.js store. These objects are sometimes received from the server without any member functions, but I wish to add functions for better organization. Instead of having to rely on exte ...

Creating interactive web applications with Python Flask by utilizing buttons to execute functions

When the button is clicked in my Flask template, I want it to trigger a Python function that I defined in app.py. The function should be accessible within the template by including this code where the function is defined: Here is an example function in ap ...

Having trouble selecting dropdown options using Selenium with Python?

I'm a beginner in using Selenium with Python and I'm attempting to retrieve index data from the Barclays Live website. After logging in and the page loads, I need to select 'Custom1' from a dropdown menu on the page. The HTML code for t ...

Steps for removing an embed after a set time period

I've been using message.delete({timeout: 3000}) to remove messages with the prefix. Now I'm wondering how I can also delete the embed I sent after a certain amount of time. if (!args[0]) return message.channel.send({ embed: { color: 1677720 ...

Creating a dynamic route in Next.js: A step-by-step guide

Is there a way to generate URLs like the following: http://localhost:3000/designer/book1 http://localhost:3000/engeniring/book1 http://localhost:3000/art/book1 If I attempt to place files with specific names in the pages folder, I would have to repeat ...

Forcing UTF-8 Encoding in JavaScript

I came across this helpful article: While following the advice of many others suggesting to use unescape(encodeURIComponent(srt)), I encountered issues. Attempting to convert the file obtained through XMLHttpRequest did not yield the desired results. Pri ...

ajax: ensure utf-8 encoding is applied

I am facing an issue with my webpage that is saved in UTF-8 encoding. In the head section of the HTML document, I have defined it as follows: <meta charset="utf-8" /> Within an external JavaScript file, there is a function called ajax_db() which ma ...

What are the steps to retrieve JSON data and display it using an unordered list in an HTML document?

My JSON data structure looks like this: { "fID": "00202020243123", "name": "John Doe", "List": ["Father", "Brother", "Cousin"] } When I render this JSON element in my model and view it in the HTML, everything works fine. However, when I try t ...

Fetching data from server using Ajax with PHP response

Need help with getting data from a PHP file on an AJAX call. Every time I make the call, it returns an error alert. Despite trying various methods, I am unable to figure out how to return an array from PHP to the AJAX call. This is what I have so far: AJ ...

I encountered an issue where the IDs did not match while using an array to update the MYSQL database

Currently, I am facing an issue while looping through an array in node JS to update a MYSQL database. The problem arises because the array starts at 0, whereas my auto-incrementing database starts at 1. This misalignment offsets the entire database by on ...

Using an HTML file as the source for an image in the <img src=""> tag is a simple process that can greatly enhance the visual appeal of your webpage

My goal is to utilize an html file as the source for an image file within the <img src=""> tag in order to prevent mixed-content warnings when using an http image over https. For instance: index.html: <img src="./image.html" > image.htm ...

Extract the response from codebird and store it in an array (or access the JSON data from the codebird's reply)

I am currently working on retrieving data from Twitter using codebird in my JavaScript script. The issue I am facing is that codebird's response is in the form of an object rather than JSON. As a result, I am unable to use eval() to convert the JSON t ...