Skip to content

Commit

Permalink
fix(ssr) assign attributes correctly (#13)
Browse files Browse the repository at this point in the history
Release 0.1.0-16
  • Loading branch information
darrenjennings authored Feb 15, 2020
1 parent b47d74d commit ae38609
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "swrv",
"version": "0.1.0-11",
"version": "0.1.0-16",
"main": "./dist/index.js",
"module": "./esm/index.js",
"scripts": {
Expand Down
23 changes: 19 additions & 4 deletions src/use-swrv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,14 @@ export default function useSWRV<Data = any, Error = any> (key: IKey, fn: fetcher
let unmounted = false
let isHydrated = false

const vm = getCurrentInstance()
const vm = getCurrentInstance() as any
const IS_SERVER = vm.$isServer
const isSsrHydration = Boolean(!IS_SERVER && (vm as any).$vnode?.elm?.dataset?.swrvKey)
const isSsrHydration = Boolean(
!IS_SERVER &&
vm.$vnode &&
vm.$vnode.elm &&
vm.$vnode.elm.dataset &&
vm.$vnode.elm.dataset.swrvKey)

config = {
...defaultConfig,
Expand Down Expand Up @@ -215,9 +220,19 @@ export default function useSWRV<Data = any, Error = any> (key: IKey, fn: fetcher
})
if (IS_SERVER) {
// make sure srwv exists in ssrContext
const swrvRes = (vm.$ssrContext && vm.$ssrContext.swrv) || []
let swrvRes = []
if (vm.$ssrContext) {
swrvRes = vm.$ssrContext.swrv = vm.$ssrContext.swrv || swrvRes
}

const ssrKey = swrvRes.length
const attrs = (vm.$vnode && vm.$vnode.data && vm.$vnode.data.attrs) || {}
if (!vm.$vnode || (vm.$node && !vm.$node.data)) {
vm.$vnode = {
data: { attrs: { 'data-swrv-key': ssrKey } }
}
}

const attrs = (vm.$vnode.data.attrs = vm.$vnode.data.attrs || {})
attrs['data-swrv-key'] = ssrKey

// Nuxt compatibility
Expand Down

0 comments on commit ae38609

Please sign in to comment.