This repository has been archived by the owner on Dec 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
feat: change ssrRef implementation #26
Labels
enhancement
New feature or request
Comments
mathe42
changed the title
feat: change ssrImplementation
feat: change ssrRef implementation
May 5, 2020
Was thinking about useing customRef but this does not exists in @vue/composition-api only in @vue/reactivity. import {
onServerPrefetch,
getCurrentInstance,
customRef
} from 'nuxt-composition-api'
export function ssrRef<T>(value: T, key: string) {
if (process.client) {
const serverValue = (window as any).__NUXT__?.ssrRefs?.[key]
if (serverValue !== undefined) {
value = serverValue
}
}
return customRef<T>((track: () => void, trigger: () => void) => {
return {
get() {
track()
return value
},
set(newValue: T) {
value = newValue
trigger()
if (process.server) {
onServerPrefetch(() => {
const vm = getCurrentInstance()!
if (!vm!.$ssrContext.nuxtState.ssrRefs) {
vm!.$ssrContext.nuxtState.ssrRefs = {}
}
vm!.$ssrContext.nuxtState.ssrRefs[key] = value
})
}
}
}
})
} |
@mathe42 Now I'm really curious 😛 |
Very interesting idea. 👍 |
Thanks :D |
Might be possible with computed with a custom setter. Not sure of the performance implications. 🤔 Is there a shortcoming of the current approach you're aware of? |
In Vue3 I want to use shallowRef on server side and ref on client side to reduce Proxy creation on the server. Maybe some sort of abstract function to interact with Like: type func = (rootKey: string) => {
client<T>: (key: string, initValue: T) => T, //get SSR data if exists
server: (key: string, value: any) => void //set SSR data
} |
see #28 |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
🆒 SSR-Ref
Sorry pressed create to soon
The text was updated successfully, but these errors were encountered: