UNPKG

nextjs-redirect

Version:

Redirect to any URL in NextJS both in client and server

100 lines (85 loc) 4.91 kB
"use strict";const _jsxFileName = "src/index.tsx";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _router = require('next/router'); var _router2 = _interopRequireDefault(_router); var _head = require('next/head'); var _head2 = _interopRequireDefault(_head); const PARAMS_ERROR = "Option {params: true} require the url to be the name of the param to search for: `redirect('to', {params:true})` will work with `/redirect?to=https://example.com`" const getParamFromClient = (paramName) => { if (typeof window === 'undefined') { return '' } const url = new URL(window.location.href) const paramValue = url.searchParams.get(paramName) if (!paramValue) { throw new Error(PARAMS_ERROR) } return paramValue } exports. default = ( redirectUrl, options ) => class extends _react2.default.Component { // Redirects on the server side first if possible static async getInitialProps({ res, query }) { if (_optionalChain([res, 'optionalAccess', _ => _.writeHead])) { let url = redirectUrl if (_optionalChain([options, 'optionalAccess', _2 => _2.params]) === true) { const param = redirectUrl if (!_optionalChain([query, 'optionalAccess', _3 => _3[param]])) { throw new Error(PARAMS_ERROR) } url = query[param] } res.writeHead(_nullishCoalesce(_optionalChain([options, 'optionalAccess', _4 => _4.statusCode]), () => ( 301)), { Location: url }) res.end() } else if (typeof window !== 'undefined') { let url = redirectUrl if (_optionalChain([options, 'optionalAccess', _5 => _5.params]) === true) { url = getParamFromClient(url) } window.location.href = url } return {} } // Redirects on the client with JavaScript if no server componentDidMount() { if (_optionalChain([options, 'optionalAccess', _6 => _6.params]) === true) { window.location.href = getParamFromClient(redirectUrl) } else if (_optionalChain([options, 'optionalAccess', _7 => _7.asUrl]) != null) { _router2.default.push(redirectUrl, options.asUrl, { shallow: true }) } else if (redirectUrl[0] === '/') { _router2.default.push(redirectUrl) } else { window.location.href = redirectUrl } } render() { let href = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _8 => _8.asUrl]), () => ( redirectUrl)) if (_optionalChain([options, 'optionalAccess', _9 => _9.params]) != null) { href = getParamFromClient(redirectUrl) } return ( _react2.default.createElement(_react2.default.Fragment, null , _react2.default.createElement(_head2.default, {__self: this, __source: {fileName: _jsxFileName, lineNumber: 76}} /* Redirects with meta refresh if no JavaScript support */ , _react2.default.createElement('noscript', {__self: this, __source: {fileName: _jsxFileName, lineNumber: 78}} , _react2.default.createElement('meta', { httpEquiv: "refresh", content: `0;url=${href}`, __self: this, __source: {fileName: _jsxFileName, lineNumber: 79}} ) ) , (_optionalChain([options, 'optionalAccess', _10 => _10.statusCode]) === undefined || _optionalChain([options, 'optionalAccess', _11 => _11.statusCode]) === 301) && ( _react2.default.createElement('link', { rel: "canonical", href: href, __self: this, __source: {fileName: _jsxFileName, lineNumber: 83}} ) ) ) /* Provides a redirect link if no meta refresh support; or children if provided */ , this.props.children ? ( this.props.children ) : ( _react2.default.createElement('p', {__self: this, __source: {fileName: _jsxFileName, lineNumber: 90}}, "Redirecting to " , _react2.default.createElement('a', { href: href, __self: this, __source: {fileName: _jsxFileName, lineNumber: 91}}, href), "…" ) ) ) ) } } const getParamFromURL = (url, param) => {}