query: prefetchInfiniteQuery - Next.js SSR - Error serializing `.dehydratedState.queries[0].state.data.pageParams[0]`
SerializableError: Error serializing .dehydratedState.queries[0].state.data.pageParams[0] returned from getServerSideProps in "/[[...sort]]". Reason: undefined cannot be serialized as JSON. Please use null or omit this value.
The pageParams array when using prefetchInfiniteQuery is returning [undefined], which cannot be serialized by Next.js. It should return a single element array of the page param that was used to prefetch the infinite query (i.e. [0]).
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 14
- Comments: 26 (6 by maintainers)
Commits related to this issue
- [feat] shop 피드 api 연결 및 SSR 추가 (#99) ## 💡 이슈 resolve #93 ## 🤩 개요 shop 피드 api 연결 및 SSR 추가입니다. ## 🧑💻 작업 사항 - [x] api 코드 및 hook 작성 - [x] shop피드 query string 넣기 - [x] PullToRefresh api... — committed to SWM-re-pashion/repashion-client by kingyong9169 2 years ago
- Implementation now uses infiniteQuery preparing for load more. Failing because of TanStack/query/issues/1458 — committed to fnocetti/ideal-adventure by deleted user 9 months ago
I’m having this same issue. Using
JSON.parse(JSON.stringify(dehydrate(queryClient)))works but feels a bit hacky.The best workaround for now would be to
JSON.parse(JSON.stringify(dehydrate(queryClient)))I fixed the issue using superjson
It’s a slightly better solution, but the drawback is the need for a new dependency
Are you using
initialData? If so, you probably have to set thepageParamstoo.E.g:
I have faced same problem, fixed like this:
May I know what’s the proper solution? I’m still getting this error
we have a fix for this with v5, you can try out the alpha version already:
https://tanstack.com/query/v5/docs/react/guides/migrating-to-v5#infinite-queries-now-need-a-defaultpageparam
This is still an issue, after following the docs as they are, this error is what happens.
we currently use
undefinedforpageParamreturned fromgetNextPageParamso that we know when to stop refetching. As long asgetNextPageParamreturns something non-undefined, we keep fetching the next page on refetches (e.g. window focus or invalidation). This is documented here.https://github.com/tannerlinsley/react-query/blob/a701340efd5fe5751cd4b317be88430533f4d0d7/src/core/infiniteQueryBehavior.ts#L59-L61
The first page is an exception to the rule (we always fetch the first page). For the first page,
undefinedis used because it allows us to assign a default value in thequeryFn, wherepageParamis injected. Standard example from the docs:In
fetchProjects, the first page fetch will fetch withcursor=0only becausepageParamis undefined.If we change this behaviour to
null, it would be a breaking change, making dx worse for these scenarios. Semantics ofnullandundefinedare also different, but that’s debatable 😅 .What dehydration does is merely make a reduced js object out of the
queryClient. We also use that mechanism for the persisters. It seems to be a nextJs requirement that js objects returned fromgetServerSidePropsandgetStaticPropsdo not contain undefined values. I don’t know the reason behind this, so I still think it is better to open a discussion / issue with Next.Tbh, I was surprised that the
JSON.parse(JSON.stringify())workaround does work, because it actually turns:into
I think this only works coincidentally, because we ignore the pageParam completely when fetching the first page:
https://github.com/tannerlinsley/react-query/blob/a701340efd5fe5751cd4b317be88430533f4d0d7/src/core/infiniteQueryBehavior.ts#L81-L84
Since v4 is around the corner, and the first element in
pageParamsapparently doesn’t matter, we could think about changing it tonullbefore writing it to the cache, after we have passed it to thequeryFn(there, we would still need undefined for the destructuring to work). Also feels a bit hacky. If anyone has other ideas, please shoot 😃can you show this in a codesandbox reproduction please?
Just wanted to report that the issue is still present in Nextjs 12.2.5 + RQ 4.29.1. Hacky workarounds solves the issue.
Bumping this issue as we ran into it again. Definitely want to avoid setting the query manually in our case, so if there’s a way to define a default
pageParamor have it returnnullrather thanundefined, we’d be super appreciative. Thanks!