Next.js Discord

Discord Forum

Param is 'undefined' in getStaticProps when navigating to subdirectory page of current page

Unanswered
Southern African anchovy posted this in #help-forum
Open in Discord
Southern African anchovyOP
Hey there,

I have a directory of pages structured as follows:
- destinations
- [region]
- [slug]
* index.tsx
* index.tsx

So when I go to www.mysite.com/destinations/OH it goes to the static page defined at destinations/[region]/index.tsx , which we will call the RegionPage.

When I got to www.mysite.com/destinations/OH/cleveland it goes to the static page defined at destinations/[region]/[slug]/index.tsx. We call this second page DestinationPage.

Destination is configured as follows:
export async function getStaticProps(props: { params: { region: string; slug: string } }): Promise<any> {
  try {
    const { params } = props;
    const { region, slug } = params;
    const regionParam = region;

    const regionUid = regionParam.toLowerCase();
    console.log(`Lookup Destination Page ${regionUid}/${slug}`);
    const pageProps = await LandingPageService.getDestinationPageProps(regionUid, slug);

    return packageStaticProps(pageProps);
  } catch (e) {
    console.log(`Destination Page ${props?.params?.region}/${props?.params?.slug} error`);
    console.log(e);
    return {
      notFound: true,
    };
  }
}

export async function getStaticPaths() {
  return { paths: [], fallback: 'blocking' };
}


On RegionPage, we have a bunch of links to the Destination page. So when a user clicks on a link, we move from www.mysite.com/destinations/OH to www.mysite.com/destinations/OH/cleveland.

Seems simple enough, but a funny thing happens when we navigate. In the console, I can see that getStaticProps looks like it's getting called twice:

Lookup Destination Page oh/cleveland
Get destination page props for oh/cleveland
Lookup Destination Page oh/undefined


Is there some side effect I'm not considering with this page structure? Is it possible there is something odd going on with Next JS's preloading logic?

6 Replies

Southern African anchovyOP
The only inkling I have of the cause is that there is some interaction with useRouter's push that I'm not considering. When the user clicks on one of the Destination names, I have an onClick defined as:
() => {
  router.push(item.url);
}}


When I look at my page loading next to the console, I see the following happen:
1. Console prints out Lookup Destination Page oh/cleveland and Get destination page props for oh/cleveland from the getStaticProps
2. The page loads and I see all of my info appear
3. Small delay, and then Lookup Destination Page oh/undefined

It's almost like DestinationPage is trying to load a second time using the url of the RegionPage. There doesn't seem to be a user-facing impact, but it causes errors and potential side effects under the hood.
Just confirmed that is happens when I go directly to the page as well
Southern African anchovyOP
UPDATE: I believe I have found the issue but now I'm not sure if the result is a bug or what Next JS intends. I'm guessing the latter

There is a div farther down the tree of DestintionPage with a style defined like so:
        <div
          data-testid="ImageHeaderLayoutContainer"
          className={styles.container}
          style={{
            background: `-webkit-linear-gradient(left, ${colors.secondaryTransparent} 0%, ${colors.primaryTransparent} 100%), url(${imageUrl}) no-repeat top center`,
            backgroundSize: 'cover',
          }}
        >


When imageUrl is undefined, it appears Next is trying to serve that from www.mysite.com/destinations/OH

What I don't get is why, from www.mysite.com/destinations/OH/cleveland, it takes that undefined URL and attempts to serve it from the parent directory. I would have thought it would try to serve something like www.mysite.com/destinations/OH/cleveland/undefined.

Does anybody know what's going on here?
this is pages directory not app directory
Southern African anchovyOP
@aardani What do you mean by thay? I define the paths in the pages directory and my components ar ein the app directory
@Southern African anchovy <@194128415954173952> What do you mean by thay? I define the paths in the pages directory and my components ar ein the app directory
The file structure you used is of pages routing sysetm but you used the App Directory tag in the forum so I was confused