When implementing getStaticProps and getStaticPaths in a dynamic route, a 404 error page is displayed

As someone new to Next.js, I decided to delve into creating a basic news website where I could access articles through this specific route:

/[category]/[article]

The directory structure of pages is organized as follows:

_pages
__[category]
____index.jsx
____[...all].jsx

Shown here is the content of [...all].jsx:

export default function Article() {
  const router = useRouter();
  const { category, all } = router.query;

  return (
    <>
      {category}/{all[0]}
    </>
  );
}

However, after incorporating the getStaticProps and getStaticPaths methods, I encountered a consistent 404 error:

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

export async function getStaticProps({ params }) {
  return { props: { params } };
}

In my search for a straightforward example of nested dynamic routing with getStaticProps, I found that the official documentation lacked sufficient coverage on this topic. Can anyone provide guidance on how to resolve this issue?

Answer №1

There are two main issues to address here. Firstly, the paths array is empty, which means no paths will be generated at build time. Secondly, setting fallback: false results in no new paths being generated at request time.

To rectify this, you can populate the paths array with the desired paths for build time generation. Alternatively, changing fallback to fallback: 'blocking' will resolve the 404 error.

export async function getStaticPaths() {
    return {
        paths: [],
        fallback: 'blocking'
    };
}

The implementation of getStaticPaths mentioned above ensures that paths are not generated during build time but instead dynamically created when requested.

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

Retrieve the direction of panning when the panning stops with hammer.js and limit the possible

I have integrated the hammer.js library along with its jQuery plugin. I followed the documentation and used the following code to initialize it on .game-card-mobile divs: /* Creating a Hammer object for swipeable game cards */ var $gameCard = $('.gam ...

When trying to access document.cookie, an empty string is returned despite the presence of cookies listed in developer tools, and the httpOnly flag is set

There are times when I encounter an empty string while trying to access document.cookie on the login page, despite the following conditions being met: The cookies are visible in the Chrome and Firefox developer tools, The httpOnly flag of the cookie I&ap ...

Sending a PHP variable to a modal using jQuery Ajax

I've encountered an issue with my jQuery ajax script. I'm struggling to pass a variable to the modal despite spending all weekend trying to debug it. Here is the link used to call the modal and the ID I want to pass: echo '<img src="./i ...

What is the reason behind decorators needing to utilize apply(this) on a function?

I've been delving into the realm of JavaScript and exploring decorator code. One thing I've noticed is that when looking at decorator code like the example below, the input function always applies to 'this' even though it doesn't a ...

The "initialized" event in angular2-tree-component fires prior to the data being loaded

Utilizing the angular2-tree-component, my goal is to display an already expanded tree. According to Angular docs, the initialized event should be used for expanding the tree after the data has been received: This event triggers after the tree model has ...

Automated navigation to login page following a serverside request on Next.js application router

Seeking advice on how to handle server-side requests to fetch user data authenticated via cookies. Missing details include forwarding cookies through the cookies function (not shown here). If authentication fails with a 401 error, I am looking to redirect ...

Adjust the z-Index of the list item element

I am attempting to create an effect where, upon clicking an icon, its background (width and height) expands to 100% of the page. However, I am struggling with ensuring that the 'effect' goes underneath the this.element and above everything else. ...

Material-UI Scroll Dialog that begins scrolling from the bottom

While attempting to incorporate a scrolling div with the ref tag inside Dialog Material-UI Design, I encountered an error stating Cannot read property 'scrollHeight' of undefined When running my code outside of the Dialog, it functions correctly ...

The Curious Case of Strange Behavior in Next.js Styled Components

Hey there, I'm currently tackling a project and running into an issue with my Sidebar Component: After (re-)starting the server, the component appears like this However, when clicking on a link item or navigating to another URL, it suddenly transform ...

Problem with Ajax functionality on Jquery Dialog

I have implemented a Jquery.dialog feature in my application for sending messages. When the user clicks on "new", the dialog box appears, allowing them to select the recipient (currently working with IDs instead of usernames). On the first opening of the ...

What is the process for setting up a single button selection using OR logic and multiple button selection using AND logic?

Currently working on implementing button functionality in React for filtering data. The requirement is that when selecting specific criteria, such as bedroom 3 or multiple selections like bedroom 2 and 3, the logic should vary. For instance, selecting bedr ...

"The Onclick and Link functions are functioning as expected when running locally, but are not working properly on the Net

Click here for the GitHub link Visit this Netlify site View image description here The Onclick event and Link tag are only functioning locally, not on the Netlify server. How can I solve this issue? Please provide assistance and share your thoughts. T ...

Search using the reference feature in sanity.io

In my schema design, I have three interconnected schemas - docTopics, subTopics, and article. I am looking to create a query for the docTopics schema that returns all references to subTopics within it. export const queryRefrence = groq` { "topi ...

When using JavaScript, links within the window.location are automatically altered

When using window.location (.assign, .replace, .href) to redirect to a product page on click, I encountered an issue where it automatically changes some of the href links. For example: instead of "previous href= 'commercial/fonts/fonts.min.css' ...

Minifying Angular using grunt leads to 'Error initializing module' issue

Currently, I have multiple controllers, models, and services set up as individual files for preparation before minification. My goal is to combine and minify all these files into one JS file for production. To illustrate how my files are structured, here ...

Is there a way to show a fallback message for unsupported video file formats?

When incorporating a video element on my webpage, I typically use the following code: <video src="some source" controls> Error message </video> Based on my knowledge, the "Error message" will only appear if the browser does not support the ...

Tips for updating an element in an array by using another element from a separate array

What is the objective? We have two arrays: orders and NewOrders We need to check for any orders with the same order_id in both arrays. If there is a match, we then compare the order status. If the order from the NewOrders array has a different status, w ...

Guide to Triggering a Page View Event in Google Tag Manager with Angular

Previously, I manually fired the Page View Event using JavaScript from app.component.ts while directly accessing Google Analytics: declare var gtag: Function; ... constructor(private router: Router) { const navEndEvents = this.router.events.pipe( fil ...

Manage the lineup of tasks in the bull queue by organizing them into groups

I am currently working on a nodejs application that handles queues using the bull library. The application is required to make multiple asynchronous HTTP requests and then process the results of these calls. I'm curious about whether bull would be an ...

How is it that this JavaScript task does not trigger an error: const a = (1, 2, 3, 4);

let x = (5, 6, 7, 8); console.log(x); let y = 5, 6, 7, 8; console.log(y); In the example above, x will be assigned a value of 8, while the second line will result in an error. What is the reason behind the success of the first assignment? How does it qua ...