Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nextjs page goes to 404 on refresh

Tags:

next.js

I'm using nextjs and graphql for a shopify POC.

I have a component that shows a list of products with links on them that point to the product page

<Link
   as={`${handle}/product/${url}`}
   href={`/product?id=${item.id};`}>
   <a>{item.title}</a>
</Link>

handle is the collection name so the url in the browser will look like http://localhost:3000/new-releases/product/Plattan-2-Bluetooth but behind the scenes its really just using a page called products and i'm passing the product id.

Now in product.js (pasted below) i'm getting the query string value of the id and doing another query to get the product. All works fine but then if i hit refresh or copy and paste the url into a new window i get 404.

I know this is something to do with routing but i'm not sure what i need to do to fix this. Thanks

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Query } from 'react-apollo';
import gql from 'graphql-tag';


class product extends Component {
  static async getInitialProps({query}) {
    console.log("query", query)
    return query ? { id: query.id.replace(';', '') } : {}
  }

  render() {

    const PRODUCT_FRAGMENT = gql`
        fragment ProductPage on Product {
          title
          description
          descriptionHtml
          id
          images(first: 10, maxWidth: 600) {
            edges {
              node {
                id
                altText
                originalSrc
              }
            }
          }
        }
      `;

    const PRODUCT_FETCH_QUERY = gql`
      query PRODUCT_FETCH_QUERY {
        node(id: "${this.props.id}") {
          __typename
          ...ProductPage
        } 
      }
      ${PRODUCT_FRAGMENT}
    `;

    return (
      <div>
         <Query query={PRODUCT_FETCH_QUERY}>
              {({ data, error, loading }) => {
                console.log("data", data)
                if (loading) return <p>Loading...</p>
                if (error) return <p>Error: {error.message}</p>
                return null}
              }
            </Query>
      </div>
    );
  }
}

product.propTypes = {

};

export default product;
like image 807
Adam Avatar asked Feb 21 '19 20:02

Adam


People also ask

Can I use next JS on Netlify to generate 404 errors?

For other errors, you can do the exact same thing with an _error.js file in the pages/ directory! The 404 error is special because it is always statically generated, but the others rely on the server. If you'd like to use the server-rendering aspects of Next.js on Netlify, check out our one-click install build plugin.

What can I do with an error in next JS?

Here's a quick example of what you could do: Because this is just like any other page component in Next.js, you can add whatever styling, links, data, or copy you'd like. For other errors, you can do the exact same thing with an _error.js file in the pages/ directory!

Does nextjs work on the local machine?

On development mode (local machine) everything works fine. Both static and dynamic routes are working properly and fetching data from the dontnet core Web API. However, when publishing the Nextjs app following this steps:

Can I use nextjs with DotNet core?

I'm using Nextjs for a front-end application and dotnet core 3.1 for the Web API. There are some pages that are static and other that are dynamic I followed the official documentation to achieve this. On development mode (local machine) everything works fine.


1 Answers

You can try these in a file called next.config.js in the root of your project

module.exports = {
  exportTrailingSlash: true
}

Check this link

like image 136
RicardoA-W Avatar answered Jan 02 '23 21:01

RicardoA-W