swr icon indicating copy to clipboard operation
swr copied to clipboard

TypeError: undefined is not an object (evaluating 'n.fallback[S]')

Open andreisocaciu opened this issue 3 years ago • 3 comments

Bug report

Description / Observed Behavior

I'm using useSWR with fallback data from getStaticProps and I'm getting a lot of errors like this.

Expected Behavior

Not getting any errors when useSWR wants to access fallback data.

Repro Steps / Code Example

// pages/index.tsx
export const getStaticProps: GetStaticProps<
  { fallback: Record<string, any> },
  { business: string }
> = async (context) => {
  // fetch data here
  return {
    props: {
      fallback: {
        [unstable_serialize(['/book/business', context.params.business])]: business,
      }
    }
  }
}

export default function IndexPage({ fallback }: InferGetStaticPropsType<typeof getStaticProps>) {
  return (
    <SWRConfig value={{ fallback }}>
      <ComponentThatReadFallbackData />
    </SWRConfig>
  );
}

Additional Context

SWR version: 2.1.5

Self-hosted Next.JS project.

Client browsers: Instagram 285.0.0, Mobile Safari 16.5

Stack trace:

// my code does this:
const { data, error } = useSWR(query.business ? ['/book/business', query.business as string] : null, ([url, link]) =>
  fetchBusiness(url, link)
);

// useSWR code breaks here
// ./node_modules/swr/core/dist/index.mjs in useSWRHandler at line 30:33
const fallback = isUndefined(fallbackData) ? config.fallback[key] : fallbackData;

Later update: The code snippet was not the correct one. I'm returning the props key in my project.

andreisocaciu avatar Jun 19 '23 08:06 andreisocaciu

It seems like you write a wrong getStaticProps function which does not pass fallback to the props of page component

https://nextjs.org/docs/pages/api-reference/functions/get-static-props#getstaticprops-return-values

promer94 avatar Jun 23 '23 16:06 promer94

What do you mean? My code is exactly like the one from the docs for complex keys.

Console logging the fallback prop received from the getStaticProps function shows this:

{
  "@\"/book/business\",\"BusinessName\",": { 
    "dataFromServer": "here" 
  }
}

And the website works most of the time, with my useSWR hook having the fallback data available to them, and the SSR delivering the correct page with the server data already loaded. So it can't be that I'm using this feature wrong.

andreisocaciu avatar Jun 23 '23 17:06 andreisocaciu

This is your example.

export const getStaticProps: GetStaticProps<
  { fallback: Record<string, any> },
  { business: string }
> = async (context) => {
  // no props
  return {
    fallback: {
        [unstable_serialize(['/book/business', context.params.business])]: business,
    }
  }
}

But according to nextjs docs, you should write

export const getStaticProps: GetStaticProps<
  { fallback: Record<string, any> },
  { business: string }
> = async (context) => {
  // correct
  return {
    props: {
      fallback: {
        [unstable_serialize(['/book/business', context.params.business])]: business,
    }
  }
}

If you could provider a valid reproduction for this problem. It would be really helpful.

promer94 avatar Jun 23 '23 17:06 promer94