-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- add `useAsync` helper to prefetch data and send in ssrContext to client - updates `serverPrefetch` helper - improves `ssrRef` handling Co-authored-by: Sebastian Krüger <2pi_r2@gmx.de> Co-authored-by: Daniel Roe <daniel@roe.dev>
- Loading branch information
Showing
12 changed files
with
161 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { ssrRef } from './ssr-ref' | ||
import { onServerPrefetch } from './server-prefetch' | ||
import { Ref, isRef } from '@vue/composition-api' | ||
|
||
export const useAsync = <T>( | ||
cb: () => T | Promise<T>, | ||
key?: string | Ref<null> | ||
): Ref<null | T> => { | ||
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." | ||
) | ||
} | ||
|
||
const _ref = isRef(key) ? key : ssrRef<T | null>(null, key) | ||
|
||
if (!_ref.value) { | ||
const p = cb() | ||
|
||
if (p instanceof Promise) { | ||
if (process.server) { | ||
onServerPrefetch(async () => { | ||
_ref.value = await p | ||
}) | ||
} else { | ||
// eslint-disable-next-line | ||
p.then(res => (_ref.value = res)) | ||
} | ||
} else { | ||
_ref.value = p | ||
} | ||
} | ||
|
||
return _ref as Ref<null | T> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { | ||
onServerPrefetch as onPrefetch, | ||
getCurrentInstance, | ||
} from '@vue/composition-api' | ||
|
||
const ssrRefFunctions = new Map< | ||
ReturnType<typeof getCurrentInstance>, | ||
(() => void)[] | ||
>() | ||
|
||
const isPending = new Map<ReturnType<typeof getCurrentInstance>, number>() | ||
|
||
let noSetup: Array<() => any> = [] | ||
|
||
export function onServerPrefetch(cb: () => any) { | ||
const vm = getCurrentInstance() | ||
|
||
if (!vm) { | ||
noSetup.push(cb) | ||
return | ||
} | ||
|
||
const pending = isPending.get(vm) || 0 | ||
|
||
isPending.set(vm, pending + 1) | ||
|
||
onPrefetch(async () => { | ||
await cb() | ||
|
||
const pending = isPending.get(vm) || 0 | ||
|
||
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) | ||
} | ||
}) | ||
} | ||
|
||
export function onServerPrefetchEnd(cb: () => any) { | ||
if (!process.server) return | ||
|
||
const vm = getCurrentInstance() | ||
|
||
if (!vm) { | ||
noSetup.push(cb) | ||
} else { | ||
const fn = ssrRefFunctions.get(vm) | ||
|
||
if (!fn) { | ||
ssrRefFunctions.set(vm, [cb]) | ||
} else { | ||
fn.push(cb) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,48 @@ | ||
import { ref, Ref, onServerPrefetch as prefetch } from '@vue/composition-api' | ||
import { ref, Ref } from '@vue/composition-api' | ||
import { onServerPrefetchEnd } from './server-prefetch' | ||
|
||
function getValue<T>(value: T | (() => T)): T { | ||
if (value instanceof Function) return value() | ||
return value | ||
} | ||
|
||
let data: any = {} | ||
let injected = false | ||
|
||
const refs: Record<string, Ref<any>> = {} | ||
|
||
export function setSSRContext(ssrContext: any) { | ||
ssrContext.nuxt.ssrRefs = data | ||
} | ||
|
||
export function injectRefs() { | ||
if (!process.server) return | ||
|
||
Object.entries(refs).forEach(([key, ref]) => { | ||
data[key] = JSON.parse(JSON.stringify(ref.value)) | ||
}) | ||
function clone<T>(obj: T): T { | ||
if (typeof obj === 'object') { | ||
return JSON.parse(JSON.stringify(obj)) | ||
} else { | ||
return obj | ||
} | ||
} | ||
|
||
/** | ||
* Creates a Ref that is in sync with the client. | ||
*/ | ||
export const ssrRef = <T>(value: T | (() => T), key?: string): Ref<T> => { | ||
const val = ref<T>(getValue(value)) | ||
|
||
if (!key) | ||
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 (!injected) { | ||
prefetch(injectRefs) | ||
injected = true | ||
} | ||
|
||
if (process.client) { | ||
const nuxtState = (window as any).__NUXT__ | ||
val.value = (nuxtState.ssrRefs || {})[key!] ?? getValue(value) | ||
} else { | ||
refs[key] = val | ||
return ref((window as any).__NUXT__?.ssrRefs?.[key] ?? getValue(value)) | ||
} | ||
|
||
return val as Ref<T> | ||
} | ||
const val = getValue(value) | ||
const initVal = clone(val) | ||
const _ref = ref(val) as Ref<T> | ||
|
||
export const onServerPrefetch = (callback: Function) => { | ||
prefetch(async () => { | ||
await callback() | ||
injectRefs() | ||
onServerPrefetchEnd(() => { | ||
if (value instanceof Function || initVal !== _ref.value) | ||
data[key] = _ref.value | ||
}) | ||
|
||
return _ref | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { expectType } from 'tsd' | ||
|
||
import { useAsync, Ref, ssrRef } from '../..' | ||
|
||
expectType<Ref<null | { a: string }>>(useAsync(async () => ({ a: 'test' }))) | ||
|
||
expectType<Ref<null | { a: string }>>( | ||
useAsync(async () => ({ a: 'test' }), ssrRef(null)) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters