Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Double Search Requests & Insights Middleware Error #6599

Open
1 task done
daryldeogracias opened this issue Mar 4, 2025 · 4 comments
Open
1 task done

Double Search Requests & Insights Middleware Error #6599

daryldeogracias opened this issue Mar 4, 2025 · 4 comments
Labels
triage Issues to be categorized by the team

Comments

@daryldeogracias
Copy link

daryldeogracias commented Mar 4, 2025

🐛 Current behavior

When performing a search request, each keypress triggers two consecutive API calls to /api/search (backend search setup) . Additionally, enabling insights={true} causes an insights error.

🔍 Steps to reproduce

  1. Replace API URL app>fetch>search.tsx[line.5] with the correct forwarded port in Codesandbox.
    • Example: Change http://localhost:3000/api/search to the correct URL (e.g., https://xyz-3000.app.codesandbox.io/api/search).
  2. Perform a search in the application.
    • Expect hydration errors.
  3. Check Network Tab in Developer Tools.
    • Observe that each request results in two consecutive API calls to /api/search.
  4. Enable insights={true} in InstantSearchNext.

Live reproduction

https://codesandbox.io/p/github/daryldeogracias/algolia-web-app/main

💭 Expected behavior

  1. Double query requests should be resolved.
  2. Insights middleware should work even when using backend search.

Package version

"next": "^15.1.6", "react-instantsearch": "^7.15.3", "react-instantsearch-nextjs": "^0.4.4", "algoliasearch": "^5.20.0", "instantsearch.js": "^4.77.3",

Operating system

macOS 15.3.1 (24D70)

Browser

Chrome Version 133.0.6943.142

Code of Conduct

  • I agree to follow this project's Code of Conduct
@daryldeogracias daryldeogracias added the triage Issues to be categorized by the team label Mar 4, 2025
@daryldeogracias
Copy link
Author

Any suggestions for insights? looks like it can't be done using the provider

@mathougui
Copy link

The short version is that you’re adding objectID to the route state in stateToRoute, but then ignoring (and removing) it in routeToState.
Because your state mapping is asymmetric, the router sees “changes” and triggers a second query.

Concretely, in stateToRoute:

if (typeof window !== "undefined") {
objectID =
new URLSearchParams(window.location.search).get("objectID") || undefined;
}

…you add objectID to the route state that’s returned.

But in routeToState:

routeToState(routeState = {} as UiState) {
return Object.keys(routeState).reduce(
(state, indexId) => ({
...state,
[indexId]: cleanState(routeState[indexId] || {}),
}),
{} as UiState
);
}

…you never read that objectID key nor put it back into the returned UiState.
Consequently, your router sees “an extra key” (objectID) vanish, so it re-syncs the route and triggers a new query.

Fix would be either removing that objectID logic or handling it more consistently: make routeToState store that key in uiState the same way it’s set in stateToRoute. That consistency will keep the route state stable and avoid the unwanted query.

I haven't had the time to dive into insights for now but it seems like there was no initialisation of our insights library.

@daryldeogracias
Copy link
Author

daryldeogracias commented Mar 5, 2025

Thanks for responding @mathougui , tried to removed entirely the stateMapping and routing part, its still triggers two consecutive api calls

https://codesandbox.io/p/github/daryldeogracias/algolia-web-app/main

@daryldeogracias
Copy link
Author

this seems to happen only when we do backend search

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage Issues to be categorized by the team
Projects
None yet
Development

No branches or pull requests

2 participants