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-04-12 08:33:22 +00:00
|
|
|
import { execOnce, warn, 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-04-12 08:33:22 +00:00
|
|
|
static displayName = 'App'
|
|
|
|
|
|
|
|
static async getInitialProps ({ Component, router, ctx }) {
|
|
|
|
const pageProps = await loadGetInitialProps(Component, ctx)
|
|
|
|
return {pageProps}
|
|
|
|
}
|
|
|
|
|
2016-10-05 23:52:50 +00:00
|
|
|
static childContextTypes = {
|
2018-04-12 08:33:22 +00:00
|
|
|
_containerProps: PropTypes.any,
|
2017-08-30 14:07:12 +00:00
|
|
|
headManager: PropTypes.object,
|
|
|
|
router: PropTypes.object
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
|
|
|
|
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-04-18 16:18:06 +00:00
|
|
|
componentDidCatch (err, info) {
|
|
|
|
// To provide clearer stacktraces in error-debug.js in development
|
|
|
|
// To provide clearer stacktraces in app.js in production
|
|
|
|
err.info = info
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV === 'production') {
|
|
|
|
// In production we render _error.js
|
|
|
|
window.next.renderError({err})
|
|
|
|
} else {
|
|
|
|
// In development we throw the error up to AppContainer from react-hot-loader
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate () {
|
|
|
|
this.scrollToHash()
|
|
|
|
}
|
|
|
|
|
|
|
|
scrollToHash () {
|
|
|
|
const { hash } = this.props
|
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
|
|
|
}
|
|
|
|
|
2017-01-12 01:58:20 +00:00
|
|
|
shouldComponentUpdate (nextProps) {
|
|
|
|
// need this check not to rerender component which has already thrown an error
|
|
|
|
return !shallowEquals(this.props, nextProps)
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
2018-04-12 08:33:22 +00:00
|
|
|
const {children} = this.props
|
2018-04-18 16:18:06 +00:00
|
|
|
return <>{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-04-24 22:25:45 +00:00
|
|
|
warn(`Warning: the 'url' property is deprecated. https://err.sh/next.js/url-deprecated`)
|
|
|
|
}
|
|
|
|
})
|
2018-04-12 08:33:22 +00:00
|
|
|
|
|
|
|
export function createUrl (router) {
|
2017-01-12 01:58:20 +00:00
|
|
|
return {
|
2018-04-12 08:33:22 +00:00
|
|
|
get query () {
|
|
|
|
warnUrl()
|
|
|
|
return router.query
|
|
|
|
},
|
|
|
|
get pathname () {
|
|
|
|
warnUrl()
|
|
|
|
return router.pathname
|
|
|
|
},
|
|
|
|
get asPath () {
|
|
|
|
warnUrl()
|
|
|
|
return router.asPath
|
|
|
|
},
|
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
|
|
|
}
|
|
|
|
}
|