Skip to content

Commit 15f16cf

Browse files
sbesh91claude
andcommitted
fix: Handle traversal navigation when canIntercept is false
For back/forward traversals, canIntercept may be false when the destination entry was created via history.replaceState, but the URL changes regardless. Only gate on canIntercept for push/replace navigations. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 0461f56 commit 15f16cf

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/router.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,25 @@ function handleNav(state, e) {
4141
// TODO: Double-check this can't fail to parse.
4242
// `.destination` is read-only, so I'm hoping it guarantees a valid URL.
4343
const url = new URL(e.destination.url);
44+
const isTraversal = e.navigationType === 'traverse';
4445

4546
if (
46-
!e.canIntercept ||
4747
e.hashChange ||
4848
e.downloadRequest !== null ||
4949
!isSameWindow(e) ||
50-
!isInScope(url)
50+
!isInScope(url) ||
51+
// For traversals (back/forward), canIntercept may be false (e.g. when the
52+
// destination entry was created via history.replaceState), but the URL
53+
// changes regardless — so we only gate on canIntercept for push/replace.
54+
(!isTraversal && !e.canIntercept)
5155
) {
5256
// This is set purely for our test suite so that we can check
5357
// if the event was ignored in another `navigate` handler.
5458
e['preact-iso-ignored'] = true;
5559
return state;
5660
}
5761

58-
e.intercept();
62+
if (e.canIntercept) e.intercept();
5963
return url.href.replace(url.origin, '');
6064
}
6165

test/router.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -968,12 +968,12 @@ describe('Router', () => {
968968

969969
expect(loc).to.deep.include({ url: '/foo', path: '/foo', searchParams: {} });
970970

971-
navigation.back();
971+
await navigation.back().finished;
972972
await sleep(10);
973973

974974
expect(loc).to.deep.include({ url: '/', path: '/', searchParams: {} });
975975

976-
navigation.forward();
976+
await navigation.forward().finished;
977977
await sleep(10);
978978

979979
expect(loc).to.deep.include({ url: '/foo', path: '/foo', searchParams: {} });

0 commit comments

Comments
 (0)