简体   繁体   English

Next.js static站点生成,为什么getStaticProps参数为空?

[英]Next.js static site generation, why is getStaticProps params empty?

Intro介绍

I'm building an app with next.js.我正在用 next.js 构建一个应用程序。 Using it's static site generation.使用它的 static 站点生成。 For some reason the params object in getStaticProps is empty.由于某种原因, getStaticProps中的参数 object 为空。 I've read the examples and docks for the last few hours and cannot see why the cause.我已经阅读了过去几个小时的示例和停靠站,但看不出原因。 Perhaps someone with more next experience can spot the problem.也许有更多下一个经验的人可以发现问题。

/pages/projects/[id].js /pages/projects/[id].js

export async function getStaticPaths() {
  return {
    paths: [
      { params: { id: '1' } },
    ],
    fallback: false
  };
}


export async function getStaticProps({params}) {
  console.log(params); // params is just an empty object
  const { data: projectData } = await axios(`${CMS_URL}/projects/${params.id}`);
  console.log(projectData);
  return {
    props: {
      ...projectData,
    }
  };
}

export default function ProjectDetailPage(props) { ...

next.config.js next.config.js

...
exportPathMap: async () => {
    const paths = {
      '/': { page: '/' },
      '/blog': { page: '/overview' },
    };

    const { data: projectsData } = await axios(`${apiUrl}/projects`);
    projectsData.forEach(project => {
      paths[`/projects/${project.id}`] = { page: '/projects/[id]', query: { id: project.id } };
    });

    console.log(paths);
    /*
      {
        '/': { page: '/' },
        '/blog': { page: '/overview' },
        '/projects/1': { page: '/projects/[id]', query: { id: 1 } }
      }
     */

    return paths;
  }
...

在此处输入图像描述

Question问题

I've added the correct folder structure, and exportPathMap .我添加了正确的文件夹结构和exportPathMap Also added the static path and props to my component.还将 static 路径和道具添加到我的组件中。 I cant see why the params object is empty.我不明白为什么参数 object 是空的。 Can you tell me why its not working?你能告诉我为什么它不起作用吗?

I almost had the same problem but I'm not so sure about how I fixed it but it is working for me now:我几乎遇到了同样的问题,但我不太确定我是如何解决它的,但它现在对我有用:

function Workshop({workshop}) {
    return (
        <div>
            Hello World
        </div>
    )
}

export async function getStaticProps({params}) {    
    const workshop = await axios.get('/your/url/here');
    return { props: { workshop: workshop.data } }
}

export async function getStaticPaths() {
    return { 
        paths: [
            { params: { id: '1' } },
            { params: { id: '2' } },
            { params: { id: '3' } }
          ], 
        fallback: false 
    }
}

export default Workshop;

This is my next.config.js file:这是我的 next.config.js 文件:

module.exports = {
    pageExtensions: ['jsx', 'js'],
    exportPathMap: async function (
            defaultPathMap, 
            { dev, dir, outDir, distDir, buildId }
        ) {
        return {
            '/': { page: '/' },
            '/workshops': { page: '/workshops' },
            '/workshops/[id]': { page: '/workshops/[id]' },
        }
    }
}

And then my structure folder: pages / workshops / [id].js然后是我的结构文件夹:pages / Workshop / [id].js

I hope it helps.我希望它有帮助。

getStaticProps is executed upon each request in development, however in production, it gets already gets executed during the build time. getStaticProps在开发中根据每个请求执行,但是在生产中,它已经在构建期间执行。 So before you even deploy your app, pages with getStaticProps are already created, cached on the server.因此,在您部署应用程序之前,已经创建了带有getStaticProps的页面,并缓存在服务器上。 Since during the build time, there is no browser, you cannot access to params or query.由于在构建期间没有浏览器,因此您无法访问参数或查询。 Becase params and query lives on the route.因为参数和查询存在于路线上。

@thibaut devigne who answered your question says he fixed but he does not how. @thibaut devigne 回答了你的问题,他说他修好了,但他没有。 His code works because he statically gets the path in getStaticPath , and once the "paths" are generated they are being passed to getStaticProps .他的代码有效,因为他静态地获取getStaticPath中的路径,并且一旦生成“路径”,它们就会被传递给getStaticProps

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM