-
Notifications
You must be signed in to change notification settings - Fork 531
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(hydration): generate cache with search parameters from server-side request #5991
Conversation
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 81c5b6e:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for tackling this! I think it can do with a couple more tests of more advanced features though
@@ -26,7 +38,7 @@ export function InitializePromise() { | |||
|
|||
const injectInitialResults = () => { | |||
let inserted = false; | |||
const results = getInitialResults(search.mainIndex); | |||
const results = getInitialResults(search.mainIndex, requestParamsList); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess I don't really understand this code right now, but how come this can't be read from waitForResults? does it need to be set before the results are known or something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's similar to what's in server.ts for the pages router, just that because we have access to the search instance outside of both waitForResults()
and injectInitialResults()
, it allows me to put the code that extracts the queries params before and directly reference them instead of passing them as a return value of promises.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my question was more about the potential desynchronising of the search client wrapper in the future if we do any changes to that. Not sure if it matters
SearchOptions, | ||
} from '../types'; | ||
|
||
type RequestParams = SearchOptions | undefined; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same question, how can this be undefined?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is good to go, however, in the future we need a good look at FX-2720 in detail (and probably also in the scope of FX-2713, maybe even warn or error for that use case as it may be broken)
Not fully sure if the code duplication around param extraction between React InstantSearch and InstantSearch.js is good long-term but ok I think
Summary
In an SSR context, the search request performed server-side is cached in the client-side search client to prevent sending an unnecessary duplicate request when the user interacts with refinements.
This is done by manually recomposing the cached results during hydration and adding it to the search client's cache mechanism. The key of a cached request uses the search parameters from the response, normally returned by Algolia, but customers can limit what fields are returned by setting the
responseFIelds
option. Ifparams
is omitted, than the app throws an error during hydration.This PR changes slightly how the request key is crafted, by instead extracting the request search parameters from the server-side search request, and using it instead of the response search parameters.
Result
Hydration now works correctly and the server-side request is effectively cached whether fields are filtered using
responseFields
or not.Note
The issue of cache miss in implementations containing multiple indices is still unsolved and can be handled by FX-2720.
Furthermore, if in those multiple indices there are multiples of the same
indexName
+indexId
, the current implementation ofgetInitialResults()
uses only the last instance for params and results, which might not be desirable.Fixes #5964
FX-2702