2017-04-10 18:35:26 +00:00
|
|
|
import React, { Component } from 'react'
|
|
|
|
import PropTypes from 'prop-types'
|
2017-01-12 01:58:20 +00:00
|
|
|
import shallowEquals from './shallow-equals'
|
2018-09-27 19:10:53 +00:00
|
|
|
import { execOnce, loadGetInitialProps } from './utils'
|
2017-08-30 14:07:12 +00:00
|
|
|
import { makePublicRouterInstance } from './router'
|
2016-10-05 23:52:50 +00:00
|
|
|
|
|
|
|
export default class App extends Component {
|
2018-08-07 03:53:06 +00:00
|
|
|
static childContextTypes = {
|
|
|
|
_containerProps: PropTypes.any,
|
|
|
|
headManager: PropTypes.object,
|
|
|
|
router: PropTypes.object
|
|
|
|
}
|
|
|
|
|
2018-04-12 08:33:22 +00:00
|
|
|
static displayName = 'App'
|
|
|
|
|
|
|
|
static async getInitialProps ({ Component, router, ctx }) {
|
|
|
|
const pageProps = await loadGetInitialProps(Component, ctx)
|
|
|
|
return {pageProps}
|
|
|
|
}
|
|
|
|
|
2017-01-12 01:58:20 +00:00
|
|
|
getChildContext () {
|
|
|
|
const { headManager } = this.props
|
2017-08-30 14:07:12 +00:00
|
|
|
return {
|
|
|
|
headManager,
|
2018-04-12 08:33:22 +00:00
|
|
|
router: makePublicRouterInstance(this.props.router),
|
2018-04-18 16:18:06 +00:00
|
|
|
_containerProps: {...this.props}
|
2017-08-30 14:07:12 +00:00
|
|
|
}
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 09:47:29 +00:00
|
|
|
// Kept here for backwards compatibility.
|
|
|
|
// When someone ended App they could call `super.componentDidCatch`. This is now deprecated.
|
|
|
|
componentDidCatch (err) {
|
|
|
|
throw err
|
2017-09-27 19:09:16 +00:00
|
|
|
}
|
|
|
|
|
2017-01-12 01:58:20 +00:00
|
|
|
render () {
|
2018-04-12 08:33:22 +00:00
|
|
|
const {router, Component, pageProps} = this.props
|
2017-03-06 16:48:35 +00:00
|
|
|
const url = createUrl(router)
|
2018-04-12 08:33:22 +00:00
|
|
|
return <Container>
|
2018-04-22 20:20:24 +00:00
|
|
|
<Component {...pageProps} url={url} />
|
2018-04-12 08:33:22 +00:00
|
|
|
</Container>
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
2017-01-12 01:58:20 +00:00
|
|
|
}
|
2016-10-05 23:52:50 +00:00
|
|
|
|
2018-04-12 08:33:22 +00:00
|
|
|
export class Container extends Component {
|
|
|
|
static contextTypes = {
|
|
|
|
_containerProps: PropTypes.any
|
|
|
|
}
|
|
|
|
|
2017-02-28 17:31:17 +00:00
|
|
|
componentDidMount () {
|
|
|
|
this.scrollToHash()
|
|
|
|
}
|
|
|
|
|
2018-08-07 03:53:06 +00:00
|
|
|
shouldComponentUpdate (nextProps) {
|
|
|
|
// need this check not to rerender component which has already thrown an error
|
|
|
|
return !shallowEquals(this.props, nextProps)
|
|
|
|
}
|
|
|
|
|
2017-02-28 17:31:17 +00:00
|
|
|
componentDidUpdate () {
|
|
|
|
this.scrollToHash()
|
|
|
|
}
|
|
|
|
|
|
|
|
scrollToHash () {
|
2018-08-11 20:04:16 +00:00
|
|
|
const { hash } = this.context._containerProps
|
2017-06-09 21:45:37 +00:00
|
|
|
if (!hash) return
|
|
|
|
|
2017-02-28 17:31:17 +00:00
|
|
|
const el = document.getElementById(hash)
|
2017-06-09 21:45:37 +00:00
|
|
|
if (!el) return
|
|
|
|
|
|
|
|
// If we call scrollIntoView() in here without a setTimeout
|
|
|
|
// it won't scroll properly.
|
|
|
|
setTimeout(() => el.scrollIntoView(), 0)
|
2017-02-28 17:31:17 +00:00
|
|
|
}
|
|
|
|
|
2016-10-05 23:52:50 +00:00
|
|
|
render () {
|
2018-08-11 20:04:16 +00:00
|
|
|
return this.props.children
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-24 22:25:45 +00:00
|
|
|
const warnUrl = execOnce(() => {
|
2018-04-25 16:23:39 +00:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2018-09-27 19:10:53 +00:00
|
|
|
console.error(`Warning: the 'url' property is deprecated. https://err.sh/zeit/next.js/url-deprecated`)
|
2018-04-24 22:25:45 +00:00
|
|
|
}
|
|
|
|
})
|
2018-04-12 08:33:22 +00:00
|
|
|
|
|
|
|
export function createUrl (router) {
|
2018-05-12 18:10:17 +00:00
|
|
|
// This is to make sure we don't references the router object at call time
|
|
|
|
const {pathname, asPath, query} = router
|
2017-01-12 01:58:20 +00:00
|
|
|
return {
|
2018-04-12 08:33:22 +00:00
|
|
|
get query () {
|
|
|
|
warnUrl()
|
2018-05-12 18:10:17 +00:00
|
|
|
return query
|
2018-04-12 08:33:22 +00:00
|
|
|
},
|
|
|
|
get pathname () {
|
|
|
|
warnUrl()
|
2018-05-12 18:10:17 +00:00
|
|
|
return pathname
|
2018-04-12 08:33:22 +00:00
|
|
|
},
|
|
|
|
get asPath () {
|
|
|
|
warnUrl()
|
2018-05-12 18:10:17 +00:00
|
|
|
return asPath
|
2018-04-12 08:33:22 +00:00
|
|
|
},
|
2017-03-06 18:14:10 +00:00
|
|
|
back: () => {
|
2018-04-12 08:33:22 +00:00
|
|
|
warnUrl()
|
2017-03-06 18:14:10 +00:00
|
|
|
router.back()
|
|
|
|
},
|
|
|
|
push: (url, as) => {
|
2018-04-12 08:33:22 +00:00
|
|
|
warnUrl()
|
2017-03-06 18:14:10 +00:00
|
|
|
return router.push(url, as)
|
|
|
|
},
|
|
|
|
pushTo: (href, as) => {
|
2018-04-12 08:33:22 +00:00
|
|
|
warnUrl()
|
2017-03-06 18:14:10 +00:00
|
|
|
const pushRoute = as ? href : null
|
|
|
|
const pushUrl = as || href
|
|
|
|
|
|
|
|
return router.push(pushRoute, pushUrl)
|
|
|
|
},
|
|
|
|
replace: (url, as) => {
|
2018-04-12 08:33:22 +00:00
|
|
|
warnUrl()
|
2017-03-06 18:14:10 +00:00
|
|
|
return router.replace(url, as)
|
|
|
|
},
|
|
|
|
replaceTo: (href, as) => {
|
2018-04-12 08:33:22 +00:00
|
|
|
warnUrl()
|
2017-03-06 18:14:10 +00:00
|
|
|
const replaceRoute = as ? href : null
|
|
|
|
const replaceUrl = as || href
|
|
|
|
|
|
|
|
return router.replace(replaceRoute, replaceUrl)
|
|
|
|
}
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
|
|
|
}
|