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
beforeNuxtRender is called too soon #8620
Comments
This is the order of logs:
|
I'm having the same issue. I put |
@MartinMalinda did you figure out some kind of workaround? i'm trying to set up Apollo Client SSR this way |
Am I not understanding this hook properly? I want it to execute and fill the state object after all of the Vue components have rendered, initialized, executed all web requests etc. Is there supposed to be another hook for that? |
@fabis94 my workaround is here: vuejs/pinia#332 (comment) Basically I created a wrap function that does |
@MartinMalinda Can you please elaborate on what |
yes, this is composition API. If you're using Nuxt So |
@MartinMalinda I debugged through my build and it looks like beforeNuxtRender gets called before Edit: I think I get it, instead of relying on beforeNuxtRender you build the state object incrementally w/ each Vue component that calls your hook. Not ideal, but at least it works until this gets fixed. |
Regarding I'm not sure that this is an issue with Moreover, you can still update the state within |
I see. In that case I was mislead by the naming, I thought it's a hook to do something "last minute before render" which on server would mean "just before sending the HTML response"
If the method is called so soon like this, even before all components propagate, it is then not so useful to set state into So perhaps someone writing these docs got confused as well? |
In that case we definitely need some sort of hook that gets called right after all rendering is done and is available through Nuxt.js plugins. 'vue-renderer:ssr:context' is a hook like that, but it's only available through Nuxt.js Modules and thus doesn't have access to any request objects that you could serialize into JS for client-side hydration (e.g. request's Apollo Client instance). What I ended up doing was putting Apollo Client inside |
Versions
Reproduction
https://github.com/MartinMalinda/before-nuxt-render
Steps to reproduce
npm install in the repo and open the dev server, open
layouts/default.vue
.What is Expected?
When I change pinia state on the server, it is passed on
beforeNuxtRender
tonuxtState
and then the state should be picked on the client.There is an initialization of pinia store:
And a slight change to the state on the server side. Where
initial name
is changed tochanged name
after 400ms. Because pinia should transfer state from the server to the client these things should happen:changed name
✅changed name
❌What is actually happening?
changed name
turns intoinitial name
on the client.The issue: beforeNuxtRender (probably)
To pass the state from server to client, pinia uses
nuxtState
together withbeforeNuxtRender
:https://github.com/posva/pinia/blob/v1/nuxt/plugin.js#L17
When I put a bunch of console logs around, it seems like the
context.beforeNuxtRender
gets called almost immediately and not in fact, just before render.Not only it does not wait the promise from
useServerPrefetch
to resolve, in this case it's called just right away so it's called even before the pinia stores initialize so not even default store state is passed.I think something had to change on the Nuxt side, because I remember this working for me when I checked it some months ago, probably on Nuxt < 2.12.
The text was updated successfully, but these errors were encountered: