-
Notifications
You must be signed in to change notification settings - Fork 100
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
SSR refs breaks the hydration in concurrent requests #310
SSR refs breaks the hydration in concurrent requests #310
Conversation
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/nuxt-community/composition-api/dbkbzcrc1 |
This looks good to me - will review shortly. |
@andrzejewsky Apologies for making some bigger changes without coming back to you first. Primarily this is just to get this merged sooner; I would welcome a future PR if you think there's something missing or this can be improved 👍 |
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 this PR - great work ❤️
thanks @danielroe ! actually, we even have confirmed that PR bu using this as a fix for some of our projects - works fine with higher traffic as well |
Hello Nuxt team,
Recently, I've got quite an interesting issue with server-side rendering and composition API. It works perfectly unless you have higher traffic on your website.
Seems like multiple requests at the same time affects the
ssrRefs
that are passed to the__NUXT__
object. The reason why requests are mixed up is this line: https://github.com/nuxt-community/composition-api/blob/main/src/ssr-ref.ts#L17 - you always refer to the global object that is shared across incoming requests. The Nuxt handle this in the following way:globalPlugin
and reset global data objectssrRefs
and modify thedata
(data points in this case to ssrRefs in__NUXT__
)Now, when we have multiple calls, it may look like this:
globalPlugin
(reseting data)globalPlugin
(reseting data) <- we just wiped up data from the previous requestssrRefs
, broken hydration)Steps to reproduce:
yarn
&yarn build
& run fixtures in prod mode:yarn fixtures:prod
/ssr-ref
siege -d 1 -c 10 -H "Cache-Control: no-cache" http://localhost:3000
prefetched-result
is empty (prefetched-
)Solution:
Always use context, to pass and modify the data that are related to the given request. Using context requires vue instance so you can't use your composition API out of the components eg. above component or in plugins.
The fix I created is using context, but still, allows you to use a global object as well - so you can use composition API outside of the
setup
, but we should keep in mind that can cause some issues (maybe worth to mention in docs?)