Skip to content

Commit

Permalink
[sitecore-jss-nextjs]: #60701 the redirects middleware has been fixe…
Browse files Browse the repository at this point in the history
…d when there is using absolute url in target
  • Loading branch information
Ruslan Matkovskyi committed Feb 6, 2024
1 parent 601430f commit 1933ae7
Showing 1 changed file with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { debug } from '@sitecore-jss/sitecore-jss';
import { MiddlewareBase, MiddlewareBaseConfig } from './middleware';

const REGEXP_CONTEXT_SITE_LANG = new RegExp(/\$siteLang/, 'gi');
const REGEXP_ABSOLUTE_URL = new RegExp('^(?:[a-z]+:)?//', 'i');

/**
* extended RedirectsMiddlewareConfig config type for RedirectsMiddleware
Expand Down Expand Up @@ -98,17 +99,19 @@ export class RedirectsMiddleware extends MiddlewareBase {
}

// Find context site language and replace token
if (REGEXP_CONTEXT_SITE_LANG.test(existsRedirect.target)) {
if (
REGEXP_CONTEXT_SITE_LANG.test(existsRedirect.target) &&
!REGEXP_ABSOLUTE_URL.test(existsRedirect.target)
) {
existsRedirect.target = existsRedirect.target.replace(
REGEXP_CONTEXT_SITE_LANG,
site.language
);
}

const url = req.nextUrl.clone();
const absoluteUrlRegex = new RegExp('^(?:[a-z]+:)?//', 'i');

if (absoluteUrlRegex.test(existsRedirect.target)) {
if (REGEXP_ABSOLUTE_URL.test(existsRedirect.target)) {
url.href = existsRedirect.target;
url.locale = req.nextUrl.locale;
} else {
Expand Down

0 comments on commit 1933ae7

Please sign in to comment.