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

Commit

Permalink
fix: re-run ssrRef factory functions on hot module reload
Browse files Browse the repository at this point in the history
closes #88 - fix: `ssrRef` and `useAsync` do not support HMR
  • Loading branch information
danielroe committed Jun 2, 2020
1 parent a19b36e commit 282b8d2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/async.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { isRef, onServerPrefetch } from '@vue/composition-api'
import type { Ref } from '@vue/composition-api'

import { globalNuxt } from './globals'
import { ssrRef } from './ssr-ref'

/**
Expand Down Expand Up @@ -39,7 +40,12 @@ export const useAsync = <T>(

const _ref = isRef(key) ? key : ssrRef<T | null>(null, key)

if (!_ref.value) {
if (
!_ref.value ||
(process.env.NODE_ENV === 'development' &&
process.client &&
window[globalNuxt].context.isHMR)
) {
const p = cb()

if (p instanceof Promise) {
Expand Down
14 changes: 13 additions & 1 deletion src/ssr-ref.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ref, computed } from '@vue/composition-api'
import type { Ref } from '@vue/composition-api'

import { globalContext } from './globals'
import { globalContext, globalNuxt } from './globals'

function getValue<T>(value: T | (() => T)): T {
if (value instanceof Function) return value()
Expand Down Expand Up @@ -48,6 +48,12 @@ export const ssrRef = <T>(value: T | (() => T), key?: string): Ref<T> => {
}

if (process.client) {
if (
process.env.NODE_ENV === 'development' &&
window[globalNuxt].context.isHMR
) {
return ref(getValue(value))
}
return ref(
(window as any)[globalContext]?.ssrRefs?.[key] ?? getValue(value)
)
Expand Down Expand Up @@ -120,6 +126,12 @@ export const shallowSsrRef = <T>(
}

if (process.client) {
if (
process.env.NODE_ENV === 'development' &&
window[globalNuxt].context.isHMR
) {
return shallowRef(getValue(value))
}
return shallowRef(
(window as any)[globalContext]?.ssrRefs?.[key] ?? getValue(value)
)
Expand Down

0 comments on commit 282b8d2

Please sign in to comment.