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
#1727)

* [sitecore-jss-nextjs]: #60701 the redirects middleware has been fixed when there is using absolute url in target

*  [sitecore-jss-nextjs]: #607601 fixed unit test

---------

Co-authored-by: Ruslan Matkovskyi <[email protected]>
  • Loading branch information
sc-ruslanmatkovskyi and Ruslan Matkovskyi authored Feb 7, 2024
1 parent c7bbfe5 commit 02ab317
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ 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');

/**
* extended RedirectsMiddlewareConfig config type for RedirectsMiddleware
Expand Down Expand Up @@ -98,17 +99,22 @@ 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.includes(hostname)
)
) {
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 02ab317

Please sign in to comment.