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

95 lines
2.9 KiB
TypeScript

import type { GameDataRequest, GameDataValue } from '#layers/types/api/gameData'
export default defineNuxtRouteMiddleware(async (to, _from) => {
const nuxtApp = useNuxtApp()
const getGameDataFromServer = (): GameDataValue | null => {
return import.meta.server
? (nuxtApp.ssrContext?.event.context.gameData ?? null)
: null
}
const serverGameData = getGameDataFromServer()
const gameDataStore = useGameDataStore()
const { setGameData } = gameDataStore
if (serverGameData) {
setGameData(serverGameData)
}
try {
// 서버 사이드에서는 스킵
if (!import.meta.client) {
return
}
// 현재 경로에서 언어 코드 추출
const gameData = gameDataStore.gameData as GameDataValue
const langCodes = gameData?.lang_codes
const currentLangCode = csrGetFinalLocale(to.path, langCodes)
const { getPathAfterLanguage } = usePathResolver()
const pageUrl = getPathAfterLanguage(to.path)
//현재 url에서 게임 도메인만 추출
// const currentDomain = window.location.hostname
// const runtimeConfig = useRuntimeConfig()
// 쿼리스트링에서 f 파라미터 값 추출 (CSR용)
// const fValue = (to.query.f as string) || ''
// 미리보기 API 호출 처리
// let finalGameDomain = currentDomain
// if (fValue === 'preview') {
// finalGameDomain = 'samplegame.onstove.com'
// }
// const req: GameDataRequest = {
// gameDomain: `${finalGameDomain}`,
// langCode: `${currentLangCode}`,
// game_alias: '',
// lang_code: `${currentLangCode}`,
// baseApiUrl: `${runtimeConfig.public.stoveApiUrl}`,
// gameId: '',
// }
// const { getGameDataExternal } = useGetGameDataExternal()
// await getGameDataExternal(req)
// error 페이지는 API 호출하지 않음
if (
pageUrl === '/error' ||
to.path.includes('/error') ||
to.path.includes('/inspection')
) {
console.log("🚀 ~ init.route.global error 페이지는 API 호출하지 않음")
showError(
createError({
statusCode: 500,
statusMessage:
'Internal Server Error',
fatal: false, // 즉시 에러 페이지로
data: { reason: 'post-not-found' },
})
)
return
}
// 허용된 언어 코드 목록≈≈
const allowedLangCodes = langCodes || []
// 현재 언어가 허용된 언어 목록에 없으면 에러 페이지로 이동
if (currentLangCode && !allowedLangCodes.includes(currentLangCode)) {
return navigateTo(`/${currentLangCode}/error`, { external: true })
}
} catch (error) {
console.error(error)
showError(
createError({
statusCode: error?.statusCode || error?.status || 500,
statusMessage:
error?.statusMessage || error?.message || 'Internal Server Error',
fatal: false, // 즉시 에러 페이지로
data: { reason: 'post-not-found' },
})
)
}
})