Skip to content

Commit

Permalink
fix: allow setting ssrRef within onGlobalSetup (#523)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Jul 27, 2021
1 parent e2aac3e commit 320c409
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
25 changes: 9 additions & 16 deletions src/runtime/composables/ssr-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,20 @@ export function setSSRContext(app: any) {
}

const useServerData = () => {
let type: 'globalRefs' | 'ssrRefs' = 'globalRefs'

const vm = getCurrentInstance()
const type: 'globalRefs' | 'ssrRefs' = vm ? 'ssrRefs' : 'globalRefs'

if (vm) {
type = 'ssrRefs'
if (process.server) {
const { ssrContext } = (vm[globalNuxt] || vm.$options).context
;(ssrContext as any).nuxt.ssrRefs = (ssrContext as any).nuxt.ssrRefs || {}
}
let ssrRefs: Record<string, any>

if (vm && process.server) {
const ssrContext = (vm![globalNuxt] || vm!.$options).context.ssrContext!
ssrRefs = (ssrContext.nuxt as any).ssrRefs =
(ssrContext.nuxt as any).ssrRefs || {}
}

const setData = (key: string, val: any) => {
switch (type) {
case 'globalRefs':
globalRefs[key] = sanitise(val)
break
case 'ssrRefs':
;(vm![globalNuxt].context.ssrContext as any).nuxt.ssrRefs[key] =
sanitise(val)
}
const refs = ssrRefs || globalRefs
refs[key] = sanitise(val)
}

return { type, setData }
Expand Down
3 changes: 2 additions & 1 deletion test/fixture/plugins/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export default () => {

onGlobalSetup(() => {
const { $config } = useContext()
ssrRef('test')
const a = ssrRef('test')
a.value = 'another'

const { title } = useMeta()
title.value = 'My fixture'
Expand Down

0 comments on commit 320c409

Please sign in to comment.