getStaticProps is not getting invoked inside blogs/[slug].js
Answered
Masai Lion posted this in #help-forum
Masai LionOP
please help me run getStaticProps
repository link: https://github.com/ayush166/nextjs
repository link: https://github.com/ayush166/nextjs
import { allPosts } from 'contentlayer/generated'
import React from 'react'
export const getStaticPaths = () => {
return {
paths: allPosts.map((p) => ({ params: { slug: p.url } })),
fallback: false,
}
}
export async function getStaticProps(){
const res = await fetch('https://jsonplaceholder.typicode.com/posts')
const repo = await res.json()
console.log("props",repo);
return { props: { repo } }
}
const BlogPage = ({repo}) => {
console.log("ds",repo);
return (
<div>{ repo}</div>
)
}
export default BlogPage22 Replies
Masai LionOP
Getstaticprops is not getting invoked
@Masai Lion Getstaticprops is not getting invoked
what do you see on the console?
Masai LionOP
no errors in server console nor browser console
there should be a console.log output from getStaticProps
in serverconsole/vs code terminal
I copied the code and try to run it and it works except this line
<div>{ repo}</div>Masai LionOP
getStaticProps function is not getting invoked that is the issue
@Ray I copied the code and try to run it and it works except this line `<div>{ repo}</div>`
Masai LionOP
repo value returned is null yes
@Masai Lion repo value returned is null yes
no, it return the JSON object and cause an error when rendering it
just cloned your repo and it does not work run
Masai LionOP
import { allPosts } from 'contentlayer/generated'
import React from 'react'
export const getStaticPaths = () => {
console.log("getStaticPaths")
return {
paths: allPosts.map((p) => ({ params: { slug: p.url } })),
fallback: false,
}
}
export const getStaticProps = async()=>{
console.log("getStaticProps")
const res = await fetch('https://jsonplaceholder.typicode.com/posts')
const repo = await res.json()
console.log("props",repo);
return { props: { repo } }
}
const BlogPage = ({repo}) => {
console.log("ds",repo);
return (
<div>Blog Page</div>
)
}
export default BlogPageyou are seeing this code right ?
at pages/blog/[slug].js
@Ray just cloned your repo and it does not work run
Masai LionOP
console.log dosen't show up for getStaticProps
my requirement it should show up
my requirement it should show up
@Masai Lion console.log dosen't show up for getStaticProps
my requirement it should show up
return {
paths: allPosts.map((p) => ({ params: { slug: p.url } })),
fallback: false,
}
because you set fallback to false
paths: allPosts.map((p) => ({ params: { slug: p.url } })),
fallback: false,
}
because you set fallback to false
try set it to true and it will run
you generate the params.slug with p.url which is
/blogs/higher-order-functions in getStaticPathsand the slug from url is higher-order-functions
p.url.replace("/blogs/", "") this should fix itAnswer
Masai LionOP
Thanks a lot @Ray everything is fine now
ðŸ»