nextjs,单击后,URL更新,但没有内容

发布于 2025-01-18 04:30:34 字数 2292 浏览 1 评论 0原文

我试图弄清楚为什么当我单击浏览器的返回按钮或用路由器设置自己的按钮时,url正在更新,而不是内容? 当我刷新页面时,内容已更新ET ET,控制台将错误打印出

错误:

错误:缩小反应错误#418;访问 https://reactjs.org/decocs/decoder.html? 418 完整 消息或使用非限制的开发环境进行完整错误,并且 其他有用的警告。

水合失败,因为初始UI与什么不匹配 在服务器上渲染。

我使用strapi nextJS启动器,几乎没有触摸api.js。

export function getStrapiURL(path) {
  return `${
    process.env.NEXT_PUBLIC_STRAPI_API_URL || "http://localhost:1337"
  }${path}`
}

// Helper to make GET requests to Strapi
export async function fetchAPI(path) {
  const requestUrl = getStrapiURL(path)
  const response = await fetch(requestUrl)
  const data = await response.json()
  return data
}

export async function getCategories() {
  const categories = await fetchAPI("/categories")
  return categories
}

export async function getCategory(slug) {
  const categories = await fetchAPI(`/categories?slug=${slug}`)
  return categories?.[0]
}

export async function getProducts() {
  const products = await fetchAPI("/products")
  return products
}

export async function getProduct(slug) {
  const products = await fetchAPI(`/products?slug=${slug}`)
  return products?.[0]
}

export async function getLibrairies() {
  const librairies = await fetchAPI("/librairies")
  return librairies
}

export async function getLibrairie(slug) {
  const librairies = await fetchAPI(`/librairies?slug=${slug}`)
  return librairies?.[0]
}

这是我的index.js juste

import React from "react"
import Head from "next/head"
import { getProducts } from "../utils/api"
import ProductsList from "../components/ProductsList"

const HomePage = ({ products }) => {
  return (
    <div>
      <Head>
        <title>Classe moyenne éditions</title>
        <meta
          name="description"
          content="Welcome to the website of Classe moyenne édditions, art books French publishing house"
        />
      </Head>
      <ProductsList products={products} />
    </div>
  )
}

export async function getStaticProps() {
  const products = await getProducts()
  return { props: { products }, revalidate: 1 }
}

export default HomePage

I'm trying to figure out why when I click to the browser's back button or setting myself a button with a router.back, the url is updating but not the content?
When I refresh the page, the content is updated et the console prints an error

the error :

Error: Minified React error #418; visit
https://reactjs.org/docs/error-decoder.html?invariant=418 for the full
message or use the non-minified dev environment for full errors and
additional helpful warnings.

Hydration failed because the initial UI does not match what was
rendered on the server.

I work with a strapi nextjs starter and barely touch the api.js

export function getStrapiURL(path) {
  return `${
    process.env.NEXT_PUBLIC_STRAPI_API_URL || "http://localhost:1337"
  }${path}`
}

// Helper to make GET requests to Strapi
export async function fetchAPI(path) {
  const requestUrl = getStrapiURL(path)
  const response = await fetch(requestUrl)
  const data = await response.json()
  return data
}

export async function getCategories() {
  const categories = await fetchAPI("/categories")
  return categories
}

export async function getCategory(slug) {
  const categories = await fetchAPI(`/categories?slug=${slug}`)
  return categories?.[0]
}

export async function getProducts() {
  const products = await fetchAPI("/products")
  return products
}

export async function getProduct(slug) {
  const products = await fetchAPI(`/products?slug=${slug}`)
  return products?.[0]
}

export async function getLibrairies() {
  const librairies = await fetchAPI("/librairies")
  return librairies
}

export async function getLibrairie(slug) {
  const librairies = await fetchAPI(`/librairies?slug=${slug}`)
  return librairies?.[0]
}

This is my index.js juste in case

import React from "react"
import Head from "next/head"
import { getProducts } from "../utils/api"
import ProductsList from "../components/ProductsList"

const HomePage = ({ products }) => {
  return (
    <div>
      <Head>
        <title>Classe moyenne éditions</title>
        <meta
          name="description"
          content="Welcome to the website of Classe moyenne édditions, art books French publishing house"
        />
      </Head>
      <ProductsList products={products} />
    </div>
  )
}

export async function getStaticProps() {
  const products = await getProducts()
  return { props: { products }, revalidate: 1 }
}

export default HomePage

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉宸 2025-01-25 04:30:34

getStaticProps 只会在构建时调用,如果您想在运行时动态获取产品,您应该使用 getServerSideProps

更多关于 next.js 数据获取的阅读:https://nextjs.org/docs/basic-features/data-fetching/overview

getStaticProps will only be called at build time, if you want to fetch the products dynamically at runtime, you should use getServerSideProps

More readings on next.js data fetching: https://nextjs.org/docs/basic-features/data-fetching/overview

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文