我在这里和那里找到了一些关于从 Contentful 采购到 NextJS 的指南,您可以在其中为静态博客提供内容。这是一个起点。
但对于单页内容,我无法使用 API 获得任何结果。不知道为什么。
这是我正在处理的代码的一些摘录,其中包含用于解释结构的注释。
// components/layout.js
(...)
import client from '../utils/contentful/client' // <-- This imports the call for contentful [const contentful = require('contentful')]
// The function bellow is supposed to reach the content and export it as a props, right?
export async function getStaticProps({entryId}) {
const paginas = await client
.getEntries({ content_type: "paginas" })
.getEntry({ entry_id: entryId })
.then((response) => response.items)
return {
props: {
paginas
},
}
}
// This function bellow is called within the <Footer /> where I pass the entry_id as a props
export function ContatoPage({ pagina }) {
getStaticProps(pagina)
// Is this how it works? Can I call this function getStaticProps() here so the
//props will be reached in the constant bellow?
const { titulo, content } = paginas.fields
return (
<Box>
<Heading>{titulo}</Heading>
<p>{content}</p>
</Box>
)
}
// There's a static contact info page hanging on every footer and I'm trying to fetch it's values from Contentful.
function Layout( ... ) {
return (
(...)
<Footer>
<ContatoPage pagina="<entry_id>" /> // this is where I pass the string value for the page I'm trying to reach.
</Footer>
}
export default Layout
任何想法或评论将不胜感激。