Files
web-temp/layers/middleware/init.route.global.ts

51 lines
1.5 KiB
TypeScript

import { DEFAULT_LOCALE_CODE, ISO_LANGUAGE_CODES } from '@/i18n.config'
import { csrGetFinalLocale } from '#layers/utils/localeUtil'
import { parseUrl, isExternalUrl } from '#layers/utils/urlUtil'
export default defineNuxtRouteMiddleware(to => {
// 서버에서는 실행하지 않음 (서버 미들웨어에서 이미 처리)
if (import.meta.server) return
// error 페이지와 inspection 페이지는 미들웨어 실행 제외
if (to.path.includes('/error') || to.path.includes('/inspection')) {
return
}
const gameDataStore = useGameDataStore()
const { langCodes, defaultLangCode, intro } = storeToRefs(gameDataStore)
// 게임 데이터 스토어가 초기화되지 않았으면 대기
if (!langCodes.value || !defaultLangCode.value || !intro.value) {
return
}
const fullPath = to.fullPath || to.path || ''
// 최종 로케일 결정
const finalLocale =
csrGetFinalLocale(fullPath, langCodes.value, defaultLangCode.value) ||
defaultLangCode.value ||
DEFAULT_LOCALE_CODE
// parseUrl을 사용하여 최종 경로 계산
const introPageUrl = intro.value?.page_url || ''
const finalPath = parseUrl(
fullPath,
finalLocale,
langCodes.value,
ISO_LANGUAGE_CODES,
introPageUrl
)
// 현재 경로와 최종 경로가 같으면 리다이렉트 불필요
if (fullPath.split('?')[0] === finalPath.split('?')[0]) {
return
}
// 외부 URL인지 확인
const isExternal = isExternalUrl(finalPath)
// 리다이렉트 수행
return navigateTo(finalPath, { external: isExternal })
})