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

fix(cache-hydration): normalize params returned from response #5946

Merged
merged 5 commits into from
Dec 5, 2023

Conversation

aymeric-giraudet
Copy link
Member

Summary

There are discrepancies between how different versions of the search engine serialize query parameters.
This makes it so this caching solution does not work with all engines.

Result

We can normalize the params returned from the request so that it matches the same format as the one used by the search client.

Copy link

codesandbox-ci bot commented Nov 29, 2023

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 3a3c579:

Sandbox Source
InstantSearch.js Configuration
react-instantsearch-app Configuration
example-react-instantsearch-default-theme Configuration
example-react-instantsearch-next-app-dir-example Configuration
example-vue-instantsearch-default-theme Configuration

@@ -1,4 +1,6 @@
// @ts-nocheck (types to be fixed during actual implementation)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops!

params: request.params,
results[key].rawResults.map((result) => ({
indexName: result.index,
params: serializeQueryParameters(qs.parse(result.params)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it might or might not be right, but in v3 this is how the cache key is computed, also from the parameters, not from the result:

https://github.com/algolia/algoliasearch-client-javascript/blob/5c1ce1308749aa0807940456393aa23ae46fb9eb/src/AlgoliaSearchCore.js#L232-L240

function deserializeQueryParameters(parameters: string) {
return parameters.split('&').reduce<Record<string, any>>((acc, parameter) => {
const [key, value] = parameter.split('=');
acc[key] = value ? decodeURIComponent(value) : '';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't think it's worth deviating from the design, but instead of decode/encode uri component, we could simplify it and leave params uri encoded

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I can't as it's also used in the mocked client.search which has decoded params, or I'd have to put a boolean argument in serializeQueryParameters like shouldEncode. Don't know if it's worth it.

Copy link
Contributor

@Haroenv Haroenv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still would like us to look into making sure we pass the params from server -> client as well to avoid this duplicate work, but that can be done later

(CI failure still to be fixed of course)

@aymeric-giraudet aymeric-giraudet merged commit 042aaac into master Dec 5, 2023
12 checks passed
@aymeric-giraudet aymeric-giraudet deleted the fix/cache-hydration branch December 5, 2023 10:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants