@sitecore-jss/sitecore-jss-nextjs 22.4.0-canary.5 → 22.4.0-canary.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -159,13 +159,13 @@ class RedirectsMiddleware extends middleware_1.MiddlewareBase {
159
159
  return modifyRedirects.length
160
160
  ? modifyRedirects.find((redirect) => {
161
161
  // Modify the redirect pattern to ignore the language prefix in the path
162
- redirect.pattern = redirect.pattern.replace(RegExp(`^[^]?/${language}/`, 'gi'), '');
162
+ // And escapes non-special "?" characters in a string or regex.
163
+ redirect.pattern = this.escapeNonSpecialQuestionMarks(redirect.pattern.replace(RegExp(`^[^]?/${language}/`, 'gi'), ''));
163
164
  // Prepare the redirect pattern as a regular expression, making it more flexible for matching URLs
164
165
  redirect.pattern = `/^\/${redirect.pattern
165
166
  .replace(/^\/|\/$/g, '') // Removes leading and trailing slashes
166
167
  .replace(/^\^\/|\/\$$/g, '') // Removes unnecessary start (^) and end ($) anchors
167
168
  .replace(/^\^|\$$/g, '') // Further cleans up anchors
168
- .replace(/(?<!\\)\?/g, '\\?') // Escapes question marks in the pattern
169
169
  .replace(/\$\/gi$/g, '')}[\/]?$/i`; // Ensures the pattern allows an optional trailing slash
170
170
  /**
171
171
  * This line checks whether the current URL query string (and all its possible permutations)
@@ -288,5 +288,45 @@ class RedirectsMiddleware extends middleware_1.MiddlewareBase {
288
288
  (0, regex_parser_1.default)(pattern).test(`/${locale}${normalizedPath}${query}`),
289
289
  ].some(Boolean));
290
290
  }
291
+ /**
292
+ * Escapes non-special "?" characters in a string or regex.
293
+ *
294
+ * - For regular strings, it escapes all unescaped "?" characters by adding a backslash (`\`).
295
+ * - For regex patterns (strings enclosed in `/.../`), it analyzes each "?" to determine if it has special meaning
296
+ * (e.g., `?` in `(abc)?`, `.*?`) or is just a literal character. Only literal "?" characters are escaped.
297
+ * @param {string} input - The input string or regex pattern.
298
+ * @returns {string} - The modified string or regex with non-special "?" characters escaped.
299
+ **/
300
+ escapeNonSpecialQuestionMarks(input) {
301
+ const regexPattern = /(?<!\\)\?/g; // Find unescaped "?" characters
302
+ const isRegex = input.startsWith('/') && input.endsWith('/'); // Check if the string is a regex
303
+ if (!isRegex) {
304
+ // If not a regex, escape all unescaped "?" characters
305
+ return input.replace(regexPattern, '\\?');
306
+ }
307
+ // If it's a regex, analyze each "?" character
308
+ let result = '';
309
+ let lastIndex = 0;
310
+ let match;
311
+ while ((match = regexPattern.exec(input)) !== null) {
312
+ const index = match.index; // Position of "?" in the string
313
+ const before = input.slice(0, index).replace(/\s+$/, ''); // Context before "?"
314
+ const lastChar = before.slice(-1); // Last character before "?"
315
+ // Determine if the "?" is a special regex symbol
316
+ const isSpecialRegexSymbol = /[\.\*\+\)\[\]]$/.test(lastChar);
317
+ if (isSpecialRegexSymbol) {
318
+ // If it's special, keep it as is
319
+ result += input.slice(lastIndex, index + 1);
320
+ }
321
+ else {
322
+ // If it's not special, escape it
323
+ result += input.slice(lastIndex, index) + '\\?';
324
+ }
325
+ lastIndex = index + 1;
326
+ }
327
+ // Append the remaining part of the string
328
+ result += input.slice(lastIndex);
329
+ return result;
330
+ }
291
331
  }
292
332
  exports.RedirectsMiddleware = RedirectsMiddleware;
@@ -153,13 +153,13 @@ export class RedirectsMiddleware extends MiddlewareBase {
153
153
  return modifyRedirects.length
154
154
  ? modifyRedirects.find((redirect) => {
155
155
  // Modify the redirect pattern to ignore the language prefix in the path
156
- redirect.pattern = redirect.pattern.replace(RegExp(`^[^]?/${language}/`, 'gi'), '');
156
+ // And escapes non-special "?" characters in a string or regex.
157
+ redirect.pattern = this.escapeNonSpecialQuestionMarks(redirect.pattern.replace(RegExp(`^[^]?/${language}/`, 'gi'), ''));
157
158
  // Prepare the redirect pattern as a regular expression, making it more flexible for matching URLs
158
159
  redirect.pattern = `/^\/${redirect.pattern
159
160
  .replace(/^\/|\/$/g, '') // Removes leading and trailing slashes
160
161
  .replace(/^\^\/|\/\$$/g, '') // Removes unnecessary start (^) and end ($) anchors
161
162
  .replace(/^\^|\$$/g, '') // Further cleans up anchors
162
- .replace(/(?<!\\)\?/g, '\\?') // Escapes question marks in the pattern
163
163
  .replace(/\$\/gi$/g, '')}[\/]?$/i`; // Ensures the pattern allows an optional trailing slash
164
164
  /**
165
165
  * This line checks whether the current URL query string (and all its possible permutations)
@@ -282,4 +282,44 @@ export class RedirectsMiddleware extends MiddlewareBase {
282
282
  regexParser(pattern).test(`/${locale}${normalizedPath}${query}`),
283
283
  ].some(Boolean));
284
284
  }
285
+ /**
286
+ * Escapes non-special "?" characters in a string or regex.
287
+ *
288
+ * - For regular strings, it escapes all unescaped "?" characters by adding a backslash (`\`).
289
+ * - For regex patterns (strings enclosed in `/.../`), it analyzes each "?" to determine if it has special meaning
290
+ * (e.g., `?` in `(abc)?`, `.*?`) or is just a literal character. Only literal "?" characters are escaped.
291
+ * @param {string} input - The input string or regex pattern.
292
+ * @returns {string} - The modified string or regex with non-special "?" characters escaped.
293
+ **/
294
+ escapeNonSpecialQuestionMarks(input) {
295
+ const regexPattern = /(?<!\\)\?/g; // Find unescaped "?" characters
296
+ const isRegex = input.startsWith('/') && input.endsWith('/'); // Check if the string is a regex
297
+ if (!isRegex) {
298
+ // If not a regex, escape all unescaped "?" characters
299
+ return input.replace(regexPattern, '\\?');
300
+ }
301
+ // If it's a regex, analyze each "?" character
302
+ let result = '';
303
+ let lastIndex = 0;
304
+ let match;
305
+ while ((match = regexPattern.exec(input)) !== null) {
306
+ const index = match.index; // Position of "?" in the string
307
+ const before = input.slice(0, index).replace(/\s+$/, ''); // Context before "?"
308
+ const lastChar = before.slice(-1); // Last character before "?"
309
+ // Determine if the "?" is a special regex symbol
310
+ const isSpecialRegexSymbol = /[\.\*\+\)\[\]]$/.test(lastChar);
311
+ if (isSpecialRegexSymbol) {
312
+ // If it's special, keep it as is
313
+ result += input.slice(lastIndex, index + 1);
314
+ }
315
+ else {
316
+ // If it's not special, escape it
317
+ result += input.slice(lastIndex, index) + '\\?';
318
+ }
319
+ lastIndex = index + 1;
320
+ }
321
+ // Append the remaining part of the string
322
+ result += input.slice(lastIndex);
323
+ return result;
324
+ }
285
325
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sitecore-jss/sitecore-jss-nextjs",
3
- "version": "22.4.0-canary.5",
3
+ "version": "22.4.0-canary.6",
4
4
  "main": "dist/cjs/index.js",
5
5
  "module": "dist/esm/index.js",
6
6
  "sideEffects": false,
@@ -73,9 +73,9 @@
73
73
  "react-dom": "^18.2.0"
74
74
  },
75
75
  "dependencies": {
76
- "@sitecore-jss/sitecore-jss": "^22.4.0-canary.5",
77
- "@sitecore-jss/sitecore-jss-dev-tools": "^22.4.0-canary.5",
78
- "@sitecore-jss/sitecore-jss-react": "^22.4.0-canary.5",
76
+ "@sitecore-jss/sitecore-jss": "^22.4.0-canary.6",
77
+ "@sitecore-jss/sitecore-jss-dev-tools": "^22.4.0-canary.6",
78
+ "@sitecore-jss/sitecore-jss-react": "^22.4.0-canary.6",
79
79
  "@vercel/kv": "^0.2.1",
80
80
  "prop-types": "^15.8.1",
81
81
  "regex-parser": "^2.2.11",
@@ -83,7 +83,7 @@
83
83
  },
84
84
  "description": "",
85
85
  "types": "types/index.d.ts",
86
- "gitHead": "3b5b7802c66adebfc32ffce00ad32cbf4f6657ee",
86
+ "gitHead": "7b4705679a487ac6a21b5a53a55cdb5da5c20904",
87
87
  "files": [
88
88
  "dist",
89
89
  "types",
@@ -65,4 +65,14 @@ export declare class RedirectsMiddleware extends MiddlewareBase {
65
65
  * @returns {string | undefined} - return query string if any of the query permutations match the provided pattern, undefined otherwise.
66
66
  */
67
67
  private isPermutedQueryMatch;
68
+ /**
69
+ * Escapes non-special "?" characters in a string or regex.
70
+ *
71
+ * - For regular strings, it escapes all unescaped "?" characters by adding a backslash (`\`).
72
+ * - For regex patterns (strings enclosed in `/.../`), it analyzes each "?" to determine if it has special meaning
73
+ * (e.g., `?` in `(abc)?`, `.*?`) or is just a literal character. Only literal "?" characters are escaped.
74
+ * @param {string} input - The input string or regex pattern.
75
+ * @returns {string} - The modified string or regex with non-special "?" characters escaped.
76
+ **/
77
+ private escapeNonSpecialQuestionMarks;
68
78
  }