【问题标题】:Nextjs page goes to 404 on refreshNextjs 页面在刷新时进入 404
【发布时间】:2019-07-15 20:31:23
【问题描述】:

我正在将 nextjs 和 graphql 用于 shopify POC。

我有一个组件显示产品列表,上面有指向产品页面的链接

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

handle 是集合名称,因此浏览器中的 url 看起来像 http://localhost:3000/new-releases/product/Plattan-2-Bluetooth 但在幕后,它实际上只是使用了一个名为 products 的页面,而我正在传递产品 ID。

现在在 product.js(粘贴在下面)中,我正在获取 id 的查询字符串值并执行另一个查询以获取产品。一切正常,但如果我点击刷新或复制并将网址粘贴到新窗口中,我会得到 404。

我知道这与路由有关,但我不确定我需要做什么来解决这个问题。谢谢

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;

【问题讨论】:

    标签: next.js


    【解决方案1】:

    您可以在项目根目录中名为 next.config.js 的文件中尝试这些操作

    module.exports = {
      exportTrailingSlash: true
    }
    

    查看this link

    【讨论】:

    • 谢谢,这就是我一直在寻找的
    • 这解决了我的问题,谢谢。没想到这么简单!
    • 注意:exportTrailingSlash 已重命名为 trailingSlash
    • 谢谢,我也遇到了这个问题。在我的情况下,页面在刷新时被重定向到主页,而地址栏保持页面 URL。我正在使用router.reload(window.location.pathname)
    【解决方案2】:

    这是因为当您使用 next/link 组件时,href 属性具有指向页面的“真实”URL,其中设置了项目 ID 的查询参数。这意味着在客户端(浏览器)上,Next.js 可以使用您的数据查询参数加载正确的页面(您的 product.js 页面)。

    但是当您从服务器加载时,无论是通过重新加载页面还是在新窗口中打开它,Next.js 都不知道要加载哪个页面,在这种情况下,我认为它会尝试查找文件 @987654326 @,当然不存在。

    如果您想拥有这些类型的 URL,您必须确保请求也被路由到服务器上的正确页面文件 (./pages/product.js)。您可以通过创建自定义服务器来做到这一点。 Next.js 存储库中有一堆examples,其中一个使用Express。这也包含在该网站的“学习”部分的一个名为Server Side Support for Clean URLs的教程中

    如果您决定使用 Express,您最终会得到如下结果:

    server.get('/:collection/product/:productUrl', (req, res) => {
      return app.render(req, res, '/product', { productUrl: req.params.productUrl})
    })
    

    这将呈现产品页面,您将在getInitialProps() 中的query 对象上获得productUrl。当然,现在您需要使用它而不是产品 ID 来获取您的数据。

    【讨论】:

    • 谢谢。我有一个运行“graphql-yoga”的后端服务器,我应该将它添加到其中还是仅用于 nextjs 的单独后端?
    • 这是一个单独的 nextjs 后端。
    • 那么,这个server.js的构建过程是什么?
    猜你喜欢
    • 2021-06-02
    • 2022-07-01
    • 2022-11-13
    • 2021-08-15
    • 2019-11-27
    • 2018-12-26
    • 2020-08-19
    • 2020-10-06
    • 1970-01-01
    相关资源
    最近更新 更多