fix: 全局移除process.client、process.server #669

This commit is contained in:
Tim
2025-08-21 10:22:33 +08:00
parent b7f5d8485c
commit cfce4d7d1d
9 changed files with 29 additions and 28 deletions

View File

@@ -50,7 +50,7 @@ onMounted(async () => {
})
const checkMilkTeaActivity = async () => {
if (!process.client) return
if (!import.meta.client) return
if (localStorage.getItem('milkTeaActivityPopupShown')) return
try {
const res = await fetch(`${API_BASE_URL}/api/activities`)
@@ -68,7 +68,7 @@ const checkMilkTeaActivity = async () => {
}
const checkInviteCodeActivity = async () => {
if (!process.client) return
if (!import.meta.client) return
if (localStorage.getItem('inviteCodeActivityPopupShown')) return
try {
const res = await fetch(`${API_BASE_URL}/api/activities`)
@@ -86,30 +86,30 @@ const checkInviteCodeActivity = async () => {
}
const closeInviteCodePopup = () => {
if (!process.client) return
if (!import.meta.client) return
localStorage.setItem('inviteCodeActivityPopupShown', 'true')
showInviteCodePopup.value = false
}
const closeMilkTeaPopup = () => {
if (!process.client) return
if (!import.meta.client) return
localStorage.setItem('milkTeaActivityPopupShown', 'true')
showMilkTeaPopup.value = false
}
const checkNotificationSetting = async () => {
if (!process.client) return
if (!import.meta.client) return
if (!authState.loggedIn) return
if (localStorage.getItem('notificationSettingPopupShown')) return
showNotificationPopup.value = true
}
const closeNotificationPopup = () => {
if (!process.client) return
if (!import.meta.client) return
localStorage.setItem('notificationSettingPopupShown', 'true')
showNotificationPopup.value = false
}
const checkNewMedals = async () => {
if (!process.client) return
if (!import.meta.client) return
if (!authState.loggedIn || !authState.userId) return
try {
const res = await fetch(`${API_BASE_URL}/api/medals?userId=${authState.userId}`)
@@ -127,7 +127,7 @@ const checkNewMedals = async () => {
}
}
const closeMedalPopup = () => {
if (!process.client) return
if (!import.meta.client) return
const seen = new Set(JSON.parse(localStorage.getItem('seenMedals') || '[]'))
newMedals.value.forEach((m) => seen.add(m.type))
localStorage.setItem('seenMedals', JSON.stringify([...seen]))

View File

@@ -40,7 +40,7 @@ const stopObserver = () => {
}
const startObserver = () => {
if (!process.client || props.pause || done.value) return
if (!import.meta.client || props.pause || done.value) return
stopObserver()
io = new IntersectionObserver(
async (entries) => {

View File

@@ -63,7 +63,7 @@ const isImageIcon = (icon) => {
}
const buildTagsUrl = (kw = '') => {
const base = API_BASE_URL || (process.client ? window.location.origin : '')
const base = API_BASE_URL || (import.meta.client ? window.location.origin : '')
const url = new URL('/api/tags', base)
if (kw) url.searchParams.set('keyword', kw)

View File

@@ -1,7 +1,7 @@
// 导出一个便捷的 toast 对象
export const toast = {
success: async (message) => {
if (process.client) {
if (import.meta.client) {
try {
const { useToast } = await import('vue-toastification')
const toastInstance = useToast()
@@ -12,7 +12,7 @@ export const toast = {
}
},
error: async (message) => {
if (process.client) {
if (import.meta.client) {
try {
const { useToast } = await import('vue-toastification')
const toastInstance = useToast()
@@ -23,7 +23,7 @@ export const toast = {
}
},
warning: async (message) => {
if (process.client) {
if (import.meta.client) {
try {
const { useToast } = await import('vue-toastification')
const toastInstance = useToast()
@@ -34,7 +34,7 @@ export const toast = {
}
},
info: async (message) => {
if (process.client) {
if (import.meta.client) {
try {
const { useToast } = await import('vue-toastification')
const toastInstance = useToast()
@@ -48,7 +48,7 @@ export const toast = {
// 导出 useToast composable
export const useToast = () => {
if (process.client) {
if (import.meta.client) {
return new Promise(async (resolve) => {
try {
const { useToast: useVueToast } = await import('vue-toastification')

View File

@@ -338,7 +338,7 @@ watch([selectedCategory, selectedTags], () => {
watch(selectedTopic, (val) => {
loadOptions()
selectedTopicCookie.value = val
if (process.client) localStorage.setItem('homeTab', val)
if (import.meta.client) localStorage.setItem('homeTab', val)
})
/** 选项首屏加载:服务端执行一次;客户端兜底 **/

View File

@@ -295,7 +295,7 @@ const commentSort = ref('NEWEST')
const isFetchingComments = ref(false)
const isMobile = useIsMobile()
const headerHeight = process.client
const headerHeight = import.meta.client
? parseFloat(getComputedStyle(document.documentElement).getPropertyValue('--header-height')) || 0
: 0
@@ -309,7 +309,7 @@ useHead(() => ({
],
}))
if (process.client) {
if (import.meta.client) {
onBeforeUnmount(() => {
window.removeEventListener('scroll', updateCurrentIndex)
if (countdownTimer) clearInterval(countdownTimer)
@@ -355,7 +355,7 @@ const updateCountdown = () => {
countdown.value = `${h}:${m}:${s}`
}
const startCountdown = () => {
if (!process.client) return
if (!import.meta.client) return
if (countdownTimer) clearInterval(countdownTimer)
updateCountdown()
countdownTimer = setInterval(updateCountdown, 1000)
@@ -515,7 +515,7 @@ watchEffect(() => {
})
// 404 客户端跳转
// if (postError.value?.statusCode === 404 && process.client) {
// if (postError.value?.statusCode === 404 && import.meta.client) {
// router.replace('/404')
// }
@@ -877,6 +877,7 @@ const gotoProfile = () => {
}
const initPage = async () => {
scrollTo(0, 0)
await fetchComments()
const hash = location.hash
const id = hash.startsWith('#comment-') ? hash.substring('#comment-'.length) : null

View File

@@ -1,7 +1,7 @@
import { clearToken } from '~/utils/auth'
export default defineNuxtPlugin(() => {
if (process.client) {
if (import.meta.client) {
const originalFetch = window.fetch
window.fetch = async (input, init) => {
const response = await originalFetch(input, init)

View File

@@ -4,7 +4,7 @@ import '~/assets/toast.css'
export default defineNuxtPlugin(async (nuxtApp) => {
// 确保只在客户端环境中注册插件
if (process.client) {
if (import.meta.client) {
try {
// 使用动态导入来避免 CommonJS 模块问题
const { default: Toast, POSITION } = await import('vue-toastification')

View File

@@ -12,7 +12,7 @@ export const authState = reactive({
role: null,
})
if (process.client) {
if (import.meta.client) {
authState.loggedIn =
localStorage.getItem(TOKEN_KEY) !== null && localStorage.getItem(TOKEN_KEY) !== ''
authState.userId = localStorage.getItem(USER_ID_KEY)
@@ -21,18 +21,18 @@ if (process.client) {
}
export function getToken() {
return process.client ? localStorage.getItem(TOKEN_KEY) : null
return import.meta.client ? localStorage.getItem(TOKEN_KEY) : null
}
export function setToken(token) {
if (process.client) {
if (import.meta.client) {
localStorage.setItem(TOKEN_KEY, token)
authState.loggedIn = true
}
}
export function clearToken() {
if (process.client) {
if (import.meta.client) {
localStorage.removeItem(TOKEN_KEY)
clearUserInfo()
authState.loggedIn = false
@@ -40,7 +40,7 @@ export function clearToken() {
}
export function setUserInfo({ id, username }) {
if (process.client) {
if (import.meta.client) {
authState.userId = id
authState.username = username
if (arguments[0] && arguments[0].role) {
@@ -53,7 +53,7 @@ export function setUserInfo({ id, username }) {
}
export function clearUserInfo() {
if (process.client) {
if (import.meta.client) {
localStorage.removeItem(USER_ID_KEY)
localStorage.removeItem(USERNAME_KEY)
localStorage.removeItem(ROLE_KEY)