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

Commit 282b8d2

Browse files
committed
fix: re-run ssrRef factory functions on hot module reload
closes #88 - fix: `ssrRef` and `useAsync` do not support HMR
1 parent a19b36e commit 282b8d2

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/async.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { isRef, onServerPrefetch } from '@vue/composition-api'
22
import type { Ref } from '@vue/composition-api'
33

4+
import { globalNuxt } from './globals'
45
import { ssrRef } from './ssr-ref'
56

67
/**
@@ -39,7 +40,12 @@ export const useAsync = <T>(
3940

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

42-
if (!_ref.value) {
43+
if (
44+
!_ref.value ||
45+
(process.env.NODE_ENV === 'development' &&
46+
process.client &&
47+
window[globalNuxt].context.isHMR)
48+
) {
4349
const p = cb()
4450

4551
if (p instanceof Promise) {

src/ssr-ref.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ref, computed } from '@vue/composition-api'
22
import type { Ref } from '@vue/composition-api'
33

4-
import { globalContext } from './globals'
4+
import { globalContext, globalNuxt } from './globals'
55

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

5050
if (process.client) {
51+
if (
52+
process.env.NODE_ENV === 'development' &&
53+
window[globalNuxt].context.isHMR
54+
) {
55+
return ref(getValue(value))
56+
}
5157
return ref(
5258
(window as any)[globalContext]?.ssrRefs?.[key] ?? getValue(value)
5359
)
@@ -120,6 +126,12 @@ export const shallowSsrRef = <T>(
120126
}
121127

122128
if (process.client) {
129+
if (
130+
process.env.NODE_ENV === 'development' &&
131+
window[globalNuxt].context.isHMR
132+
) {
133+
return shallowRef(getValue(value))
134+
}
123135
return shallowRef(
124136
(window as any)[globalContext]?.ssrRefs?.[key] ?? getValue(value)
125137
)

0 commit comments

Comments
 (0)