I have an API
that gets a list of documents from FaunaDB
for a NextJS
app.
I want to generate a separate page for each document. I’m trying to do so using getStaticProps
and getStaticPaths
, but I’m not sure how to query the API for specific documents by a differentiable parameter, e.g. title - which doesn’t duplicate.
export default function Title({ paper }) {
// paper is just the entire dataset, and isn't split by id or author etc.
return (
<div>
{paper.map(paper => (
<h1>{paper.data.title}</h1>
))}
</div>
)
}
export async function getStaticProps({ params }) {
// ideally, results should be split down to e.g. `/api/getPapers/title`, but this didn't work
const results = await fetch(`http://localhost:3000/api/getPapers/`).then(res => res.json());
return {
props: {
paper: results
}
}
}
export async function getStaticPaths() {
const papers = await fetch('http://localhost:3000/api/getPapers').then(res => res.json());
const paths = papers.map(paper => {
return {
params: {
authors: paper.data.authors.toString()
}
}
})
return {
paths,
fallback: false
}
}
The problem is that the component Title
receives the whole dataset and not a subset split by title via getStaticProps
.
Is there someway to query and splitting the data by single document so I can find each page, e.g. by navigating to app.com/<data>/<document_1>
for instance app.com/title/<respective-title>
?