@sitecore-jss/sitecore-jss-nextjs 22.2.0-canary.2 → 22.2.0-canary.20

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.
@@ -12,4 +12,4 @@ exports.EDITING_PASS_THROUGH_HEADERS = ['authorization', 'cookie'];
12
12
  /**
13
13
  * Default allowed origins for editing requests. This is used to enforce CORS, CSP headers.
14
14
  */
15
- exports.EDITING_ALLOWED_ORIGINS = ['https://pages*.cloud', 'https://pages.sitecorecloud.io'];
15
+ exports.EDITING_ALLOWED_ORIGINS = ['https://pages.sitecorecloud.io'];
@@ -224,7 +224,6 @@ class MetadataHandler {
224
224
  },
225
225
  // Cache the preview data for 3 seconds to ensure the page is rendered with the correct preview data not the cached one
226
226
  {
227
- path: query.route,
228
227
  maxAge: 3,
229
228
  });
230
229
  // Cookies with the SameSite=Lax policy set by Next.js setPreviewData function causes CORS issue
@@ -69,8 +69,7 @@ class RedirectsMiddleware extends middleware_1.MiddlewareBase {
69
69
  url.href = existsRedirect.target;
70
70
  }
71
71
  else {
72
- const source = `${url.pathname}${url.search}`;
73
- url.search = existsRedirect.isQueryStringPreserved ? url.search : '';
72
+ const source = this.normalizeUrl(url.pathname, url.search);
74
73
  const urlFirstPart = existsRedirect.target.split('/')[1];
75
74
  if (this.locales.includes(urlFirstPart)) {
76
75
  url.locale = urlFirstPart;
@@ -80,31 +79,23 @@ class RedirectsMiddleware extends middleware_1.MiddlewareBase {
80
79
  .replace((0, regex_parser_1.default)(existsRedirect.pattern), existsRedirect.target)
81
80
  .replace(/^\/\//, '/')
82
81
  .split('?');
83
- url.pathname = target[0];
82
+ url.pathname = `${target[0]}`;
84
83
  if (target[1]) {
85
- const newParams = new URLSearchParams(target[1]);
86
- for (const [key, val] of newParams.entries()) {
87
- url.searchParams.append(key, val);
88
- }
84
+ url.search = `?${target[1]}`;
89
85
  }
86
+ const newURL = new URL(`${url.pathname}${existsRedirect.isQueryStringPreserved ? url.search : ''}`, url.origin);
87
+ url.href = newURL.href;
90
88
  }
91
89
  const redirectUrl = decodeURIComponent(url.href);
92
90
  /** return Response redirect with http code of redirect type **/
93
91
  switch (existsRedirect.redirectType) {
94
92
  case site_1.REDIRECT_TYPE_301:
95
- return server_1.NextResponse.redirect(redirectUrl, {
96
- status: 301,
97
- statusText: 'Moved Permanently',
98
- headers: res === null || res === void 0 ? void 0 : res.headers,
99
- });
93
+ return server_1.NextResponse.redirect(redirectUrl, Object.assign(Object.assign({}, res), { status: 301, statusText: 'Moved Permanently', headers: res === null || res === void 0 ? void 0 : res.headers }));
100
94
  case site_1.REDIRECT_TYPE_302:
101
- return server_1.NextResponse.redirect(redirectUrl, {
102
- status: 302,
103
- statusText: 'Found',
104
- headers: res === null || res === void 0 ? void 0 : res.headers,
105
- });
106
- case site_1.REDIRECT_TYPE_SERVER_TRANSFER:
107
- return server_1.NextResponse.rewrite(redirectUrl, res);
95
+ return server_1.NextResponse.redirect(redirectUrl, Object.assign(Object.assign({}, res), { status: 302, statusText: 'Found', headers: res === null || res === void 0 ? void 0 : res.headers }));
96
+ case site_1.REDIRECT_TYPE_SERVER_TRANSFER: {
97
+ return this.rewrite(redirectUrl, req, res || server_1.NextResponse.next());
98
+ }
108
99
  default:
109
100
  return res || server_1.NextResponse.next();
110
101
  }
@@ -149,8 +140,9 @@ class RedirectsMiddleware extends middleware_1.MiddlewareBase {
149
140
  getExistsRedirect(req, siteName) {
150
141
  return __awaiter(this, void 0, void 0, function* () {
151
142
  const redirects = yield this.redirectsService.fetchRedirects(siteName);
152
- const tragetURL = req.nextUrl.pathname;
153
- const targetQS = req.nextUrl.search || '';
143
+ const normalizedUrl = new URL(this.normalizeUrl(req.nextUrl.pathname, req.nextUrl.search || ''), req.nextUrl.href);
144
+ const tragetURL = normalizedUrl.pathname;
145
+ const targetQS = normalizedUrl.search || '';
154
146
  const language = this.getLanguage(req);
155
147
  const modifyRedirects = structuredClone(redirects);
156
148
  return modifyRedirects.length
@@ -173,5 +165,45 @@ class RedirectsMiddleware extends middleware_1.MiddlewareBase {
173
165
  : undefined;
174
166
  });
175
167
  }
168
+ /**
169
+ * When a user clicks on a link generated by the Link component from next/link,
170
+ * Next.js adds special parameters in the route called path.
171
+ * This method removes these special parameters.
172
+ * @param {string} pathname
173
+ * @param {string} queryString
174
+ * @returns {string} modified url
175
+ */
176
+ normalizeUrl(pathname, queryString) {
177
+ if (!queryString) {
178
+ return pathname;
179
+ }
180
+ /**
181
+ * Prepare special parameters for exclusion.
182
+ */
183
+ const splittedPathname = pathname
184
+ .split('/')
185
+ .filter((route) => route)
186
+ .map((route) => `path=${route}`);
187
+ /**
188
+ * Remove special parameters(Next.JS)
189
+ * Example: /about/contact/us
190
+ * When a user clicks on this link, Next.js should generate a link for the middleware, formatted like this:
191
+ * http://host/about/contact/us?path=about&path=contact&path=us
192
+ */
193
+ const newQueryString = queryString
194
+ .replace(/^\?/, '')
195
+ .split('&')
196
+ .filter((param) => {
197
+ if (!splittedPathname.includes(param)) {
198
+ return param;
199
+ }
200
+ return false;
201
+ })
202
+ .join('&');
203
+ if (newQueryString) {
204
+ return `${pathname}?${newQueryString}`;
205
+ }
206
+ return pathname;
207
+ }
176
208
  }
177
209
  exports.RedirectsMiddleware = RedirectsMiddleware;
@@ -9,4 +9,4 @@ export const EDITING_PASS_THROUGH_HEADERS = ['authorization', 'cookie'];
9
9
  /**
10
10
  * Default allowed origins for editing requests. This is used to enforce CORS, CSP headers.
11
11
  */
12
- export const EDITING_ALLOWED_ORIGINS = ['https://pages*.cloud', 'https://pages.sitecorecloud.io'];
12
+ export const EDITING_ALLOWED_ORIGINS = ['https://pages.sitecorecloud.io'];
@@ -219,7 +219,6 @@ export class MetadataHandler {
219
219
  },
220
220
  // Cache the preview data for 3 seconds to ensure the page is rendered with the correct preview data not the cached one
221
221
  {
222
- path: query.route,
223
222
  maxAge: 3,
224
223
  });
225
224
  // Cookies with the SameSite=Lax policy set by Next.js setPreviewData function causes CORS issue
@@ -63,8 +63,7 @@ export class RedirectsMiddleware extends MiddlewareBase {
63
63
  url.href = existsRedirect.target;
64
64
  }
65
65
  else {
66
- const source = `${url.pathname}${url.search}`;
67
- url.search = existsRedirect.isQueryStringPreserved ? url.search : '';
66
+ const source = this.normalizeUrl(url.pathname, url.search);
68
67
  const urlFirstPart = existsRedirect.target.split('/')[1];
69
68
  if (this.locales.includes(urlFirstPart)) {
70
69
  url.locale = urlFirstPart;
@@ -74,31 +73,23 @@ export class RedirectsMiddleware extends MiddlewareBase {
74
73
  .replace(regexParser(existsRedirect.pattern), existsRedirect.target)
75
74
  .replace(/^\/\//, '/')
76
75
  .split('?');
77
- url.pathname = target[0];
76
+ url.pathname = `${target[0]}`;
78
77
  if (target[1]) {
79
- const newParams = new URLSearchParams(target[1]);
80
- for (const [key, val] of newParams.entries()) {
81
- url.searchParams.append(key, val);
82
- }
78
+ url.search = `?${target[1]}`;
83
79
  }
80
+ const newURL = new URL(`${url.pathname}${existsRedirect.isQueryStringPreserved ? url.search : ''}`, url.origin);
81
+ url.href = newURL.href;
84
82
  }
85
83
  const redirectUrl = decodeURIComponent(url.href);
86
84
  /** return Response redirect with http code of redirect type **/
87
85
  switch (existsRedirect.redirectType) {
88
86
  case REDIRECT_TYPE_301:
89
- return NextResponse.redirect(redirectUrl, {
90
- status: 301,
91
- statusText: 'Moved Permanently',
92
- headers: res === null || res === void 0 ? void 0 : res.headers,
93
- });
87
+ return NextResponse.redirect(redirectUrl, Object.assign(Object.assign({}, res), { status: 301, statusText: 'Moved Permanently', headers: res === null || res === void 0 ? void 0 : res.headers }));
94
88
  case REDIRECT_TYPE_302:
95
- return NextResponse.redirect(redirectUrl, {
96
- status: 302,
97
- statusText: 'Found',
98
- headers: res === null || res === void 0 ? void 0 : res.headers,
99
- });
100
- case REDIRECT_TYPE_SERVER_TRANSFER:
101
- return NextResponse.rewrite(redirectUrl, res);
89
+ return NextResponse.redirect(redirectUrl, Object.assign(Object.assign({}, res), { status: 302, statusText: 'Found', headers: res === null || res === void 0 ? void 0 : res.headers }));
90
+ case REDIRECT_TYPE_SERVER_TRANSFER: {
91
+ return this.rewrite(redirectUrl, req, res || NextResponse.next());
92
+ }
102
93
  default:
103
94
  return res || NextResponse.next();
104
95
  }
@@ -143,8 +134,9 @@ export class RedirectsMiddleware extends MiddlewareBase {
143
134
  getExistsRedirect(req, siteName) {
144
135
  return __awaiter(this, void 0, void 0, function* () {
145
136
  const redirects = yield this.redirectsService.fetchRedirects(siteName);
146
- const tragetURL = req.nextUrl.pathname;
147
- const targetQS = req.nextUrl.search || '';
137
+ const normalizedUrl = new URL(this.normalizeUrl(req.nextUrl.pathname, req.nextUrl.search || ''), req.nextUrl.href);
138
+ const tragetURL = normalizedUrl.pathname;
139
+ const targetQS = normalizedUrl.search || '';
148
140
  const language = this.getLanguage(req);
149
141
  const modifyRedirects = structuredClone(redirects);
150
142
  return modifyRedirects.length
@@ -167,4 +159,44 @@ export class RedirectsMiddleware extends MiddlewareBase {
167
159
  : undefined;
168
160
  });
169
161
  }
162
+ /**
163
+ * When a user clicks on a link generated by the Link component from next/link,
164
+ * Next.js adds special parameters in the route called path.
165
+ * This method removes these special parameters.
166
+ * @param {string} pathname
167
+ * @param {string} queryString
168
+ * @returns {string} modified url
169
+ */
170
+ normalizeUrl(pathname, queryString) {
171
+ if (!queryString) {
172
+ return pathname;
173
+ }
174
+ /**
175
+ * Prepare special parameters for exclusion.
176
+ */
177
+ const splittedPathname = pathname
178
+ .split('/')
179
+ .filter((route) => route)
180
+ .map((route) => `path=${route}`);
181
+ /**
182
+ * Remove special parameters(Next.JS)
183
+ * Example: /about/contact/us
184
+ * When a user clicks on this link, Next.js should generate a link for the middleware, formatted like this:
185
+ * http://host/about/contact/us?path=about&path=contact&path=us
186
+ */
187
+ const newQueryString = queryString
188
+ .replace(/^\?/, '')
189
+ .split('&')
190
+ .filter((param) => {
191
+ if (!splittedPathname.includes(param)) {
192
+ return param;
193
+ }
194
+ return false;
195
+ })
196
+ .join('&');
197
+ if (newQueryString) {
198
+ return `${pathname}?${newQueryString}`;
199
+ }
200
+ return pathname;
201
+ }
170
202
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sitecore-jss/sitecore-jss-nextjs",
3
- "version": "22.2.0-canary.2",
3
+ "version": "22.2.0-canary.20",
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.2",
76
- "@sitecore-jss/sitecore-jss-dev-tools": "^22.2.0-canary.2",
77
- "@sitecore-jss/sitecore-jss-react": "^22.2.0-canary.2",
75
+ "@sitecore-jss/sitecore-jss": "^22.2.0-canary.20",
76
+ "@sitecore-jss/sitecore-jss-dev-tools": "^22.2.0-canary.20",
77
+ "@sitecore-jss/sitecore-jss-react": "^22.2.0-canary.20",
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": "ba1b71ef1209c0e3bdd3557d315a0e2a46ba4580",
85
+ "gitHead": "c525b47d1480e3e9ba40751f8493513f59fe25d0",
86
86
  "files": [
87
87
  "dist",
88
88
  "types",
@@ -37,4 +37,13 @@ export declare class RedirectsMiddleware extends MiddlewareBase {
37
37
  * @private
38
38
  */
39
39
  private getExistsRedirect;
40
+ /**
41
+ * When a user clicks on a link generated by the Link component from next/link,
42
+ * Next.js adds special parameters in the route called path.
43
+ * This method removes these special parameters.
44
+ * @param {string} pathname
45
+ * @param {string} queryString
46
+ * @returns {string} modified url
47
+ */
48
+ private normalizeUrl;
40
49
  }