From 2d7899af3ba9001a68ccee59246e76434065c76b Mon Sep 17 00:00:00 2001 From: Anton Kechashin Date: Thu, 19 May 2022 17:16:32 +0300 Subject: [PATCH] #530555: [SXA][Headless] Redirects with defined language in source URL don't work (#1023) * #530555: [SXA][Headless] Redirects with defined language in source URL don't work * #530555: Avoid importing NextURL * #530555: Move locales property inside RedirectsMiddleware instead of RedirectsPlugin * #530555: Move locales property inside RedirectsMiddlewareConfig --- .../src/lib/middleware/plugins/redirects.ts | 2 ++ .../src/edge/redirects-middleware.ts | 28 +++++++++++++------ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/packages/create-sitecore-jss/src/templates/nextjs-sxa/src/lib/middleware/plugins/redirects.ts b/packages/create-sitecore-jss/src/templates/nextjs-sxa/src/lib/middleware/plugins/redirects.ts index 9b971794db..583c632090 100644 --- a/packages/create-sitecore-jss/src/templates/nextjs-sxa/src/lib/middleware/plugins/redirects.ts +++ b/packages/create-sitecore-jss/src/templates/nextjs-sxa/src/lib/middleware/plugins/redirects.ts @@ -2,6 +2,7 @@ import { NextRequest, NextResponse } from 'next/server'; import { RedirectsMiddleware } from '@sitecore-jss/sitecore-jss-nextjs/edge'; import config from 'temp/config'; import { MiddlewarePlugin } from '..'; +import nextConfig from '../../../../next.config'; class RedirectsPlugin implements MiddlewarePlugin { private redirectsMiddleware: RedirectsMiddleware; @@ -12,6 +13,7 @@ class RedirectsPlugin implements MiddlewarePlugin { endpoint: config.graphQLEndpoint, apiKey: config.sitecoreApiKey, siteName: config.jssAppName, + locales: nextConfig().i18n.locales, }); } diff --git a/packages/sitecore-jss-nextjs/src/edge/redirects-middleware.ts b/packages/sitecore-jss-nextjs/src/edge/redirects-middleware.ts index fdafae2f24..66c7e7b018 100644 --- a/packages/sitecore-jss-nextjs/src/edge/redirects-middleware.ts +++ b/packages/sitecore-jss-nextjs/src/edge/redirects-middleware.ts @@ -12,14 +12,16 @@ import { /** * extended RedirectsMiddlewareConfig config type for RedirectsMiddleware */ -export type RedirectsMiddlewareConfig = Omit; - +export type RedirectsMiddlewareConfig = Omit & { + locales: string[]; +}; /** * Middleware / handler fetches all redirects from Sitecore instance by grapqhl service * compares with current url and redirects to target url */ export class RedirectsMiddleware { private redirectsService: GraphQLRedirectsService; + private locales: string[]; /** * NOTE: we provide native fetch for compatibility on Next.js Edge Runtime @@ -28,6 +30,7 @@ export class RedirectsMiddleware { */ constructor(config: RedirectsMiddlewareConfig) { this.redirectsService = new GraphQLRedirectsService({ ...config, fetch: fetch }); + this.locales = config.locales; } /** @@ -39,17 +42,22 @@ export class RedirectsMiddleware { } private handler = async (req: NextRequest): Promise => { - const url = req.nextUrl.clone(); // Find the redirect from result of RedirectService - - const existsRedirect = await this.getExistsRedirect(url); + const existsRedirect = await this.getExistsRedirect(req); if (!existsRedirect) { return NextResponse.next(); } + const url = req.nextUrl.clone(); url.search = existsRedirect.isQueryStringPreserved ? url.search : ''; - url.pathname = existsRedirect.target; + const urlFirstPart = existsRedirect.target.split('/')[1]; + if (this.locales.includes(urlFirstPart)) { + url.locale = urlFirstPart; + url.pathname = existsRedirect.target.replace(`/${urlFirstPart}`, ''); + } else { + url.pathname = existsRedirect.target; + } const redirectUrl = decodeURIComponent(url.href); /** return Response redirect with http code of redirect type **/ @@ -71,11 +79,13 @@ export class RedirectsMiddleware { * @returns Promise * @private */ - private async getExistsRedirect(url: URL): Promise { + private async getExistsRedirect(req: NextRequest): Promise { const redirects = await this.redirectsService.fetchRedirects(); - return redirects.find((redirect: RedirectInfo) => - regexParser(redirect.pattern).test(url.pathname) + return redirects.find( + (redirect: RedirectInfo) => + regexParser(redirect.pattern).test(req.nextUrl.pathname) || + regexParser(redirect.pattern).test(`/${req.nextUrl.locale}${req.nextUrl.pathname}`) ); } }