Files
web-temp/layers/middleware/init.route.global.ts
2026-01-16 14:55:02 +09:00

45 lines
1.6 KiB
TypeScript

export default defineNuxtRouteMiddleware(to => {
// server에서는 실행X -----
if (import.meta.server) return
// error 페이지는 실행X -----
if (to.path.includes('/error')) return
// inspection 페이지는 실행X -----
if (to.path.includes('/inspection')) return
const gameDataStore = useGameDataStore()
const { langCodes, intro } = storeToRefs(gameDataStore)
// app.vue에서 설정한 스토어 값이 없으면 대기
if (!langCodes.value || !intro.value) return
// -------------------------------------------------------------------------------
// [Home Redirect]
// -------------------------------------------------------------------------------
const gamePath = getPathAfterLanguage(to.path)
const langCode = csrGetFinalLocale(to.path, langCodes.value)
const isRootPath = gamePath === '' || gamePath === '/'
if (isRootPath) {
// intro.page_url이 있으면 해당 URL로 리다이렉트, 없으면 /home으로
const introPageUrl = intro.value?.page_url
const redirectPath = getIntroRedirectPath(
introPageUrl,
langCode,
to.fullPath
)
// 무한 리다이렉트 방지: 현재 경로와 리다이렉트할 URL 비교
const normalizedFinalUrl = redirectPath.split('?')[0] // 리다이렉트 경로 (쿼리스트링 제외)
const isExternal = isExternalUrl(redirectPath)
const isSamePath = !isExternal && to.path === normalizedFinalUrl
if (!isSamePath) {
// 다른 경로에서 접근한 경우에만 리다이렉트
return navigateTo(redirectPath, { external: isExternal })
}
}
})