Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

JSON parse error when reading the cache #2828

Closed
andreyvital opened this issue Sep 15, 2019 · 4 comments 路 Fixed by #2862
Closed

JSON parse error when reading the cache #2828

andreyvital opened this issue Sep 15, 2019 · 4 comments 路 Fixed by #2862

Comments

@andreyvital
Copy link

Describe the bug 馃悰

SyntaxError: Unexpected token o in JSON at position

Sorry I am not able to create an example right now, but something fishy is going on: I am using Next.js to SSR Algolia with results, and:

  1. I enable one filter in my app then it updates the URL;
  2. Right after that the result is served and a cache is stored for that state;
  3. Then when I "remove" that filter (resetting to the initial value) it crashes with the error above.

I was stepping through it in the debugger and apparently the 1st cache entry is correct: JSON serialized response. When I change the filter to the initial value, the object that gets stored there is an object (not a JSON string) then it crashes when JSON.parse is executed.

I'll try to provide an example later on but do you have any thoughts? I am currently stuck with this issue and it is limiting me to move forward. 馃槩

@Haroenv
Copy link
Contributor

Haroenv commented Sep 16, 2019

Hi @andreyvital, I'm not sure which cache you are talking about, can you clarify with an example? We have a demo setup with next here: https://codesandbox.io/s/github/algolia/react-instantsearch/tree/master/examples/next

@andreyvital
Copy link
Author

andreyvital commented Sep 16, 2019

Yeah sure, try this one: https://codesandbox.io/s/example-next-cifw3. Just click on the refinement list on the side.

tkrugg added a commit that referenced this issue Oct 23, 2019
It appears the search client cache uses string for values `{ [key: string]: string }`.
When hydrating it for preventing a new request on the client, we need to
serialize the value into a JSON string.

fixes: #2828
tkrugg added a commit that referenced this issue Oct 23, 2019
It appears the search client cache uses string for values `{ [key: string]: string }`.
When hydrating it for preventing a new request on the client, we need to
serialize the value into a JSON string.

fixes: #2828
tkrugg added a commit that referenced this issue Oct 24, 2019
It appears the search client cache uses string for values `{ [key: string]: string }`.
When hydrating it for preventing a new request on the client, we need to
serialize the value into a JSON string.

fixes: #2828
tkrugg added a commit that referenced this issue Oct 25, 2019
# [6.0.0-beta.2](v6.0.0-beta.1...v6.0.0-beta.2) (2019-10-25)

### Bug Fixes

* serialize cache value on hydrate ([#2862](#2862)) ([3319665](3319665)), closes [#2828](#2828)
@tkrugg
Copy link
Contributor

tkrugg commented Oct 25, 2019

Hi @andreyvital

I just released React InstantSearch 6.0.0-beta.2 and updated your example to prevent the second query.

https://codesandbox.io/s/example-next-93sq3

There 2 things I did:

  1. update react-instantsearch-dom to 6.0.0-beta.2
  2. add onSearchParameters prop (see my screenshot of your modified components/app.js file). We'll make sure to make all this clear in the docs.

image

I hope this helps!

@Haroenv
Copy link
Contributor

Haroenv commented Oct 25, 2019

The docs says to spread the props (which can be done here too)

Haroenv pushed a commit to algolia/instantsearch that referenced this issue Jan 4, 2023
It appears the search client cache uses string for values `{ [key: string]: string }`.
When hydrating it for preventing a new request on the client, we need to
serialize the value into a JSON string.

fixes: algolia/react-instantsearch#2828
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants