Deployment of NextJS optional catch all routes causing issues

We are currently working on a project with a dynamic route [productId]. Within this page, we have multiple other pages that contain optional catch-all routes. The structure in the pages folder looks like this:

[productId]
  contentOne
    [[...slugOne]]
 

Unfortunately, there seems to be an issue with the optional catch-all routes not functioning properly when the pages are statically generated. For example, productId/contentOne does not work, whereas productOne/contentOne/extra works just fine. This problem only occurs when the project is deployed on Vercel. All routes function correctly when running locally.

The following is the getStaticPaths function:

export async function getStaticPaths() {    
  return {    
    paths: [],    
     fallback: true,    
   }    
 } 

And here is the getStaticProps function:

export async function getStaticProps({ locale }) {
  return {
    props: {
      test: 'test',
      ...(await serverSideTranslations(locale, ['common'])),
    }
  }
}

Answer №1

We currently have an unresolved matter regarding this specific issue: https://github.com/vercel/next.js/issues/30631

In the meantime, we had to implement rewrites in our next.config file:

async rewrites() {
return {

  beforeFiles: [

   {
     source: '/:productId/contentOne',
     destination: '/:productId/contentOne/index'
   }
  ]
}

Answer №2

When employing the getStaticPaths function to create your pages, utilizing { fallback: true } won't lead to a 404 error. In development mode, this setup functions smoothly due to the presence of Automatic Static Optimization in Next.js, which ensures server-side rendering even for static pages.

If you're operating an SSR site with Next.js, enabling the fallback option will display a static page with empty properties. This behavior is intentional and it's up to you to manage this by either client-side redirection or presenting a custom 404 or placeholder content page.

On the other hand, when using next export to build and export your pages, configuring the fallback setting has no effect (as is expected with next export). Consequently, the page simply won't be generated, requiring your server to handle the 404 scenario by delivering a 404 error page or redirecting users elsewhere.

If you're encountering issues where it "doesn't work," are you receiving a 404 error or just seeing a blank page? The explanations outlined above shed light on why either of these scenarios might occur.

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

Discovering the worth of a selected radio button using JavaScript

I need to extract the value of a radio button without any validation or clicking on a button. My goal is to retrieve the live value as soon as the user changes the selected radio button. I've attempted multiple codes, but none of them have been succ ...

Using setTimeout() in JavaScript to create a delay between functions

Is there a way to add a delay between the execution of each function in my program? Currently, I have the following setup: function function0() { setTimeout(function1, 3000); setTimeout(function2, 3000); setTimeout(function0, 3000); } While t ...

Ways to separate a portion of the information from an AJAX Success response

I am currently working on a PHP code snippet that retrieves data from a database as follows: <?php include './database_connect.php'; $ppid=$_POST['selectPatientID']; $query="SELECT * FROM patient WHERE p_Id='$ppid'"; $r ...

Transforming a numeric value into a 4-byte array using JavaScript

Attempting to write a node server has brought me to the challenge of sending a 32-bit integer to a C# client as the header. The bit shift operators are a bit confusing for me and I am uncertain about how to proceed. It seems that my C# client expects thes ...

JavaScript parameter not found

I am currently working on a block type plugin for Moodle and encountering some difficulties with my JS code. Due to my limited knowledge in JS and JSON, I am having trouble identifying the issue at hand. My code includes a function that adds a custom actio ...

Issue with React Router functionality not functioning

I am currently facing an issue with my react-router setup. You can find the code I am using by visiting this link - https://github.com/rocky-jaiswal/lehrer-node/tree/master/frontend Although it is a basic setup for react-router, I am experiencing difficu ...

What could be causing the sanity cli to encounter an error during installation?

I'm facing an issue while attempting to execute the following commands: npm install -g @sanity/cli >> sanity init --coupon javascriptmastery2022 and this error message is displayed: sanity : File C:\Users\...\AppData\Roaming ...

Unable to utilize a variable from React Context for accessing the value within an object

I'm facing a peculiar yet seemingly straightforward issue. My object named vatRates is structured as follows: const vatRates = { "AD": 0.00, "CZ": 1.21, "RO": 1.19, "DE": 1.19, "RS": 0.00, ...

Making a column in a Vue data grid return as a clickable button

My goal is to utilize vue.js grid to display multiple columns with calculated text values, along with a clickable column at the end that triggers a dynamic action based on a parameter (such as calling an API in Laravel). However, when I include the last c ...

Using *ngIf to construct SVG icons in Angular 2 doesn't contribute to the DOM in any way

I am currently utilizing icoMoon to import a series of SVG icons. The structure of the html I'm trying to create using ngIf is as follows: <div class="contactIcon"> <svg class="icon icon-envelop"> <use xlink:href="symbol-d ...

JSReports encountered an unexpected token "<" in the JSON at position 0

Seeking assistance from those knowledgeable in JSReports, but open to suggestions from all... I've investigated the common issue of "unexpected token < in JSON at position 0", which typically arises when attempting to parse an HTML-formatted strin ...

Having trouble saving text in a textbox?

Is there a way to store text in a text box for future use? Every time I reload the page, the text in the box disappears. I have attempted the following solution: <td align='left' bgcolor='#cfcfcf'><input type="text" name="tx ...

What is the process for converting/executing TypeScript into JavaScript?

Having trouble running https://github.com/airgram/airgram Encountering this warning message from the post (node:9374) Warning: To load an ES module, set "type": "module" Have already added {"type": "module"} To pa ...

Direct your attention to the cursor problem in Internet Explorer

Here is a snippet of code that automatically changes background images: function changeBackground() { currentBackground++; if(currentBackground > 3) currentBackground = 0; $('body').fadeOut(0, function() { $('body&ap ...

Sequelize.Model not being recognized for imported model

I am encountering an issue while trying to implement a sequelize N:M relation through another table. The error message I keep receiving is as follows: throw new Error(${this.name}.belongsToMany called with something that's not a subclass of Sequelize ...

"We encountered an error with the external script while trying to load file content using jQuery's ajax function. It

//C.php <script type="text/javascript"> $(document).ready(function(e) { $('div').load("D.php"); }); </script> <div></div> //D.php <script type="text/javascript" src="D.js"></script> //D.js console.log(45 ...

Measuring data entries within JSON array utilizing JavaScript and Postman

A specific component is returning two records: { "value": [ { "ID": 5, "Pupil": 1900031265, "Offer": false, }, { "ID": 8, "Pupil": 1900035302, "Offer": false, "OfferDetail": "" } ] } My task i ...

Storing data in MongoDB using JavaScript on a web platform

Imagine a straightforward website with a common structure <html> <head></head> <body> <script type="text/javascript"> </script> </body> </html> Can data be written to MongoDB fr ...

Determining the navigation changes within a React browser

Hi there, I'm using browserHistory.goForward() and browserHistory.goBack() to navigate forward and backward in my app with arrow buttons. However, I need a way to determine if the route has actually changed after executing browserHistory.goForward/goB ...

What is the best way to implement conditional rendering for the next/image component in a React project?

I am facing a challenge with my library that is being utilized across various websites. One of these websites is built on Next JS, and I am looking to implement the next/image component for all images on this particular site without impacting the functio ...