Next.js页面在localhost上工作但不在vercel上工作

qmelpv7a  于 2023-04-20  发布在  其他
关注(0)|答案(1)|浏览(178)

我正在为一个网站构建博客,我刚刚完成了搜索结果页面。当我在localhost上尝试它时,它工作得很好,但当我将其部署到vercel时,它根本不工作。searchParams.query在线未定义,因为当我在线访问网页时,标题是:Searching for: undefined下面是我的代码,它使用了Sanity和Next 13:

import { getSearchResults } from "@/sanity/utils"
import Link from "next/link"
import { SearchBar } from "@/components"

export async function generateMetadata({ searchParams }) {
  return {
    title: `Searching for: ${searchParams.query}`
  }
}

export default async function SearchPage({ searchParams }) {
  const searchResults = await     getSearchResults(searchParams.query)

  return (
<main className="mt-[70px] min-h-screen">
  <div className="bg-byzantine-blue text-white py-10 px-5">
    <div className="container mx-auto flex flex-col justify-center items-center">
      <h2 className="text-lg md:text-xl mb-3 md:mb-5">Search results for:</h2>
      <SearchBar />
      <p className="mt-2 text-text-blue text-lg">{searchResults.length} {searchResults.length === 1 ? 'article' : 'articles'}</p>
    </div>
  </div>
  <div className="max-w-screen-web mx-auto py-10 px-6 md:px-10 flex flex-col gap-6 md:gap-10">
    {searchResults.map(article => (
      <Article key={article.id} {...article} />
    ))}
  </div>
</main>
  )
}

const Article = ({ description, id, title }) => {
  return (
<article className="border-solid border-[1px] flex flex-col items-center border-neutral-200 px-3 py-6">
  <h5 className="text-xl font-medium text-center">{title}</h5>
  <p className="mt-3 md:text-lg text-neutral-600 text-center max-w-4xl">{description}</p>
  <Link href={`/blog/${id}`} className="md:text-lg inline-block hover:bg-blue-500 mt-4 px-3 py-2 bg-byzantine-blue text-white">View article</Link>
</article>
  )
}

下面是<Searchbar />组件的代码:

"use client"
import React, { useState } from 'react'
import { useRouter, useSearchParams } from 'next/navigation'

export default function SearchBar() {
  const router = useRouter();
  const searchParams = useSearchParams();

  const search = useSearchParams().get('query')

  const [query, setQuery] = useState(search ? search : '');

  const handleSearch = () => {
    router.push(`/search?query=${query}`);
  }

  return (
    <div className='flex justify-center md:gap-4 gap-2  w-[80%] md:w-full max-w-xl mx-auto'>
      <input type="text" placeholder='Search for articles:' className='bg-transparent py-2 border-solid border-[2px] border-blue-400 px-4 md:px-6 grow placeholder:text-text-blue outline-none text-white text-lg focus:bg-white focus:text-black focus:placeholder:text-neutral-400' value={query} onChange={(e) => setQuery(e.target.value)} />
      <button onClick={handleSearch} className='bg-gold hover:bg-gold-hover px-4 md:px-6 text-lg text-white'>
    Search
  </button>
</div>
  )
}

任何帮助都很感激。谢谢!

zbq4xfa0

zbq4xfa01#

经过几个小时的努力,我找到了一个解决方案:

export const dynamic = 'force-dynamic'

我刚把它添加到page.js,就可以开始了

相关问题