Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace react-router with @reach/router #6918

Merged
merged 27 commits into from
Aug 6, 2018
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
af1227c
Get @reach/router working probably in development
KyleAMathews Jul 31, 2018
e0f2bec
Moer stuff working
KyleAMathews Aug 1, 2018
091ce37
Don't support to as an object
KyleAMathews Aug 1, 2018
7a6c868
Add back support for activeClassName & activeStyle
KyleAMathews Aug 1, 2018
48e3733
Fix path for RouteHandlers
KyleAMathews Aug 1, 2018
874acf1
Pull in parse-path util from react-router
KyleAMathews Aug 1, 2018
a31706a
remove console.logs and add TODO
KyleAMathews Aug 2, 2018
596796b
Merge remote-tracking branch 'origin/master' into reach-router
KyleAMathews Aug 2, 2018
9b7e648
Remove now unused webpack rules to trim down react-router
KyleAMathews Aug 2, 2018
fed26d1
Some fixes from merge
KyleAMathews Aug 2, 2018
65e7d86
Fix problems identified in review earlier
KyleAMathews Aug 3, 2018
9db958c
Remove old typescript definitions
KyleAMathews Aug 3, 2018
75ebf29
Merge remote-tracking branch 'origin/master' into reach-router
KyleAMathews Aug 4, 2018
96c1c92
Restore and update the <Link> documentation for v2/@reach/router
KyleAMathews Aug 4, 2018
33583a2
Also set the className when the site is active per @pieh's advice
KyleAMathews Aug 4, 2018
cee482f
Upgrade client-only-paths example site
KyleAMathews Aug 4, 2018
ad0b949
Upgrade simple-auth example site
KyleAMathews Aug 4, 2018
11753c0
Fix lint errors
KyleAMathews Aug 4, 2018
197ed0e
Merge remote-tracking branch 'origin/master' into reach-router
KyleAMathews Aug 6, 2018
7c61690
Migration docs
KyleAMathews Aug 6, 2018
8c0f084
Note that can't use relative routes w/ @reach/router
KyleAMathews Aug 6, 2018
97ff092
Fix/remove tests that are irrelevant now
KyleAMathews Aug 6, 2018
01b9959
Fix imports
KyleAMathews Aug 6, 2018
4c7e80c
Use v2 version of children for layout
KyleAMathews Aug 6, 2018
bc7035c
mini typos
m-allanson Aug 6, 2018
ea00a10
Merge remote-tracking branch 'origin/master' into reach-router
KyleAMathews Aug 6, 2018
06d9c77
Document that history prop no longer passed to page components
KyleAMathews Aug 6, 2018
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
8 changes: 0 additions & 8 deletions packages/gatsby-link/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
import * as React from "react";
import { NavLinkProps } from "react-router-dom";
import { LocationDescriptor } from "history";

export interface GatsbyLinkProps extends NavLinkProps {
onClick?: (event: any) => void
className?: string
style?:any;
}

export const push: (to: LocationDescriptor) => void;
export const replace: (to: LocationDescriptor) => void;
Expand Down
77 changes: 30 additions & 47 deletions packages/gatsby-link/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*global __PATH_PREFIX__ */
import PropTypes from "prop-types"
import React from "react"
import { Link, NavLink } from "react-router-dom"
import { Link, Location } from "@reach/router"
import { polyfill } from "react-lifecycles-compat"
import { createLocation, createPath } from "history"
import { parsePath } from "gatsby"

