refactor. 게임 데이터 로딩 및 언어 전환 로직 개선
- LanguageSwitcher.vue에서 setLocale 및 gameData 로딩 로직 통합 - useGameDataLoader.ts 추가로 게임 데이터 로딩 기능 구현 - init.route.global.ts에서 초기화 로직 개선 및 중복 호출 방지 - useGameDataStore.ts에 currentLangCode 상태 추가 및 설정 함수 구현 Made-with: Cursor
This commit is contained in:
@@ -2,7 +2,9 @@ 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 => {
|
||||
let pending: Promise<void> | null = null
|
||||
|
||||
export default defineNuxtRouteMiddleware(async to => {
|
||||
// 서버에서는 실행하지 않음 (서버 미들웨어에서 이미 처리)
|
||||
if (import.meta.server) return
|
||||
|
||||
@@ -12,7 +14,8 @@ export default defineNuxtRouteMiddleware(to => {
|
||||
}
|
||||
|
||||
const gameDataStore = useGameDataStore()
|
||||
const { langCodes, defaultLangCode, intro } = storeToRefs(gameDataStore)
|
||||
const { langCodes, defaultLangCode, intro, currentLangCode, gameData } =
|
||||
storeToRefs(gameDataStore)
|
||||
|
||||
// 게임 데이터 스토어가 초기화되지 않았으면 대기
|
||||
if (!langCodes.value || !defaultLangCode.value || !intro.value) {
|
||||
@@ -39,7 +42,33 @@ export default defineNuxtRouteMiddleware(to => {
|
||||
|
||||
// 현재 경로와 최종 경로가 같으면 리다이렉트 불필요
|
||||
if (fullPath.split('?')[0] === finalPath.split('?')[0]) {
|
||||
return
|
||||
// gameData가 없으면(초기 진입 등) 여기서 강제 호출하지 않음
|
||||
// - 초기 gameData는 서버 미들웨어에서 주입되는 구조
|
||||
if (!gameData.value) return
|
||||
|
||||
// 초기 hydration에서 currentLangCode가 비어있으면 현재 로케일로 동기화만
|
||||
if (!currentLangCode.value) {
|
||||
gameDataStore.setCurrentLangCode(finalLocale)
|
||||
return
|
||||
}
|
||||
|
||||
// 로케일이 같으면 불필요한 재호출 방지
|
||||
if (
|
||||
`${currentLangCode.value}`.toLowerCase() ===
|
||||
`${finalLocale}`.toLowerCase()
|
||||
) {
|
||||
return
|
||||
}
|
||||
|
||||
// 동시에 여러 번 호출되는 것 방지 (라우트 훅/미들웨어 중복 등)
|
||||
if (pending) return pending
|
||||
|
||||
const { loadGameData } = useGameDataLoader()
|
||||
pending = loadGameData(finalLocale).finally(() => {
|
||||
pending = null
|
||||
})
|
||||
|
||||
return pending
|
||||
}
|
||||
|
||||
// 외부 URL인지 확인
|
||||
|
||||
Reference in New Issue
Block a user