Skip to content

Commit 09a55de

Browse files
authored
fix: Ensure the router ignores download links (#128)
1 parent 0c0a7fa commit 09a55de

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/router.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ function handleNav(state, action) {
4343
link.origin != location.origin ||
4444
/^#/.test(href) ||
4545
!/^(_?self)?$/i.test(link.target) ||
46-
!isInScope(href)
46+
!isInScope(href) ||
47+
link.download
4748
) {
4849
return state;
4950
}

test/router.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,27 @@ describe('Router', () => {
819819
pushState.restore();
820820
});
821821

822+
it('should ignore clicks on download links', async () => {
823+
const downloadHref = URL.createObjectURL(new Blob(['Hello World!'], { type: 'text/plain' }));
824+
825+
render(
826+
<LocationProvider>
827+
<Router>
828+
<a href={downloadHref} download="hello-world.txt">Download Me</a>
829+
</Router>
830+
<ShallowLocation />
831+
</LocationProvider>,
832+
scratch
833+
);
834+
835+
scratch.querySelector('a[download]').click();
836+
await sleep(1);
837+
838+
// If the router attempted to navigate, the page would throw a SecurityError
839+
// and the test would fail.
840+
expect(true).to.equal(true);
841+
});
842+
822843
it('should normalize children', async () => {
823844
const pushState = sinon.spy(history, 'pushState');
824845
const Route = sinon.fake(() => <a href="/foo#foo">foo</a>);

0 commit comments

Comments
 (0)