forked from payloadcms/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredirects.js
78 lines (62 loc) · 2 KB
/
redirects.js
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
69
70
71
72
73
74
75
76
77
78
import { formatPermalink } from './src/utilities/formatPermalink.js'
export const redirects = async () => {
const staticRedirects = [
{
source: '/docs',
destination: '/docs/getting-started/what-is-payload',
permanent: true,
},
{
source: '/roadmap',
destination: 'https://github.com/payloadcms/payload/discussions/categories/roadmap',
permanent: true,
},
]
const internetExplorerRedirect = {
source: '/:path((?!ie-incompatible.html$).*)', // all pages except the incompatibility page
has: [
{
type: 'header',
key: 'user-agent',
value: '(.*Trident.*)', // all ie browsers
},
],
permanent: false,
destination: '/ie-incompatible.html',
}
const redirectsRes = await fetch(
`${process.env.NEXT_PUBLIC_CMS_URL}/api/redirects?limit=1000&depth=1`,
)
const redirectsData = await redirectsRes.json()
const { docs } = redirectsData
let dynamicRedirects = []
if (docs) {
docs.forEach(doc => {
const { from, to: { type, url, reference } = {} } = doc
let source = from.replace(process.env.NEXT_PUBLIC_SITE_URL, '').split('?')[0].toLowerCase()
if (source.endsWith('/')) source = source.slice(0, -1) // a trailing slash will break this redirect
let destination = '/'
if (type === 'custom' && url) {
destination = url.replace(process.env.NEXT_PUBLIC_SITE_URL, '')
}
if (
type === 'reference' &&
typeof reference.value === 'object' &&
reference?.value?._status === 'published'
) {
destination = `${process.env.NEXT_PUBLIC_SITE_URL}/${formatPermalink(reference)}`
}
const redirect = {
source,
destination,
permanent: true,
}
if (source.startsWith('/') && destination && source !== destination) {
return dynamicRedirects.push(redirect)
}
return
})
}
const redirects = [...staticRedirects, internetExplorerRedirect, ...dynamicRedirects]
return redirects
}