1
1
mirror of https://github.com/primer/css.git synced 2024-11-30 19:53:11 +03:00
css/docs/redirect.js
2019-02-04 13:28:09 -08:00

31 lines
738 B
JavaScript

import Router from 'next/router'
/**
* Export this as your default from a page, and it'll redirect both server-
* and client-side:
*
* ```js
* import {redirect} from '../docs/utils'
* export default redirect('/some/path')
* ```
*/
export default function redirect(uri, status = 303) {
// XXX this doesn't need to extend React.Component because
// it doesn't "do" anything React-y
return class {
static getInitialProps({res}) {
// the "context" object passed to getInitialProps() will
// have a "res" (response) object if we're server-side
if (res) {
res.writeHead(status, {Location: uri})
res.end()
}
}
render() {
Router.replace(uri)
return null
}
}
}