Skip to content

Commit 1c7df59

Browse files
author
Ruslan Matkovskyi
committed
CHANGELOG has been changed
1 parent a8a44b2 commit 1c7df59

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ Our versioning strategy is as follows:
1515

1616
* `[nextjs][sitecore-jss-nextjs]` Support for Component Library feature in XMCloud ([#1987](https://github.com/Sitecore/jss/pull/1987))
1717

18+
### 🐛 Bug Fixes
19+
20+
* `[sitecore-jss-nextjs]` Fixed handling of ? inside square brackets [] in regex patterns to prevent incorrect escaping ([#1999](https://github.com/Sitecore/jss/pull/1999))
21+
1822
## 22.3.0 / 22.3.1
1923

2024
### 🐛 Bug Fixes

packages/sitecore-jss-nextjs/src/middleware/redirects-middleware.ts

+20-18
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,9 @@ export class RedirectsMiddleware extends MiddlewareBase {
200200
? modifyRedirects.find((redirect: RedirectInfo & { matchedQueryString?: string }) => {
201201
// Modify the redirect pattern to ignore the language prefix in the path
202202
// And escapes non-special "?" characters in a string or regex.
203-
redirect.pattern = this.escapeNonSpecialQuestionMarks(redirect.pattern.replace(RegExp(`^[^]?/${language}/`, 'gi'), ''));
203+
redirect.pattern = this.escapeNonSpecialQuestionMarks(
204+
redirect.pattern.replace(RegExp(`^[^]?/${language}/`, 'gi'), '')
205+
);
204206

205207
// Prepare the redirect pattern as a regular expression, making it more flexible for matching URLs
206208
redirect.pattern = `/^\/${redirect.pattern
@@ -365,9 +367,9 @@ export class RedirectsMiddleware extends MiddlewareBase {
365367

366368
/**
367369
* Escapes non-special "?" characters in a string or regex.
368-
*
370+
*
369371
* - For regular strings, it escapes all unescaped "?" characters by adding a backslash (`\`).
370-
* - For regex patterns (strings enclosed in `/.../`), it analyzes each "?" to determine if it has special meaning
372+
* - For regex patterns (strings enclosed in `/.../`), it analyzes each "?" to determine if it has special meaning
371373
* (e.g., `?` in `(abc)?`, `.*?`) or is just a literal character. Only literal "?" characters are escaped.
372374
* @param {string} input - The input string or regex pattern.
373375
* @returns {string} - The modified string or regex with non-special "?" characters escaped.
@@ -377,8 +379,8 @@ export class RedirectsMiddleware extends MiddlewareBase {
377379
const isRegex = input.startsWith('/') && input.endsWith('/'); // Check if the string is a regex
378380

379381
if (!isRegex) {
380-
// If not a regex, escape all unescaped "?" characters
381-
return input.replace(regexPattern, '\\?');
382+
// If not a regex, escape all unescaped "?" characters
383+
return input.replace(regexPattern, '\\?');
382384
}
383385

384386
// If it's a regex, analyze each "?" character
@@ -387,21 +389,21 @@ export class RedirectsMiddleware extends MiddlewareBase {
387389

388390
let match;
389391
while ((match = regexPattern.exec(input)) !== null) {
390-
const index = match.index; // Position of "?" in the string
391-
const before = input.slice(0, index).replace(/\s+$/, ''); // Context before "?"
392-
const lastChar = before.slice(-1); // Last character before "?"
392+
const index = match.index; // Position of "?" in the string
393+
const before = input.slice(0, index).replace(/\s+$/, ''); // Context before "?"
394+
const lastChar = before.slice(-1); // Last character before "?"
393395

394-
// Determine if the "?" is a special regex symbol
395-
const isSpecialRegexSymbol = /[\.\*\+\)\[\]]$/.test(lastChar);
396+
// Determine if the "?" is a special regex symbol
397+
const isSpecialRegexSymbol = /[\.\*\+\)\[\]]$/.test(lastChar);
396398

397-
if (isSpecialRegexSymbol) {
398-
// If it's special, keep it as is
399-
result += input.slice(lastIndex, index + 1);
400-
} else {
401-
// If it's not special, escape it
402-
result += input.slice(lastIndex, index) + '\\?';
403-
}
404-
lastIndex = index + 1;
399+
if (isSpecialRegexSymbol) {
400+
// If it's special, keep it as is
401+
result += input.slice(lastIndex, index + 1);
402+
} else {
403+
// If it's not special, escape it
404+
result += input.slice(lastIndex, index) + '\\?';
405+
}
406+
lastIndex = index + 1;
405407
}
406408

407409
// Append the remaining part of the string

0 commit comments

Comments
 (0)