Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
fix: remove no-setup ssrRefs until solution
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed May 8, 2020
1 parent e3f7221 commit 707fb25
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
11 changes: 9 additions & 2 deletions src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ Vue.use(CompositionApi)
*
* @type {Plugin} plugin
*/
const plugin = function (context) {
if (process.server && context.app.context.ssrContext) {
const plugin = context => {
if (!process.server) return

const { setup } = context.app
context.app.setup = (...args) => {
if (setup instanceof Function) setup(...args)
// Run instantiating functions that must be run within setup()
}
if (context.app.context.ssrContext) {
setSSRContext(context.app.context.ssrContext)
}
}
Expand Down
26 changes: 9 additions & 17 deletions src/server-prefetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,10 @@ import { ComponentInstance } from '@vue/composition-api/dist/component'
const ssrRefFunctions = new WeakMap<ComponentInstance, (() => void)[]>()
const isPending = new WeakMap<ComponentInstance, number>()

let noSetup: Array<() => any> = []

export function onServerPrefetch(cb: () => any) {
const vm = getCurrentInstance()

if (!vm) {
noSetup.push(cb)
return
}
if (!vm) throw new Error('ssrRef must be called within setup()')

const pending = isPending.get(vm) || 0

Expand All @@ -29,8 +24,6 @@ export function onServerPrefetch(cb: () => any) {
if (pending <= 1) {
const fn = ssrRefFunctions.get(vm) || []
await Promise.all(fn.map(p => p()))
await Promise.all(noSetup.map(p => p()))
noSetup = []
} else {
isPending.set(vm, pending - 1)
}
Expand All @@ -42,15 +35,14 @@ export function onFinalServerPrefetch(cb: () => any) {

const vm = getCurrentInstance()

if (!vm) {
noSetup.push(cb)
} else {
const fn = ssrRefFunctions.get(vm)
if (!vm)
throw new Error('onFinalServerPrefetch must be called within setup()')

if (!fn) {
ssrRefFunctions.set(vm, [cb])
} else {
fn.push(cb)
}
const fn = ssrRefFunctions.get(vm)

if (!fn) {
ssrRefFunctions.set(vm, [cb])
} else {
fn.push(cb)
}
}

0 comments on commit 707fb25

Please sign in to comment.