@sitecore-jss/sitecore-jss-nextjs 22.2.0-canary.61 → 22.2.0-canary.63
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.
- package/dist/cjs/middleware/index.js +3 -1
- package/dist/cjs/middleware/redirects-middleware.js +63 -8
- package/dist/esm/middleware/index.js +1 -0
- package/dist/esm/middleware/redirects-middleware.js +63 -8
- package/package.json +5 -5
- package/types/middleware/index.d.ts +1 -0
- package/types/middleware/redirects-middleware.d.ts +11 -0
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MultisiteMiddleware = exports.PersonalizeMiddleware = exports.RedirectsMiddleware = exports.debug = void 0;
|
|
3
|
+
exports.MultisiteMiddleware = exports.PersonalizeMiddleware = exports.RedirectsMiddleware = exports.MiddlewareBase = exports.debug = void 0;
|
|
4
4
|
var sitecore_jss_1 = require("@sitecore-jss/sitecore-jss");
|
|
5
5
|
Object.defineProperty(exports, "debug", { enumerable: true, get: function () { return sitecore_jss_1.debug; } });
|
|
6
|
+
var middleware_1 = require("./middleware");
|
|
7
|
+
Object.defineProperty(exports, "MiddlewareBase", { enumerable: true, get: function () { return middleware_1.MiddlewareBase; } });
|
|
6
8
|
var redirects_middleware_1 = require("./redirects-middleware");
|
|
7
9
|
Object.defineProperty(exports, "RedirectsMiddleware", { enumerable: true, get: function () { return redirects_middleware_1.RedirectsMiddleware; } });
|
|
8
10
|
var personalize_middleware_1 = require("./personalize-middleware");
|
|
@@ -15,6 +15,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
15
15
|
exports.RedirectsMiddleware = void 0;
|
|
16
16
|
const sitecore_jss_1 = require("@sitecore-jss/sitecore-jss");
|
|
17
17
|
const site_1 = require("@sitecore-jss/sitecore-jss/site");
|
|
18
|
+
const utils_1 = require("@sitecore-jss/sitecore-jss/utils");
|
|
18
19
|
const server_1 = require("next/server");
|
|
19
20
|
const regex_parser_1 = __importDefault(require("regex-parser"));
|
|
20
21
|
const middleware_1 = require("./middleware");
|
|
@@ -70,7 +71,7 @@ class RedirectsMiddleware extends middleware_1.MiddlewareBase {
|
|
|
70
71
|
url.href = existsRedirect.target;
|
|
71
72
|
}
|
|
72
73
|
else {
|
|
73
|
-
const source = `${url.pathname.replace(/\/*$/gi, '')}${
|
|
74
|
+
const source = `${url.pathname.replace(/\/*$/gi, '')}${existsRedirect.matchedQueryString}`;
|
|
74
75
|
const urlFirstPart = existsRedirect.target.split('/')[1];
|
|
75
76
|
if (this.locales.includes(urlFirstPart)) {
|
|
76
77
|
req.nextUrl.locale = urlFirstPart;
|
|
@@ -156,17 +157,52 @@ class RedirectsMiddleware extends middleware_1.MiddlewareBase {
|
|
|
156
157
|
const modifyRedirects = structuredClone(redirects);
|
|
157
158
|
return modifyRedirects.length
|
|
158
159
|
? modifyRedirects.find((redirect) => {
|
|
160
|
+
// Modify the redirect pattern to ignore the language prefix in the path
|
|
159
161
|
redirect.pattern = redirect.pattern.replace(RegExp(`^[^]?/${language}/`, 'gi'), '');
|
|
162
|
+
// Prepare the redirect pattern as a regular expression, making it more flexible for matching URLs
|
|
160
163
|
redirect.pattern = `/^\/${redirect.pattern
|
|
161
|
-
.replace(/^\/|\/$/g, '')
|
|
162
|
-
.replace(/^\^\/|\/\$$/g, '')
|
|
163
|
-
.replace(/^\^|\$$/g, '')
|
|
164
|
-
.replace(/(?<!\\)\?/g, '\\?')
|
|
165
|
-
.replace(/\$\/gi$/g, '')}[\/]?$/
|
|
164
|
+
.replace(/^\/|\/$/g, '') // Removes leading and trailing slashes
|
|
165
|
+
.replace(/^\^\/|\/\$$/g, '') // Removes unnecessary start (^) and end ($) anchors
|
|
166
|
+
.replace(/^\^|\$$/g, '') // Further cleans up anchors
|
|
167
|
+
.replace(/(?<!\\)\?/g, '\\?') // Escapes question marks in the pattern
|
|
168
|
+
.replace(/\$\/gi$/g, '')}[\/]?$/i`; // Ensures the pattern allows an optional trailing slash
|
|
169
|
+
/**
|
|
170
|
+
* This line checks whether the current URL query string (and all its possible permutations)
|
|
171
|
+
* matches the redirect pattern.
|
|
172
|
+
*
|
|
173
|
+
* Query parameters in URLs can come in different orders, but logically they represent the
|
|
174
|
+
* same information (e.g., "key1=value1&key2=value2" is the same as "key2=value2&key1=value1").
|
|
175
|
+
* To account for this, the method `isPermutedQueryMatch` generates all possible permutations
|
|
176
|
+
* of the query parameters and checks if any of those permutations match the regex pattern for the redirect.
|
|
177
|
+
*
|
|
178
|
+
* NOTE: This fix is specifically implemented for Netlify, where query parameters are sometimes
|
|
179
|
+
* automatically sorted, which can cause issues with matching redirects if the order of query
|
|
180
|
+
* parameters is important. By checking every possible permutation, we ensure that redirects
|
|
181
|
+
* work correctly on Netlify despite this behavior.
|
|
182
|
+
*
|
|
183
|
+
* It passes several pieces of information to the function:
|
|
184
|
+
* 1. `pathname`: The normalized URL path without query parameters (e.g., '/about').
|
|
185
|
+
* 2. `queryString`: The current query string from the URL, which will be permuted and matched (e.g., '?key1=value1&key2=value2').
|
|
186
|
+
* 3. `pattern`: The regex pattern for the redirect that we are trying to match against the URL (e.g., '/about?key1=value1').
|
|
187
|
+
* 4. `locale`: The locale part of the URL (if any), which helps support multilingual URLs.
|
|
188
|
+
*
|
|
189
|
+
* If one of the permutations of the query string matches the redirect pattern, the function
|
|
190
|
+
* returns the matched query string, which is stored in `matchedQueryString`. If no match is found,
|
|
191
|
+
* it returns `undefined`. The `matchedQueryString` is later used to indicate whether the query
|
|
192
|
+
* string contributed to a successful redirect match.
|
|
193
|
+
*/
|
|
194
|
+
const matchedQueryString = this.isPermutedQueryMatch({
|
|
195
|
+
pathname: tragetURL,
|
|
196
|
+
queryString: targetQS,
|
|
197
|
+
pattern: redirect.pattern,
|
|
198
|
+
locale: req.nextUrl.locale,
|
|
199
|
+
});
|
|
200
|
+
// Save the matched query string (if found) into the redirect object
|
|
201
|
+
redirect.matchedQueryString = matchedQueryString || '';
|
|
202
|
+
// Return the redirect if the URL path or any query string permutation matches the pattern
|
|
166
203
|
return (((0, regex_parser_1.default)(redirect.pattern).test(tragetURL) ||
|
|
167
|
-
(0, regex_parser_1.default)(redirect.pattern).test(`${tragetURL.replace(/\/*$/gi, '')}${targetQS}`) ||
|
|
168
204
|
(0, regex_parser_1.default)(redirect.pattern).test(`/${req.nextUrl.locale}${tragetURL}`) ||
|
|
169
|
-
|
|
205
|
+
matchedQueryString) &&
|
|
170
206
|
(redirect.locale
|
|
171
207
|
? redirect.locale.toLowerCase() === req.nextUrl.locale.toLowerCase()
|
|
172
208
|
: true));
|
|
@@ -233,5 +269,24 @@ class RedirectsMiddleware extends middleware_1.MiddlewareBase {
|
|
|
233
269
|
}
|
|
234
270
|
return redirect;
|
|
235
271
|
}
|
|
272
|
+
/**
|
|
273
|
+
* Checks if the current URL query matches the provided pattern, considering all permutations of query parameters.
|
|
274
|
+
* It constructs all possible query parameter permutations and tests them against the pattern.
|
|
275
|
+
* @param {Object} params - The parameters for the URL match.
|
|
276
|
+
* @param {string} params.pathname - The current URL pathname.
|
|
277
|
+
* @param {string} params.queryString - The current URL query string.
|
|
278
|
+
* @param {string} params.pattern - The regex pattern to test the constructed URLs against.
|
|
279
|
+
* @param {string} [params.locale] - The locale prefix to include in the URL if present.
|
|
280
|
+
* @returns {string | undefined} - return query string if any of the query permutations match the provided pattern, undefined otherwise.
|
|
281
|
+
*/
|
|
282
|
+
isPermutedQueryMatch({ pathname, queryString, pattern, locale, }) {
|
|
283
|
+
const paramsArray = Array.from(new URLSearchParams(queryString).entries());
|
|
284
|
+
const listOfPermuted = (0, utils_1.getPermutations)(paramsArray).map((permutation) => '?' + permutation.map(([key, value]) => `${key}=${value}`).join('&'));
|
|
285
|
+
const normalizedPath = pathname.replace(/\/*$/gi, '');
|
|
286
|
+
return listOfPermuted.find((query) => [
|
|
287
|
+
(0, regex_parser_1.default)(pattern).test(`${normalizedPath}${query}`),
|
|
288
|
+
(0, regex_parser_1.default)(pattern).test(`/${locale}${normalizedPath}${query}`),
|
|
289
|
+
].some(Boolean));
|
|
290
|
+
}
|
|
236
291
|
}
|
|
237
292
|
exports.RedirectsMiddleware = RedirectsMiddleware;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { debug } from '@sitecore-jss/sitecore-jss';
|
|
2
|
+
export { MiddlewareBase } from './middleware';
|
|
2
3
|
export { RedirectsMiddleware } from './redirects-middleware';
|
|
3
4
|
export { PersonalizeMiddleware } from './personalize-middleware';
|
|
4
5
|
export { MultisiteMiddleware } from './multisite-middleware';
|
|
@@ -9,6 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
};
|
|
10
10
|
import { debug } from '@sitecore-jss/sitecore-jss';
|
|
11
11
|
import { GraphQLRedirectsService, REDIRECT_TYPE_301, REDIRECT_TYPE_302, REDIRECT_TYPE_SERVER_TRANSFER, } from '@sitecore-jss/sitecore-jss/site';
|
|
12
|
+
import { getPermutations } from '@sitecore-jss/sitecore-jss/utils';
|
|
12
13
|
import { NextResponse } from 'next/server';
|
|
13
14
|
import regexParser from 'regex-parser';
|
|
14
15
|
import { MiddlewareBase } from './middleware';
|
|
@@ -64,7 +65,7 @@ export class RedirectsMiddleware extends MiddlewareBase {
|
|
|
64
65
|
url.href = existsRedirect.target;
|
|
65
66
|
}
|
|
66
67
|
else {
|
|
67
|
-
const source = `${url.pathname.replace(/\/*$/gi, '')}${
|
|
68
|
+
const source = `${url.pathname.replace(/\/*$/gi, '')}${existsRedirect.matchedQueryString}`;
|
|
68
69
|
const urlFirstPart = existsRedirect.target.split('/')[1];
|
|
69
70
|
if (this.locales.includes(urlFirstPart)) {
|
|
70
71
|
req.nextUrl.locale = urlFirstPart;
|
|
@@ -150,17 +151,52 @@ export class RedirectsMiddleware extends MiddlewareBase {
|
|
|
150
151
|
const modifyRedirects = structuredClone(redirects);
|
|
151
152
|
return modifyRedirects.length
|
|
152
153
|
? modifyRedirects.find((redirect) => {
|
|
154
|
+
// Modify the redirect pattern to ignore the language prefix in the path
|
|
153
155
|
redirect.pattern = redirect.pattern.replace(RegExp(`^[^]?/${language}/`, 'gi'), '');
|
|
156
|
+
// Prepare the redirect pattern as a regular expression, making it more flexible for matching URLs
|
|
154
157
|
redirect.pattern = `/^\/${redirect.pattern
|
|
155
|
-
.replace(/^\/|\/$/g, '')
|
|
156
|
-
.replace(/^\^\/|\/\$$/g, '')
|
|
157
|
-
.replace(/^\^|\$$/g, '')
|
|
158
|
-
.replace(/(?<!\\)\?/g, '\\?')
|
|
159
|
-
.replace(/\$\/gi$/g, '')}[\/]?$/
|
|
158
|
+
.replace(/^\/|\/$/g, '') // Removes leading and trailing slashes
|
|
159
|
+
.replace(/^\^\/|\/\$$/g, '') // Removes unnecessary start (^) and end ($) anchors
|
|
160
|
+
.replace(/^\^|\$$/g, '') // Further cleans up anchors
|
|
161
|
+
.replace(/(?<!\\)\?/g, '\\?') // Escapes question marks in the pattern
|
|
162
|
+
.replace(/\$\/gi$/g, '')}[\/]?$/i`; // Ensures the pattern allows an optional trailing slash
|
|
163
|
+
/**
|
|
164
|
+
* This line checks whether the current URL query string (and all its possible permutations)
|
|
165
|
+
* matches the redirect pattern.
|
|
166
|
+
*
|
|
167
|
+
* Query parameters in URLs can come in different orders, but logically they represent the
|
|
168
|
+
* same information (e.g., "key1=value1&key2=value2" is the same as "key2=value2&key1=value1").
|
|
169
|
+
* To account for this, the method `isPermutedQueryMatch` generates all possible permutations
|
|
170
|
+
* of the query parameters and checks if any of those permutations match the regex pattern for the redirect.
|
|
171
|
+
*
|
|
172
|
+
* NOTE: This fix is specifically implemented for Netlify, where query parameters are sometimes
|
|
173
|
+
* automatically sorted, which can cause issues with matching redirects if the order of query
|
|
174
|
+
* parameters is important. By checking every possible permutation, we ensure that redirects
|
|
175
|
+
* work correctly on Netlify despite this behavior.
|
|
176
|
+
*
|
|
177
|
+
* It passes several pieces of information to the function:
|
|
178
|
+
* 1. `pathname`: The normalized URL path without query parameters (e.g., '/about').
|
|
179
|
+
* 2. `queryString`: The current query string from the URL, which will be permuted and matched (e.g., '?key1=value1&key2=value2').
|
|
180
|
+
* 3. `pattern`: The regex pattern for the redirect that we are trying to match against the URL (e.g., '/about?key1=value1').
|
|
181
|
+
* 4. `locale`: The locale part of the URL (if any), which helps support multilingual URLs.
|
|
182
|
+
*
|
|
183
|
+
* If one of the permutations of the query string matches the redirect pattern, the function
|
|
184
|
+
* returns the matched query string, which is stored in `matchedQueryString`. If no match is found,
|
|
185
|
+
* it returns `undefined`. The `matchedQueryString` is later used to indicate whether the query
|
|
186
|
+
* string contributed to a successful redirect match.
|
|
187
|
+
*/
|
|
188
|
+
const matchedQueryString = this.isPermutedQueryMatch({
|
|
189
|
+
pathname: tragetURL,
|
|
190
|
+
queryString: targetQS,
|
|
191
|
+
pattern: redirect.pattern,
|
|
192
|
+
locale: req.nextUrl.locale,
|
|
193
|
+
});
|
|
194
|
+
// Save the matched query string (if found) into the redirect object
|
|
195
|
+
redirect.matchedQueryString = matchedQueryString || '';
|
|
196
|
+
// Return the redirect if the URL path or any query string permutation matches the pattern
|
|
160
197
|
return ((regexParser(redirect.pattern).test(tragetURL) ||
|
|
161
|
-
regexParser(redirect.pattern).test(`${tragetURL.replace(/\/*$/gi, '')}${targetQS}`) ||
|
|
162
198
|
regexParser(redirect.pattern).test(`/${req.nextUrl.locale}${tragetURL}`) ||
|
|
163
|
-
|
|
199
|
+
matchedQueryString) &&
|
|
164
200
|
(redirect.locale
|
|
165
201
|
? redirect.locale.toLowerCase() === req.nextUrl.locale.toLowerCase()
|
|
166
202
|
: true));
|
|
@@ -227,4 +263,23 @@ export class RedirectsMiddleware extends MiddlewareBase {
|
|
|
227
263
|
}
|
|
228
264
|
return redirect;
|
|
229
265
|
}
|
|
266
|
+
/**
|
|
267
|
+
* Checks if the current URL query matches the provided pattern, considering all permutations of query parameters.
|
|
268
|
+
* It constructs all possible query parameter permutations and tests them against the pattern.
|
|
269
|
+
* @param {Object} params - The parameters for the URL match.
|
|
270
|
+
* @param {string} params.pathname - The current URL pathname.
|
|
271
|
+
* @param {string} params.queryString - The current URL query string.
|
|
272
|
+
* @param {string} params.pattern - The regex pattern to test the constructed URLs against.
|
|
273
|
+
* @param {string} [params.locale] - The locale prefix to include in the URL if present.
|
|
274
|
+
* @returns {string | undefined} - return query string if any of the query permutations match the provided pattern, undefined otherwise.
|
|
275
|
+
*/
|
|
276
|
+
isPermutedQueryMatch({ pathname, queryString, pattern, locale, }) {
|
|
277
|
+
const paramsArray = Array.from(new URLSearchParams(queryString).entries());
|
|
278
|
+
const listOfPermuted = getPermutations(paramsArray).map((permutation) => '?' + permutation.map(([key, value]) => `${key}=${value}`).join('&'));
|
|
279
|
+
const normalizedPath = pathname.replace(/\/*$/gi, '');
|
|
280
|
+
return listOfPermuted.find((query) => [
|
|
281
|
+
regexParser(pattern).test(`${normalizedPath}${query}`),
|
|
282
|
+
regexParser(pattern).test(`/${locale}${normalizedPath}${query}`),
|
|
283
|
+
].some(Boolean));
|
|
284
|
+
}
|
|
230
285
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sitecore-jss/sitecore-jss-nextjs",
|
|
3
|
-
"version": "22.2.0-canary.
|
|
3
|
+
"version": "22.2.0-canary.63",
|
|
4
4
|
"main": "dist/cjs/index.js",
|
|
5
5
|
"module": "dist/esm/index.js",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -72,9 +72,9 @@
|
|
|
72
72
|
"react-dom": "^18.2.0"
|
|
73
73
|
},
|
|
74
74
|
"dependencies": {
|
|
75
|
-
"@sitecore-jss/sitecore-jss": "^22.2.0-canary.
|
|
76
|
-
"@sitecore-jss/sitecore-jss-dev-tools": "^22.2.0-canary.
|
|
77
|
-
"@sitecore-jss/sitecore-jss-react": "^22.2.0-canary.
|
|
75
|
+
"@sitecore-jss/sitecore-jss": "^22.2.0-canary.63",
|
|
76
|
+
"@sitecore-jss/sitecore-jss-dev-tools": "^22.2.0-canary.63",
|
|
77
|
+
"@sitecore-jss/sitecore-jss-react": "^22.2.0-canary.63",
|
|
78
78
|
"@vercel/kv": "^0.2.1",
|
|
79
79
|
"prop-types": "^15.8.1",
|
|
80
80
|
"regex-parser": "^2.2.11",
|
|
@@ -82,7 +82,7 @@
|
|
|
82
82
|
},
|
|
83
83
|
"description": "",
|
|
84
84
|
"types": "types/index.d.ts",
|
|
85
|
-
"gitHead": "
|
|
85
|
+
"gitHead": "320dcbeb8398d1a7854fd93244f73fc6b9ee8f66",
|
|
86
86
|
"files": [
|
|
87
87
|
"dist",
|
|
88
88
|
"types",
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { debug } from '@sitecore-jss/sitecore-jss';
|
|
2
|
+
export { MiddlewareBase, MiddlewareBaseConfig } from './middleware';
|
|
2
3
|
export { RedirectsMiddleware, RedirectsMiddlewareConfig } from './redirects-middleware';
|
|
3
4
|
export { PersonalizeMiddleware, PersonalizeMiddlewareConfig } from './personalize-middleware';
|
|
4
5
|
export { MultisiteMiddleware, MultisiteMiddlewareConfig } from './multisite-middleware';
|
|
@@ -54,4 +54,15 @@ export declare class RedirectsMiddleware extends MiddlewareBase {
|
|
|
54
54
|
* @returns {NextResponse<unknown>} The redirect response.
|
|
55
55
|
*/
|
|
56
56
|
private createRedirectResponse;
|
|
57
|
+
/**
|
|
58
|
+
* Checks if the current URL query matches the provided pattern, considering all permutations of query parameters.
|
|
59
|
+
* It constructs all possible query parameter permutations and tests them against the pattern.
|
|
60
|
+
* @param {Object} params - The parameters for the URL match.
|
|
61
|
+
* @param {string} params.pathname - The current URL pathname.
|
|
62
|
+
* @param {string} params.queryString - The current URL query string.
|
|
63
|
+
* @param {string} params.pattern - The regex pattern to test the constructed URLs against.
|
|
64
|
+
* @param {string} [params.locale] - The locale prefix to include in the URL if present.
|
|
65
|
+
* @returns {string | undefined} - return query string if any of the query permutations match the provided pattern, undefined otherwise.
|
|
66
|
+
*/
|
|
67
|
+
private isPermutedQueryMatch;
|
|
57
68
|
}
|