@sitecore-jss/sitecore-jss-nextjs 22.2.0-canary.24 → 22.2.0-canary.25

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.
@@ -64,27 +64,29 @@ class RedirectsMiddleware extends middleware_1.MiddlewareBase {
64
64
  existsRedirect.target.includes(hostname))) {
65
65
  existsRedirect.target = existsRedirect.target.replace(REGEXP_CONTEXT_SITE_LANG, site.language);
66
66
  }
67
- const url = req.nextUrl.clone();
67
+ const url = this.normalizeUrl(req.nextUrl.clone());
68
68
  if (REGEXP_ABSOLUTE_URL.test(existsRedirect.target)) {
69
69
  url.href = existsRedirect.target;
70
70
  }
71
71
  else {
72
- const source = this.normalizeUrl(url.pathname, url.search);
72
+ const source = `${url.pathname.replace(/\/*$/gi, '')}${url.search}`;
73
73
  const urlFirstPart = existsRedirect.target.split('/')[1];
74
74
  if (this.locales.includes(urlFirstPart)) {
75
- url.locale = urlFirstPart;
75
+ req.nextUrl.locale = urlFirstPart;
76
76
  existsRedirect.target = existsRedirect.target.replace(`/${urlFirstPart}`, '');
77
77
  }
78
78
  const target = source
79
79
  .replace((0, regex_parser_1.default)(existsRedirect.pattern), existsRedirect.target)
80
80
  .replace(/^\/\//, '/')
81
81
  .split('?');
82
- url.pathname = `${target[0]}`;
83
82
  if (target[1]) {
84
- url.search = `?${target[1]}`;
83
+ const movedSearchParams = existsRedirect.isQueryStringPreserved
84
+ ? url.search.replace(/^\?/gi, '&')
85
+ : '';
86
+ url.search = '?' + new URLSearchParams(`${target[1]}${movedSearchParams}`).toString();
85
87
  }
86
- const newURL = new URL(`${url.pathname}${existsRedirect.isQueryStringPreserved ? url.search : ''}`, url.origin);
87
- url.href = newURL.href;
88
+ const prepareNewURL = new URL(`${target[0]}${url.search}`, url.origin);
89
+ url.href = prepareNewURL.href;
88
90
  }
89
91
  const redirectUrl = decodeURIComponent(url.href);
90
92
  /** return Response redirect with http code of redirect type **/
@@ -140,7 +142,7 @@ class RedirectsMiddleware extends middleware_1.MiddlewareBase {
140
142
  getExistsRedirect(req, siteName) {
141
143
  return __awaiter(this, void 0, void 0, function* () {
142
144
  const redirects = yield this.redirectsService.fetchRedirects(siteName);
143
- const normalizedUrl = new URL(this.normalizeUrl(req.nextUrl.pathname, req.nextUrl.search || ''), req.nextUrl.href);
145
+ const normalizedUrl = this.normalizeUrl(req.nextUrl.clone());
144
146
  const tragetURL = normalizedUrl.pathname;
145
147
  const targetQS = normalizedUrl.search || '';
146
148
  const language = this.getLanguage(req);
@@ -155,7 +157,7 @@ class RedirectsMiddleware extends middleware_1.MiddlewareBase {
155
157
  .replace(/(?<!\\)\?/g, '\\?')
156
158
  .replace(/\$\/gi$/g, '')}[\/]?$/gi`;
157
159
  return (((0, regex_parser_1.default)(redirect.pattern).test(tragetURL) ||
158
- (0, regex_parser_1.default)(redirect.pattern).test(`${tragetURL}${targetQS}`) ||
160
+ (0, regex_parser_1.default)(redirect.pattern).test(`${tragetURL.replace(/\/*$/gi, '')}${targetQS}`) ||
159
161
  (0, regex_parser_1.default)(redirect.pattern).test(`/${req.nextUrl.locale}${tragetURL}`) ||
160
162
  (0, regex_parser_1.default)(redirect.pattern).test(`/${req.nextUrl.locale}${tragetURL}${targetQS}`)) &&
161
163
  (redirect.locale
@@ -169,18 +171,17 @@ class RedirectsMiddleware extends middleware_1.MiddlewareBase {
169
171
  * When a user clicks on a link generated by the Link component from next/link,
170
172
  * Next.js adds special parameters in the route called path.
171
173
  * This method removes these special parameters.
172
- * @param {string} pathname
173
- * @param {string} queryString
174
- * @returns {string} modified url
174
+ * @param {URL} url
175
+ * @returns {string} normalize url
175
176
  */
176
- normalizeUrl(pathname, queryString) {
177
- if (!queryString) {
178
- return pathname;
177
+ normalizeUrl(url) {
178
+ if (!url.search) {
179
+ return url;
179
180
  }
180
181
  /**
181
182
  * Prepare special parameters for exclusion.
182
183
  */
183
- const splittedPathname = pathname
184
+ const splittedPathname = url.pathname
184
185
  .split('/')
185
186
  .filter((route) => route)
186
187
  .map((route) => `path=${route}`);
@@ -190,7 +191,7 @@ class RedirectsMiddleware extends middleware_1.MiddlewareBase {
190
191
  * When a user clicks on this link, Next.js should generate a link for the middleware, formatted like this:
191
192
  * http://host/about/contact/us?path=about&path=contact&path=us
192
193
  */
193
- const newQueryString = queryString
194
+ const newQueryString = url.search
194
195
  .replace(/^\?/, '')
195
196
  .split('&')
196
197
  .filter((param) => {
@@ -201,9 +202,9 @@ class RedirectsMiddleware extends middleware_1.MiddlewareBase {
201
202
  })
202
203
  .join('&');
203
204
  if (newQueryString) {
204
- return `${pathname}?${newQueryString}`;
205
+ return new URL(`${url.pathname}?${newQueryString}`, url.origin);
205
206
  }
206
- return pathname;
207
+ return new URL(`${url.pathname}`, url.origin);
207
208
  }
208
209
  }
209
210
  exports.RedirectsMiddleware = RedirectsMiddleware;
@@ -58,27 +58,29 @@ export class RedirectsMiddleware extends MiddlewareBase {
58
58
  existsRedirect.target.includes(hostname))) {
59
59
  existsRedirect.target = existsRedirect.target.replace(REGEXP_CONTEXT_SITE_LANG, site.language);
60
60
  }
61
- const url = req.nextUrl.clone();
61
+ const url = this.normalizeUrl(req.nextUrl.clone());
62
62
  if (REGEXP_ABSOLUTE_URL.test(existsRedirect.target)) {
63
63
  url.href = existsRedirect.target;
64
64
  }
65
65
  else {
66
- const source = this.normalizeUrl(url.pathname, url.search);
66
+ const source = `${url.pathname.replace(/\/*$/gi, '')}${url.search}`;
67
67
  const urlFirstPart = existsRedirect.target.split('/')[1];
68
68
  if (this.locales.includes(urlFirstPart)) {
69
- url.locale = urlFirstPart;
69
+ req.nextUrl.locale = urlFirstPart;
70
70
  existsRedirect.target = existsRedirect.target.replace(`/${urlFirstPart}`, '');
71
71
  }
72
72
  const target = source
73
73
  .replace(regexParser(existsRedirect.pattern), existsRedirect.target)
74
74
  .replace(/^\/\//, '/')
75
75
  .split('?');
76
- url.pathname = `${target[0]}`;
77
76
  if (target[1]) {
78
- url.search = `?${target[1]}`;
77
+ const movedSearchParams = existsRedirect.isQueryStringPreserved
78
+ ? url.search.replace(/^\?/gi, '&')
79
+ : '';
80
+ url.search = '?' + new URLSearchParams(`${target[1]}${movedSearchParams}`).toString();
79
81
  }
80
- const newURL = new URL(`${url.pathname}${existsRedirect.isQueryStringPreserved ? url.search : ''}`, url.origin);
81
- url.href = newURL.href;
82
+ const prepareNewURL = new URL(`${target[0]}${url.search}`, url.origin);
83
+ url.href = prepareNewURL.href;
82
84
  }
83
85
  const redirectUrl = decodeURIComponent(url.href);
84
86
  /** return Response redirect with http code of redirect type **/
@@ -134,7 +136,7 @@ export class RedirectsMiddleware extends MiddlewareBase {
134
136
  getExistsRedirect(req, siteName) {
135
137
  return __awaiter(this, void 0, void 0, function* () {
136
138
  const redirects = yield this.redirectsService.fetchRedirects(siteName);
137
- const normalizedUrl = new URL(this.normalizeUrl(req.nextUrl.pathname, req.nextUrl.search || ''), req.nextUrl.href);
139
+ const normalizedUrl = this.normalizeUrl(req.nextUrl.clone());
138
140
  const tragetURL = normalizedUrl.pathname;
139
141
  const targetQS = normalizedUrl.search || '';
140
142
  const language = this.getLanguage(req);
@@ -149,7 +151,7 @@ export class RedirectsMiddleware extends MiddlewareBase {
149
151
  .replace(/(?<!\\)\?/g, '\\?')
150
152
  .replace(/\$\/gi$/g, '')}[\/]?$/gi`;
151
153
  return ((regexParser(redirect.pattern).test(tragetURL) ||
152
- regexParser(redirect.pattern).test(`${tragetURL}${targetQS}`) ||
154
+ regexParser(redirect.pattern).test(`${tragetURL.replace(/\/*$/gi, '')}${targetQS}`) ||
153
155
  regexParser(redirect.pattern).test(`/${req.nextUrl.locale}${tragetURL}`) ||
154
156
  regexParser(redirect.pattern).test(`/${req.nextUrl.locale}${tragetURL}${targetQS}`)) &&
155
157
  (redirect.locale
@@ -163,18 +165,17 @@ export class RedirectsMiddleware extends MiddlewareBase {
163
165
  * When a user clicks on a link generated by the Link component from next/link,
164
166
  * Next.js adds special parameters in the route called path.
165
167
  * This method removes these special parameters.
166
- * @param {string} pathname
167
- * @param {string} queryString
168
- * @returns {string} modified url
168
+ * @param {URL} url
169
+ * @returns {string} normalize url
169
170
  */
170
- normalizeUrl(pathname, queryString) {
171
- if (!queryString) {
172
- return pathname;
171
+ normalizeUrl(url) {
172
+ if (!url.search) {
173
+ return url;
173
174
  }
174
175
  /**
175
176
  * Prepare special parameters for exclusion.
176
177
  */
177
- const splittedPathname = pathname
178
+ const splittedPathname = url.pathname
178
179
  .split('/')
179
180
  .filter((route) => route)
180
181
  .map((route) => `path=${route}`);
@@ -184,7 +185,7 @@ export class RedirectsMiddleware extends MiddlewareBase {
184
185
  * When a user clicks on this link, Next.js should generate a link for the middleware, formatted like this:
185
186
  * http://host/about/contact/us?path=about&path=contact&path=us
186
187
  */
187
- const newQueryString = queryString
188
+ const newQueryString = url.search
188
189
  .replace(/^\?/, '')
189
190
  .split('&')
190
191
  .filter((param) => {
@@ -195,8 +196,8 @@ export class RedirectsMiddleware extends MiddlewareBase {
195
196
  })
196
197
  .join('&');
197
198
  if (newQueryString) {
198
- return `${pathname}?${newQueryString}`;
199
+ return new URL(`${url.pathname}?${newQueryString}`, url.origin);
199
200
  }
200
- return pathname;
201
+ return new URL(`${url.pathname}`, url.origin);
201
202
  }
202
203
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sitecore-jss/sitecore-jss-nextjs",
3
- "version": "22.2.0-canary.24",
3
+ "version": "22.2.0-canary.25",
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.24",
76
- "@sitecore-jss/sitecore-jss-dev-tools": "^22.2.0-canary.24",
77
- "@sitecore-jss/sitecore-jss-react": "^22.2.0-canary.24",
75
+ "@sitecore-jss/sitecore-jss": "22.2.0-canary.25",
76
+ "@sitecore-jss/sitecore-jss-dev-tools": "22.2.0-canary.25",
77
+ "@sitecore-jss/sitecore-jss-react": "22.2.0-canary.25",
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": "2c4ab55cbdb879def573fd9e88b6d5d2dc530c36",
85
+ "gitHead": "2f4820efddf4454eeee58ed1b2cc251969efdf5b",
86
86
  "files": [
87
87
  "dist",
88
88
  "types",
@@ -41,9 +41,8 @@ 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 {string} pathname
45
- * @param {string} queryString
46
- * @returns {string} modified url
44
+ * @param {URL} url
45
+ * @returns {string} normalize url
47
46
  */
48
47
  private normalizeUrl;
49
48
  }