'404 when requesting a single entry in contentful
I am trying to request a single entry from contentful CMS with the getEntry() method. I keep getting a 404. I really don't understand what am I doing wrong.
export async function getStaticProps({ params }) {
const p = params.id;
const client = contentful.createClient({
space: process.env.CONTENTFUL_SPACE_ID,
environment: process.env.CONTENTFUL_ENVIRONMENT_ID,
accessToken: process.env.CONTENTFUL_ACCESS_TOKEN,
});
const entry = await client.getEntry(p);
return { props: { entry: entry } };
}
Solution 1:[1]
I created a small project to replicate the issue, but I couldn't. It works fine for me. This is what my sample code looks like:
import {createClient} from 'contentful'
export default function Comp() {
return (
<div>
Hello
</div>
)
}
export async function getStaticProps({ params }) {
const p = params.id;
const client = createClient({
space: process.env.CONTENTFUL_SPACE_ID,
environment: process.env.CONTENTFUL_ENVIRONMENT_ID,
accessToken: process.env.CONTENTFUL_ACCESS_TOKEN,
});
const entry = await client.getEntry(p);
return { props: { entry: entry } };
}
export async function getStaticPaths() {
const client = createClient({
space: process.env.CONTENTFUL_SPACE_ID,
environment: process.env.CONTENTFUL_ENVIRONMENT_ID,
accessToken: process.env.CONTENTFUL_ACCESS_TOKEN,
});
const entry = await client.getEntries({
content_type: "blogPost"
})
const paths = entry.items.map(item => {return {params: {id: item.sys.id}}});
return { paths, fallback:true}
}
You might be getting a 404 because the paths might be correctly configured. If the above code sample doesn't help, feel free to link the repo or share more details :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | harshil1712 |