Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
NextJS - Get dynamic variable in getStaticProps

So I have getStaticProps and need to do some data fetching based on a variable here is an example

export async function getStaticProps() {
  const res = await fetch(localWordpressUrl, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      query: `
        query AllPostsQuery {
          posts(where: {categoryName: "blog"}) {
            nodes {
              slug
              content
              title
            }
          }
        }
      `,
    })
  });

  const json = await res.json();
  return {
    props: {
      posts: json.data.posts,
    }
  };
}

Where categoryName: "blog" needs to be a variable instead of hard coded. I know you can get the slug, but what I need is before the slug. i.e. site.com/blog/slug. Any suggestions on this?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You're actually really close. What you need is to grab the context out of your getStaticProps function. Add this to your getStaticProps function, and look at the console log to see what's happening.

export async function getStaticProps(context) {
  console.log(JSON.stringify(context, null, 3));

  const { asPath, req, res, pathname, query } = context;
  if (req) {
    let localWordpressURL = req.headers.host;
    console.log(localWordpressURL);
  }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!