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

View File

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

View File

@@ -63,7 +63,7 @@ const isImageIcon = (icon) => {
} }
const buildTagsUrl = (kw = '') => { 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) const url = new URL('/api/tags', base)
if (kw) url.searchParams.set('keyword', kw) if (kw) url.searchParams.set('keyword', kw)

View File

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

View File

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

View File

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

View File

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

View File

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