Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions gatsby-browser.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,33 @@

import 'react-tooltip/dist/react-tooltip.css'

const siteOrigins = ['https://csv.js.org']

const isExternalHttpLink = (href) => {
try {
const url = new URL(href, window.location.href)
const origins = [window.location.origin, ...siteOrigins]
return ['http:', 'https:'].includes(url.protocol) && !origins.includes(url.origin)
} catch (e) {
return false
}
}

const addNoopener = (anchor) => {
const rel = new Set((anchor.getAttribute('rel') || '').split(/\s+/).filter(Boolean))
rel.add('noopener')
anchor.setAttribute('rel', Array.from(rel).join(' '))
}

const openExternalLinksInNewTab = () => {
document.querySelectorAll('a[href]').forEach((anchor) => {
if (!isExternalHttpLink(anchor.href)) {
return
}
anchor.setAttribute('target', '_blank')
addNoopener(anchor)
})
}

export const onInitialClientRender = openExternalLinksInNewTab
export const onRouteUpdate = openExternalLinksInNewTab
Loading