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.

context.req is missing in getStaticProps

See original GitHub issue

Feature request

Is your feature request related to a problem? Please describe.

For my website I’ve built up some APIs via /pages/api for the various data fetching needs that my application has. Sometimes I want to hit those endpoints via the client, and other times via the server, and often at build time using getStaticProps. Unfortunately, at the moment I cannot call those API methods from the getStaticProps method.

My initial instinct was to try:

export const getStaticProps = async () => {
    const data = await fetch('/api/mydata');
}

But that gives me TypeError: Only absolute URLs are supported. It makes sense because you must provide a hostname to fetch() when it’s run at build time. How do I give it the hostname of whatever the current server is (localhost during development, but my production server on production)… Not sure.

Describe the solution you’d like

Ideally I could use the code above to fetch data from my API inside getStaticProps. Or alternatively, if context.req was exposed in getStaticProps then I could do something like this to figure out my current hostname:

// This doesn't work.
export const getStaticProps = async (context) => {
    const data = await fetch(context.req.headers.host + '/api/mydata');
}

However the above code doesn’t work because context.req doesn’t exist when using getStaticProps.

Describe alternatives you’ve considered

For now, I’ve had to go with something like this:

export const getStaticProps = async (context) => {
    const URL = process.env.NODE_ENV === 'production' ? 'https://mydeployedsite.com' : 'http://localhost:3000';
    const data = await fetch(URL + '/api/mydata');
}

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
0x6a68commented, Oct 13, 2020

@Timer IMO this documentation leads to the same confusion; https://swr.vercel.app/docs/with-nextjs#pre-rendering

1reaction
Timercommented, Aug 24, 2020

You can refactor your API endpoint into a getData method and just the req/res wrapper!

Read more comments on GitHub >

github_iconTop Results From Across the Web

getStaticProps not working well with TypeScript #16522 - GitHub
Property 'slug' is missing in type 'ParsedUrlQuery' but required in type '{ slug: string ... export function getStaticProps(context: GetStaticPropsContext): ...
Read more >
Data Fetching: getStaticProps - Next.js
Context parameter​​ params contains the route parameters for pages using dynamic routes. For example, if the page name is [id]. js , then...
Read more >
How to make Next.js getStaticProps work with typescript
Your original example is almost correct but the getStaticProps function expression is missing async . Try this:
Read more >
Using getStaticProps and getStaticPaths with TypeScript
This is the line that causes the error as context. params has the type ParsedUrlQuery | undefined and slug does not exist in...
Read more >
How to use getStaticPaths on dynamic routes in Next.js
You will learn how to make use of Next.js getStaticPaths & getStaticProps on a dynamic route to create a Master-Detail UI commonly used...
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