[Docs] Clarify using api functions in getStaticProps/getStaticPaths
See original GitHub issueFeature 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:
- Created 3 years ago
- Reactions:6
- Comments:6 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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
because you’re fetching your own api in
getStaticProps
. Instead you would create a function like in the code you quoted.Are you saying that the below code
should be transformed in to