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.

unexpected params in getStaticProps

See original GitHub issue

Bug report

Describe the bug

I’m getting unexpected or undocumented results for getStaticProps with a dynamic route parameter. The params slug is an array of path parts (looks like path.split('/')). It occurs for me when the page is in a dynamic folder and named index.js and not with a spread filename ie [...slug].js. Here’s an example:

Problem case:

// at pages/abc/[slug]/index.js
export async function getStaticProps({ params }) {
  console.log(params); // --- @build: { slug: 'a' } @runtime: { slug: ['abc', 'a'] }
}

What I expected:

// at pages/abc/[slug]/index.js
export async function getStaticProps({ params }) {
  console.log(params); // { slug: 'some-slug' }
}

I get these errors at runtime after deploying to now serverless. Builds are successful and the page appears to work but found the error in the now function logs.

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
Aleksioncommented, Jun 16, 2020

For anyone else stumbling on this, I wrote this ugly fix that seems to catch the edge cases I’ve hit so far:

const cleanNextParams = (queryParams: string[]) => {
  let params = [...queryParams]

  // Sort out next params
  if (params.includes("_next")) {
    const indexOfNext = params.findIndex((e) => e === "_next")
    params.splice(indexOfNext, 3)
  }

  // remove .json from params
  params = params.map((p) => p.replace(".json", ""))

  return params
}

export const getStaticParam = (queryParam: string | string[], path: string, param: string) => {
  if (typeof queryParam === "string") {
    return queryParam
  }

  const indexOfParam = path.split("/").findIndex((el) => el === param)

  if (indexOfParam < 0) {
    return null
  }

  const cleanedParams = cleanNextParams(queryParam)

  return cleanedParams[indexOfParam]
}

// pages/blog/[article].ts
export const getStaticProps = ({params}) => {
   const path = "/blog/[article]"
   const articleId = getStaticParam(params.article, path, "[article]")
}

2reactions
timneutkenscommented, Jun 23, 2020
Read more comments on GitHub >

github_iconTop Results From Across the Web

NextJS - getStaticPaths - Paths seems not bound to one of the ...
NextJS - getStaticPaths - Paths seems not bound to one of the params · You can explicitly trigger a 404 page in your...
Read more >
Next.js query params inside getStaticProps (Incremental Static ...
Have you ever wondered how to access query parameters "context.req.query" inside getStaticProps ???In this video we will look at a possible ...
Read more >
Why can't I get query params in ```getStaticProps```?-Reactjs
getInitialProps and getServerSideProps can know all the possible query params because the page is generated at runtime, whenever you receive a new request....
Read more >
Efficient SSG in Next.js with WPGraphQL
In this article, I will discuss best practices around Static Site Generation in Next.js with dynamic routes and static paths.
Read more >
getStaticProps slug param returning favicon.ico : r/nextjs - Reddit
But there is an unexpected/strange behavior going on. Let's say I enter in /user/page/example. When I console.log(params.slug), it prints " ...
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