Encountering a Next.js prerendering issue when using getStaticPaths

I am currently developing a Next.js application. Here is the structure of my files:

cpanearme
 -components
   -listitem.js
 -pages
    -home
       -index.js
     -firm
        -[id].js

Below is the code for a list item that redirects to the dynamic route:

<a className='lead'
      onClick={() => {
        router.push({
          pathname: `/firm/${firm.id}`,
          query: { data: firm.id},
        })
      }}
>View Their Profile and Reviews</a>

The following is the complete dynamic page setup:

import { withRouter } from 'next/router'
import Image from 'next/image'
import Link from 'next/link'
import StickyNavbar from '../../components/StickyNavbar'
import ReviewItem from "../../components/ReviewItem"
import Footer from '../../components/Footer'
import path from "path"
import fs from "fs"

<!-- Rest of the JavaScript code -->

During the building phase, I encountered an error after implementing getStaticPaths. The specific issue is perplexing and does not yield relevant results when searched online.

Error: Export encountered errors on following paths:
        /firm/[id]
        /taxfaqs/[id]

Although everything passes successfully with get static props, I am unsure how to address the error related to [id]. Could it possibly be [...id] instead?

Answer №1

There appears to be some misunderstanding regarding the functionality of getStaticPaths and getStaticProps.

getStaticPaths is used in dynamic routes (such as /firm/[id]) to specify the list of paths that need to be pre-rendered to HTML during build time. This means you must return an array of potential paths for that particular route.

// Generates `firm/firm1` and `firm/firm2` paths during build time
export async function getStaticPaths() {
    return {
        paths: [
            { params: { id: 'firm1' } },
            { params: { id: 'firm2' } }
        ],
        fallback: true
    };
}

getStaticProps will then generate the props for each page based on the paths provided by getStaticPaths during build time.

// Generates props for a given path during build time
export async function getStaticProps({ params }) {
    // Implement your own logic to fetch data for the specified path and pass it to `firm`
    // If the route is like `/firm/firm1`, then params.id will be `firm1`
    return {
        props: {
            firm
        }
    };
}

Finally, in your page component, you can access the data passed through the props.

const Profile = ({ firm }) => {
    // The variable `firm` will hold the data for the specified path
}

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

Display a specific division depending on the outcome of an Ajax request within a PHP form

My PHP form has a layout similar to this: <form> <div id="inid"> National ID: <input type="text" id="individual_nid" oninput="getIndividualName(this.value)" /> </div> <hr /> name: <div id="individua ...

Leveraging an array retrieved through JQuery AJAX for auto-complete data in materializecss

I am delving into materializecss for the first time, aiming to enhance its auto complete feature by incorporating an array of options obtained from a database. Unfortunately, my attempts have not been successful. After scouring through Stack Overflow and o ...

Exploring Node.js: Managing Memory Consumption for Efficiency

I'm currently setting up multiple Node applications on one server, and I'm particularly worried about memory usage. Is there a specific amount of physical memory needed for a basic Node.js express service to run? Also, is there a method to limit ...

Showing Firestore Data as a map type: Issue encountered - React child cannot be an Object

Retrieving data from firestore: const [product, setProduct] = useState([]); const fetchProducts = async () => { const querySnapshot = await getDocs(collection(db, "products")); const productsArray = []; querySnapshot.forEach((doc) => { ...

Countdown Clock in JavaScript for Automatic Logout

I am currently working on creating a countdown timer in JavaScript that should decrement the value of a field by one every minute (for example, starting at 20 and then changing to 19, 18, 17, and so on). However, I am facing issues with its functionality ...

creating interconnections between input fields by employing event listeners in JavaScript

I've been experimenting with binding multiple input fields to a span element using jQuery. Specifically, I have two input fields containing integer values and the sum of these values is displayed in a span element. I want the span element to update wh ...

Leveraging JS Variables within Twig Template

I am trying to incorporate a JavaScript variable into Twig. Despite attempting to implement the solution mentioned here, my code below does not render the map properly. If I remove the part between "var polylineCoordinates" and "polyline.setMap(map);", th ...

Change a Python string in the format 'min:sec' into a time object so it can be displayed correctly and sorted by the jQuery tablesorter

Currently, I am facing an issue where tablesorter is only working correctly on string representations of time that are below '25:00'. Any values above this are being sorted lower than strings like '24:12' or '09:24'. It seems ...

Updates to the visibility of sides on ThreeJS materials

When viewed from the back, the side is hidden as desired, but I am struggling to determine if it is visible from the renderer or camera. new THREE.MeshBasicMaterial({ map: new, THREE.TextureLoader().load('image.jpg'), side: THREE. ...

Azure SQL Server linked with a REST API built on Express.js/Node.js encountered an error stating: "TypeError: req.sql is not a function"

Having trouble connecting to Azure SQL Server with express4-tedious. I'm working on building an app in react-native with a Node/Express server (REST API), but encountered this error after setting up express4-tedios in Express: req.sql is not a functio ...

`To filter out JSON data that does not exist in Javascript, follow these steps:``

Utilizing JavaScript Fetch to retrieve JSON data, I am aiming to present the information in a well-arranged HTML layout. I encountered challenges when attempting to process certain content. The majority of the data objects I am parsing include images that ...

Constance in JavaScript

Looking to create constants in Javascript and seeking advice on the best way to do so. While I am aware that true constants don't technically exist, I'm finding it difficult to change values after exporting them. Are constants necessary? Is the ...

Ajax encounters difficulty in parsing JSON data

I have thoroughly researched similar questions on this topic, but none of them have provided a solution to my problem. My current challenge involves parsing JSON data that is being returned from a PHP script running on the server. I have already used JSON ...

Using jQuery, prevent the user from entering text into an input box when a checkbox

In my project, there is a checkbox that determines whether an input box is disabled. The issue arises when I try to disable the input based on the database value. For example, when the value is 0, it should display as disabled false; however, the input rem ...

Why does the behavior of creating relative URLs with `new URL()` change when the first parameter is a variable?

Recently, I've been diving into the world of web workers in NextJs. I tried to follow their example, but ran into an issue that's been bugging me. It seems that passing the worker relative URL as a variable to new URL(url, baseUrl) isn't wor ...

I am struggling to display the data fetched by Next.js on the page

I am struggling to display the data from the first file in my tanstack table or as a string within the HTML, even though I can see it in a console.log when grabbed by axios. The tanstack table worked fine with hardcoded data. In the console image provided, ...

Passing a variable to a modal in AngularJS

In my project, I am utilizing https://github.com/simpulton/angularjs-wizard and have made some modifications to it (specifically changed var app to $scope). It is functioning well, however, I am facing an issue where I need to pass a variable to the open f ...

Encountering undefined JavaScript functions when called from HTML

I had most of these functions working perfectly, but after restarting my program, I'm now encountering issues with undefined functions in Chrome. I can't pinpoint the exact problem, and even though I've checked through Eclipse, I still can&a ...

Unable to bind click eventListener to dynamic HTML in Angular 12 module

I am facing an issue with click binding on dynamic HTML. I attempted using the setTimeout function, but the click event is not binding to the button. Additionally, I tried using template reference on the button and obtaining the value with @ViewChildren, h ...

Attempting to download an image through an axios fetch call

There is an issue I am facing while trying to retrieve an image from the website www.thispersondoesnotexit.com. function getImage() { axios({ method: 'get', url: 'https://www.thispersondoesnotexist.com/image' }) ...