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

During SSR the url doesn't get read #1062

Closed
ArcoMul opened this issue Sep 20, 2021 · 17 comments
Closed

During SSR the url doesn't get read #1062

ArcoMul opened this issue Sep 20, 2021 · 17 comments

Comments

@ArcoMul
Copy link

ArcoMul commented Sep 20, 2021

Bug 馃悶

What is the current behavior?

During SSR the page gets rendered with an empty state because the state doesn't get read from the url.

Make a sandbox with the current behavior

See SSR example, neither routing.router.read or routing.router.onUpdate gets called.

What is the expected behavior?

That vue-instantsearch tries to retrieve the current state from the URL, using the routing.router methods

Does this happen only in specific situations?

No

What is the proposed solution?

That either routing.router.onUpdate gets called so that the callback can be used to push the state into vue-instantsearch, or that routing.router.read gets called so that either the state is retrieved automatically.

What is the version you are using?

3.3.0 and 4.0.1

@ArcoMul
Copy link
Author

ArcoMul commented Sep 20, 2021

Might be that I'm not aware of another way to push the state into vue-instantsearch, the example suggest though that onUpdate should be called during server-side rendering. If there is any other preferred way I'm happy to hear so :-)

See also the post I created on Discourse: https://discourse.algolia.com/t/how-to-pass-current-url-to-vue-instantsearch-routing-during-ssr/13476

@reinoldus
Copy link

I think it's not the url, my ssr is not working either anymore, this sandbox works:
https://codesandbox.io/s/serene-leftpad-jp7gb

this one doesn't:
https://codesandbox.io/s/crazy-sid-tkwu7?file=/package.json

Only change is that nuxt was upgrade to 2.15.8

not sure what deps it broke

@reinoldus
Copy link

I reused my old yarn.lock and it works again, either some vue package or some instansearch.js package caused this issue. I leave it to the maintainers to figure out which one sh*t the bed

@Haroenv
Copy link
Contributor

Haroenv commented Sep 28, 2021

Those two sandboxes look exactly the same to me, even the nuxt version resolves to 2.15.0 in both, the . Unfortunately I have no idea what's the difference that could be causing the url not to be read. I'm guessing maybe codesandbox doesn't respect the lock file somehow? can you reproduce this working/not working locally too?

@reinoldus
Copy link

That is highly interesting, so what I did was "yarn upgrade" in the other sandbox, not sure why the yarn.lock didn't update, I ran yarn upgrade again and now the lock file has been updated.

And yes I had the exact same issue locally:
I did "yarn upgrade" locally and the "url not read" bug occurred.

I then copied an old yarn.lock over and upgrade packages (nuxt etc.) individually, but not vue-instantsearch and the bug didn't occur, so my assumption is that some of the "@algolia" dependencies caused the issue.

@Haroenv
Copy link
Contributor

Haroenv commented Sep 28, 2021

do you have a place with the actual packages that are different (a new lock file or something?)

@ArcoMul
Copy link
Author

ArcoMul commented Sep 28, 2021

For your information, in this discourse topic we were also digging a bit deeper: https://discourse.algolia.com/t/how-to-pass-current-url-to-vue-instantsearch-routing-during-ssr/13476/7

I came there to the conclusion that things break in between vue-instantsearch 3.0.3 and 3.3.0, but I also had weird behaviour where it wouldn't work any more after reverting back to a version which should work.

@Haroenv
Copy link
Contributor

Haroenv commented Sep 28, 2021

Between those versions InstantSearch.js (a dependency) has changed a lot of versions, I guess that would be the issue, but not sure yet. Could you pinpoint it to a certain version via a lock file?

@reinoldus
Copy link

@Haroenv
Copy link
Contributor

Haroenv commented Sep 28, 2021

In that case you can try the following things and see if that continues to work for you:

@ArcoMul
Copy link
Author

ArcoMul commented Oct 5, 2021

@Haroenv I found that from on instantsearch.js 4.29.1 SSR breaks, 4.29.0 still works

@ArcoMul
Copy link
Author

ArcoMul commented Oct 5, 2021

With 'SSR breaks' I mean that the url doesn't get read, and thus every page on the server is rendered without any state

And I tested that in combination with vue-instantsearch 3.3.0

@Haroenv
Copy link
Contributor

Haroenv commented Oct 5, 2021

hmm, seems like it's the same issue as showed up in #1066, sorry that I haven't yet found out exactly why https://github.com/algolia/instantsearch.js/pull/4849/files causes the URL to be read / written as empty (read too early and saved?)

@ArcoMul
Copy link
Author

ArcoMul commented Oct 5, 2021

Here is a sandbox demonstrating the issue: https://codesandbox.io/s/cranky-mountain-2p5pg?file=/package.json
When navigating to the Search page, selecting an refinement option, then refreshing the page keeps the refinement selected because SSR renders the list correctly.

After increasing the version of instantsearch.js in the "resolutions" section of package.json:21 to version 4.29.1 the option of the refinementlist doesn't stay selected because SSR renders the page with an empty state (and thus no option selected)

@ArcoMul
Copy link
Author

ArcoMul commented Oct 5, 2021

@Haroenv Great that the issue has been found, I'll stick to 4.29.0 of instantsearch.js for now

Haroenv added a commit to algolia/instantsearch that referenced this issue Jan 27, 2022
fixes algolia/vue-instantsearch#1066
fixes algolia/vue-instantsearch#1062

see: #4849

In Vue InstantSearch SSR, the mainIndex gets init'ed *before* it does in regular InstantSearch (see https://codesandbox.io/s/beautiful-mestorf-ijc75?file), and at that point the ui state should already be initialized (before start).
@Haroenv
Copy link
Contributor

Haroenv commented Jan 31, 2022

We have just finished a comprehensive refresh of the server-side interaction with routing in the case of Vue InstantSearch. This managed to close this issue for both v3 in 3.9.0 and v4 in 4.3.2.

The pull requests in question are:

If the component at the root is the one doing findResultsState, it doesn't have access to $vnode, and in that branch the component name is required to be passed via the first argument.

This is a little more involved, but fixes the issue where routing is not taken in account. Essentially in InstantSearch it was changed so that routing only gets read on .start, but Vue InstantSearch was faking start.

Luckily, in a recent version of InstantSearch, some functionality of server rendering / initial results has moved inside InstantSearch itself, and it avoids the issue, as we can use regular 'start' from then on.

@Haroenv Haroenv closed this as completed Jan 31, 2022
@ArcoMul
Copy link
Author

ArcoMul commented Feb 9, 2022

@Haroenv Finally got around testing this, the latest version fixes the issue! 馃帀

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