Should computed track nested refs?
#14462
Unanswered
illusionaries
asked this question in
Help/Questions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
related: #7873, #6994
I found nesting a
refinside acomputedpretty helpful when building my blog:computedthat takes the current route and finds the corresponding pagepage: Promise<Page>, then usedefineAsyncComponentto load it.The
Pagetype is something like:refwhich is initiallynull, and will be updated withpage.then(x => outline.value = x.__outline).As mentioned in #7873 and #6994, this is not the intended usage of the api, but i found it quit useful, and is working as intended on the client side. A minimal reproduction is at https://stackblitz.com/github/illusionaries/ssr-computed, I will also paste the core code here:
But I noticed some inconsistencies between client build and SSR build, even between different versions of vue.
On the client side, regardless of the version, the computed is executed only once, and num is updated from 0 to 1 after 1 second, which is expected.
On the SSR side, with vue latest (3.5.28), the computed got executed twice, and the rendered result is
<span data-v-8338d79c>0</span>. But with vue 3.6 beta (actually, just override"@vue/runtime-core": "beta"is enough), the computed got executed only once, and the rendered result is<span data-v-8338d79c>1</span>.I haven't dug into the source code yet, but I suppose it has something to do with the
computedtracking the internally createdref(at least, the VSCode extension highlights the internalrefas a dependency). On the server side, changing the internalrefvalue caused the computed to evaluate, replacing thefoo.numwith 0 again. But somehow on the client side, therefchange does not trigger an re-evaluation.Is the inconsistency expected or is it actually a bug? If it's expected, is there any recommended way to achieve the same effect without nesting a
refinside acomputed?I have tried to use
defineExposein the page component to expose the outline, but template ref does not work with SSR as far as I know, because the component instance is not created on the server side anduseTemplateRefalways returns null.Beta Was this translation helpful? Give feedback.
All reactions