export function withPrefix(path) {
return normalizePath(`${__PATH_PREFIX__}/${path}`)
Expand All @@ -16,10 +16,6 @@ function normalizePath(path) {
const NavLinkPropTypes = {
activeClassName: PropTypes.string,
activeStyle: PropTypes.object,
exact: PropTypes.bool,
strict: PropTypes.bool,
isActive: PropTypes.func,
location: PropTypes.object,
}

// Set up IntersectionObserver
Expand Down Expand Up @@ -50,36 +46,26 @@ class GatsbyLink extends React.Component {
IOSupported = true
}

const { location } = context.router.history
const to = createLocation(props.to, null, null, location)
const { location } = props

this.state = {
path: createPath(to),
to,
IOSupported,
location,
}
this.handleRef = this.handleRef.bind(this)
}

static getDerivedStateFromProps(nextProps, prevState) {
if (prevState.to === nextProps.to) return null
const to = createLocation(nextProps.to, null, null, prevState.location)
const path = createPath(to)
return { path, to }
}

componentDidUpdate(prevProps, prevState) {
// Preserve non IO functionality if no support
if (this.props.to !== prevProps.to && !this.state.IOSupported) {
___loader.enqueue(this.state.path)
___loader.enqueue(parsePath(this.props.to).pathname)
}
}

componentDidMount() {
// Preserve non IO functionality if no support
if (!this.state.IOSupported) {
___loader.enqueue(this.state.to.pathname)
___loader.enqueue(parsePath(this.props.to).pathname)
}
}

Expand All @@ -89,26 +75,33 @@ class GatsbyLink extends React.Component {
if (this.state.IOSupported && ref) {
// If IO supported and element reference found, setup Observer functionality
handleIntersection(ref, () => {
___loader.enqueue(this.state.to.pathname)
___loader.enqueue(parsePath(this.props.to).pathname)
})
}
}

render() {
const { onClick, onMouseEnter, ...rest } = this.props
let El
if (Object.keys(NavLinkPropTypes).some(propName => this.props[propName])) {
El = NavLink
const { onClick, onMouseEnter, location, ...rest } = this.props
let getProps
if (this.props.getProps) {
getProps = this.props.getProps
} else {
El = Link
getProps = ({ isCurrent }) =>
isCurrent
? {
className: this.props.activeClassName,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to add default className (similiar how styles are merged in line below)
react-router's NavLink does that - https://github.com/ReactTraining/react-router/blob/master/packages/react-router-dom/modules/NavLink.js#L41-L42 if we want to maintain backward compatibility

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah good catch, fixed in 33583a2

style: { ...this.props.style, ...this.props.activeStyle },
}
: null
}

return (
<El
<Link
getProps={getProps}
onMouseEnter={e => {
// eslint-disable-line
onMouseEnter && onMouseEnter(e)
___loader.hovering(this.state.path)
___loader.hovering(parsePath(this.props.to).pathname)
}}
onClick={e => {
// eslint-disable-line
Expand All @@ -123,44 +116,30 @@ class GatsbyLink extends React.Component {
!e.ctrlKey &&
!e.shiftKey
) {
e.preventDefault()
// Is this link pointing to a hash on the same page? If so,
// just scroll there.
let pathname = this.state.path
if (pathname.split(`#`).length > 1) {
pathname = pathname
.split(`#`)
.slice(0, -1)
.join(``)
}
const { pathname, hash } = parsePath(this.props.to)
if (pathname === window.location.pathname) {
const hashFragment = this.state.path
.split(`#`)
.slice(1)
.join(`#`)
const element = hashFragment
? document.getElementById(hashFragment)
: null
const element = hash ? document.getElementById(hash) : null
if (element !== null) {
element.scrollIntoView()
return true
} else {
// This is just a normal link to the current page so let's emulate default
// browser behavior by scrolling now to the top of the page.
window.scrollTo(0, 0)
return true
}
}

// Make sure the necessary scripts and data are
// loaded before continuing.
e.preventDefault()
window.___push(this.state.to)
window.___push(this.props.to)
}

return true
}}
{...rest}
to={this.state.to}
to={this.props.to}
innerRef={this.handleRef}
/>
)
Expand All @@ -178,7 +157,11 @@ GatsbyLink.contextTypes = {
router: PropTypes.object,
}

export default polyfill(GatsbyLink)
const withLocation = Comp => props => (
<Location>{location => <Comp location={location} {...props} />}</Location>
)

export default withLocation(polyfill(GatsbyLink))

export const push = to => {
window.___push(to)
Expand Down
14 changes: 6 additions & 8 deletions packages/gatsby-react-router-scroll/src/ScrollBehaviorContext.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import React from "react"
import { withRouter } from "react-router-dom"
import ScrollBehavior from "scroll-behavior"
import PropTypes from "prop-types"
import { globalHistory as history } from "@reach/router/lib/history"

import SessionStorage from "./StateStorage"

const propTypes = {
shouldUpdateScroll: PropTypes.func,
children: PropTypes.element.isRequired,
location: PropTypes.object.isRequired,
history: PropTypes.object.isRequired,
}

const childContextTypes = {
Expand All @@ -20,8 +19,6 @@ class ScrollContext extends React.Component {
constructor(props, context) {
super(props, context)

const { history } = props

this.scrollBehavior = new ScrollBehavior({
addTransitionHook: history.listen,
stateStorage: new SessionStorage(),
Expand All @@ -39,21 +36,22 @@ class ScrollContext extends React.Component {
}

componentDidUpdate(prevProps) {
const { location, history } = this.props
const { location } = this.props
const prevLocation = prevProps.location

if (location === prevLocation) {
return
}

const prevRouterProps = {
history: prevProps.history,
location: prevProps.location,
}

// The "scroll-behavior" package expects the "action" to be on the location
// object so let's copy it over.
location.action = history.action
if (!location.action) {
location.action = `push`
}
this.scrollBehavior.updateScroll(prevRouterProps, { history, location })
}

Expand Down Expand Up @@ -101,4 +99,4 @@ class ScrollContext extends React.Component {
ScrollContext.propTypes = propTypes
ScrollContext.childContextTypes = childContextTypes

export default withRouter(ScrollContext)
export default ScrollContext
46 changes: 12 additions & 34 deletions packages/gatsby/cache-dir/find-page.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// TODO add tests especially for handling prefixed links.
import { matchPath } from "react-router-dom"
import { match as matchPath } from "@reach/router/lib/utils"
import stripPrefix from "./strip-prefix"

const pageCache = {}
Expand Down Expand Up @@ -34,40 +34,18 @@ export default (pages, pathPrefix = ``) => rawPathname => {
// Array.prototype.find is not supported in IE so we use this somewhat odd
// work around.
pages.some(page => {
if (page.matchPath) {
// Try both the path and matchPath
if (
matchPath(trimmedPathname, { path: page.path }) ||
matchPath(trimmedPathname, {
path: page.matchPath,
})
) {
foundPage = page
pageCache[trimmedPathname] = page
return true
}
} else {
if (
matchPath(trimmedPathname, {
path: page.path,
exact: true,
})
) {
foundPage = page
pageCache[trimmedPathname] = page
return true
}
let pathToMatch = page.matchPath ? page.matchPath : page.path
if (matchPath(pathToMatch, trimmedPathname)) {
foundPage = page
pageCache[trimmedPathname] = page
return true
}

// Finally, try and match request with default document.
if (
matchPath(trimmedPathname, {
path: page.path + `index.html`,
})
) {
foundPage = page
pageCache[trimmedPathname] = page
return true
}
// Finally, try and match request with default document.
if (matchPath(`${page.path}index.html`, trimmedPathname)) {
foundPage = page
pageCache[trimmedPathname] = page
return true
}

return false
Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby/cache-dir/gatsby-browser-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from "react"
import PropTypes from "prop-types"
import Link, { withPrefix, push, replace, navigateTo } from "gatsby-link"
import PageRenderer from "./public-page-renderer"
import parsePath from "./parse-path"

const StaticQueryContext = React.createContext({})

Expand Down Expand Up @@ -42,6 +43,7 @@ export {
Link,
withPrefix,
graphql,
parsePath,
push,
replace,
navigateTo, // TODO: remove navigateTo for v3
Expand Down
10 changes: 0 additions & 10 deletions packages/gatsby/cache-dir/history.js

This file was deleted.

Loading