-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredirects.js
More file actions
68 lines (56 loc) · 1.78 KB
/
Copy pathredirects.js
File metadata and controls
68 lines (56 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* Enable www ↔ apex redirect only in production-like deploys, not Vercel preview/dev.
* Docker / self-hosted: NODE_ENV=production and no VERCEL_ENV set → enabled.
*/
function shouldEnableCanonicalHostRedirect() {
if (process.env.VERCEL_ENV === 'production') return true
if (process.env.VERCEL_ENV === 'preview' || process.env.VERCEL_ENV === 'development') {
return false
}
return process.env.NODE_ENV === 'production'
}
/** @returns {object | null} */
function getCanonicalHostRedirect() {
if (!shouldEnableCanonicalHostRedirect()) return null
const raw = process.env.NEXT_PUBLIC_SERVER_URL
if (!raw || typeof raw !== 'string') return null
let url
try {
url = new URL(raw.trim())
} catch {
return null
}
const host = url.hostname.toLowerCase()
const localhostHosts = new Set(['localhost', '127.0.0.1', '::1'])
if (localhostHosts.has(host)) return null
const alternateHost = host.startsWith('www.') ? host.slice(4) : `www.${host}`
if (!alternateHost) return null
const canonicalBase = `${url.protocol}//${host}`.replace(/\/$/, '')
return {
source: '/:path*',
has: [{ type: 'host', value: alternateHost }],
destination: `${canonicalBase}/:path*`,
permanent: true,
}
}
const redirects = async () => {
const internetExplorerRedirect = {
destination: '/ie-incompatible.html',
has: [
{
type: 'header',
key: 'user-agent',
value: '(.*Trident.*)', // all ie browsers
},
],
permanent: false,
source: '/:path((?!ie-incompatible.html$).*)', // all pages except the incompatibility page
}
const list = [internetExplorerRedirect]
const canonicalRedirect = getCanonicalHostRedirect()
if (canonicalRedirect) {
list.push(canonicalRedirect)
}
return list
}
export default redirects