From ef1ac680d59d0019adc26cd3b82d81e9a3a0aa7b Mon Sep 17 00:00:00 2001 From: VarunVAshrit <159520405+VarunVAshrit@users.noreply.github.com> Date: Wed, 30 Oct 2024 18:13:51 +0530 Subject: [PATCH] :zap: Set brazil ssl to radix ssl automation #2539 (#2608) --- web/common/helpers/redirects.ts | 7 +++++++ web/middleware.ts | 9 +++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/web/common/helpers/redirects.ts b/web/common/helpers/redirects.ts index f16a023e6..5d460936a 100644 --- a/web/common/helpers/redirects.ts +++ b/web/common/helpers/redirects.ts @@ -9,6 +9,13 @@ export const getExternalRedirectUrl = async (slug: string, locale: string): Prom return getClient(false).fetch(externalRedirects, { slug: slug, slugWithLocale: `/${locale}${slug}` }) } +export const getWWWRedirect = (host: string, pathname: string): string | undefined => { + if (!host.includes("www")) { + return `https://www.${host}${pathname}`; + } + return undefined; +}; + export const getDnsRedirect = (host: string, pathname: string) => { const dns = host.replace('http://', '').replace('https://', '').replace('www.', '') diff --git a/web/middleware.ts b/web/middleware.ts index bc9154d1f..50022a9b2 100644 --- a/web/middleware.ts +++ b/web/middleware.ts @@ -1,5 +1,5 @@ /* eslint-disable @typescript-eslint/ban-ts-comment */ -import { getRedirectUrl, getDnsRedirect, getExternalRedirectUrl } from './common/helpers/redirects' +import { getRedirectUrl, getDnsRedirect, getExternalRedirectUrl, getWWWRedirect } from './common/helpers/redirects' import { NextRequest, NextResponse } from 'next/server' import { getLocaleFromName } from './lib/localization' import { Flags } from './common/helpers/datasetHelpers' @@ -50,7 +50,6 @@ export async function middleware(request: NextRequest) { if ((PUBLIC_FILE.test(pathname) && !isDotHtml) || pathname.includes('/api/')) { return undefined } - // Check if it is a DNS redirect const host = String(request.headers.get('host')) const dnsRedirect = getDnsRedirect(host, pathname) @@ -58,6 +57,12 @@ export async function middleware(request: NextRequest) { return NextResponse.redirect(dnsRedirect, PERMANENT_REDIRECT) } + // Check if it is a WWW redirect + const wwwRedirect = getWWWRedirect(host, pathname) + if (wwwRedirect) { + return NextResponse.redirect(wwwRedirect, PERMANENT_REDIRECT) + } + // Redirect external links to news which is now archived if link doesn't exist in Sanity if (Flags.HAS_ARCHIVED_NEWS && pathname.startsWith('/news') && !pathname.startsWith('/news/archive')) { const existsInSanity = await pathExistsInSanity(pathname, isPreview)