diff --git a/app/error.vue b/app/error.vue index e2f4873..d9ab11f 100644 --- a/app/error.vue +++ b/app/error.vue @@ -64,6 +64,7 @@ const { tm } = useI18n() const props = withDefaults(defineProps(), { error: () => ({}) }) +const router = useRouter() const nuxtError = useError() const currentError = computed(() => props.error || nuxtError.value) @@ -79,12 +80,22 @@ const localePath = useLocalePath() // const handleError = () => clearError({ redirect: '/' }) const handleError = () => { window.location.href = localePath('/') - // clearError({ redirect: `${localePath('/')}` }) + // clearError({ redirect: `${localePath('/brand')}` }) +} +const handleBack = () => { + // 에러 상태를 클리어하고 이전 페이지로 이동 + // popstate 이벤트가 이미 발생한 경우이므로 추가 동작 불필요 + // 키보드 백스페이스의 경우에만 명시적으로 뒤로가기 실행 + window.location.href = localePath('/') + // navigateTo(`${router.currentRoute.value.path}`) } // 500 에러 발생 시 /error 페이지로 리다이렉트 onMounted(() => { + console.log("🚀 ~ 1111 handleBack ~ router:", router) + console.log("🚀 ~ 2222 router.currentRoute.value.path) ~ router:", router.currentRoute.value.path) + const statusCode = currentError.value?.statusCode console.log("🚀 ~ 2222 currentError==:", currentError.value?.message) console.log("🚀 ~ 222222 statusCode:", statusCode) @@ -100,6 +111,30 @@ onMounted(() => { nextTick(() => { isLoading.value = false }) + + // 백스페이스 키 처리 + const handleKeydown = (e: KeyboardEvent) => { + console.log("🚀 ~ handleKeydown ~ e:", e.key) + if (e.key === 'Backspace' && + !['INPUT', 'TEXTAREA'].includes((e.target as HTMLElement).tagName)) { + e.preventDefault() + handleBack() + } + } + + // 브라우저 뒤로가기 버튼 처리 + const handlePopState = () => { + // clearError() + handleBack() + } + + window.addEventListener('keydown', handleKeydown) + window.addEventListener('popstate', handlePopState) + + onUnmounted(() => { + window.removeEventListener('keydown', handleKeydown) + window.removeEventListener('popstate', handlePopState) + }) }) \ No newline at end of file