Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(useWebSocket): ssr support #3370

Merged
merged 1 commit into from Oct 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/core/useWebSocket/index.ts
@@ -1,7 +1,7 @@
import type { Ref } from 'vue-demi'
import { ref, watch } from 'vue-demi'
import type { Fn, MaybeRefOrGetter } from '@vueuse/shared'
import { toRef, tryOnScopeDispose, useIntervalFn } from '@vueuse/shared'
import { isClient, toRef, tryOnScopeDispose, useIntervalFn } from '@vueuse/shared'
import { useEventListener } from '../useEventListener'

export type WebSocketStatus = 'OPEN' | 'CONNECTING' | 'CLOSED'
Expand Down Expand Up @@ -186,7 +186,7 @@ export function useWebSocket<Data = any>(

// Status code 1000 -> Normal Closure https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent/code
const close: WebSocket['close'] = (code = 1000, reason) => {
if (!wsRef.value)
if (!isClient || !wsRef.value)
return
explicitlyClosed = true
resetHeartbeat()
Expand Down Expand Up @@ -288,11 +288,13 @@ export function useWebSocket<Data = any>(
}

if (autoClose) {
useEventListener(window, 'beforeunload', () => close())
useEventListener('beforeunload', () => close())
tryOnScopeDispose(close)
}

const open = () => {
if (!isClient)
return
close()
explicitlyClosed = false
retried = 0
Expand Down