Router history corruption on back navigation - page added instead of removed

Hi,

I’m working on a Capacitor hybrid app using Framework7 Svelte (8.3.4) with the following view configuration:

view: {
  animate: false,
  stackPages: false,
  browserHistory: true,
  pushState: true
}

The Problem

When navigating back (either via swipe back on iOS or physical back button on Android), the router history gets corrupted — instead of removing the current page from history, it adds it again.

Example:

  1. Navigate: / → /page1/ → /page2/ → /page3/ → /page4/
  2. History at /page4/: ["/", "/page1/", "/page2/", "/page3/"]
  3. Press back → navigates to /page3/ ✓
  4. History after back: ["/", "/page1/", "/page2/", "/page3/", "/page4/"] ← /page4/ added instead of removed!
  5. Press back again → navigates to wrong page, DOM breaks, links become unclickable

I confirmed this with f7.on('routeChange', ...) event listener logging both newRoute.url and router.history.

What I’ve tried

  • preloadPreviousPage: false → fixes history corruption but back navigation shows blank page (previous page not rendered)
  • uniqueHistory: true + allowDuplicateUrls: false → doesn’t fix the issue
  • browserHistoryInitialMatch: true → no effect
  • browserHistorySeparator: '' → no effect
  • Manually deleting from history array → unstable behavior
  • Disabling browserHistory and pushState → fixes corruption but loses browser history functionality

Current workaround

On Android: intercept the back button, show “Press again to exit” toast on second press, exit app on third. This masks the problem but doesn’t fix it.

On iOS: disabled swipe back (mdSwipeBack: false) to avoid the issue entirely.

Question

Is this a known bug with browserHistory: true + pushState: true in Capacitor environment? Is there a recommended way to use back navigation without history corruption?

Any help would be appreciated. Thanks!

I found a fix for this. The problem is in framework7-svelte (shared/components-router.js, pageComponentLoader), not in the F7 router itself.

Cause (my best guess from a DOM trace): with preloadPreviousPage, F7 moves the preloaded previous page before the current page with insertBefore (back.js), but pageComponentLoader pushes it at the end of viewRouter.pages. With Svelte 5 keyed {#each}, removing a block removes its whole DOM range, so the page F7 moved into that range is removed together with the old page. On the next back, the following preloaded page becomes .page-current, so one page gets skipped.

Fix: in pageComponentLoader:

  1. Keep a copy of the current children before changing pages, and for preload (backward) loads insert the new page before the current page instead of pushing it at the end:
const childrenBefore = Array.from(el.children);

if (options.preload) {
  const current = viewRouter.pages.findIndex(
    (p) => p.el && p.el.classList && p.el.classList.contains('page-current')
  );
  if (current >= 0) viewRouter.pages.splice(current, 0, pageData);
  else viewRouter.pages.push(pageData);
} else {
  viewRouter.pages.push(pageData);
}
  1. Resolve with the newly added element instead of the last child:
function onDidUpdate(componentRouterData) {
  if (componentRouterData !== viewRouter || resolved) return;
  const added = Array.from(el.children).filter((c) => childrenBefore.indexOf(c) < 0);
  if (!added.length) return; // only removals happened, wait
  app.f7events.off('viewRouterDidUpdate', onDidUpdate);
  const pageEl = added[added.length - 1];
  pageData.el = pageEl;
  resolve(pageEl);
  resolved = true;
}

I apply it with patch-package. After that, repeated router.back() walks through the whole history correctly (tested with 5+ pages, slow and fast).

Notes: this only covers the preload path, and I haven’t verified the Svelte internals beyond the DOM trace.