From 98b94f4972ee0232ca43591fb4702f0b52289142 Mon Sep 17 00:00:00 2001 From: ShaneK Date: Thu, 14 May 2026 06:29:11 -0700 Subject: [PATCH] fix(vue-router): prevent out-of-bounds index when popping across tabs --- packages/vue-router/src/viewStacks.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/vue-router/src/viewStacks.ts b/packages/vue-router/src/viewStacks.ts index 8c9f0cbefca..0e00d22de9c 100644 --- a/packages/vue-router/src/viewStacks.ts +++ b/packages/vue-router/src/viewStacks.ts @@ -196,8 +196,15 @@ export const createViewStacks = (router: Router) => { if (!viewStack) return; const startIndex = viewStack.findIndex((v) => v === viewItem); + if (startIndex === -1) return; - for (let i = startIndex + 1; i < startIndex - delta; i++) { + // delta from popstate reflects browser history depth, which can exceed + // the outlet's view stack when tab switches build up history without + // adding new view items. Clamp to the stack length so we never index + // past the end of the array. + const endIndex = Math.min(viewStack.length, startIndex - delta); + + for (let i = startIndex + 1; i < endIndex; i++) { const viewItem = viewStack[i]; viewItem.mount = false; viewItem.ionPageElement = undefined; @@ -233,8 +240,11 @@ export const createViewStacks = (router: Router) => { if (!viewStack) return; const startIndex = viewStack.findIndex((v) => v === viewItem); + if (startIndex === -1) return; + + const endIndex = Math.min(viewStack.length, startIndex + delta); - for (let i = startIndex + 1; i < startIndex + delta; i++) { + for (let i = startIndex + 1; i < endIndex; i++) { viewStack[i].mount = true; } };