@sitecore-jss/sitecore-jss-nextjs 22.1.4-canary.1 → 22.1.4-canary.2
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 +81 -26
- package/dist/esm/middleware/index.js +1 -0
- package/dist/esm/middleware/redirects-middleware.js +81 -26
- package/package.json +5 -5
- package/types/middleware/index.d.ts +1 -0
- package/types/middleware/redirects-middleware.d.ts +13 -2
|
@@ -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");
|
|
@@ -64,13 +65,14 @@ class RedirectsMiddleware extends middleware_1.MiddlewareBase {
|
|
|
64
65
|
!(REGEXP_ABSOLUTE_URL.test(existsRedirect.target) &&
|
|
65
66
|
existsRedirect.target.includes(hostname))) {
|
|
66
67
|
existsRedirect.target = existsRedirect.target.replace(REGEXP_CONTEXT_SITE_LANG, site.language);
|
|
68
|
+
req.nextUrl.locale = site.language;
|
|
67
69
|
}
|
|
68
70
|
const url = this.normalizeUrl(req.nextUrl.clone());
|
|
69
71
|
if (REGEXP_ABSOLUTE_URL.test(existsRedirect.target)) {
|
|
70
72
|
url.href = existsRedirect.target;
|
|
71
73
|
}
|
|
72
74
|
else {
|
|
73
|
-
const source = `${url.pathname.replace(/\/*$/gi, '')}${
|
|
75
|
+
const source = `${url.pathname.replace(/\/*$/gi, '')}${existsRedirect.matchedQueryString}`;
|
|
74
76
|
const urlFirstPart = existsRedirect.target.split('/')[1];
|
|
75
77
|
if (this.locales.includes(urlFirstPart)) {
|
|
76
78
|
req.nextUrl.locale = urlFirstPart;
|
|
@@ -92,18 +94,20 @@ class RedirectsMiddleware extends middleware_1.MiddlewareBase {
|
|
|
92
94
|
}
|
|
93
95
|
const prepareNewURL = new URL(`${target[0]}${url.search}`, url.origin);
|
|
94
96
|
url.href = prepareNewURL.href;
|
|
97
|
+
url.pathname = prepareNewURL.pathname;
|
|
98
|
+
url.search = prepareNewURL.search;
|
|
99
|
+
url.locale = req.nextUrl.locale;
|
|
95
100
|
}
|
|
96
|
-
const redirectUrl = decodeURIComponent(url.href);
|
|
97
101
|
/** return Response redirect with http code of redirect type **/
|
|
98
102
|
switch (existsRedirect.redirectType) {
|
|
99
103
|
case site_1.REDIRECT_TYPE_301: {
|
|
100
|
-
return this.createRedirectResponse(
|
|
104
|
+
return this.createRedirectResponse(url, res, 301, 'Moved Permanently');
|
|
101
105
|
}
|
|
102
106
|
case site_1.REDIRECT_TYPE_302: {
|
|
103
|
-
return this.createRedirectResponse(
|
|
107
|
+
return this.createRedirectResponse(url, res, 302, 'Found');
|
|
104
108
|
}
|
|
105
109
|
case site_1.REDIRECT_TYPE_SERVER_TRANSFER: {
|
|
106
|
-
return this.rewrite(
|
|
110
|
+
return this.rewrite(url.href, req, res || server_1.NextResponse.next());
|
|
107
111
|
}
|
|
108
112
|
default:
|
|
109
113
|
return res || server_1.NextResponse.next();
|
|
@@ -149,27 +153,58 @@ class RedirectsMiddleware extends middleware_1.MiddlewareBase {
|
|
|
149
153
|
getExistsRedirect(req, siteName) {
|
|
150
154
|
return __awaiter(this, void 0, void 0, function* () {
|
|
151
155
|
const redirects = yield this.redirectsService.fetchRedirects(siteName);
|
|
152
|
-
const
|
|
153
|
-
const tragetURL = normalizedUrl.pathname;
|
|
154
|
-
const targetQS = normalizedUrl.search || '';
|
|
156
|
+
const { pathname: targetURL, search: targetQS = '', locale } = this.normalizeUrl(req.nextUrl.clone());
|
|
155
157
|
const language = this.getLanguage(req);
|
|
156
158
|
const modifyRedirects = structuredClone(redirects);
|
|
157
159
|
return modifyRedirects.length
|
|
158
160
|
? modifyRedirects.find((redirect) => {
|
|
161
|
+
// Modify the redirect pattern to ignore the language prefix in the path
|
|
159
162
|
redirect.pattern = redirect.pattern.replace(RegExp(`^[^]?/${language}/`, 'gi'), '');
|
|
163
|
+
// Prepare the redirect pattern as a regular expression, making it more flexible for matching URLs
|
|
160
164
|
redirect.pattern = `/^\/${redirect.pattern
|
|
161
|
-
.replace(/^\/|\/$/g, '')
|
|
162
|
-
.replace(/^\^\/|\/\$$/g, '')
|
|
163
|
-
.replace(/^\^|\$$/g, '')
|
|
164
|
-
.replace(/(?<!\\)\?/g, '\\?')
|
|
165
|
-
.replace(/\$\/gi$/g, '')}[\/]?$/
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
165
|
+
.replace(/^\/|\/$/g, '') // Removes leading and trailing slashes
|
|
166
|
+
.replace(/^\^\/|\/\$$/g, '') // Removes unnecessary start (^) and end ($) anchors
|
|
167
|
+
.replace(/^\^|\$$/g, '') // Further cleans up anchors
|
|
168
|
+
.replace(/(?<!\\)\?/g, '\\?') // Escapes question marks in the pattern
|
|
169
|
+
.replace(/\$\/gi$/g, '')}[\/]?$/i`; // Ensures the pattern allows an optional trailing slash
|
|
170
|
+
/**
|
|
171
|
+
* This line checks whether the current URL query string (and all its possible permutations)
|
|
172
|
+
* matches the redirect pattern.
|
|
173
|
+
*
|
|
174
|
+
* Query parameters in URLs can come in different orders, but logically they represent the
|
|
175
|
+
* same information (e.g., "key1=value1&key2=value2" is the same as "key2=value2&key1=value1").
|
|
176
|
+
* To account for this, the method `isPermutedQueryMatch` generates all possible permutations
|
|
177
|
+
* of the query parameters and checks if any of those permutations match the regex pattern for the redirect.
|
|
178
|
+
*
|
|
179
|
+
* NOTE: This fix is specifically implemented for Netlify, where query parameters are sometimes
|
|
180
|
+
* automatically sorted, which can cause issues with matching redirects if the order of query
|
|
181
|
+
* parameters is important. By checking every possible permutation, we ensure that redirects
|
|
182
|
+
* work correctly on Netlify despite this behavior.
|
|
183
|
+
*
|
|
184
|
+
* It passes several pieces of information to the function:
|
|
185
|
+
* 1. `pathname`: The normalized URL path without query parameters (e.g., '/about').
|
|
186
|
+
* 2. `queryString`: The current query string from the URL, which will be permuted and matched (e.g., '?key1=value1&key2=value2').
|
|
187
|
+
* 3. `pattern`: The regex pattern for the redirect that we are trying to match against the URL (e.g., '/about?key1=value1').
|
|
188
|
+
* 4. `locale`: The locale part of the URL (if any), which helps support multilingual URLs.
|
|
189
|
+
*
|
|
190
|
+
* If one of the permutations of the query string matches the redirect pattern, the function
|
|
191
|
+
* returns the matched query string, which is stored in `matchedQueryString`. If no match is found,
|
|
192
|
+
* it returns `undefined`. The `matchedQueryString` is later used to indicate whether the query
|
|
193
|
+
* string contributed to a successful redirect match.
|
|
194
|
+
*/
|
|
195
|
+
const matchedQueryString = this.isPermutedQueryMatch({
|
|
196
|
+
pathname: targetURL,
|
|
197
|
+
queryString: targetQS,
|
|
198
|
+
pattern: redirect.pattern,
|
|
199
|
+
locale,
|
|
200
|
+
});
|
|
201
|
+
// Save the matched query string (if found) into the redirect object
|
|
202
|
+
redirect.matchedQueryString = matchedQueryString || '';
|
|
203
|
+
// Return the redirect if the URL path or any query string permutation matches the pattern
|
|
204
|
+
return (((0, regex_parser_1.default)(redirect.pattern).test(targetURL) ||
|
|
205
|
+
(0, regex_parser_1.default)(redirect.pattern).test(`/${req.nextUrl.locale}${targetURL}`) ||
|
|
206
|
+
matchedQueryString) &&
|
|
207
|
+
(redirect.locale ? redirect.locale.toLowerCase() === locale.toLowerCase() : true));
|
|
173
208
|
})
|
|
174
209
|
: undefined;
|
|
175
210
|
});
|
|
@@ -178,7 +213,7 @@ class RedirectsMiddleware extends middleware_1.MiddlewareBase {
|
|
|
178
213
|
* When a user clicks on a link generated by the Link component from next/link,
|
|
179
214
|
* Next.js adds special parameters in the route called path.
|
|
180
215
|
* This method removes these special parameters.
|
|
181
|
-
* @param {
|
|
216
|
+
* @param {NextURL} url
|
|
182
217
|
* @returns {string} normalize url
|
|
183
218
|
*/
|
|
184
219
|
normalizeUrl(url) {
|
|
@@ -208,14 +243,15 @@ class RedirectsMiddleware extends middleware_1.MiddlewareBase {
|
|
|
208
243
|
return false;
|
|
209
244
|
})
|
|
210
245
|
.join('&');
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
246
|
+
const newUrl = new URL(`${url.pathname}?${newQueryString}`, url.origin);
|
|
247
|
+
url.search = newUrl.search;
|
|
248
|
+
url.pathname = newUrl.pathname;
|
|
249
|
+
url.href = newUrl.href;
|
|
250
|
+
return url;
|
|
215
251
|
}
|
|
216
252
|
/**
|
|
217
253
|
* Helper function to create a redirect response and remove the x-middleware-next header.
|
|
218
|
-
* @param {
|
|
254
|
+
* @param {NextURL} url The URL to redirect to.
|
|
219
255
|
* @param {Response} res The response object.
|
|
220
256
|
* @param {number} status The HTTP status code of the redirect.
|
|
221
257
|
* @param {string} statusText The status text of the redirect.
|
|
@@ -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';
|
|
@@ -58,13 +59,14 @@ export class RedirectsMiddleware extends MiddlewareBase {
|
|
|
58
59
|
!(REGEXP_ABSOLUTE_URL.test(existsRedirect.target) &&
|
|
59
60
|
existsRedirect.target.includes(hostname))) {
|
|
60
61
|
existsRedirect.target = existsRedirect.target.replace(REGEXP_CONTEXT_SITE_LANG, site.language);
|
|
62
|
+
req.nextUrl.locale = site.language;
|
|
61
63
|
}
|
|
62
64
|
const url = this.normalizeUrl(req.nextUrl.clone());
|
|
63
65
|
if (REGEXP_ABSOLUTE_URL.test(existsRedirect.target)) {
|
|
64
66
|
url.href = existsRedirect.target;
|
|
65
67
|
}
|
|
66
68
|
else {
|
|
67
|
-
const source = `${url.pathname.replace(/\/*$/gi, '')}${
|
|
69
|
+
const source = `${url.pathname.replace(/\/*$/gi, '')}${existsRedirect.matchedQueryString}`;
|
|
68
70
|
const urlFirstPart = existsRedirect.target.split('/')[1];
|
|
69
71
|
if (this.locales.includes(urlFirstPart)) {
|
|
70
72
|
req.nextUrl.locale = urlFirstPart;
|
|
@@ -86,18 +88,20 @@ export class RedirectsMiddleware extends MiddlewareBase {
|
|
|
86
88
|
}
|
|
87
89
|
const prepareNewURL = new URL(`${target[0]}${url.search}`, url.origin);
|
|
88
90
|
url.href = prepareNewURL.href;
|
|
91
|
+
url.pathname = prepareNewURL.pathname;
|
|
92
|
+
url.search = prepareNewURL.search;
|
|
93
|
+
url.locale = req.nextUrl.locale;
|
|
89
94
|
}
|
|
90
|
-
const redirectUrl = decodeURIComponent(url.href);
|
|
91
95
|
/** return Response redirect with http code of redirect type **/
|
|
92
96
|
switch (existsRedirect.redirectType) {
|
|
93
97
|
case REDIRECT_TYPE_301: {
|
|
94
|
-
return this.createRedirectResponse(
|
|
98
|
+
return this.createRedirectResponse(url, res, 301, 'Moved Permanently');
|
|
95
99
|
}
|
|
96
100
|
case REDIRECT_TYPE_302: {
|
|
97
|
-
return this.createRedirectResponse(
|
|
101
|
+
return this.createRedirectResponse(url, res, 302, 'Found');
|
|
98
102
|
}
|
|
99
103
|
case REDIRECT_TYPE_SERVER_TRANSFER: {
|
|
100
|
-
return this.rewrite(
|
|
104
|
+
return this.rewrite(url.href, req, res || NextResponse.next());
|
|
101
105
|
}
|
|
102
106
|
default:
|
|
103
107
|
return res || NextResponse.next();
|
|
@@ -143,27 +147,58 @@ export class RedirectsMiddleware extends MiddlewareBase {
|
|
|
143
147
|
getExistsRedirect(req, siteName) {
|
|
144
148
|
return __awaiter(this, void 0, void 0, function* () {
|
|
145
149
|
const redirects = yield this.redirectsService.fetchRedirects(siteName);
|
|
146
|
-
const
|
|
147
|
-
const tragetURL = normalizedUrl.pathname;
|
|
148
|
-
const targetQS = normalizedUrl.search || '';
|
|
150
|
+
const { pathname: targetURL, search: targetQS = '', locale } = this.normalizeUrl(req.nextUrl.clone());
|
|
149
151
|
const language = this.getLanguage(req);
|
|
150
152
|
const modifyRedirects = structuredClone(redirects);
|
|
151
153
|
return modifyRedirects.length
|
|
152
154
|
? modifyRedirects.find((redirect) => {
|
|
155
|
+
// Modify the redirect pattern to ignore the language prefix in the path
|
|
153
156
|
redirect.pattern = redirect.pattern.replace(RegExp(`^[^]?/${language}/`, 'gi'), '');
|
|
157
|
+
// Prepare the redirect pattern as a regular expression, making it more flexible for matching URLs
|
|
154
158
|
redirect.pattern = `/^\/${redirect.pattern
|
|
155
|
-
.replace(/^\/|\/$/g, '')
|
|
156
|
-
.replace(/^\^\/|\/\$$/g, '')
|
|
157
|
-
.replace(/^\^|\$$/g, '')
|
|
158
|
-
.replace(/(?<!\\)\?/g, '\\?')
|
|
159
|
-
.replace(/\$\/gi$/g, '')}[\/]?$/
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
159
|
+
.replace(/^\/|\/$/g, '') // Removes leading and trailing slashes
|
|
160
|
+
.replace(/^\^\/|\/\$$/g, '') // Removes unnecessary start (^) and end ($) anchors
|
|
161
|
+
.replace(/^\^|\$$/g, '') // Further cleans up anchors
|
|
162
|
+
.replace(/(?<!\\)\?/g, '\\?') // Escapes question marks in the pattern
|
|
163
|
+
.replace(/\$\/gi$/g, '')}[\/]?$/i`; // Ensures the pattern allows an optional trailing slash
|
|
164
|
+
/**
|
|
165
|
+
* This line checks whether the current URL query string (and all its possible permutations)
|
|
166
|
+
* matches the redirect pattern.
|
|
167
|
+
*
|
|
168
|
+
* Query parameters in URLs can come in different orders, but logically they represent the
|
|
169
|
+
* same information (e.g., "key1=value1&key2=value2" is the same as "key2=value2&key1=value1").
|
|
170
|
+
* To account for this, the method `isPermutedQueryMatch` generates all possible permutations
|
|
171
|
+
* of the query parameters and checks if any of those permutations match the regex pattern for the redirect.
|
|
172
|
+
*
|
|
173
|
+
* NOTE: This fix is specifically implemented for Netlify, where query parameters are sometimes
|
|
174
|
+
* automatically sorted, which can cause issues with matching redirects if the order of query
|
|
175
|
+
* parameters is important. By checking every possible permutation, we ensure that redirects
|
|
176
|
+
* work correctly on Netlify despite this behavior.
|
|
177
|
+
*
|
|
178
|
+
* It passes several pieces of information to the function:
|
|
179
|
+
* 1. `pathname`: The normalized URL path without query parameters (e.g., '/about').
|
|
180
|
+
* 2. `queryString`: The current query string from the URL, which will be permuted and matched (e.g., '?key1=value1&key2=value2').
|
|
181
|
+
* 3. `pattern`: The regex pattern for the redirect that we are trying to match against the URL (e.g., '/about?key1=value1').
|
|
182
|
+
* 4. `locale`: The locale part of the URL (if any), which helps support multilingual URLs.
|
|
183
|
+
*
|
|
184
|
+
* If one of the permutations of the query string matches the redirect pattern, the function
|
|
185
|
+
* returns the matched query string, which is stored in `matchedQueryString`. If no match is found,
|
|
186
|
+
* it returns `undefined`. The `matchedQueryString` is later used to indicate whether the query
|
|
187
|
+
* string contributed to a successful redirect match.
|
|
188
|
+
*/
|
|
189
|
+
const matchedQueryString = this.isPermutedQueryMatch({
|
|
190
|
+
pathname: targetURL,
|
|
191
|
+
queryString: targetQS,
|
|
192
|
+
pattern: redirect.pattern,
|
|
193
|
+
locale,
|
|
194
|
+
});
|
|
195
|
+
// Save the matched query string (if found) into the redirect object
|
|
196
|
+
redirect.matchedQueryString = matchedQueryString || '';
|
|
197
|
+
// Return the redirect if the URL path or any query string permutation matches the pattern
|
|
198
|
+
return ((regexParser(redirect.pattern).test(targetURL) ||
|
|
199
|
+
regexParser(redirect.pattern).test(`/${req.nextUrl.locale}${targetURL}`) ||
|
|
200
|
+
matchedQueryString) &&
|
|
201
|
+
(redirect.locale ? redirect.locale.toLowerCase() === locale.toLowerCase() : true));
|
|
167
202
|
})
|
|
168
203
|
: undefined;
|
|
169
204
|
});
|
|
@@ -172,7 +207,7 @@ export class RedirectsMiddleware extends MiddlewareBase {
|
|
|
172
207
|
* When a user clicks on a link generated by the Link component from next/link,
|
|
173
208
|
* Next.js adds special parameters in the route called path.
|
|
174
209
|
* This method removes these special parameters.
|
|
175
|
-
* @param {
|
|
210
|
+
* @param {NextURL} url
|
|
176
211
|
* @returns {string} normalize url
|
|
177
212
|
*/
|
|
178
213
|
normalizeUrl(url) {
|
|
@@ -202,14 +237,15 @@ export class RedirectsMiddleware extends MiddlewareBase {
|
|
|
202
237
|
return false;
|
|
203
238
|
})
|
|
204
239
|
.join('&');
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
240
|
+
const newUrl = new URL(`${url.pathname}?${newQueryString}`, url.origin);
|
|
241
|
+
url.search = newUrl.search;
|
|
242
|
+
url.pathname = newUrl.pathname;
|
|
243
|
+
url.href = newUrl.href;
|
|
244
|
+
return url;
|
|
209
245
|
}
|
|
210
246
|
/**
|
|
211
247
|
* Helper function to create a redirect response and remove the x-middleware-next header.
|
|
212
|
-
* @param {
|
|
248
|
+
* @param {NextURL} url The URL to redirect to.
|
|
213
249
|
* @param {Response} res The response object.
|
|
214
250
|
* @param {number} status The HTTP status code of the redirect.
|
|
215
251
|
* @param {string} statusText The status text of the redirect.
|
|
@@ -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.1.4-canary.
|
|
3
|
+
"version": "22.1.4-canary.2",
|
|
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.1.4-canary.
|
|
76
|
-
"@sitecore-jss/sitecore-jss-dev-tools": "^22.1.4-canary.
|
|
77
|
-
"@sitecore-jss/sitecore-jss-react": "^22.1.4-canary.
|
|
75
|
+
"@sitecore-jss/sitecore-jss": "^22.1.4-canary.2",
|
|
76
|
+
"@sitecore-jss/sitecore-jss-dev-tools": "^22.1.4-canary.2",
|
|
77
|
+
"@sitecore-jss/sitecore-jss-react": "^22.1.4-canary.2",
|
|
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": "9c234dbfd9a89e86bc4d2c2371b2e848e2df2291",
|
|
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';
|
|
@@ -41,17 +41,28 @@ export declare class RedirectsMiddleware extends MiddlewareBase {
|
|
|
41
41
|
* When a user clicks on a link generated by the Link component from next/link,
|
|
42
42
|
* Next.js adds special parameters in the route called path.
|
|
43
43
|
* This method removes these special parameters.
|
|
44
|
-
* @param {
|
|
44
|
+
* @param {NextURL} url
|
|
45
45
|
* @returns {string} normalize url
|
|
46
46
|
*/
|
|
47
47
|
private normalizeUrl;
|
|
48
48
|
/**
|
|
49
49
|
* Helper function to create a redirect response and remove the x-middleware-next header.
|
|
50
|
-
* @param {
|
|
50
|
+
* @param {NextURL} url The URL to redirect to.
|
|
51
51
|
* @param {Response} res The response object.
|
|
52
52
|
* @param {number} status The HTTP status code of the redirect.
|
|
53
53
|
* @param {string} statusText The status text of the redirect.
|
|
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
|
}
|