fix: add SSR safety checks for navigator and window objects

This commit is contained in:
2025-10-10 12:18:01 +00:00
parent 7e426da91a
commit ce448adc63

View File

@@ -345,11 +345,13 @@ export default function BibleReaderNew({ initialVersion, initialBook, initialCha
console.log('You are now offline. Only downloaded content is available.') console.log('You are now offline. Only downloaded content is available.')
} }
// Set initial state // Set initial state (client-side only)
setIsOnline(navigator.onLine) if (typeof window !== 'undefined') {
setIsOnline(navigator.onLine)
}
// Check for offline mode preference // Check for offline mode preference
const offlineParam = new URLSearchParams(window.location.search).get('offline') const offlineParam = typeof window !== 'undefined' ? new URLSearchParams(window.location.search).get('offline') : null
if (offlineParam === 'true') { if (offlineParam === 'true') {
setIsOfflineMode(true) setIsOfflineMode(true)
} }
@@ -902,12 +904,12 @@ export default function BibleReaderNew({ initialVersion, initialBook, initialCha
const maxChapters = currentBook?.chapters?.length || 1 const maxChapters = currentBook?.chapters?.length || 1
const handleShare = async () => { const handleShare = async () => {
if (!selectedBook || !selectedChapter) return if (!selectedBook || !selectedChapter || typeof window === 'undefined') return
const shareUrl = `${window.location.origin}/${locale}/bible?version=${selectedVersion}&book=${selectedBook}&chapter=${selectedChapter}` const shareUrl = `${window.location.origin}/${locale}/bible?version=${selectedVersion}&book=${selectedBook}&chapter=${selectedChapter}`
const shareText = currentBook ? `${currentBook.name} ${selectedChapter}` : `Chapter ${selectedChapter}` const shareText = currentBook ? `${currentBook.name} ${selectedChapter}` : `Chapter ${selectedChapter}`
if (navigator.share) { if (typeof navigator !== 'undefined' && navigator.share) {
try { try {
await navigator.share({ await navigator.share({
title: shareText, title: shareText,
@@ -921,7 +923,7 @@ export default function BibleReaderNew({ initialVersion, initialBook, initialCha
console.error('Failed to copy link:', clipError) console.error('Failed to copy link:', clipError)
} }
} }
} else { } else if (typeof navigator !== 'undefined') {
// Fallback: copy to clipboard // Fallback: copy to clipboard
try { try {
await navigator.clipboard.writeText(shareUrl) await navigator.clipboard.writeText(shareUrl)
@@ -1067,12 +1069,16 @@ export default function BibleReaderNew({ initialVersion, initialBook, initialCha
} }
const handleCopyVerse = (verse: BibleVerse) => { const handleCopyVerse = (verse: BibleVerse) => {
if (typeof navigator === 'undefined') return
const text = `${currentBook?.name} ${selectedChapter}:${verse.verseNum} - ${verse.text}` const text = `${currentBook?.name} ${selectedChapter}:${verse.verseNum} - ${verse.text}`
navigator.clipboard.writeText(text).then(() => { navigator.clipboard.writeText(text).then(() => {
setCopyFeedback({ setCopyFeedback({
open: true, open: true,
message: t('copied') message: t('copied')
}) })
}).catch(err => {
console.error('Failed to copy verse:', err)
}) })
} }