@sitecore-jss/sitecore-jss-nextjs 21.1.0-canary.40 → 21.1.0-canary.42

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.
@@ -25,7 +25,13 @@ class RedirectsMiddleware {
25
25
  * @param {RedirectsMiddlewareConfig} [config] redirects middleware config
26
26
  */
27
27
  constructor(config) {
28
+ this.config = config;
28
29
  this.handler = (req) => __awaiter(this, void 0, void 0, function* () {
30
+ if ((this.config.disabled && this.config.disabled(req, server_1.NextResponse.next())) ||
31
+ this.excludeRoute(req.nextUrl.pathname) ||
32
+ (this.config.excludeRoute && this.config.excludeRoute(req.nextUrl.pathname))) {
33
+ return server_1.NextResponse.next();
34
+ }
29
35
  // Find the redirect from result of RedirectService
30
36
  const existsRedirect = yield this.getExistsRedirect(req);
31
37
  if (!existsRedirect) {
@@ -73,6 +79,16 @@ class RedirectsMiddleware {
73
79
  getHandler() {
74
80
  return this.handler;
75
81
  }
82
+ excludeRoute(pathname) {
83
+ if (pathname.includes('.') || // Ignore files
84
+ pathname.startsWith('/api/') || // Ignore Next.js API calls
85
+ pathname.startsWith('/sitecore/') || // Ignore Sitecore API calls
86
+ pathname.startsWith('/_next') // Ignore next service calls
87
+ ) {
88
+ return true;
89
+ }
90
+ return false;
91
+ }
76
92
  /**
77
93
  * Method returns RedirectInfo when matches
78
94
  * @param {NextRequest} req
@@ -82,13 +98,15 @@ class RedirectsMiddleware {
82
98
  getExistsRedirect(req) {
83
99
  return __awaiter(this, void 0, void 0, function* () {
84
100
  const redirects = yield this.redirectsService.fetchRedirects();
85
- return redirects.find((redirect) => {
86
- return ((regex_parser_1.default(redirect.pattern.toLowerCase()).test(req.nextUrl.pathname.toLowerCase()) ||
87
- regex_parser_1.default(redirect.pattern.toLowerCase()).test(`/${req.nextUrl.locale}${req.nextUrl.pathname}`.toLowerCase())) &&
88
- (redirect.locale
89
- ? redirect.locale.toLowerCase() === req.nextUrl.locale.toLowerCase()
90
- : true));
91
- });
101
+ return redirects.length
102
+ ? redirects.find((redirect) => {
103
+ return ((regex_parser_1.default(redirect.pattern.toLowerCase()).test(req.nextUrl.pathname.toLowerCase()) ||
104
+ regex_parser_1.default(redirect.pattern.toLowerCase()).test(`/${req.nextUrl.locale}${req.nextUrl.pathname}`.toLowerCase())) &&
105
+ (redirect.locale
106
+ ? redirect.locale.toLowerCase() === req.nextUrl.locale.toLowerCase()
107
+ : true));
108
+ })
109
+ : undefined;
92
110
  });
93
111
  }
94
112
  }
@@ -19,7 +19,13 @@ export class RedirectsMiddleware {
19
19
  * @param {RedirectsMiddlewareConfig} [config] redirects middleware config
20
20
  */
21
21
  constructor(config) {
22
+ this.config = config;
22
23
  this.handler = (req) => __awaiter(this, void 0, void 0, function* () {
24
+ if ((this.config.disabled && this.config.disabled(req, NextResponse.next())) ||
25
+ this.excludeRoute(req.nextUrl.pathname) ||
26
+ (this.config.excludeRoute && this.config.excludeRoute(req.nextUrl.pathname))) {
27
+ return NextResponse.next();
28
+ }
23
29
  // Find the redirect from result of RedirectService
24
30
  const existsRedirect = yield this.getExistsRedirect(req);
25
31
  if (!existsRedirect) {
@@ -67,6 +73,16 @@ export class RedirectsMiddleware {
67
73
  getHandler() {
68
74
  return this.handler;
69
75
  }
76
+ excludeRoute(pathname) {
77
+ if (pathname.includes('.') || // Ignore files
78
+ pathname.startsWith('/api/') || // Ignore Next.js API calls
79
+ pathname.startsWith('/sitecore/') || // Ignore Sitecore API calls
80
+ pathname.startsWith('/_next') // Ignore next service calls
81
+ ) {
82
+ return true;
83
+ }
84
+ return false;
85
+ }
70
86
  /**
71
87
  * Method returns RedirectInfo when matches
72
88
  * @param {NextRequest} req
@@ -76,13 +92,15 @@ export class RedirectsMiddleware {
76
92
  getExistsRedirect(req) {
77
93
  return __awaiter(this, void 0, void 0, function* () {
78
94
  const redirects = yield this.redirectsService.fetchRedirects();
79
- return redirects.find((redirect) => {
80
- return ((regexParser(redirect.pattern.toLowerCase()).test(req.nextUrl.pathname.toLowerCase()) ||
81
- regexParser(redirect.pattern.toLowerCase()).test(`/${req.nextUrl.locale}${req.nextUrl.pathname}`.toLowerCase())) &&
82
- (redirect.locale
83
- ? redirect.locale.toLowerCase() === req.nextUrl.locale.toLowerCase()
84
- : true));
85
- });
95
+ return redirects.length
96
+ ? redirects.find((redirect) => {
97
+ return ((regexParser(redirect.pattern.toLowerCase()).test(req.nextUrl.pathname.toLowerCase()) ||
98
+ regexParser(redirect.pattern.toLowerCase()).test(`/${req.nextUrl.locale}${req.nextUrl.pathname}`.toLowerCase())) &&
99
+ (redirect.locale
100
+ ? redirect.locale.toLowerCase() === req.nextUrl.locale.toLowerCase()
101
+ : true));
102
+ })
103
+ : undefined;
86
104
  });
87
105
  }
88
106
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sitecore-jss/sitecore-jss-nextjs",
3
- "version": "21.1.0-canary.40",
3
+ "version": "21.1.0-canary.42",
4
4
  "main": "dist/cjs/index.js",
5
5
  "module": "dist/esm/index.js",
6
6
  "sideEffects": false,
@@ -70,9 +70,9 @@
70
70
  "react-dom": "^18.1.0"
71
71
  },
72
72
  "dependencies": {
73
- "@sitecore-jss/sitecore-jss": "^21.1.0-canary.40",
74
- "@sitecore-jss/sitecore-jss-dev-tools": "^21.1.0-canary.40",
75
- "@sitecore-jss/sitecore-jss-react": "^21.1.0-canary.40",
73
+ "@sitecore-jss/sitecore-jss": "^21.1.0-canary.42",
74
+ "@sitecore-jss/sitecore-jss-dev-tools": "^21.1.0-canary.42",
75
+ "@sitecore-jss/sitecore-jss-react": "^21.1.0-canary.42",
76
76
  "node-html-parser": "^6.0.0",
77
77
  "prop-types": "^15.7.2",
78
78
  "regex-parser": "^2.2.11",
@@ -80,7 +80,7 @@
80
80
  },
81
81
  "description": "",
82
82
  "types": "types/index.d.ts",
83
- "gitHead": "9521debb96bd4621eae47e230df42465904f2159",
83
+ "gitHead": "ea47c3f3fbfb0f54968b48d806486f914ca66c94",
84
84
  "files": [
85
85
  "dist",
86
86
  "types",
@@ -5,12 +5,28 @@ import { GraphQLRedirectsServiceConfig } from '@sitecore-jss/sitecore-jss/site';
5
5
  */
6
6
  export declare type RedirectsMiddlewareConfig = Omit<GraphQLRedirectsServiceConfig, 'fetch'> & {
7
7
  locales: string[];
8
+ /**
9
+ * Function used to determine if route should be excluded from RedirectsMiddleware.
10
+ * By default, files (pathname.includes('.')), Next.js API routes (pathname.startsWith('/api/')), and Sitecore API routes (pathname.startsWith('/sitecore/')) are ignored.
11
+ * This is an important performance consideration since Next.js Edge middleware runs on every request.
12
+ * @param {string} pathname The pathname
13
+ * @returns {boolean} Whether to exclude the route from RedirectsMiddleware
14
+ */
15
+ excludeRoute?: (pathname: string) => boolean;
16
+ /**
17
+ * function, determines if middleware should be turned off, based on cookie, header, or other considerations
18
+ * @param {NextRequest} [req] optional: request object from middleware handler
19
+ * @param {NextResponse} [res] optional: response object from middleware handler
20
+ * @returns {boolean} false by default
21
+ */
22
+ disabled?: (req?: NextRequest, res?: NextResponse) => boolean;
8
23
  };
9
24
  /**
10
25
  * Middleware / handler fetches all redirects from Sitecore instance by grapqhl service
11
26
  * compares with current url and redirects to target url
12
27
  */
13
28
  export declare class RedirectsMiddleware {
29
+ protected config: RedirectsMiddlewareConfig;
14
30
  private redirectsService;
15
31
  private locales;
16
32
  /**
@@ -22,6 +38,7 @@ export declare class RedirectsMiddleware {
22
38
  * @returns route handler
23
39
  */
24
40
  getHandler(): (req: NextRequest) => Promise<NextResponse>;
41
+ protected excludeRoute(pathname: string): boolean;
25
42
  private handler;
26
43
  /**
27
44
  * Method returns RedirectInfo when matches