Skip to content

Commit

Permalink
🎨 codesplit
Browse files Browse the repository at this point in the history
  • Loading branch information
BorghildSelle committed Feb 16, 2024
1 parent b0a1cbd commit 4223a48
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 88 deletions.
89 changes: 1 addition & 88 deletions web/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import withBundleAnalyzer from '@next/bundle-analyzer'
import nextTranspileModules from 'next-transpile-modules'
import { dataset, defaultLanguage, domain, languages } from './languages.js'
import securityHeaders from './securityHeaders.js'
import { createClient } from '@sanity/client'
import { sanityConfig } from './sanityServerClient.js'
import { getAllRedirects } from './redirects.js'

const withTM = nextTranspileModules(['friendly-challenge'])

Expand All @@ -15,14 +14,6 @@ const withBundle = withBundleAnalyzer({

const locales = languages.map((lang) => lang.locale)

/**
* @param {string} name
* @returns string
*/
export const getLocaleFromName = (name) => {
return languages.find((lang) => lang.name === name)?.locale || defaultLanguage.locale
}

const getPageExtensions = (dataset) => {
const extensions = ['tsx', 'ts', 'js', 'jsx']
const globalOnlyExtensions = ['global.tsx', 'global.ts']
Expand All @@ -41,84 +32,6 @@ const getPageExtensions = (dataset) => {

const pageExtensions = getPageExtensions(dataset)

const externalRedirectGroq = /* groq */ `
*[_type == "externalRedirect" && !(_id in path('drafts.**'))]{
from,
to
}
`

const redirectsGroq = /* groq */ `
*[_type == "redirect" && !(_id in path('drafts.**'))]{
"lang": _lang,
from,
"to": to->slug.current
}
`

const getExternalRedirects = async () => {
const result = await createClient(sanityConfig).fetch(externalRedirectGroq)
const externalRedirects = result
.filter((e) => e)
.map((externalRedirect) => {
return {
source: externalRedirect.from,
permanent: true,
destination: externalRedirect.to,
}
})
return [
...externalRedirects,
// Redirect IE users to not-supported page
{
source: '/',
has: [
{
type: 'header',
key: 'user-agent',
value: '.*(MSIE|Trident).*',
},
],
permanent: true,
destination: '/not-supported.html',
},
// redirects for /50 site
['global', 'global-development', 'global-test'].includes(dataset) && {
source: '/50/en/:slug*',
destination: '/magazine',
permanent: true,
},
['global', 'global-development', 'global-test'].includes(dataset) && {
source: '/50/:slug*',
destination: '/no/magasin',
permanent: true,
},
]
}

const getInternalRedirects = async () => {
const result = await createClient(sanityConfig).fetch(redirectsGroq)
const redirects = result
.filter((e) => e)
.map((redirect) => {
const to = redirect.to === '/' ? '' : redirect.to
const locale = '/' + getLocaleFromName(redirect.lang)
const nextRedirect = {
source: redirect.from,
destination: `${locale}${to}`,
permanent: true,
}
return redirect.from.startsWith(locale) ? { ...nextRedirect, locale: false } : nextRedirect
})

return [...redirects]
}

const getAllRedirects = async () => {
const result = await Promise.all([getExternalRedirects(), getInternalRedirects()])
return result.flat()
}

export default withBundle(
withTM({
output: 'standalone',
Expand Down
89 changes: 89 additions & 0 deletions web/redirects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { createClient } from '@sanity/client'
import { sanityConfig } from './sanityServerClient.js'
import { dataset, languages, defaultLanguage } from './languages.js'

/**
* @param {string} name
* @returns string
*/
export const getLocaleFromName = (name) => {
return languages.find((lang) => lang.name === name)?.locale || defaultLanguage.locale
}

const externalRedirectGroq = /* groq */ `
*[_type == "externalRedirect" && !(_id in path('drafts.**'))]{
from,
to
}
`

const redirectsGroq = /* groq */ `
*[_type == "redirect" && !(_id in path('drafts.**'))]{
"lang": _lang,
from,
"to": to->slug.current
}
`

const getExternalRedirects = async () => {
const result = await createClient(sanityConfig).fetch(externalRedirectGroq)
const externalRedirects = result
.filter((e) => e)
.map((externalRedirect) => {
return {
source: externalRedirect.from,
permanent: true,
destination: externalRedirect.to,
}
})
return [
...externalRedirects,
// Redirect IE users to not-supported page
{
source: '/',
has: [
{
type: 'header',
key: 'user-agent',
value: '.*(MSIE|Trident).*',
},
],
permanent: true,
destination: '/not-supported.html',
},
// redirects for /50 site
['global', 'global-development', 'global-test'].includes(dataset) && {
source: '/50/en/:slug*',
destination: '/magazine',
permanent: true,
},
['global', 'global-development', 'global-test'].includes(dataset) && {
source: '/50/:slug*',
destination: '/no/magasin',
permanent: true,
},
]
}

const getInternalRedirects = async () => {
const result = await createClient(sanityConfig).fetch(redirectsGroq)
const redirects = result
.filter((e) => e)
.map((redirect) => {
const to = redirect.to === '/' ? '' : redirect.to
const locale = '/' + getLocaleFromName(redirect.lang)
const nextRedirect = {
source: redirect.from,
destination: `${locale}${to}`,
permanent: true,
}
return redirect.from.startsWith(locale) ? { ...nextRedirect, locale: false } : nextRedirect
})

return [...redirects]
}

export const getAllRedirects = async () => {
const result = await Promise.all([getExternalRedirects(), getInternalRedirects()])
return result.flat()
}
1 change: 1 addition & 0 deletions web/sanityServerClient.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Needed as vanillajs when use in next.config.js to collect redirects
export const sanityConfig = {
dataset: process.env.NEXT_PUBLIC_SANITY_DATASET || 'global',
projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID || 'h61q9gi9',
Expand Down

0 comments on commit 4223a48

Please sign in to comment.