question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Docs] Clarify using api functions in getStaticProps/getStaticPaths

See original GitHub issue

Feature request

At the end of the getStaticProps api specification there’s the following text:

Note: You should not use fetch() to call an API route in your application. Instead, directly import the API route and call its function yourself. You may need to slightly refactor your code for this approach.

Fetching from an external API is fine!

I think adding an example like this, would be helpful to instruct how you can actually use your own API in getStaticProps. The following code shows that you have to return something instead of using res.send() which would be undefined. Also, this is also true for getStaticPaths right?.

export default async function functionName() {
  const handler = await fetch(//...'); // Fetch or do something else
  const response = await handler.json();
  
  return response;
}
import functionName from './api/functionName'

//...

export default async function getStatticProps() {
  const response = await functionName();

  //...

}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:6
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
awareness481commented, Dec 7, 2020

@vladandrei0 Yes, but only in the case you want to fetch your own api in getXYProps.

So for example, the code below would not work

export async function getStaticProps() {
  const data = await fetch('http://localhost:3000/api/method')
  
  return {
    props: {
      data
   }
  }
}

because you’re fetching your own api in getStaticProps. Instead you would create a function like in the code you quoted.

2reactions
mkamranhamidcommented, Nov 3, 2020

Are you saying that the below code

export async function getStaticProps() {
  // Call an external API endpoint to get posts.
  // You can use any data fetching library
  const res = await fetch('https://.../posts')
  const posts = await res.json()

  // By returning { props: posts }, the Blog component
  // will receive `posts` as a prop at build time
  return {
    props: {
      posts,
    },
  }
}

should be transformed in to

// post.js
export const fetchPosts = async () => {
  // Call an external API endpoint to get posts.
  // You can use any data fetching library
  const res = await fetch('https://.../posts')
  const posts = await res.json()
  return posts
}

// index.js
import { fetchPosts } from 'post.js'
...
...
export async function getStaticProps() {
  const posts = await fetchPosts();
  // By returning { props: posts }, the Blog component
  // will receive `posts` as a prop at build time
  return {
    props: {
      posts,
    },
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Data Fetching: getStaticProps - Next.js
This new directory has support for colocated data fetching at the component level, using the new React use hook and an extended fetch...
Read more >
How do getStaticPaths and getStaticProps work together?
I have read the Next.js docs, and I know how to work with both getStaticProps and getStaticPaths. What I'm missing is how they...
Read more >
How to use getStaticPaths on dynamic routes in Next.js
In other words, you can use getStaticPaths on a page that uses a dynamic route, and whenever you use this function on that...
Read more >
Jamstack Overview - Envato Author Help Center
Next.js has a function called getStaticProps; with getStaticProps ... .org/docs/api-reference/data-fetching/get-static-paths#fallback-false.
Read more >
Next.js SSG(static site generation) - YouTube
In this video I am going to explain you what is SSG in next.js and we are going to talk about 4 thing:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found