From e03f5945ea88e8f3eea86bcc8d16e2c4b9f56d02 Mon Sep 17 00:00:00 2001 From: Jorge Alvarez Date: Fri, 27 Nov 2020 13:38:55 +0100 Subject: [PATCH] Improve handling of href links. --- CHANGELOG.md | 4 ++++ package-lock.json | 14 +++++++------- package.json | 6 +++--- src/components/navigate.svelte | 3 ++- src/spa_router.js | 6 ++++-- 5 files changed, 20 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8babe0e..fe7fbab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Svelte Router changelog +## 5.8.1 + +Improve handling of href links. + ## 5.8.0 Added prefix param to constrain routes. diff --git a/package-lock.json b/package-lock.json index 0a8a222..fd5248c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "svelte-router-spa", - "version": "5.8.0", + "version": "5.8.1", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -575,9 +575,9 @@ } }, "eslint": { - "version": "7.12.1", - "resolved": "https://registry.npmjs.org/eslint/-/eslint-7.12.1.tgz", - "integrity": "sha512-HlMTEdr/LicJfN08LB3nM1rRYliDXOmfoO4vj39xN6BLpFzF00hbwBoqHk8UcJ2M/3nlARZWy/mslvGEuZFvsg==", + "version": "7.14.0", + "resolved": "https://registry.npmjs.org/eslint/-/eslint-7.14.0.tgz", + "integrity": "sha512-5YubdnPXrlrYAFCKybPuHIAH++PINe1pmKNc5wQRB9HSbqIK1ywAnntE3Wwua4giKu0bjligf1gLF6qxMGOYRA==", "dev": true, "requires": { "@babel/code-frame": "^7.0.0", @@ -1891,9 +1891,9 @@ } }, "svelte": { - "version": "3.29.4", - "resolved": "https://registry.npmjs.org/svelte/-/svelte-3.29.4.tgz", - "integrity": "sha512-oW0fGHlyFFMvzRtIvOs84b0fOc0gmZNQcL5Is3hxuTpvaYX3pfd8oHy4KnOvbq4Ca6SG6AHdRMk7OhApTo0NqA==", + "version": "3.30.0", + "resolved": "https://registry.npmjs.org/svelte/-/svelte-3.30.0.tgz", + "integrity": "sha512-z+hdIACb9TROGvJBQWcItMtlr4s0DBUgJss6qWrtFkOoIInkG+iAMo/FJZQFyDBQZc+dul2+TzYSi/tpTT5/Ag==", "dev": true }, "symbol-tree": { diff --git a/package.json b/package.json index da6aec8..36428ee 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "svelte-router-spa", - "version": "5.8.0", + "version": "5.8.1", "description": "A full featured router component for Svelte and your Single Page Applications.", "main": "src/index.js", "svelte": "src/index.js", @@ -15,10 +15,10 @@ "license": "MIT", "devDependencies": { "chai": "^4.2.0", - "eslint": "^7.12.1", + "eslint": "^7.14.0", "mocha": "^8.2.1", "mocha-jsdom": "^2.0.0", - "svelte": "^3.29.4" + "svelte": "^3.30.0" }, "dependencies": { "url-params-parser": "^1.0.3" diff --git a/src/components/navigate.svelte b/src/components/navigate.svelte index ec442d6..043c53d 100644 --- a/src/components/navigate.svelte +++ b/src/components/navigate.svelte @@ -6,7 +6,7 @@ export let styles = '' export let lang = null - onMount(function() { + onMount(function () { if (lang) { const route = localisedRoute(to, lang) if (route) { @@ -16,6 +16,7 @@ }) function navigate(event) { + if (event.metaKey || event.ctrlKey || event.shiftKey) return event.preventDefault() event.stopPropagation() navigateTo(to) diff --git a/src/spa_router.js b/src/spa_router.js index 52d7c71..f89ab4e 100644 --- a/src/spa_router.js +++ b/src/spa_router.js @@ -109,12 +109,14 @@ function routeIsActive(queryPath, includePath = false) { if (typeof window !== 'undefined') { // Avoid full page reload on local routes window.addEventListener('click', (event) => { + if (event.target.localName.toLowerCase() !== 'a') return + if (event.metaKey || event.ctrlKey || event.shiftKey) return + const sitePrefix = routerOptions.prefix ? `/${routerOptions.prefix.toLowerCase()}` : '' const targetHostNameInternal = event.target.pathname && event.target.hostname === window.location.hostname - const eventIsAnchor = event.target.localName === 'a' const prefixMatchPath = sitePrefix.length > 1 ? event.target.pathname.startsWith(sitePrefix) : true - if (targetHostNameInternal && eventIsAnchor && prefixMatchPath) { + if (targetHostNameInternal && prefixMatchPath) { event.preventDefault() let navigatePathname = event.target.pathname + event.target.search