Next.js 如果包含[id].js页面,如何在Google主机上部署NEXTJS应用程序?

Next.js 如果包含[id].js页面,如何在Google主机上部署NEXTJS应用程序?,next.js,Next.js,我有一个使用谷歌托管的应用程序。在实施动态路由之前,我通过执行next export和firebase deploy托管了它 在我的[id].js文件中,我使用getServerSideProps获取数据,在执行next export时,我得到了下一个错误: 无法导出带有getServerSideProps的页面 解决方案是“从package.json中删除next export”。但是,如果没有下一次导出,我如何托管我的应用程序?为什么不在vercel上托管你的应用程序?它让一切变得如此简单。

我有一个使用谷歌托管的应用程序。在实施动态路由之前,我通过执行
next export
firebase deploy
托管了它

在我的
[id].js
文件中,我使用
getServerSideProps
获取数据,在执行
next export
时,我得到了下一个错误:

无法导出带有getServerSideProps的页面


解决方案是“从package.json中删除
next export
”。但是,如果没有下一次导出,我如何托管我的应用程序?

为什么不在vercel上托管你的应用程序?它让一切变得如此简单。如果你真的想把它放在google上,用getStaticProps替换getServerSideProps,或者用客户端渲染

我找到了一个解决方案,那就是使用
getStaticProps
GetStaticPath
而不是
getServerSideProps
,下面是如何正确使用它的示例:

// pages/posts/[id].js

function Post({ post }) {
  // Render post...
}

// This function gets called at build time
export async function getStaticPaths() {
  // Call an external API endpoint to get posts
  const res = await fetch('https://.../posts')
  const posts = await res.json()

  // Get the paths we want to pre-render based on posts
  const paths = posts.map((post) => ({
    params: { id: post.id },
  }))

  // We'll pre-render only these paths at build time.
  // { fallback: false } means other routes should 404.
  return { paths, fallback: false }
}

// This also gets called at build time
export async function getStaticProps({ params }) {
  // params contains the post `id`.
  // If the route is like /posts/1, then params.id is 1
  const res = await fetch(`https://.../posts/${params.id}`)
  const post = await res.json()

  // Pass post data to the page via props
  return { props: { post } }
}

export default Post

对于getStaticProps,我必须使用getStaticPaths,但如何将getStaticPaths与动态数据一起使用?只需将所有数据添加到getStaticPaths中,例如,如果您有一个名为[id]的动态页面,那么在getStaticPaths中,您将添加数据的所有id,以便可以将getStaticProps与动态数据一起使用。在文档中提到过,你可以在这里检查,这里是一个例子,如果你想在google@宠儿上主持,是的,你是对的!我在谷歌上搜索,没有找到任何使用getServerSideProps的解决方案,我使用getStaticProps和getStaticPaths,我根据我的内部数据生成所有可能的路径,请用示例更新您的答案,我会将其标记为解决方案。