Files
web-temp/layers/composables/useGetGameAlias.ts

54 lines
1.4 KiB
TypeScript

import { getHeader } from 'h3'
import { useRuntimeConfig, useRequestEvent } from 'nuxt/app'
export const useGetGameAlias = () => {
const config = useRuntimeConfig()
const baseDomain = (config.public.baseDomain || '.onstove.com') as string
// 서버 사이드에서 실행되는 경우
if (!import.meta.client) {
try {
const event = useRequestEvent()
if (event) {
// 미들웨어에서 설정한 gameAlias가 있다면 우선 사용
if (event.context.gameAlias) {
return event.context.gameAlias
}
const host = getHeader(event, 'host') || ''
const isGameAliasExtractable = host.includes(baseDomain)
if (isGameAliasExtractable) {
const subdomain = host.split('.')[0]
if (subdomain && subdomain !== 'www') {
return subdomain
}
}
}
} catch (error) {
console.error('useGetGameAlias server error: ', error)
}
}
// 클라이언트 사이드에서 실행되는 경우
if (import.meta.client) {
try {
const host = window.location.host
const isGameAliasExtractable = host.includes(baseDomain)
if (isGameAliasExtractable) {
const subdomain = host.split('.')[0]
if (subdomain && subdomain !== 'www') {
return subdomain
}
}
} catch (error) {
console.error('useGetGameAlias client error: ', error)
}
}
return ''
}