From d4bb5a16ee406d4af1c99ade48dbb4d14b2ce5c6 Mon Sep 17 00:00:00 2001 From: Ruslan Matkovskyi Date: Tue, 6 Feb 2024 09:39:47 +0100 Subject: [PATCH 1/2] [sitecore-jss-nextjs]: #60701 the redirects middleware has been fixed when there is using absolute url in target --- .../src/middleware/redirects-middleware.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/sitecore-jss-nextjs/src/middleware/redirects-middleware.ts b/packages/sitecore-jss-nextjs/src/middleware/redirects-middleware.ts index 203e83cf11..a17ecd4474 100644 --- a/packages/sitecore-jss-nextjs/src/middleware/redirects-middleware.ts +++ b/packages/sitecore-jss-nextjs/src/middleware/redirects-middleware.ts @@ -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 @@ -98,7 +99,10 @@ 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 @@ -106,9 +110,8 @@ export class RedirectsMiddleware extends MiddlewareBase { } 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 { From df91a9983853b0cef9cdbc0f99b3f165aaf8b990 Mon Sep 17 00:00:00 2001 From: Ruslan Matkovskyi Date: Tue, 6 Feb 2024 09:48:12 +0100 Subject: [PATCH 2/2] =?UTF-8?q?=EF=BB=BF=20[sitecore-jss-nextjs]:=20#60760?= =?UTF-8?q?1=20fixed=20unit=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + .../src/middleware/redirects-middleware.ts | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4463c8c569..81324117aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ Our versioning strategy is as follows: * `[templates/nextjs-sxa]` Fix feature `show Grid column` in Experience Editor. ([#1704](https://github.com/Sitecore/jss/pull/1704)) * `[sitecore-jss-nextjs] [templates/nextjs-xmcloud]` SDK initialization rejections are now correctly handled. Errors should no longer occur after getSDK() promises resolve when they shouldn't (for example, getting Events SDK in development environment) ([#1712](https://github.com/Sitecore/jss/pull/1712) [#1715](https://github.com/Sitecore/jss/pull/1715) [#1716](https://github.com/Sitecore/jss/pull/1716)) * `[sitecore-jss-nextjs]` Remove custom loader function i.e. `sitecoreLoader` to enable NextImage to use built-in image optimization from vercel. ([#1726](https://github.com/Sitecore/jss/pull/1726)) +* `[sitecore-jss-nextjs]` Fix redirects middleware for working with absolute url where is using site language context ([#1727](https://github.com/Sitecore/jss/pull/1727)) ### 🛠 Breaking Changes diff --git a/packages/sitecore-jss-nextjs/src/middleware/redirects-middleware.ts b/packages/sitecore-jss-nextjs/src/middleware/redirects-middleware.ts index a17ecd4474..63b212cb84 100644 --- a/packages/sitecore-jss-nextjs/src/middleware/redirects-middleware.ts +++ b/packages/sitecore-jss-nextjs/src/middleware/redirects-middleware.ts @@ -12,7 +12,7 @@ import { import { debug } from '@sitecore-jss/sitecore-jss'; import { MiddlewareBase, MiddlewareBaseConfig } from './middleware'; -const REGEXP_CONTEXT_SITE_LANG = new RegExp(/\$siteLang/, 'gi'); +const REGEXP_CONTEXT_SITE_LANG = new RegExp(/\$siteLang/, 'i'); const REGEXP_ABSOLUTE_URL = new RegExp('^(?:[a-z]+:)?//', 'i'); /** @@ -101,7 +101,10 @@ export class RedirectsMiddleware extends MiddlewareBase { // Find context site language and replace token if ( REGEXP_CONTEXT_SITE_LANG.test(existsRedirect.target) && - !REGEXP_ABSOLUTE_URL.test(existsRedirect.target) + !( + REGEXP_ABSOLUTE_URL.test(existsRedirect.target) && + existsRedirect.target.includes(hostname) + ) ) { existsRedirect.target = existsRedirect.target.replace( REGEXP_CONTEXT_SITE_LANG,