Skip to content
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

fix: reactive in SSR #546

Merged
merged 3 commits into from
Oct 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/apis/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
export * from './state'
export {
isReactive,
isRef,
isRaw,
markRaw,
reactive,
ref,
customRef,
Ref,
set,
shallowReactive,
shallowRef,
toRaw,
toRef,
toRefs,
triggerRef,
unref,
UnwrapRef,
isReadonly,
shallowReadonly,
proxyRefs,
ShallowUnwrapRef,
} from '../reactivity'
export * from './lifecycle'
export * from './watch'
export * from './computed'
Expand Down
22 changes: 0 additions & 22 deletions src/apis/state.ts

This file was deleted.

11 changes: 8 additions & 3 deletions src/reactivity/reactive.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AnyObject } from '../types/basic'
import { getRegisteredVueOrDefault } from '../runtimeContext'
import { isPlainObject, def, warn } from '../utils'
import { isPlainObject, def, warn, hasOwn } from '../utils'
import { isComponentInstance, defineComponentInstance } from '../utils/helper'
import { RefKey } from '../utils/symbols'
import { isRef, UnwrapRef } from './ref'
Expand Down Expand Up @@ -111,6 +111,11 @@ function observe<T>(obj: T): T {
observed = vm._data.$$state
}

// in SSR, there is no __ob__. Mock for reactivity check
if (!hasOwn(observed, '__ob__')) {
def(observed, '__ob__', {})
}

return observed
}

Expand Down Expand Up @@ -154,7 +159,7 @@ export function shallowReactive(obj: any): any {
configurable: true,
get: function getterHandler() {
const value = getter ? getter.call(obj) : val
ob.dep.depend()
ob.dep?.depend()
return value
},
set: function setterHandler(newVal) {
Expand All @@ -164,7 +169,7 @@ export function shallowReactive(obj: any): any {
} else {
val = newVal
}
ob.dep.notify()
ob.dep?.notify()
},
})
}
Expand Down
8 changes: 6 additions & 2 deletions test/helpers/wait-for-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const Vue = require('vue')
// })
// .then(done)

window.waitForUpdate = (initialCb) => {
const waitForUpdate = (initialCb) => {
let end
const queue = initialCb ? [initialCb] : []

Expand Down Expand Up @@ -69,7 +69,11 @@ window.waitForUpdate = (initialCb) => {
return chainer
}

exports.waitForUpdate = window.waitForUpdate
if (typeof window !== 'undefined') {
window.waitForUpdate = waitForUpdate
}

exports.waitForUpdate = waitForUpdate

function timeout(n) {
return (next) => setTimeout(next, n)
Expand Down
4 changes: 2 additions & 2 deletions test/ssr/serverPrefetch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const {
ref,
onServerPrefetch,
getCurrentInstance,
provide,
inject,
isReactive,
reactive,
} = require('../../src')

function fetch(result) {
Expand Down
47 changes: 47 additions & 0 deletions test/ssr/ssrReactive.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @jest-environment node
*/

import Vue from '../vue'
import { isReactive, reactive, ref, isRaw, isRef, shallowRef } from '../../src'
import { createRenderer } from 'vue-server-renderer'

describe('SSR Reactive', () => {
beforeEach(() => {
process.env.VUE_ENV = 'server'
})

it('should in SSR context', async () => {
expect(typeof window).toBe('undefined')
expect((Vue.observable({}) as any).__ob__).toBeUndefined()
})

it('should render', async () => {
const app = new Vue({
setup() {
return {
count: ref(42),
}
},
render(this: any, h) {
return h('div', this.count)
},
})

const serverRenderer = createRenderer()
const html = await serverRenderer.renderToString(app)
expect(html).toBe('<div data-server-rendered="true">42</div>')
})

it('reactive + isReactive', async () => {
const state = reactive({})
expect(isReactive(state)).toBe(true)
expect(isRaw(state)).toBe(false)
})

it('shallowRef + isRef', async () => {
const state = shallowRef({})
expect(isRef(state)).toBe(true)
expect(isRaw(state)).toBe(false)
})
})