Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/timneutkens/next-universal-redirect
Higher order component to handle both client and server side redirecting with Next.js
https://github.com/timneutkens/next-universal-redirect
javascript nextjs react
Last synced: 21 days ago
JSON representation
Higher order component to handle both client and server side redirecting with Next.js
- Host: GitHub
- URL: https://github.com/timneutkens/next-universal-redirect
- Owner: timneutkens
- Created: 2017-02-21T21:31:26.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-02-22T21:15:21.000Z (almost 8 years ago)
- Last Synced: 2024-12-21T09:40:01.857Z (23 days ago)
- Topics: javascript, nextjs, react
- Language: JavaScript
- Homepage:
- Size: 1.95 KB
- Stars: 13
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Next.js universal redirect
Higher order component to handle both client and server side redirecting with [Next.js](https://github.com/zeit/next.js)
Note: this package is made for Next.js 2.0+
Example: [with-universal-redirect.now.sh](https://with-universal-redirect.now.sh/)
## Installation
`npm install --save next-universal-redirect`
## Usage
Create a `_error.js` file in the `pages` directory of your project
`pages/_error.js`:
```js
import ErrorComponent from 'next/error'
import redirect from 'next-universal-redirect'// This renders the default Next.js error/404 component
class Error extends React.Component {
static getInitialProps ({ req, res, xhr, pathname }) {
const statusCode = res ? res.statusCode : (xhr ? xhr.status : null)
return { statusCode }
}render () {
return
}
}// Create a map of redirects
const list = new Map()
list.set('/example', '/example2')// Calling `rewrite(list)()` will return a HOC that will match the provided list against incoming page requests and will redirect accordingly.
export default redirect(list)(Error)
```