diff --git a/themes/hexo/components/Header.js b/themes/hexo/components/Header.js
index 9430c1db..eb811550 100644
--- a/themes/hexo/components/Header.js
+++ b/themes/hexo/components/Header.js
@@ -90,10 +90,8 @@ const Header = props => {
function updateHeaderHeight () {
setTimeout(() => {
- if (window) {
- const wrapperElement = document.getElementById('wrapper')
- wrapperTop = wrapperElement?.offsetTop
- }
+ const wrapperElement = document.getElementById('wrapper')
+ wrapperTop = wrapperElement?.offsetTop
}, 500)
}
diff --git a/themes/hexo/components/Progress.js b/themes/hexo/components/Progress.js
index b31ae16e..938f42f1 100644
--- a/themes/hexo/components/Progress.js
+++ b/themes/hexo/components/Progress.js
@@ -1,4 +1,5 @@
import React, { useEffect, useState } from 'react'
+import { isBrowser } from '@/lib/utils'
/**
* 顶部页面阅读进度条
@@ -9,7 +10,7 @@ const Progress = ({ targetRef, showPercent = true }) => {
const currentRef = targetRef?.current || targetRef
const [percent, changePercent] = useState(0)
const scrollListener = () => {
- const target = currentRef || (typeof document !== 'undefined' && document.getElementById('container'))
+ const target = currentRef || (isBrowser() && document.getElementById('container'))
if (target) {
const clientHeight = target.clientHeight
const scrollY = window.pageYOffset
diff --git a/themes/medium/LayoutSearch.js b/themes/medium/LayoutSearch.js
index 1b329b1c..6aa2ec5a 100644
--- a/themes/medium/LayoutSearch.js
+++ b/themes/medium/LayoutSearch.js
@@ -5,13 +5,14 @@ import TagGroups from './components/TagGroups'
import CategoryGroup from './components/CategoryGroup'
import BlogPostListScroll from './components/BlogPostListScroll'
import { useEffect } from 'react'
+import { isBrowser } from '@/lib/utils'
export const LayoutSearch = (props) => {
const { locale } = useGlobal()
const { keyword } = props
useEffect(() => {
setTimeout(() => {
- const container = typeof document !== 'undefined' && document.getElementById('container')
+ const container = isBrowser() && document.getElementById('container')
if (container && container.innerHTML) {
const re = new RegExp(`${keyword}`, 'gim')
container.innerHTML = container.innerHTML.replace(re, `
${keyword}`)
diff --git a/themes/medium/components/Progress.js b/themes/medium/components/Progress.js
index 50d2bc92..9a7cc7e0 100644
--- a/themes/medium/components/Progress.js
+++ b/themes/medium/components/Progress.js
@@ -1,4 +1,5 @@
import React, { useEffect, useState } from 'react'
+import { isBrowser } from '@/lib/utils'
/**
* 顶部页面阅读进度条
@@ -9,7 +10,7 @@ const Progress = ({ targetRef, showPercent = true }) => {
const currentRef = targetRef?.current || targetRef
const [percent, changePercent] = useState(0)
const scrollListener = () => {
- const target = currentRef || (typeof document !== 'undefined' && document.getElementById('container'))
+ const target = currentRef || (isBrowser() && document.getElementById('container'))
if (target) {
const clientHeight = target.clientHeight
const scrollY = window.pageYOffset
diff --git a/themes/next/Layout404.js b/themes/next/Layout404.js
index a9d18559..8c1bc421 100644
--- a/themes/next/Layout404.js
+++ b/themes/next/Layout404.js
@@ -1,19 +1,18 @@
import { useRouter } from 'next/router'
import LayoutBase from './LayoutBase'
import { useEffect } from 'react'
+import { isBrowser } from '@/lib/utils'
export const Layout404 = props => {
const router = useRouter()
useEffect(() => {
// 延时3秒如果加载失败就返回首页
setTimeout(() => {
- if (window) {
- const article = typeof document !== 'undefined' && document.getElementById('container')
- if (!article) {
- router.push('/').then(() => {
- // console.log('找不到页面', router.asPath)
- })
- }
+ const article = isBrowser() && document.getElementById('container')
+ if (!article) {
+ router.push('/').then(() => {
+ // console.log('找不到页面', router.asPath)
+ })
}
}, 3000)
}, [])
diff --git a/themes/next/LayoutArchive.js b/themes/next/LayoutArchive.js
index f4f6ed3e..1b322fd0 100644
--- a/themes/next/LayoutArchive.js
+++ b/themes/next/LayoutArchive.js
@@ -26,16 +26,14 @@ export const LayoutArchive = (props) => {
})
useEffect(() => {
- if (window) {
- const anchor = window.location.hash
- if (anchor) {
- setTimeout(() => {
- const anchorElement = document.getElementById(anchor.substring(1))
- if (anchorElement) {
- anchorElement.scrollIntoView({ block: 'start', behavior: 'smooth' })
- }
- }, 300)
- }
+ const anchor = window.location.hash
+ if (anchor) {
+ setTimeout(() => {
+ const anchorElement = document.getElementById(anchor.substring(1))
+ if (anchorElement) {
+ anchorElement.scrollIntoView({ block: 'start', behavior: 'smooth' })
+ }
+ }, 300)
}
}, [])
diff --git a/themes/next/LayoutSearch.js b/themes/next/LayoutSearch.js
index e3e58025..b1e0be2f 100644
--- a/themes/next/LayoutSearch.js
+++ b/themes/next/LayoutSearch.js
@@ -2,12 +2,13 @@ import LayoutBase from './LayoutBase'
import StickyBar from './components/StickyBar'
import BlogPostListScroll from './components/BlogPostListScroll'
import { useGlobal } from '@/lib/global'
+import { isBrowser } from '@/lib/utils'
export const LayoutSearch = (props) => {
const { locale } = useGlobal()
const { posts, keyword } = props
setTimeout(() => {
- const container = typeof document !== 'undefined' && document.getElementById('container')
+ const container = isBrowser() && document.getElementById('container')
if (container && container.innerHTML) {
const re = new RegExp(`${keyword}`, 'gim')
container.innerHTML = container.innerHTML.replace(re, `
${keyword}`)
diff --git a/themes/next/LayoutSlug.js b/themes/next/LayoutSlug.js
index 8e7ddd72..9e1b0341 100644
--- a/themes/next/LayoutSlug.js
+++ b/themes/next/LayoutSlug.js
@@ -8,6 +8,7 @@ import TocDrawer from './components/TocDrawer'
import { useRef } from 'react'
import CONFIG_NEXT from './config_next'
import { ArticleLock } from './components/ArticleLock'
+import { isBrowser } from '@/lib/utils'
export const LayoutSlug = (props) => {
const { post, latestPosts, lock, validPassword } = props
@@ -26,7 +27,7 @@ export const LayoutSlug = (props) => {
}
const drawerRight = useRef(null)
- const targetRef = typeof window !== 'undefined' ? document.getElementById('container') : null
+ const targetRef = isBrowser() ? document.getElementById('container') : null
const floatSlot = post?.toc?.length > 1
?
{
drawerRight?.current?.handleSwitchVisible()
diff --git a/themes/next/components/Header.js b/themes/next/components/Header.js
index df6db180..77c6ce61 100644
--- a/themes/next/components/Header.js
+++ b/themes/next/components/Header.js
@@ -74,10 +74,8 @@ export default function Header(props) {
function updateHeaderHeight() {
setTimeout(() => {
- if (window) {
- const wrapperElement = document.getElementById('wrapper')
- wrapperTop = wrapperElement.offsetTop
- }
+ const wrapperElement = document.getElementById('wrapper')
+ wrapperTop = wrapperElement.offsetTop
}, 500)
}
diff --git a/themes/next/components/Live2DWaifu.js b/themes/next/components/Live2DWaifu.js
index f6d383f4..c21fc79a 100644
--- a/themes/next/components/Live2DWaifu.js
+++ b/themes/next/components/Live2DWaifu.js
@@ -4,9 +4,7 @@ import { loadExternalResource } from '@/lib/utils'
export default function Live2DWife() {
useEffect(() => {
- if (window) {
- initLive2DWife()
- }
+ initLive2DWife()
}, [])
return <>
diff --git a/themes/next/components/Progress.js b/themes/next/components/Progress.js
index 19ca0ffa..91dfecc5 100644
--- a/themes/next/components/Progress.js
+++ b/themes/next/components/Progress.js
@@ -1,4 +1,5 @@
import React, { useEffect, useState } from 'react'
+import { isBrowser } from '@/lib/utils'
/**
* 顶部页面阅读进度条
@@ -9,7 +10,7 @@ const Progress = ({ targetRef, showPercent = true }) => {
const currentRef = targetRef?.current || targetRef
const [percent, changePercent] = useState(0)
const scrollListener = () => {
- const target = currentRef || (typeof document !== 'undefined' && document.getElementById('container'))
+ const target = currentRef || (isBrowser() && document.getElementById('container'))
if (target) {
const clientHeight = target.clientHeight
const scrollY = window.pageYOffset
diff --git a/themes/next/components/RewardButton.js b/themes/next/components/RewardButton.js
index 6de4f449..44733d6d 100644
--- a/themes/next/components/RewardButton.js
+++ b/themes/next/components/RewardButton.js
@@ -8,14 +8,10 @@ import Image from 'next/image'
*/
const RewardButton = () => {
const openPopover = () => {
- if (window) {
- document.getElementById('reward-qrcode').classList.remove('hidden')
- }
+ document.getElementById('reward-qrcode').classList.remove('hidden')
}
const closePopover = () => {
- if (window) {
- document.getElementById('reward-qrcode').classList.add('hidden')
- }
+ document.getElementById('reward-qrcode').classList.add('hidden')
}
return (
diff --git a/themes/next/components/SideBarDrawer.js b/themes/next/components/SideBarDrawer.js
index 8a22505d..213269f4 100644
--- a/themes/next/components/SideBarDrawer.js
+++ b/themes/next/components/SideBarDrawer.js
@@ -33,17 +33,15 @@ const SideBarDrawer = ({ post, cRef, tags, slot, categories, currentCategory })
// 点击按钮更改侧边抽屉状态
const switchSideDrawerVisible = (showStatus) => {
- if (window) {
- const sideBarDrawer = window.document.getElementById('sidebar-drawer')
- const sideBarDrawerBackground = window.document.getElementById('sidebar-drawer-background')
+ const sideBarDrawer = window.document.getElementById('sidebar-drawer')
+ const sideBarDrawerBackground = window.document.getElementById('sidebar-drawer-background')
- if (showStatus) {
- sideBarDrawer.classList.replace('-ml-80', 'ml-0')
- sideBarDrawerBackground.classList.replace('hidden', 'block')
- } else {
- sideBarDrawer.classList.replace('ml-0', '-ml-80')
- sideBarDrawerBackground.classList.replace('block', 'hidden')
- }
+ if (showStatus) {
+ sideBarDrawer.classList.replace('-ml-80', 'ml-0')
+ sideBarDrawerBackground.classList.replace('hidden', 'block')
+ } else {
+ sideBarDrawer.classList.replace('ml-0', '-ml-80')
+ sideBarDrawerBackground.classList.replace('block', 'hidden')
}
}
diff --git a/themes/next/components/WordCount.js b/themes/next/components/WordCount.js
index 37cd9efc..7188d90c 100644
--- a/themes/next/components/WordCount.js
+++ b/themes/next/components/WordCount.js
@@ -18,17 +18,15 @@ export default function WordCount() {
* 更新字数统计和阅读时间
*/
function countWords() {
- if (window) {
- const articleElement = document.getElementById('notion-article')
- if (articleElement) {
- const articleText = deleteHtmlTag(articleElement.innerHTML)
- const wordCount = fnGetCpmisWords(articleText)
- // 阅读速度 300-500每分钟
- document.getElementById('wordCount').innerHTML = wordCount
- document.getElementById('readTime').innerHTML = Math.floor(wordCount / 400) + 1
- const wordCountWrapper = document.getElementById('wordCountWrapper')
- wordCountWrapper.classList.remove('hidden')
- }
+ const articleElement = document.getElementById('notion-article')
+ if (articleElement) {
+ const articleText = deleteHtmlTag(articleElement.innerHTML)
+ const wordCount = fnGetCpmisWords(articleText)
+ // 阅读速度 300-500每分钟
+ document.getElementById('wordCount').innerHTML = wordCount
+ document.getElementById('readTime').innerHTML = Math.floor(wordCount / 400) + 1
+ const wordCountWrapper = document.getElementById('wordCountWrapper')
+ wordCountWrapper.classList.remove('hidden')
}
}