Skip to content

Commit

Permalink
Fix console error before applying a frontend route redirect (badges#3176
Browse files Browse the repository at this point in the history
)
  • Loading branch information
paulmelnikow authored and calebcartwright committed Mar 8, 2019
1 parent 3fc9b81 commit b288c99
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 17 deletions.
16 changes: 1 addition & 15 deletions frontend/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,2 @@
import React from 'react'
import Main from '../components/main'
import redirectLegacyRoutes from '../lib/redirect-legacy-routes'

export default class IndexPage extends React.Component {
render() {
// It seems like putting this in `componentDidMount()` should work.
// however, that does not seem to be called often enough, resulting in the
// redirect sometimes not occurring.
if (typeof window !== 'undefined') {
redirectLegacyRoutes()
}

return <Main {...this.props} />
}
}
export default Main
7 changes: 5 additions & 2 deletions gatsby-browser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Adapted from https://github.com/gatsbyjs/gatsby/issues/8413
import redirectLegacyRoutes from './frontend/lib/redirect-legacy-routes'

// Adapted from https://github.com/gatsbyjs/gatsby/issues/8413
function scrollToElementId(id) {
const el = document.querySelector(id)
if (el) {
Expand All @@ -11,6 +12,8 @@ function scrollToElementId(id) {

export function onRouteUpdate({ location: { hash } }) {
if (hash) {
window.setTimeout(() => scrollToElementId(hash), 10)
if (!redirectLegacyRoutes()) {
window.setTimeout(() => scrollToElementId(hash), 10)
}
}
}

0 comments on commit b288c99

Please sign in to comment.