Skip to content

Commit c134d8a

Browse files
committed
fix: make sure SSR still work
1 parent d49aaa1 commit c134d8a

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

packages/vue-final-modal/src/useApi.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type CoreModal from './components/CoreModal/CoreModal.vue'
66
import { internalVfmSymbol } from './injectionSymbols'
77

88
import type { ComponentProps, Constructor, InternalVfm, ModalSlot, ModalSlotOptions, RawProps, UseModalOptions, UseModalOptionsPrivate, UseModalReturnType, Vfm } from './Modal'
9-
import { getActiveVfm } from './plugin'
9+
import { activeVfm, getActiveVfm } from './plugin'
1010

1111
/**
1212
* Returns the vfm instance. Equivalent to using `$vfm` inside
@@ -77,15 +77,28 @@ export function useModal<P = InstanceType<typeof VueFinalModal>['$props']>(_opti
7777
})
7878

7979
if (options.modelValue === true) {
80-
nextTick(() => {
81-
const vfm = useVfm()
82-
vfm?.dynamicModals.push(options)
83-
})
80+
// nextTick will break the SSR, so use `activeVfm` first and then `useVfm()`
81+
if (activeVfm) {
82+
activeVfm?.dynamicModals.push(options)
83+
}
84+
else {
85+
nextTick(() => {
86+
const vfm = useVfm()
87+
vfm?.dynamicModals.push(options)
88+
})
89+
}
8490
}
8591

8692
async function open(): Promise<string> {
87-
await nextTick()
88-
const vfm = useVfm()
93+
// nextTick will break the SSR, so use `activeVfm` first and then `useVfm()`
94+
let vfm: Vfm
95+
if (activeVfm) {
96+
vfm = activeVfm
97+
}
98+
else {
99+
await nextTick()
100+
vfm = useVfm()
101+
}
89102
if (options.modelValue)
90103
return Promise.resolve('[Vue Final Modal] modal is already opened.')
91104

0 commit comments

Comments
 (0)