Skip to content

Commit 0ccca9b

Browse files
authored
Merge pull request #4 from tywmick/extend-client-side
Extend client-side redirects
2 parents e237af1 + b33e7bd commit 0ccca9b

File tree

4 files changed

+41
-10
lines changed

4 files changed

+41
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"url": "https://github.com/pablopunk/nextjs-redirect"
5353
},
5454
"scripts": {
55-
"build": "sucrase src -d dist --transforms imports,typescript && npm run generate-types && npm run prettier",
55+
"build": "sucrase src -d dist --transforms imports,typescript,jsx && npm run generate-types && npm run prettier",
5656
"generate-types": "dts-bundle-generator -o typings.d.ts src/index.tsx",
5757
"prettier": "prettier src/*.tsx ./typings.d.ts --write",
5858
"prepare": "npm run build"

src/index.tsx

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,55 @@
11
import React from 'react'
22
import Router from 'next/router'
3+
import Head from 'next/head'
34

45
export default (
56
redirectUrl: string,
67
options?: { asUrl?: string; statusCode?: number }
78
) =>
89
class extends React.Component {
10+
// Redirects on the server side first if possible
911
static async getInitialProps({ res }) {
10-
if (res) {
12+
if (res?.writeHead) {
1113
res.writeHead(options?.statusCode ?? 301, { Location: redirectUrl })
1214
res.end()
13-
} else {
14-
if (options?.asUrl != null) {
15-
Router.push(redirectUrl, options.asUrl, { shallow: true })
16-
} else {
17-
Router.push(redirectUrl)
18-
}
1915
}
20-
2116
return {}
2217
}
18+
19+
// Redirects on the client with JavaScript if no server
20+
componentDidMount() {
21+
if (options?.asUrl != null) {
22+
Router.push(redirectUrl, options.asUrl, { shallow: true })
23+
} else if (redirectUrl[0] === '/') {
24+
Router.push(redirectUrl)
25+
} else {
26+
window.location.href = redirectUrl
27+
}
28+
}
29+
2330
render() {
24-
return React.createElement('div')
31+
const href = options?.asUrl ?? redirectUrl
32+
return (
33+
<>
34+
<Head
35+
children={
36+
<>
37+
{/* Redirects with meta refresh if no JavaScript support */}
38+
<noscript>
39+
<meta httpEquiv="refresh" content={`0;url=${href}`} />
40+
</noscript>
41+
{(options?.statusCode === undefined ||
42+
options?.statusCode === 301) && (
43+
<link rel="canonical" href={href} />
44+
)}
45+
</>
46+
}
47+
/>
48+
{/* Provides a redirect link if no meta refresh support */}
49+
<p>
50+
Redirecting to <a href={href}>{href}</a>&hellip;
51+
</p>
52+
</>
53+
)
2554
}
2655
}

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"compilerOptions": {
3+
"jsx": "react",
34
"target": "es5",
45
"lib": ["dom", "dom.iterable", "esnext"],
56
"skipLibCheck": true,

typings.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ declare const _default: (
88
}
99
) => {
1010
new (): {
11+
componentDidMount(): void
1112
render(): any
1213
}
1314
getInitialProps({ res }: { res: any }): Promise<{}>

0 commit comments

Comments
 (0)