From 14970701f713da2a709862123c78c67f82995a44 Mon Sep 17 00:00:00 2001 From: sohot8653 Date: Fri, 26 Sep 2025 09:17:41 +0900 Subject: [PATCH] =?UTF-8?q?[=ED=8E=98=EC=9D=B4=EC=A7=80=20=EB=A1=9C?= =?UTF-8?q?=EB=94=A9=20=EC=B2=98=EB=A6=AC=20=EC=B6=94=EA=B0=80]=20?= =?UTF-8?q?=ED=8E=98=EC=9D=B4=EC=A7=80=20=EC=9D=B4=EB=8F=99=20=EC=8B=9C=20?= =?UTF-8?q?=EC=9E=90=EB=8F=99=EC=9C=BC=EB=A1=9C=20=EB=A1=9C=EB=94=A9=20?= =?UTF-8?q?=ED=91=9C=EC=8B=9C=20=EB=B0=8F=20DOM=20=EB=A0=8C=EB=8D=94?= =?UTF-8?q?=EB=A7=81=20=EC=99=84=EB=A3=8C=20=ED=9B=84=20=EB=A1=9C=EB=94=A9?= =?UTF-8?q?=20=EC=A2=85=EB=A3=8C=20=EA=B8=B0=EB=8A=A5=EC=9D=84=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=ED=95=98=EB=8A=94=20=ED=94=8C=EB=9F=AC=EA=B7=B8?= =?UTF-8?q?=EC=9D=B8=20=EC=B6=94=EA=B0=80.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/page-loading.client.ts | 53 ++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 plugins/page-loading.client.ts diff --git a/plugins/page-loading.client.ts b/plugins/page-loading.client.ts new file mode 100644 index 0000000..93cff14 --- /dev/null +++ b/plugins/page-loading.client.ts @@ -0,0 +1,53 @@ +/** + * 페이지 이동 시 자동 로딩 처리 플러그인 + * 모든 페이지 이동에서 GlobalLoading을 자동으로 표시하고 + * DOM 렌더링 완료 후 자동으로 숨깁니다. + */ +export default defineNuxtPlugin(() => { + const { startLoading } = useLoading(); + const router = useRouter(); + + let loadingStopFn: (() => void) | null = null; + let isNavigating = false; + + // 페이지 이동 시작 시 로딩 시작 + router.beforeEach((to, from) => { + // 같은 페이지 내에서의 이동은 무시 + if (to.path === from.path) { + return; + } + + // 이미 로딩 중이면 무시 + if (isNavigating) { + return; + } + + isNavigating = true; + loadingStopFn = startLoading("페이지를 불러오는 중..."); + }); + + // 페이지 이동 완료 후 로딩 종료 + router.afterEach(() => { + if (!isNavigating || !loadingStopFn) { + return; + } + + // DOM이 완전히 렌더링된 후 로딩 종료 + nextTick(() => { + setTimeout(() => { + loadingStopFn?.(); + loadingStopFn = null; + isNavigating = false; + }, 200); + }); + }); + + // 에러 발생 시 로딩 강제 종료 + router.onError(() => { + if (loadingStopFn) { + loadingStopFn(); + loadingStopFn = null; + isNavigating = false; + } + }); +});