@sitecore-jss/sitecore-jss-nextjs 22.7.0-canary.9 → 22.8.0-beta.0

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.
@@ -24,11 +24,20 @@ class MiddlewareBase {
24
24
  * @returns {boolean} is prefetch
25
25
  */
26
26
  isPrefetch(req) {
27
- return (
28
- // eslint-disable-next-line prettier/prettier
29
- req.headers.get('purpose') === 'prefetch' || // Pages Router
30
- req.headers.get('Next-Router-Prefetch') === '1' // App Router
31
- );
27
+ const isMobile = req.headers.get('sec-ch-ua-mobile') === '?1';
28
+ const userAgent = req.headers.get('user-agent') || '';
29
+ const isKnownPlatform = /iPhone|Mac|Linux|Windows|Android/i.test(userAgent);
30
+ const isKnownDevice = isMobile || isKnownPlatform;
31
+ const purpose = req.headers.get('purpose');
32
+ const nextRouterPrefetch = req.headers.get('Next-Router-Prefetch');
33
+ const middlewarePrefetch = req.headers.get('x-middleware-prefetch');
34
+ // Some real navigations on different devices may incorrectly include 'prefetch' headers.
35
+ // To avoid skipping personalization in such cases, we treat 'x-middleware-prefetch' as a more reliable signal of true prefetch behavior.
36
+ if (isKnownDevice && middlewarePrefetch === '1') {
37
+ return false;
38
+ }
39
+ // Otherwise, standard prefetch detection
40
+ return purpose === 'prefetch' || nextRouterPrefetch === '1' || middlewarePrefetch === '1';
32
41
  }
33
42
  excludeRoute(pathname) {
34
43
  var _a, _b;
@@ -26,7 +26,7 @@ class PersonalizeMiddleware extends middleware_1.MiddlewareBase {
26
26
  constructor(config) {
27
27
  super(config);
28
28
  this.config = config;
29
- this.handler = (req, res, options) => __awaiter(this, void 0, void 0, function* () {
29
+ this.processPersonalizationRequest = (req, res, options) => __awaiter(this, void 0, void 0, function* () {
30
30
  const pathname = req.nextUrl.pathname;
31
31
  const language = this.getLanguage(req);
32
32
  const hostname = this.getHostHeader(req) || this.defaultHostname;
@@ -52,7 +52,7 @@ class PersonalizeMiddleware extends middleware_1.MiddlewareBase {
52
52
  }
53
53
  const site = this.getSite(req, response);
54
54
  // Get personalization info from Experience Edge
55
- const personalizeInfo = yield this.personalizeService.getPersonalizeInfo(pathname, language, site.name);
55
+ const personalizeInfo = yield this.getPersonalizeInfo(pathname, language, site.name);
56
56
  if (!personalizeInfo) {
57
57
  // Likely an invalid route / language
58
58
  sitecore_jss_1.debug.personalize('skipped (personalize info not found)');
@@ -69,6 +69,7 @@ class PersonalizeMiddleware extends middleware_1.MiddlewareBase {
69
69
  // Disable preflight caching to force revalidation on client-side navigation (personalization WILL be influenced).
70
70
  // Note the reason we don't move this any earlier in the middleware is that we would then be sacrificing performance for non-personalized pages.
71
71
  response.headers.set('x-middleware-cache', 'no-cache');
72
+ response.headers.set('Cache-Control', 'no-store, must-revalidate');
72
73
  return response;
73
74
  }
74
75
  yield this.initPersonalizeServer({
@@ -127,7 +128,7 @@ class PersonalizeMiddleware extends middleware_1.MiddlewareBase {
127
128
  getHandler() {
128
129
  return (req, res, options) => __awaiter(this, void 0, void 0, function* () {
129
130
  try {
130
- return yield this.handler(req, res, options);
131
+ return yield this.processPersonalizationRequest(req, res, options);
131
132
  }
132
133
  catch (error) {
133
134
  console.log('Personalize middleware failed:');
@@ -229,5 +230,10 @@ class PersonalizeMiddleware extends middleware_1.MiddlewareBase {
229
230
  return results;
230
231
  }, results);
231
232
  }
233
+ getPersonalizeInfo(pathname, language, siteName) {
234
+ return __awaiter(this, void 0, void 0, function* () {
235
+ return this.personalizeService.getPersonalizeInfo(pathname, language, siteName);
236
+ });
237
+ }
232
238
  }
233
239
  exports.PersonalizeMiddleware = PersonalizeMiddleware;
@@ -65,7 +65,7 @@ class RedirectsMiddleware extends middleware_1.MiddlewareBase {
65
65
  const { pathname: incomingURL, search: incomingQS = '' } = this.normalizeUrl(req.nextUrl.clone());
66
66
  const locale = this.getLanguage(req);
67
67
  const normalizedPath = incomingURL.replace(/\/*$/gi, '');
68
- const redirects = yield this.redirectsService.fetchRedirects(siteName);
68
+ const redirects = yield this.getRedirects(siteName);
69
69
  const language = this.getLanguage(req);
70
70
  const modifyRedirects = structuredClone(redirects);
71
71
  let matchedQueryString;
@@ -221,6 +221,17 @@ class RedirectsMiddleware extends middleware_1.MiddlewareBase {
221
221
  return response;
222
222
  });
223
223
  }
224
+ /**
225
+ * Fetches all redirects for a given site from the Sitecore instance
226
+ * @param {string} siteName - The name of the site to fetch redirects for
227
+ * @returns {Promise<RedirectInfo[]>} A promise that resolves to an array of redirect information
228
+ * @protected
229
+ */
230
+ getRedirects(siteName) {
231
+ return __awaiter(this, void 0, void 0, function* () {
232
+ return yield this.redirectsService.fetchRedirects(siteName);
233
+ });
234
+ }
224
235
  /**
225
236
  * When a user clicks on a link generated by the Link component from next/link,
226
237
  * Next.js adds special parameters in the route called path.
@@ -21,11 +21,20 @@ export class MiddlewareBase {
21
21
  * @returns {boolean} is prefetch
22
22
  */
23
23
  isPrefetch(req) {
24
- return (
25
- // eslint-disable-next-line prettier/prettier
26
- req.headers.get('purpose') === 'prefetch' || // Pages Router
27
- req.headers.get('Next-Router-Prefetch') === '1' // App Router
28
- );
24
+ const isMobile = req.headers.get('sec-ch-ua-mobile') === '?1';
25
+ const userAgent = req.headers.get('user-agent') || '';
26
+ const isKnownPlatform = /iPhone|Mac|Linux|Windows|Android/i.test(userAgent);
27
+ const isKnownDevice = isMobile || isKnownPlatform;
28
+ const purpose = req.headers.get('purpose');
29
+ const nextRouterPrefetch = req.headers.get('Next-Router-Prefetch');
30
+ const middlewarePrefetch = req.headers.get('x-middleware-prefetch');
31
+ // Some real navigations on different devices may incorrectly include 'prefetch' headers.
32
+ // To avoid skipping personalization in such cases, we treat 'x-middleware-prefetch' as a more reliable signal of true prefetch behavior.
33
+ if (isKnownDevice && middlewarePrefetch === '1') {
34
+ return false;
35
+ }
36
+ // Otherwise, standard prefetch detection
37
+ return purpose === 'prefetch' || nextRouterPrefetch === '1' || middlewarePrefetch === '1';
29
38
  }
30
39
  excludeRoute(pathname) {
31
40
  var _a, _b;
@@ -23,7 +23,7 @@ export class PersonalizeMiddleware extends MiddlewareBase {
23
23
  constructor(config) {
24
24
  super(config);
25
25
  this.config = config;
26
- this.handler = (req, res, options) => __awaiter(this, void 0, void 0, function* () {
26
+ this.processPersonalizationRequest = (req, res, options) => __awaiter(this, void 0, void 0, function* () {
27
27
  const pathname = req.nextUrl.pathname;
28
28
  const language = this.getLanguage(req);
29
29
  const hostname = this.getHostHeader(req) || this.defaultHostname;
@@ -49,7 +49,7 @@ export class PersonalizeMiddleware extends MiddlewareBase {
49
49
  }
50
50
  const site = this.getSite(req, response);
51
51
  // Get personalization info from Experience Edge
52
- const personalizeInfo = yield this.personalizeService.getPersonalizeInfo(pathname, language, site.name);
52
+ const personalizeInfo = yield this.getPersonalizeInfo(pathname, language, site.name);
53
53
  if (!personalizeInfo) {
54
54
  // Likely an invalid route / language
55
55
  debug.personalize('skipped (personalize info not found)');
@@ -66,6 +66,7 @@ export class PersonalizeMiddleware extends MiddlewareBase {
66
66
  // Disable preflight caching to force revalidation on client-side navigation (personalization WILL be influenced).
67
67
  // Note the reason we don't move this any earlier in the middleware is that we would then be sacrificing performance for non-personalized pages.
68
68
  response.headers.set('x-middleware-cache', 'no-cache');
69
+ response.headers.set('Cache-Control', 'no-store, must-revalidate');
69
70
  return response;
70
71
  }
71
72
  yield this.initPersonalizeServer({
@@ -124,7 +125,7 @@ export class PersonalizeMiddleware extends MiddlewareBase {
124
125
  getHandler() {
125
126
  return (req, res, options) => __awaiter(this, void 0, void 0, function* () {
126
127
  try {
127
- return yield this.handler(req, res, options);
128
+ return yield this.processPersonalizationRequest(req, res, options);
128
129
  }
129
130
  catch (error) {
130
131
  console.log('Personalize middleware failed:');
@@ -226,4 +227,9 @@ export class PersonalizeMiddleware extends MiddlewareBase {
226
227
  return results;
227
228
  }, results);
228
229
  }
230
+ getPersonalizeInfo(pathname, language, siteName) {
231
+ return __awaiter(this, void 0, void 0, function* () {
232
+ return this.personalizeService.getPersonalizeInfo(pathname, language, siteName);
233
+ });
234
+ }
229
235
  }
@@ -59,7 +59,7 @@ export class RedirectsMiddleware extends MiddlewareBase {
59
59
  const { pathname: incomingURL, search: incomingQS = '' } = this.normalizeUrl(req.nextUrl.clone());
60
60
  const locale = this.getLanguage(req);
61
61
  const normalizedPath = incomingURL.replace(/\/*$/gi, '');
62
- const redirects = yield this.redirectsService.fetchRedirects(siteName);
62
+ const redirects = yield this.getRedirects(siteName);
63
63
  const language = this.getLanguage(req);
64
64
  const modifyRedirects = structuredClone(redirects);
65
65
  let matchedQueryString;
@@ -215,6 +215,17 @@ export class RedirectsMiddleware extends MiddlewareBase {
215
215
  return response;
216
216
  });
217
217
  }
218
+ /**
219
+ * Fetches all redirects for a given site from the Sitecore instance
220
+ * @param {string} siteName - The name of the site to fetch redirects for
221
+ * @returns {Promise<RedirectInfo[]>} A promise that resolves to an array of redirect information
222
+ * @protected
223
+ */
224
+ getRedirects(siteName) {
225
+ return __awaiter(this, void 0, void 0, function* () {
226
+ return yield this.redirectsService.fetchRedirects(siteName);
227
+ });
228
+ }
218
229
  /**
219
230
  * When a user clicks on a link generated by the Link component from next/link,
220
231
  * Next.js adds special parameters in the route called path.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sitecore-jss/sitecore-jss-nextjs",
3
- "version": "22.7.0-canary.9",
3
+ "version": "22.8.0-beta.0",
4
4
  "main": "dist/cjs/index.js",
5
5
  "module": "dist/esm/index.js",
6
6
  "sideEffects": false,
@@ -72,16 +72,16 @@
72
72
  "react-dom": "^19.1.0"
73
73
  },
74
74
  "dependencies": {
75
- "@sitecore-jss/sitecore-jss": "22.7.0-canary.9",
76
- "@sitecore-jss/sitecore-jss-dev-tools": "22.7.0-canary.9",
77
- "@sitecore-jss/sitecore-jss-react": "22.7.0-canary.9",
75
+ "@sitecore-jss/sitecore-jss": "22.8.0-beta.0",
76
+ "@sitecore-jss/sitecore-jss-dev-tools": "22.8.0-beta.0",
77
+ "@sitecore-jss/sitecore-jss-react": "22.8.0-beta.0",
78
78
  "@vercel/kv": "^0.2.1",
79
79
  "regex-parser": "^2.2.11",
80
80
  "sync-disk-cache": "^2.1.0"
81
81
  },
82
82
  "description": "",
83
83
  "types": "types/index.d.ts",
84
- "gitHead": "93198b6321eb6be9882cb5888826e741925a770d",
84
+ "gitHead": "2d1e3b85231daf9f632180483b35f0b2041987c6",
85
85
  "files": [
86
86
  "dist",
87
87
  "types",
@@ -115,6 +115,7 @@ export declare class PersonalizeMiddleware extends MiddlewareBase {
115
115
  * @returns An array of personalize executions
116
116
  */
117
117
  protected getPersonalizeExecutions(personalizeInfo: PersonalizeInfo, language: string): PersonalizeExecution[];
118
- private handler;
118
+ protected processPersonalizationRequest: (req: NextRequest, res?: NextResponse, options?: PersonalizeOptions) => Promise<NextResponse>;
119
+ protected getPersonalizeInfo(pathname: string, language: string, siteName: string): Promise<PersonalizeInfo | undefined>;
119
120
  }
120
121
  export {};
@@ -45,6 +45,13 @@ export declare class RedirectsMiddleware extends MiddlewareBase {
45
45
  * @returns {Promise<NextResponse>} The redirect response.
46
46
  */
47
47
  protected processRedirectRequest(req: NextRequest, res?: NextResponse): Promise<NextResponse>;
48
+ /**
49
+ * Fetches all redirects for a given site from the Sitecore instance
50
+ * @param {string} siteName - The name of the site to fetch redirects for
51
+ * @returns {Promise<RedirectInfo[]>} A promise that resolves to an array of redirect information
52
+ * @protected
53
+ */
54
+ protected getRedirects(siteName: string): Promise<RedirectInfo[]>;
48
55
  /**
49
56
  * When a user clicks on a link generated by the Link component from next/link,
50
57
  * Next.js adds special parameters in the route called path.