Skip to content

Commit

Permalink
Improve handling of href links.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgegorka committed Nov 27, 2020
1 parent 9d0bc5c commit e03f594
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Svelte Router changelog

## 5.8.1

Improve handling of href links.

## 5.8.0

Added prefix param to constrain routes.
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion src/components/navigate.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
export let styles = ''
export let lang = null
onMount(function() {
onMount(function () {
if (lang) {
const route = localisedRoute(to, lang)
if (route) {
Expand All @@ -16,6 +16,7 @@
})
function navigate(event) {
if (event.metaKey || event.ctrlKey || event.shiftKey) return
event.preventDefault()
event.stopPropagation()
navigateTo(to)
Expand Down
6 changes: 4 additions & 2 deletions src/spa_router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit e03f594

Please sign in to comment.