Skip to content

Commit

Permalink
Use fork of catch-links to avoid its dependency on the url module whi…
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleAMathews committed May 9, 2017
1 parent cd0ae5f commit 26bde96
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
3 changes: 0 additions & 3 deletions packages/gatsby-plugin-catch-links/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
],
"author": "Kyle Mathews <[email protected]>",
"license": "MIT",
"dependencies": {
"catch-links": "^2.0.1"
},
"devDependencies": {
"babel-cli": "^6.24.1"
}
Expand Down
45 changes: 45 additions & 0 deletions packages/gatsby-plugin-catch-links/src/catch-links.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module.exports = function(root, cb) {
root.addEventListener(`click`, function(ev) {
if (
ev.altKey ||
ev.ctrlKey ||
ev.metaKey ||
ev.shiftKey ||
ev.defaultPrevented
) {
return true
}

var anchor = null
for (var n = ev.target; n.parentNode; n = n.parentNode) {
if (n.nodeName === `A`) {
anchor = n
break
}
}
if (!anchor) return true

// IE clears the host value if the anchor href changed after creation, e.g.
// in React. Creating a new anchor element to ensure host value is present
var a1 = document.createElement(`a`)
a1.href = anchor.href

// In IE, the default port is included in the anchor host but excluded from
// the location host. This affects the ability to directly compare
// location host to anchor host. For example: http://example.com would
// have a location.host of 'example.com' and an anchor.host of
// 'example.com:80' Creating anchor from the location.href to normalize the
// host value.
var a2 = document.createElement(`a`)
a2.href = location.href

if (a1.host !== a2.host) return true

ev.preventDefault()

var base = location.protocol + `//` + location.host

cb(anchor.getAttribute(`href`))
return false
})
}
11 changes: 8 additions & 3 deletions packages/gatsby-plugin-catch-links/src/gatsby-browser.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import catchLinks from "catch-links"
import browserHistory from "react-router/lib/browserHistory"
import catchLinks from "./catch-links"
import createHistory from "history/createBrowserHistory"

const history = createHistory()
console.log(history)
window.gatsby_history = history

catchLinks(window, href => {
console.log(href)
browserHistory.push(href)
// TODO figure out why this isn't working...
history.push(href)
})

0 comments on commit 26bde96

Please sign in to comment.