fix. 서버 미들웨어, 미들웨어 수정
This commit is contained in:
@@ -1,92 +1,83 @@
|
||||
import type { GameDataValue } from '#layers/types/api/gameData'
|
||||
export default defineNuxtRouteMiddleware(to => {
|
||||
// error 페이지는 실행X -----
|
||||
if (to.path.includes('/error')) return
|
||||
|
||||
export default defineNuxtRouteMiddleware(async (to, _from) => {
|
||||
const nuxtApp = useNuxtApp()
|
||||
const getGameDataFromServer = (): GameDataValue | null => {
|
||||
return import.meta.server
|
||||
? (nuxtApp.ssrContext?.event.context.gameData ?? null)
|
||||
: null
|
||||
}
|
||||
// inspection 페이지는 실행X -----
|
||||
if (to.path.includes('inspection')) return
|
||||
|
||||
const serverGameData = getGameDataFromServer()
|
||||
const gameDataStore = useGameDataStore()
|
||||
const { gameData } = storeToRefs(gameDataStore)
|
||||
|
||||
const { setGameData } = gameDataStore
|
||||
// app.vue에서 설정한 스토어 값이 없으면 대기
|
||||
if (!gameData.value) return
|
||||
|
||||
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')
|
||||
) {
|
||||
console.log('🚀 ~ init.route.global error 페이지는 API 호출하지 않음')
|
||||
showError(
|
||||
createError({
|
||||
statusCode: 500,
|
||||
statusMessage: 'Internal Server Error',
|
||||
fatal: false, // 즉시 에러 페이지로
|
||||
data: { reason: 'post-not-found' },
|
||||
})
|
||||
const gamePath = getPathAfterLanguage(to.path)
|
||||
const langCode = import.meta.client
|
||||
? csrGetFinalLocale(to.path, gameData.value.lang_codes)
|
||||
: ssrGetFinalLocale(
|
||||
to.path,
|
||||
useRequestHeaders(['accept-language']),
|
||||
gameData.value.lang_codes,
|
||||
gameData.value.default_lang_code
|
||||
)
|
||||
return
|
||||
|
||||
const isRootPath = gamePath === '' || gamePath === '/'
|
||||
|
||||
if (isRootPath) {
|
||||
// gameData.intro.page_url이 있으면 해당 URL로 리다이렉트, 없으면 /home으로
|
||||
const introPageUrl = gameData.value?.intro?.page_url
|
||||
let defaultPath = `/${langCode}/home`
|
||||
|
||||
if (introPageUrl && introPageUrl.trim() !== '') {
|
||||
// 외부 URL인지 확인
|
||||
const isExternalUrl =
|
||||
introPageUrl.startsWith('http://') ||
|
||||
introPageUrl.startsWith('https://')
|
||||
|
||||
if (isExternalUrl) {
|
||||
// 외부 URL인 경우 그대로 사용
|
||||
defaultPath = introPageUrl
|
||||
} else {
|
||||
// 내부 경로인 경우 언어 코드 패턴 확인
|
||||
const normalizedIntroUrl = introPageUrl.split('?')[0] // 쿼리스트링 제외
|
||||
const languagePattern = /^\/[a-z]{2}(-[a-z]{2})?(\/|$)/
|
||||
const hasLanguageCode = languagePattern.test(normalizedIntroUrl)
|
||||
|
||||
if (hasLanguageCode) {
|
||||
// 이미 언어 코드가 있으면 그대로 사용
|
||||
defaultPath = introPageUrl
|
||||
} else {
|
||||
// 언어 코드가 없으면 추가
|
||||
const pathWithSlash = normalizedIntroUrl.startsWith('/')
|
||||
? normalizedIntroUrl
|
||||
: `/${normalizedIntroUrl}`
|
||||
defaultPath = `/${langCode}${pathWithSlash}`
|
||||
|
||||
// 쿼리스트링이 있으면 다시 추가
|
||||
if (introPageUrl.includes('?')) {
|
||||
defaultPath += '?' + introPageUrl.split('?')[1]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 허용된 언어 코드 목록≈≈
|
||||
const allowedLangCodes = langCodes || []
|
||||
// 무한 리다이렉트 방지: 현재 경로와 리다이렉트할 URL 비교
|
||||
const normalizedFinalUrl = defaultPath.split('?')[0] // 쿼리스트링 제외
|
||||
const currentPath = to.path
|
||||
const isExternalUrl =
|
||||
defaultPath.startsWith('http://') || defaultPath.startsWith('https://')
|
||||
const isSamePath = !isExternalUrl && currentPath === normalizedFinalUrl
|
||||
|
||||
// 현재 언어가 허용된 언어 목록에 없으면 에러 페이지로 이동
|
||||
if (currentLangCode && !allowedLangCodes.includes(currentLangCode)) {
|
||||
return navigateTo(`/${currentLangCode}/error`, { external: true })
|
||||
if (!isSamePath) {
|
||||
// 다른 경로에서 접근한 경우에만 리다이렉트
|
||||
const queryString = to.fullPath.includes('?')
|
||||
? '?' + to.fullPath.split('?')[1]
|
||||
: ''
|
||||
const redirectUrl =
|
||||
queryString && !defaultPath.includes('?')
|
||||
? `${defaultPath}${queryString}`
|
||||
: defaultPath
|
||||
return navigateTo(redirectUrl, { external: isExternalUrl })
|
||||
}
|
||||
} 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' },
|
||||
})
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user