Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

getstaticpaths in nextjs

//pages
ews[slug].tsx
export async function getStaticPaths() {
  const res = await fetch(`${baseUrl}/wp-json/wp/v2/posts`)
  const posts = await res.json()
  const paths = posts.map(({ slug }) => ({ params: { slug: `${slug}` } }))
  return {
    paths,
    fallback: true,
    //The paths that have not been generated at build time will not result in a 404 page. 
    //Instead, fallback: true This will be used to automatically render
    //the page with the required props.
  }
}

export async function getStaticProps(context) {
  const resPost = await fetch(`${baseUrl}/wp-json/wp/v2/posts?slug=${context.params.slug}`)

  const post = await resPost.json()

  return {
    props: {
      post,
    },
    revalidate: 10,
  }
}
Comment

next js getStaticPaths

// create a post folder and index.js file in pages/posts folder
import Link from 'next/link';
const Posts = ({ posts }) => (
  <div>
    {
      posts.map(post => (
        <div>
          <Link href={`/posts/${post.id}`}><a style={{fontSize:'20px',textDecoration: 'underline'}}>{post.title}</a></Link>
          <p>{post.body}</p>
        </div>
      ))
    }
  </div>
)

export async function getServerSideProps() {
  const res = await fetch(`https://jsonplaceholder.typicode.com/posts`)
  const posts = await res.json()
  return { props: { posts } }
}
export default Posts;



// then add another file in 
pagesposts[id].js

function Post({ post }) {
    // Render post...
    return (
        <div>
            <h1>{post.title}</h1>
            <p>{post.body}</p>
        </div>
    )
}

export async function getStaticPaths() {
    const res = await fetch('https://jsonplaceholder.typicode.com/posts')
    const posts = await res.json()
    const paths = posts.map((post) => ({
        params: { id: post.id.toString() },
    }))

    // We'll pre-render only these paths at build time.
    // { fallback: false } means other routes should 404.
    return { paths, fallback: true }
}

export async function getStaticProps({ params }) {
    const res = await fetch(`https://jsonplaceholder.typicode.com/posts/${params.id}`)
    const post = await res.json()
    return { 
        props: { post },
        revalidate: 10,
    }
}
export default Post
Comment

next js getStaticPaths

function Post({ post }) {
    // Render post...
    return (
        <div>
            <h1>{post.title}</h1>
            <p>{post.body}</p>
        </div>
    )
}

export async function getStaticPaths() {
    // Call an external API endpoint to get posts
    const res = await fetch('https://jsonplaceholder.typicode.com/posts')
    const posts = await res.json()
    const paths = posts.map((post) => ({
        params: { id: post.id.toString() },
    }))

    return { paths, fallback: true }
}

export async function getStaticProps({ params }) {
    const res = await fetch(`https://jsonplaceholder.typicode.com/posts/${params.id}`)
    const post = await res.json()
    return { 
        props: { post },
        revalidate: 10,
    }
}

export default Post
Comment

next js getStaticPaths

function Post({ post }) {
    // Render post...
    return (
        <div>
            <h1>{post.title}</h1>
            <p>{post.body}</p>
        </div>
    )
}

export async function getStaticPaths() {
    // Call an external API endpoint to get posts
    const res = await fetch('https://jsonplaceholder.typicode.com/posts')
    const posts = await res.json()
    const paths = posts.map((post) => ({
        params: { id: post.id.toString() },
    }))

    return { paths, fallback: true }
}

export async function getStaticProps({ params }) {
    const res = await fetch(`https://jsonplaceholder.typicode.com/posts/${params.id}`)
    const post = await res.json()
    return { 
        props: { post },
        revalidate: 10,
    }
}

export default Post
Comment

next js getStaticPaths

// create a post folder and index.js file in pages/posts folder
import Link from 'next/link';
const Posts = ({ posts }) => (
  <div>
    {
      posts.map(post => (
        <div>
          <Link href={`/posts/${post.id}`}><a style={{fontSize:'20px',textDecoration: 'underline'}}>{post.title}</a></Link>
          <p>{post.body}</p>
        </div>
      ))
    }
  </div>
)

export async function getServerSideProps() {
  const res = await fetch(`https://jsonplaceholder.typicode.com/posts`)
  const posts = await res.json()
  return { props: { posts } }
}
export default Posts;



// then add another file in 
pagesposts[id].js

function Post({ post }) {
    // Render post...
    return (
        <div>
            <h1>{post.title}</h1>
            <p>{post.body}</p>
        </div>
    )
}

export async function getStaticPaths() {
    const res = await fetch('https://jsonplaceholder.typicode.com/posts')
    const posts = await res.json()
    const paths = posts.map((post) => ({
        params: { id: post.id.toString() },
    }))

    // We'll pre-render only these paths at build time.
    // { fallback: false } means other routes should 404.
    return { paths, fallback: true }
}

export async function getStaticProps({ params }) {
    const res = await fetch(`https://jsonplaceholder.typicode.com/posts/${params.id}`)
    const post = await res.json()
    return { 
        props: { post },
        revalidate: 10,
    }
}
export default Post
Comment

PREVIOUS NEXT
Code Example
Typescript :: typescript filter list by property 
Typescript :: angular create object 
Typescript :: react native elements input phone number max characters 
Typescript :: ion modal dismiss 
Typescript :: get key of enum typescript 
Typescript :: latex figure over two columns 
Typescript :: angular mailto on button click 
Typescript :: sort array elements in descending order based on object key 
Typescript :: java sort arraylist of objects by field descending 
Typescript :: vsc typescript auto build on save 
Typescript :: what is endurance testing 
Typescript :: typescript default public or private 
Typescript :: typescript-eslint disable 
Typescript :: ternary operator typescript 
Typescript :: nodejs exec exit code 
Typescript :: Error: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions 
Typescript :: size of array typescript 
Typescript :: reddit requests 429 
Typescript :: how to declare an empty array in typescript 
Typescript :: angular 13 viewchild 
Typescript :: pdf viewer ionic 4 
Typescript :: object.fromentries typescript 
Typescript :: select field where name starts a in sql 
Typescript :: typescript arr numbers and strings 
Typescript :: making barplots in r 
Typescript :: wordpress number of posts by user 
Typescript :: embed youtube search results into website 
Typescript :: basic variable typescript 
Typescript :: hide elements in 2s jquery 
Typescript :: typescript convert string to character array 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =