Skip to content

Commit 03aa24f

Browse files
committed
fix(ssr): prevent crash in ssr scenario
1 parent 5b3d60d commit 03aa24f

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/hooks/useAudioControl.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function useCreateAudioElement(
1717
) {
1818
const audioElementRef = useRef<HTMLAudioElement>()
1919

20-
if (!audioElementRef.current) {
20+
if (typeof document !== "undefined" && !audioElementRef.current) {
2121
const audio = (audioElementRef.current = document.createElement("audio"))
2222

2323
audio.addEventListener("play", (e) => {
@@ -73,16 +73,16 @@ export function useAudioControl(options: UseAudioControlOptions) {
7373
},
7474
onEnded: options.onEnded,
7575
})
76-
const [isPlaying, setPlaying] = useState(
77-
() => !audioElementRef?.current?.paused,
76+
const [isPlaying, setPlaying] = useState(() =>
77+
audioElementRef.current ? !audioElementRef.current.paused : false,
7878
)
7979
const [audioDuration, setAudioDuration] = useState<number | undefined>()
8080
const [currentTime, setCurrentTime] = useState<number>(0)
8181
const [bufferedSeconds, setBufferedSeconds] = useState<number | undefined>(
8282
undefined,
8383
)
8484
const [muted, setMuted] = useState<boolean>(
85-
() => audioElementRef?.current!.muted,
85+
() => audioElementRef.current?.muted ?? false,
8686
)
8787

8888
const playAudio = useCallback(async (src: string) => {

0 commit comments

Comments
 (0)