refactor. 게임 데이터 초기화 개선
- LanguageSwitcher.vue에서 언어 전환 시 게임 데이터 즉시 갱신 로직 개선 - init-game-data.server.ts 파일 추가로 서버 사이드에서 게임 데이터 초기화 로직 구현 - app.vue에서 불필요한 게임 데이터 초기화 로직 제거
This commit is contained in:
11
app/app.vue
11
app/app.vue
@@ -1,13 +1,9 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { useNuxtApp } from 'nuxt/app'
|
|
||||||
|
|
||||||
const nuxtApp = useNuxtApp()
|
|
||||||
const { locale } = useI18n()
|
const { locale } = useI18n()
|
||||||
const gameDataStore = useGameDataStore()
|
const gameDataStore = useGameDataStore()
|
||||||
const modalStore = useModalStore()
|
const modalStore = useModalStore()
|
||||||
const scrollStore = useScrollStore()
|
const scrollStore = useScrollStore()
|
||||||
|
|
||||||
const { setGameData } = gameDataStore
|
|
||||||
const { confirm, alert } = modalStore
|
const { confirm, alert } = modalStore
|
||||||
const {
|
const {
|
||||||
gameName,
|
gameName,
|
||||||
@@ -95,13 +91,6 @@ const setupGameHead = () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (import.meta.server) {
|
|
||||||
const gameData = nuxtApp.ssrContext?.event?.context?.gameData
|
|
||||||
if (gameData) {
|
|
||||||
setGameData(gameData)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
setupGameHead()
|
setupGameHead()
|
||||||
|
|
||||||
let rafId: number | null = null
|
let rafId: number | null = null
|
||||||
|
|||||||
@@ -233,10 +233,10 @@ const switchLanguage = async () => {
|
|||||||
})
|
})
|
||||||
localeCookie.value = selectedLocale.value.toLowerCase()
|
localeCookie.value = selectedLocale.value.toLowerCase()
|
||||||
|
|
||||||
|
// gameData 즉시 갱신 (1순위)
|
||||||
|
await loadGameData(selectedLocale.value)
|
||||||
// i18n locale 변경 (SPA)
|
// i18n locale 변경 (SPA)
|
||||||
await setLocale(selectedLocale.value as any)
|
await setLocale(selectedLocale.value as any)
|
||||||
// gameData가 언어별로 달라지는 영역(Header/GNB 등) 즉시 갱신
|
|
||||||
await loadGameData(selectedLocale.value)
|
|
||||||
// Nuxt SPA 라우팅으로 페이지 이동
|
// Nuxt SPA 라우팅으로 페이지 이동
|
||||||
await navigateTo(path)
|
await navigateTo(path)
|
||||||
}
|
}
|
||||||
|
|||||||
20
layers/plugins/init-game-data.server.ts
Normal file
20
layers/plugins/init-game-data.server.ts
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import type { GameDataValue } from '#layers/types/api/gameData'
|
||||||
|
|
||||||
|
export default defineNuxtPlugin({
|
||||||
|
name: 'hydrate-game-data',
|
||||||
|
dependsOn: ['pinia'],
|
||||||
|
setup(_nuxtApp) {
|
||||||
|
if (!import.meta.server) return
|
||||||
|
|
||||||
|
const event = useRequestEvent()
|
||||||
|
const gameData = (event?.context as { gameData?: GameDataValue })?.gameData
|
||||||
|
const currentLangCode = (event?.context as { currentLangCode?: string })
|
||||||
|
?.currentLangCode
|
||||||
|
|
||||||
|
if (!gameData) return
|
||||||
|
|
||||||
|
const gameDataStore = useGameDataStore()
|
||||||
|
gameDataStore.setCurrentLangCode(currentLangCode)
|
||||||
|
gameDataStore.setGameData(gameData)
|
||||||
|
},
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user