NextJS and Algolia 6
Hello,
I'm having troubles with using Algolia 6 and NextJS 9.
I'm doing this on the getInitialProps method
Home.getInitialProps = async ({ asPath }) => {
const queryState =
asPath && asPath.includes('?') ? readURL(asPath) : initialFiltersState;
const searchState = generateSearchState(queryState);
const resultsState = await findResultsState(Search, {
...DEFAULT_PROPS,
searchState
});
return {
searchState,
resultsState
};
};
For example, my searchState can be
{
searchState: {
configure: {
aroundLatLng: true,
page: 1,
filters: 'property:"Flat" OR property:"house" AND localisation:"Annecy"'
}
}
}
I should have no results but I got full hits. When the client takes the lead, I got my 0 results.
DEFAULT_PROPS are my agolia client and indexName
Thanks
Hi @YoannDelpierre you're on the right track but it's hard to tell what's missing in your code if you can't provide it fully. Here is a fully working Next.js example https://codesandbox.io/s/github/algolia/react-instantsearch/blob/master/examples/next/, does this help?
Yes I based my work on your exemple
In fact my code was working well previous Algolia 6. I have the feeling that i'm missing something but not sure...
Do you need more code to help ?
yes, currently from your code snippets I can't know what readUrl(...) does, or what's the value of initialFiltersState. Also I'm not sure I understand what you mean by
I should have no results but I got full hits. When the client takes the lead, I got my 0 results.
Ideally, you'd follow the template we provide for opening issues with a reproducible example.
Describe the bug 🐛
A clear and concise description of what the bug is.
To Reproduce 🔍
Steps to reproduce the behavior:
- Go to '...'
- Click on '....'
- Scroll down to '....'
- See error
A live example helps a lot! We have a simple online template for you to use for your explanations:
https://codesandbox.io/s/github/algolia/create-instantsearch-app/tree/templates/react-instantsearch
Expected behavior 💭
A clear and concise description of what you expected to happen.
Screenshots 🖥
If applicable, add screenshots to help explain your problem.
Environment:
- OS: [e.g. iOS / windows / mac / android]
- Browser [e.g. chrome, safari]
- Version [e.g. 22]
Additional context
Add any other context about the problem here.
Hard to give a reproductible example because it's not open sourced :(
About
I should have no results but I got full hits. When the client takes the lead, I got my 0 results.
I mean that i'm sure that my searchState will return me 0 results from Algolia API. But I got all my result. Which make me thinking that my searchState is not well configured ?
For what it worths, I try the same code as your example
const searchState = pathToSearchState(asPath));
const resultsState = await findResultsState(Search, {
...DEFAULT_PROPS,
searchState
});
return {
searchState,
resultsState
};
DEFAULT_PROPS are my algolia client and indexName
export const DEFAULT_PROPS = {
searchClient,
indexName: 'myIndexName'
};
And it returns all my result, it seems that my searchState is not taken into.
Sorry @YoannDelpierre, I wish I could debug code without reading it 😄.
If something is wrong it's most likely not in the snippets your showed if you copy pasted them from our examples.
You're saying your code worked on React InstantSearch 5, and broke when moving to React InstantSearch 6. What I would do is go again through the upgrade guide v5 to v6 to see I haven't missed anything.
One other way we can help is if you provide a curl of query that's fired, it might give us a hint on what's missing.
I can provide you a codesandbox, is it good for your ?
that would help us help you, absolutely!
Do you have an email to send it, avoid exposing it 👌
please send it to [email protected], and let's continue this discussion over email.
Any news on this thread?
Can you open a new issue @furlanrapha? The example with Next.js in this repository worked for us. It's possible that you forgot to spread the onSearchParameters prop to the InstantSearch component from the root
Forgot to share here but adding onSearchParameters worked for me! Thanks @Haroenv!