Skip to content

Commit

Permalink
fix: set ssrContext through plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed May 4, 2020
1 parent bce426b commit 3ba85f5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const compositionApiModule: Module<any> = function () {
this.options.build.babel.plugins = this.options.build.babel.plugins || []
this.options.build.babel.plugins.push(join(__dirname, 'babel'))

this.options.build.transpile = this.options.build.transpile || []
this.options.build.transpile.push(/nuxt-composition-api/)

this.options.plugins = this.options.plugins || []
this.options.plugins.push(resolve(this.options.buildDir || '', dst))
}
Expand All @@ -24,7 +27,7 @@ export const meta = require('../package.json')

export { useFetch } from './fetch'
export { withContext } from './context'
export { ssrRef, onServerPrefetch } from './ssr-ref'
export { ssrRef, onServerPrefetch, setSSRContext } from './ssr-ref'

export {
ComponentRenderProxy,
Expand Down
18 changes: 18 additions & 0 deletions src/plugin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
/**
* @typedef {import('@nuxt/types').Plugin} Plugin
*/

import Vue from 'vue'
import CompositionApi from '@vue/composition-api'

import { setSSRContext } from 'nuxt-composition-api'

Vue.use(CompositionApi)

/**
*
* @type {Plugin} plugin
*/
const plugin = function (context) {
if (process.server && context.app.context.ssrContext) {
setSSRContext(context.app.context.ssrContext)
}
}

export default plugin
19 changes: 9 additions & 10 deletions src/ssr-ref.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import {
ref,
onServerPrefetch as prefetch,
getCurrentInstance,
Ref,
} from '@vue/composition-api'
import { ref, Ref, onServerPrefetch as prefetch } from '@vue/composition-api'

function getValue(value: any) {
if (typeof value === 'function') return value()
return value
}

let ssrContext: any
let injected = false

const refs: [string, Ref<any>][] = []

export function setSSRContext(context: any) {
ssrContext = ssrContext || context
}

export function injectRefs() {
if (!process.server) return
if (!process.server || !ssrContext) return

if (!ssrContext.nuxt.ssrRefs) ssrContext.nuxt.ssrRefs = {}

Expand All @@ -26,16 +26,15 @@ export function injectRefs() {

export const ssrRef = <T>(value: T, key?: string) => {
const val = ref<T>(getValue(value))
const vm = getCurrentInstance()!

if (!key)
throw new Error(
"You must provide a key. You can have it generated automatically by adding 'nuxt-composition-api/babel' to your Babel plugins."
)

if (!ssrContext) {
ssrContext = ssrContext || vm.$ssrContext
if (!injected) {
prefetch(injectRefs)
injected = true
}

if (process.client) {
Expand Down

0 comments on commit 3ba85f5

Please sign in to comment.