Describe the bug 馃悰
This is also related to: #2958 and #2949
I can't reproduce the Next.js SSR example with getStaticProps or getServerSideProps instead of getInitialProps. It won't work. I only turn the getInitialProps into getServerSideProps:
I have 2 debugs into the getServerSideProps functions which means:
export const getServerSideProps = async ({ req, res, query, ...restProps }) => {
console.log('[SSR] get server-side props: ', req.url, query, restProps);
const searchState = pathToSearchState(restProps.asPath);
const resultsState = await findResultsState(SearchApp, {
searchClient,
indexName: '...',
searchState,
});
...
console.log('[SSR] search page: ', searchState, resultsState);
and it doesn't work and show the following bug:
event - compiled successfully
[SSR] get server-side props: /search?query=fras&page=1&configure%5BhitsPerPage%5D=12 { query: 'fras', page: '1', 'configure[hitsPerPage]': '12' } {}
[SSR] search page: { query: 'fras', page: '1', configure: { hitsPerPage: '12' } } { rawResults: [ { hits: [], nbHits: 0, nbPages: 0, processingtimeMS: 0 } ],
state:
SearchParameters {
facets: [],
disjunctiveFacets: [],
hierarchicalFacets: [],
facetsRefinements: {},
facetsExcludes: {},
disjunctiveFacetsRefinements: {},
numericRefinements: {},
tagRefinements: [],
hierarchicalFacetsRefinements: {},
index: 'customers',
highlightPreTag: '<ais-highlight-0000000000>',
highlightPostTag: '</ais-highlight-0000000000>' } }
Error: Error serializing `.resultsState.state` returned from `getServerSideProps` in "/search".
Reason: `object` ("[object Object]") cannot be serialized as JSON. Please only return JSON serializable data types.
at isSerializable (/home/paneladm/projects/paneladm-dev/medina-app/node_modules/next/dist/lib/is-serializable-props.js:9:7)
at Object.entries.every (/home/paneladm/projects/paneladm-dev/medina-app/node_modules/next/dist/lib/is-serializable-props.js:7:503)
at Array.every (<anonymous>)
at isSerializable (/home/paneladm/projects/paneladm-dev/medina-app/node_modules/next/dist/lib/is-serializable-props.js:7:304)
at Object.entries.every (/home/paneladm/projects/paneladm-dev/medina-app/node_modules/next/dist/lib/is-serializable-props.js:7:503)
at Array.every (<anonymous>)
at isSerializable (/home/paneladm/projects/paneladm-dev/medina-app/node_modules/next/dist/lib/is-serializable-props.js:7:304)
at isSerializableProps (/home/paneladm/projects/paneladm-dev/medina-app/node_modules/next/dist/lib/is-serializable-props.js:9:219)
at renderToHTML (/home/paneladm/projects/paneladm-dev/medina-app/node_modules/next/dist/next-server/server/render.js:37:395)
A live example helps a lot! We have a simple online template for you to use for your explanations:
https://github.com/algolia/react-instantsearch/blob/master/examples/next/pages/index.js
Expected behavior 馃挱
A result without any serialize errors like in the getInitialProps example:

Environment:
Linux debian 4.9.0-13-amd64 #1 SMP Debian 4.9.228-1 (2020-07-05) x86_64 GNU/LinuxGoogle Chrome - Version 84.0.4147.105 (Official Build) (64-bit)10.20.16.14.46.6.016.13.116.13.19.5.0Additional context
Add any other context about the problem here.
This seems related to the SearchResults class. It's serialisable to JSON, but Next.js might not do it by default. Does the situation change if you call JSON.parse(JSON.stringify(resultsState))? If that's the case, we need to use the toJson method ourselves directly
Hey @Haroenv, thanks for reaching out!
Yes, I don't see any errors after doing a parse in this way.
But I continue fetching via browser also. Maybe I'm not following the docs appropriately (but it's another case).
Yes, it's expected that the results are also fetched frontend once the query changes. Do you have a sandbox with unexpected behaviour?
Not unexpected behaviour but I thought the first query might be done on the server-side instead, in case I refresh the browser.
Ah, that's unexpected, the first query on the browser is supposed to already be cached, and that's the case with our demo, if that's not the case for your project, could you make an example with that please?
Maybe is because the mock request I'm doing for empty query. I don't know the cause.
Here's a codesandbox: https://codesandbox.io/s/nextjs-instantsearch-ohphp
And here's the doc I followed: algolia.com - building search UI - mocking-a-search-request
Your application on codesandbox does not seem to include the code from the guide, are you saying it's solved or not? thanks
About the error "yes". For what you just asked about the SSR "no".
I'm going to ask separately. Thanks for helping me. :)
Most helpful comment
This seems related to the SearchResults class. It's serialisable to JSON, but Next.js might not do it by default. Does the situation change if you call
JSON.parse(JSON.stringify(resultsState))? If that's the case, we need to use the toJson method ourselves directly