All Projects → pablopunk → nextjs-redirect

pablopunk / nextjs-redirect

Licence: MIT license
HOC to redirect to any URL in NextJS both in client and server

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects
HTML
75241 projects

Projects that are alternatives of or similar to nextjs-redirect

Json Flatfile Datastore
Simple JSON flat file data store with support for typed and dynamic data.
Stars: ✭ 212 (+47.22%)
Mutual labels:  dynamic
github-readme-activity-graph
A dynamically generated activity graph to show your GitHub activities of last 31 days.
Stars: ✭ 1,029 (+614.58%)
Mutual labels:  dynamic
KDynamic
An Alternative to MacOS Mojave Dynamic Theme and Wallpaper Changer
Stars: ✭ 48 (-66.67%)
Mutual labels:  dynamic
Flexml
🚀基于Litho的Android高性能动态业务容器。
Stars: ✭ 225 (+56.25%)
Mutual labels:  dynamic
Signals.jl
Multi-Paradigm Dynamic Fast Functional Reactive Programming in Julia
Stars: ✭ 37 (-74.31%)
Mutual labels:  dynamic
farside
A smart redirecting gateway for various frontend services
Stars: ✭ 378 (+162.5%)
Mutual labels:  redirect
Wallpapper
💻 Console application for creating dynamic wallpapers for macOS Mojave and newer
Stars: ✭ 2,533 (+1659.03%)
Mutual labels:  dynamic
dynamic-threadpool
📌 强大的动态线程池框架,附带监控报警功能。支持 JDK、Tomcat、Jetty、Undertow 线程池;Dubbo、Dubbox、RabbitMQ、RocketMQ、Hystrix 消费线程池(更多框架线程池还在适配中)。内置两种使用模式:轻量级依赖配置中心以及无中间件依赖版本。
Stars: ✭ 3,609 (+2406.25%)
Mutual labels:  dynamic
react-app-loader
Production ready library for handling Microfrontends
Stars: ✭ 75 (-47.92%)
Mutual labels:  hoc
rrx
⚛️ Minimal React router using higher order components
Stars: ✭ 69 (-52.08%)
Mutual labels:  hoc
Victoire
Fullstack Symfony CMS: The perfect mix between a framework and a CMS
Stars: ✭ 227 (+57.64%)
Mutual labels:  dynamic
rainbow-bash-prompt
Make your bash prompt dynamically and randomly rainbow
Stars: ✭ 49 (-65.97%)
Mutual labels:  dynamic
venusscript
A dynamic, interpreted, scripting language written in Java.
Stars: ✭ 17 (-88.19%)
Mutual labels:  dynamic
Vue Dynamic Form Component
Vue dynamic nested form component, support nested Object/Hashmap/Array. Vue动态多级表单组件,支持嵌套对象/Hashmap/数组。
Stars: ✭ 220 (+52.78%)
Mutual labels:  dynamic
Equinox
🌇 🌃 Create dynamic wallpapers for macOS.
Stars: ✭ 737 (+411.81%)
Mutual labels:  dynamic
Progress dialog
A light weight library to easily manage a progress dialog with simple steps whenever you need to do it. You can easily show and hide it.
Stars: ✭ 196 (+36.11%)
Mutual labels:  dynamic
Ryder
Runtime redirection of method calls for .NET Core.
Stars: ✭ 34 (-76.39%)
Mutual labels:  redirect
scrapy helper
Dynamic configurable crawl (动态可配置化爬虫)
Stars: ✭ 84 (-41.67%)
Mutual labels:  dynamic
react-useragent
Higher order component to add user agent information to your react components
Stars: ✭ 13 (-90.97%)
Mutual labels:  hoc
laravel-livewire-modals
Dynamic Laravel Livewire Bootstrap modals.
Stars: ✭ 63 (-56.25%)
Mutual labels:  dynamic

nextjs-redirect

Redirect to any URL in NextJS both in client and server

Install

npm install nextjs-redirect

Usage

Let's say you want to create a page /donate that redirects the user to paypal.me with a default value for the money. You create the page as you always do in NextJS (pages/donate.js) and then just use this component with the URL you want:

// pages/donate.js
import redirect from 'nextjs-redirect'
export default redirect('https://paypal.me/pablopunk/5')

You can checkout this example live in pablopunk.com

Status code (301, 302...)

By default, it will send a 301 status code. This can be customized by an optional parameter:

redirect('https://google.es', { statusCode: 302 })

Client side dynamic routes (as)

When redirecting on the client side, if the redirected page is dynamic (pages/user/[userId]/info.js), the following redirect will trigger a page refresh:

redirect('/user/42/info')

In this case you can use the asUrl option to make a smooth transition between pages without any refresh:

redirect('/user/[userId]/info', { asUrl: '/user/42/info' })

Static export

This package is compatible with next export since version 4.0.0. See PR #4 for more details.

Custom UI component (HOC)

In case the navigation is happening client-side, you can use this package as a HOC to provide your custom components/styles for the UI:

import redirect from 'nextjs-redirect'

const Redirect = redirect('https://github.com/pablopunk')

export default () => (
  <Redirect>
    <MyLayout>Redirecting to github!</MyLayout>
  </Redirect>
)

Redirect to URL from parameters

Let's say you have a single page called /redirect, and you wanna use it for all kinds of redirects:

  • /redirect?to=https://google.com
  • /redirect?next=https://twitter.com
  • /redirect?url=https://pablopunk.com

Pretty cool huh!? You can do this with nextjs-redirect by passing the name of the parameter you want on the url. For the examples above:

// NOTE: These are 3 separate examples, you can only choose one name per page
redirect('to', {params: true})
redirect('next', {params: true})
redirect('url', {params: true})

Related

Working with locales routes? Take a look at nextjs-redirect-locale.

Native redirects

There's now a native way of handling redirects on NextJS. You can read more about it here. It requires you to modify your next.config.js. Personally I still think nextjs-redirect is a more friendly way of doing it, and also more flexible. For example you can do dynamic redirects on the server, depeding on the request, which is useful when working with locales (checkout nextjs-redirect-locale) and other request-dependent redirects. It also allows you make client redirects with a custom layout.

License

MIT

Author

me
Pablo Varela
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].