refactor. 미들웨어에서 언어 코드 설정 및 API 호출 로직 개선
- init.route.global.ts에서 현재 언어 코드 설정 추가 - pageData.global.ts에서 언어 코드 관리 및 API 호출 방식 개선 (csr -> ssr) - gameData.ts에서 현재 언어 코드 설정 추가
This commit is contained in:
@@ -5,7 +5,7 @@ import { parseUrl, isExternalUrl } from '#layers/utils/urlUtil'
|
||||
let pending: Promise<void> | null = null
|
||||
|
||||
export default defineNuxtRouteMiddleware(async to => {
|
||||
// 서버에서는 실행하지 않음 (서버 미들웨어에서 이미 처리)
|
||||
// server에서는 실행X (서버 미들웨어에서 이미 처리) -----
|
||||
if (import.meta.server) return
|
||||
|
||||
// error 페이지와 inspection 페이지는 미들웨어 실행 제외
|
||||
@@ -43,7 +43,6 @@ export default defineNuxtRouteMiddleware(async to => {
|
||||
// 현재 경로와 최종 경로가 같으면 리다이렉트 불필요
|
||||
if (fullPath.split('?')[0] === finalPath.split('?')[0]) {
|
||||
// gameData가 없으면(초기 진입 등) 여기서 강제 호출하지 않음
|
||||
// - 초기 gameData는 서버 미들웨어에서 주입되는 구조
|
||||
if (!gameData.value) return
|
||||
|
||||
// 초기 hydration에서 currentLangCode가 비어있으면 현재 로케일로 동기화만
|
||||
@@ -71,6 +70,9 @@ export default defineNuxtRouteMiddleware(async to => {
|
||||
return pending
|
||||
}
|
||||
|
||||
// 현재 언어 코드 설정
|
||||
gameDataStore.setCurrentLangCode(finalLocale)
|
||||
|
||||
// 외부 URL인지 확인
|
||||
const isExternal = isExternalUrl(finalPath)
|
||||
|
||||
|
||||
@@ -2,43 +2,39 @@ import { usePageDataStore } from '#layers/stores/usePageDataStore'
|
||||
import { useLoadingStore } from '#layers/stores/useLoadingStore'
|
||||
import { commonFetch } from '#layers/utils/apiUtil'
|
||||
import { getGameDomain, getPathAfterLanguage } from '#layers/utils/urlUtil'
|
||||
import { DEFAULT_LOCALE_CODE } from '@/i18n.config'
|
||||
import type { PageDataResponse } from '#layers/types/api/pageData'
|
||||
|
||||
export default defineNuxtRouteMiddleware(async (to, _from) => {
|
||||
const runtimeConfig = useRuntimeConfig()
|
||||
|
||||
// server에서는 실행X -----
|
||||
if (import.meta.server) return
|
||||
|
||||
// error 페이지는 실행X -----
|
||||
if (to.path.includes('/error')) return
|
||||
|
||||
// inspection 페이지는 실행X -----
|
||||
if (to.path.includes('/inspection')) return
|
||||
|
||||
const event = useRequestEvent()
|
||||
const gameDataStore = useGameDataStore()
|
||||
const pageDataStore = usePageDataStore()
|
||||
const loadingStore = useLoadingStore()
|
||||
|
||||
const { langCodes, defaultLangCode } = storeToRefs(gameDataStore)
|
||||
const { currentLangCode: currentLangCodeRef } = storeToRefs(gameDataStore)
|
||||
|
||||
const currentLangCode = currentLangCodeRef.value || DEFAULT_LOCALE_CODE
|
||||
const stoveApiServerBaseUrl = runtimeConfig.public.stoveApiUrlServer
|
||||
const stoveApiBaseUrl = runtimeConfig.public.stoveApiUrl
|
||||
const accessToken = csrGetAccessToken()
|
||||
const gameDomain = getGameDomain()
|
||||
const stoveApiOrigin = import.meta.server
|
||||
? stoveApiServerBaseUrl
|
||||
: stoveApiBaseUrl
|
||||
const gameDomain = getGameDomain(event ?? undefined)
|
||||
const pathWithoutLocale = getPathAfterLanguage(to.path)
|
||||
const langCode =
|
||||
csrGetFinalLocale(to.path, langCodes.value, defaultLangCode.value) || 'ko'
|
||||
|
||||
let pageDataResponse: PageDataResponse | null = null
|
||||
|
||||
try {
|
||||
// 페이지 이동 시 로딩 상태 시작
|
||||
loadingStore.startFullLoading()
|
||||
|
||||
const headers = {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
}
|
||||
|
||||
let queryParams: Record<string, string>
|
||||
let apiUrl: string
|
||||
|
||||
@@ -51,27 +47,24 @@ export default defineNuxtRouteMiddleware(async (to, _from) => {
|
||||
const urlParams = new URLSearchParams(queryString)
|
||||
const pageSeq = urlParams.get('page_seq') || ''
|
||||
const pageVer = urlParams.get('page_ver') || ''
|
||||
const queryLangCode = urlParams.get('lang_code') || langCode
|
||||
const queryLangCode = urlParams.get('lang_code') || currentLangCode
|
||||
|
||||
apiUrl = `${stoveApiBaseUrl}/pub-comm/v1.0/template/page/preview`
|
||||
queryParams = {
|
||||
lang_code: queryLangCode,
|
||||
page_seq: pageSeq,
|
||||
page_ver: pageVer,
|
||||
_t: Date.now().toString(), // 캐시 무효화를 위한 타임스탬프
|
||||
}
|
||||
} else {
|
||||
apiUrl = `${stoveApiBaseUrl}/pub-comm/v2.0/template/page`
|
||||
apiUrl = `${stoveApiOrigin}/pub-comm/v2.0/template/page`
|
||||
queryParams = {
|
||||
game_domain: gameDomain,
|
||||
lang_code: langCode,
|
||||
lang_code: currentLangCode,
|
||||
page_url: pathWithoutLocale,
|
||||
_t: Date.now().toString(), // 캐시 무효화를 위한 타임스탬프
|
||||
}
|
||||
}
|
||||
|
||||
pageDataResponse = (await commonFetch('GET', apiUrl, {
|
||||
headers,
|
||||
query: queryParams,
|
||||
})) as PageDataResponse | null
|
||||
|
||||
@@ -126,7 +119,7 @@ export default defineNuxtRouteMiddleware(async (to, _from) => {
|
||||
},
|
||||
})
|
||||
}
|
||||
return navigateTo(`/${langCode}/home`)
|
||||
return navigateTo(`/${currentLangCode}/home`)
|
||||
}
|
||||
|
||||
// [TODO]
|
||||
|
||||
@@ -91,6 +91,9 @@ function fnLocaleMiddleware(
|
||||
intro
|
||||
)
|
||||
|
||||
// 현재 언어 코드 설정
|
||||
event.context.currentLangCode = finalLocale
|
||||
|
||||
// finalPath가 유효하지 않으면 리다이렉트하지 않음
|
||||
if (!finalPath || typeof finalPath !== 'string') {
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user