|
| 1 | +import { sanityFetch } from '@/sanity/lib/live' |
| 2 | +import { externalRedirects, redirects } from '@/sanity/queries/redirects' |
| 3 | + |
| 4 | +export const getRedirectUrl = async (slug: string, locale: string) => { |
| 5 | + const { data } = await sanityFetch({ |
| 6 | + query: redirects, |
| 7 | + params: { |
| 8 | + slug: slug, |
| 9 | + slugWithLocale: `/${locale}${slug}`, |
| 10 | + }, |
| 11 | + }) |
| 12 | + return data |
| 13 | +} |
| 14 | + |
| 15 | +export const getExternalRedirectUrl = async (slug: string, locale: string) => { |
| 16 | + const { data } = await sanityFetch({ |
| 17 | + query: externalRedirects, |
| 18 | + params: { |
| 19 | + slug: slug, |
| 20 | + slugWithLocale: `/${locale}${slug}`, |
| 21 | + }, |
| 22 | + }) |
| 23 | + return data |
| 24 | +} |
| 25 | + |
| 26 | +export const getWWWRedirect = ( |
| 27 | + requestLocale: string, |
| 28 | + host: string, |
| 29 | + pathname: string, |
| 30 | +): string | undefined => { |
| 31 | + if (!host.includes('www')) { |
| 32 | + return `https://www.${host}/${requestLocale}${pathname}` |
| 33 | + } |
| 34 | + return undefined |
| 35 | +} |
| 36 | + |
| 37 | +export const getDnsRedirect = (host: string, pathname: string) => { |
| 38 | + const dns = host |
| 39 | + .replace('http://', '') |
| 40 | + .replace('https://', '') |
| 41 | + .replace('www.', '') |
| 42 | + |
| 43 | + if (dns === 'statoil.com') { |
| 44 | + return `https://www.equinor.com${pathname}` |
| 45 | + } |
| 46 | + |
| 47 | + if (dns === 'equinor.kr') { |
| 48 | + return `https://www.equinor.co.kr${pathname}` |
| 49 | + } |
| 50 | + |
| 51 | + const redirect = |
| 52 | + dnsRedirects.find(redirect => redirect.from === dns + pathname) || |
| 53 | + dnsRedirects.find(redirect => redirect.from === dns) |
| 54 | + |
| 55 | + return redirect && `https://www.equinor.com${redirect.to}` |
| 56 | +} |
| 57 | + |
| 58 | +const dnsRedirects = [ |
| 59 | + { |
| 60 | + from: 'equinor.co.uk/mariner', |
| 61 | + to: '/en/energy/mariner', |
| 62 | + }, |
| 63 | + { |
| 64 | + from: 'equinor.co.uk', |
| 65 | + to: '/en/where-we-are/united-kingdom', |
| 66 | + }, |
| 67 | + { |
| 68 | + from: 'equinorpensjon.no', |
| 69 | + to: '/no/om-oss/equinor-pensjon', |
| 70 | + }, |
| 71 | + { |
| 72 | + from: 'statoilpensjon.no', |
| 73 | + to: '/no/om-oss/equinor-pensjon', |
| 74 | + }, |
| 75 | + { |
| 76 | + from: 'equinor.co.tz', |
| 77 | + to: '/en/where-we-are/tanzania', |
| 78 | + }, |
| 79 | + { |
| 80 | + from: 'statoil.co.tz', |
| 81 | + to: '/en/where-we-are/tanzania', |
| 82 | + }, |
| 83 | + { |
| 84 | + from: 'equinor.no', |
| 85 | + to: '/no', |
| 86 | + }, |
| 87 | + { |
| 88 | + from: 'equinorventures.com', |
| 89 | + to: '/en/energy/ventures', |
| 90 | + }, |
| 91 | + { |
| 92 | + from: 'h2hsaltend.co.uk', |
| 93 | + to: '/en/energy/h2h-saltend', |
| 94 | + }, |
| 95 | + { |
| 96 | + from: 'equinor.ca', |
| 97 | + to: '/en/where-we-are/canada', |
| 98 | + }, |
| 99 | + { |
| 100 | + from: 'techstars.equinor.com', |
| 101 | + to: '/en/energy/techstars', |
| 102 | + }, |
| 103 | + { |
| 104 | + from: 'equinor.com.au', |
| 105 | + to: '/en/where-we-are/australia', |
| 106 | + }, |
| 107 | + { |
| 108 | + from: 'digitalfieldworker.equinor.com', |
| 109 | + to: '/energy/digitalfieldworker', |
| 110 | + }, |
| 111 | + { |
| 112 | + from: 'eu2nsea.com', |
| 113 | + to: '/energy/eu2nsea', |
| 114 | + }, |
| 115 | + { |
| 116 | + from: 'eu2nsea.eu', |
| 117 | + to: '/energy/eu2nsea', |
| 118 | + }, |
| 119 | + { |
| 120 | + from: 'morgendagenshelter.no', |
| 121 | + to: '/no/om-oss/sponsing-og-stotte', |
| 122 | + }, |
| 123 | + { |
| 124 | + from: 'data.equinor.com', |
| 125 | + to: '/energy/data-sharing', |
| 126 | + }, |
| 127 | +] |
0 commit comments