@sitecore-jss/sitecore-jss-nextjs 21.1.0-canary.99 → 21.1.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.
- package/LICENSE.txt +202 -202
- package/README.md +10 -10
- package/dist/cjs/components/NextImage.js +8 -2
- package/dist/cjs/index.js +7 -2
- package/dist/cjs/middleware/index.js +3 -1
- package/dist/cjs/middleware/middleware.js +70 -0
- package/dist/cjs/middleware/multisite-middleware.js +13 -30
- package/dist/cjs/middleware/personalize-middleware.js +14 -40
- package/dist/cjs/middleware/redirects-middleware.js +30 -25
- package/dist/cjs/services/base-graphql-sitemap-service.js +204 -0
- package/dist/cjs/services/graphql-sitemap-service.js +10 -176
- package/dist/cjs/services/mutisite-graphql-sitemap-service.js +81 -0
- package/dist/esm/components/NextImage.js +8 -2
- package/dist/esm/index.js +4 -3
- package/dist/esm/middleware/index.js +1 -0
- package/dist/esm/middleware/middleware.js +66 -0
- package/dist/esm/middleware/multisite-middleware.js +13 -30
- package/dist/esm/middleware/personalize-middleware.js +15 -41
- package/dist/esm/middleware/redirects-middleware.js +30 -25
- package/dist/esm/services/base-graphql-sitemap-service.js +199 -0
- package/dist/esm/services/graphql-sitemap-service.js +9 -175
- package/dist/esm/services/mutisite-graphql-sitemap-service.js +77 -0
- package/package.json +5 -5
- package/types/index.d.ts +4 -3
- package/types/middleware/index.d.ts +1 -0
- package/types/middleware/middleware.d.ts +68 -0
- package/types/middleware/multisite-middleware.d.ts +3 -25
- package/types/middleware/personalize-middleware.d.ts +8 -41
- package/types/middleware/redirects-middleware.d.ts +7 -29
- package/types/services/base-graphql-sitemap-service.d.ts +149 -0
- package/types/services/graphql-sitemap-service.d.ts +7 -103
- package/types/services/mutisite-graphql-sitemap-service.d.ts +42 -0
|
@@ -13,40 +13,39 @@ exports.MultisiteMiddleware = void 0;
|
|
|
13
13
|
const server_1 = require("next/server");
|
|
14
14
|
const site_1 = require("@sitecore-jss/sitecore-jss/site");
|
|
15
15
|
const sitecore_jss_1 = require("@sitecore-jss/sitecore-jss");
|
|
16
|
+
const middleware_1 = require("./middleware");
|
|
16
17
|
/**
|
|
17
18
|
* Middleware / handler for multisite support
|
|
18
19
|
*/
|
|
19
|
-
class MultisiteMiddleware {
|
|
20
|
+
class MultisiteMiddleware extends middleware_1.MiddlewareBase {
|
|
20
21
|
/**
|
|
21
22
|
* @param {MultisiteMiddlewareConfig} [config] Multisite middleware config
|
|
22
23
|
*/
|
|
23
24
|
constructor(config) {
|
|
25
|
+
super(config);
|
|
24
26
|
this.config = config;
|
|
25
27
|
this.handler = (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
26
|
-
var _a
|
|
28
|
+
var _a;
|
|
27
29
|
const pathname = req.nextUrl.pathname;
|
|
28
|
-
const
|
|
29
|
-
const hostname =
|
|
30
|
+
const language = this.getLanguage(req);
|
|
31
|
+
const hostname = this.getHostHeader(req) || this.defaultHostname;
|
|
30
32
|
sitecore_jss_1.debug.multisite('multisite middleware start: %o', {
|
|
31
33
|
pathname,
|
|
34
|
+
language,
|
|
32
35
|
hostname,
|
|
33
36
|
});
|
|
34
|
-
if (!hostHeader) {
|
|
35
|
-
sitecore_jss_1.debug.multisite(`host header is missing, default ${hostname} is used`);
|
|
36
|
-
}
|
|
37
37
|
// Response will be provided if other middleware is run before us
|
|
38
38
|
let response = res || server_1.NextResponse.next();
|
|
39
|
-
if (this.excludeRoute(pathname)
|
|
40
|
-
|
|
41
|
-
sitecore_jss_1.debug.multisite('skipped (route excluded)');
|
|
39
|
+
if (this.isPreview(req) || this.excludeRoute(pathname)) {
|
|
40
|
+
sitecore_jss_1.debug.multisite('skipped (%s)', this.isPreview(req) ? 'preview' : 'route excluded');
|
|
42
41
|
return response;
|
|
43
42
|
}
|
|
44
43
|
// Site name can be forced by query string parameter or cookie
|
|
45
|
-
const siteName = req.nextUrl.searchParams.get(
|
|
44
|
+
const siteName = req.nextUrl.searchParams.get(this.SITE_SYMBOL) ||
|
|
46
45
|
(this.config.useCookieResolution &&
|
|
47
46
|
this.config.useCookieResolution(req) &&
|
|
48
|
-
((
|
|
49
|
-
this.config.
|
|
47
|
+
((_a = req.cookies.get(this.SITE_SYMBOL)) === null || _a === void 0 ? void 0 : _a.value)) ||
|
|
48
|
+
this.config.siteResolver.getByHost(hostname).name;
|
|
50
49
|
// Rewrite to site specific path
|
|
51
50
|
const rewritePath = (0, site_1.getSiteRewrite)(pathname, {
|
|
52
51
|
siteName,
|
|
@@ -56,7 +55,7 @@ class MultisiteMiddleware {
|
|
|
56
55
|
rewriteUrl.pathname = rewritePath;
|
|
57
56
|
response = server_1.NextResponse.rewrite(rewriteUrl);
|
|
58
57
|
// Share site name with the following executed middlewares
|
|
59
|
-
response.cookies.set(
|
|
58
|
+
response.cookies.set(this.SITE_SYMBOL, siteName);
|
|
60
59
|
// Share rewrite path with following executed middlewares
|
|
61
60
|
response.headers.set('x-sc-rewrite', rewritePath);
|
|
62
61
|
sitecore_jss_1.debug.multisite('multisite middleware end: %o', {
|
|
@@ -67,7 +66,6 @@ class MultisiteMiddleware {
|
|
|
67
66
|
});
|
|
68
67
|
return response;
|
|
69
68
|
});
|
|
70
|
-
this.defaultHostname = config.defaultHostname || 'localhost';
|
|
71
69
|
}
|
|
72
70
|
/**
|
|
73
71
|
* Gets the Next.js middleware handler with error handling
|
|
@@ -85,20 +83,5 @@ class MultisiteMiddleware {
|
|
|
85
83
|
}
|
|
86
84
|
});
|
|
87
85
|
}
|
|
88
|
-
excludeRoute(pathname) {
|
|
89
|
-
if (pathname.includes('.') || // Ignore files
|
|
90
|
-
pathname.startsWith('/api/') || // Ignore Next.js API calls
|
|
91
|
-
pathname.startsWith('/sitecore/') || // Ignore Sitecore API calls
|
|
92
|
-
pathname.startsWith('/_next') // Ignore next service calls
|
|
93
|
-
) {
|
|
94
|
-
return true;
|
|
95
|
-
}
|
|
96
|
-
return false;
|
|
97
|
-
}
|
|
98
|
-
extractDebugHeaders(incomingHeaders) {
|
|
99
|
-
const headers = {};
|
|
100
|
-
incomingHeaders.forEach((value, key) => (headers[key] = value));
|
|
101
|
-
return headers;
|
|
102
|
-
}
|
|
103
86
|
}
|
|
104
87
|
exports.MultisiteMiddleware = MultisiteMiddleware;
|
|
@@ -13,30 +13,28 @@ exports.PersonalizeMiddleware = void 0;
|
|
|
13
13
|
const server_1 = require("next/server");
|
|
14
14
|
const personalize_1 = require("@sitecore-jss/sitecore-jss/personalize");
|
|
15
15
|
const sitecore_jss_1 = require("@sitecore-jss/sitecore-jss");
|
|
16
|
+
const middleware_1 = require("./middleware");
|
|
16
17
|
/**
|
|
17
18
|
* Middleware / handler to support Sitecore Personalize
|
|
18
19
|
*/
|
|
19
|
-
class PersonalizeMiddleware {
|
|
20
|
+
class PersonalizeMiddleware extends middleware_1.MiddlewareBase {
|
|
20
21
|
/**
|
|
21
22
|
* @param {PersonalizeMiddlewareConfig} [config] Personalize middleware config
|
|
22
23
|
*/
|
|
23
24
|
constructor(config) {
|
|
25
|
+
super(config);
|
|
24
26
|
this.config = config;
|
|
25
27
|
this.handler = (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
26
|
-
var _a, _b;
|
|
27
|
-
const hostHeader = (_a = req.headers.get('host')) === null || _a === void 0 ? void 0 : _a.split(':')[0];
|
|
28
|
-
const hostname = hostHeader || this.defaultHostname;
|
|
29
28
|
const pathname = req.nextUrl.pathname;
|
|
30
|
-
const language =
|
|
31
|
-
const
|
|
29
|
+
const language = this.getLanguage(req);
|
|
30
|
+
const hostname = this.getHostHeader(req) || this.defaultHostname;
|
|
32
31
|
let browserId = this.getBrowserId(req);
|
|
33
32
|
sitecore_jss_1.debug.personalize('personalize middleware start: %o', {
|
|
34
33
|
pathname,
|
|
35
34
|
language,
|
|
35
|
+
hostname,
|
|
36
|
+
browserId,
|
|
36
37
|
});
|
|
37
|
-
if (!hostHeader) {
|
|
38
|
-
sitecore_jss_1.debug.personalize(`host header is missing, default ${hostname} is used`);
|
|
39
|
-
}
|
|
40
38
|
// Response will be provided if other middleware is run before us (e.g. redirects)
|
|
41
39
|
let response = res || server_1.NextResponse.next();
|
|
42
40
|
if (this.config.disabled && this.config.disabled(req, response)) {
|
|
@@ -45,13 +43,13 @@ class PersonalizeMiddleware {
|
|
|
45
43
|
}
|
|
46
44
|
if (response.redirected || // Don't attempt to personalize a redirect
|
|
47
45
|
this.isPreview(req) || // No need to personalize for preview (layout data is already prepared for preview)
|
|
48
|
-
this.excludeRoute(pathname)
|
|
49
|
-
(this.config.excludeRoute && this.config.excludeRoute(pathname))) {
|
|
46
|
+
this.excludeRoute(pathname)) {
|
|
50
47
|
sitecore_jss_1.debug.personalize('skipped (%s)', response.redirected ? 'redirected' : this.isPreview(req) ? 'preview' : 'route excluded');
|
|
51
48
|
return response;
|
|
52
49
|
}
|
|
50
|
+
const site = this.getSite(req, res);
|
|
53
51
|
// Get personalization info from Experience Edge
|
|
54
|
-
const personalizeInfo = yield this.personalizeService.getPersonalizeInfo(pathname, language,
|
|
52
|
+
const personalizeInfo = yield this.personalizeService.getPersonalizeInfo(pathname, language, site.name);
|
|
55
53
|
if (!personalizeInfo) {
|
|
56
54
|
// Likely an invalid route / language
|
|
57
55
|
sitecore_jss_1.debug.personalize('skipped (personalize info not found)');
|
|
@@ -71,7 +69,9 @@ class PersonalizeMiddleware {
|
|
|
71
69
|
// Execute targeted experience in CDP
|
|
72
70
|
const { ua } = (0, server_1.userAgent)(req);
|
|
73
71
|
const params = this.getExperienceParams(req);
|
|
74
|
-
const pointOfSale = this.config.getPointOfSale
|
|
72
|
+
const pointOfSale = this.config.getPointOfSale
|
|
73
|
+
? this.config.getPointOfSale(site, language)
|
|
74
|
+
: personalize_1.PosResolver.resolve(site, language);
|
|
75
75
|
const variantId = yield this.cdpService.executeExperience(personalizeInfo.contentId, browserId, ua, pointOfSale, params);
|
|
76
76
|
if (!variantId) {
|
|
77
77
|
sitecore_jss_1.debug.personalize('skipped (no variant identified)');
|
|
@@ -97,7 +97,7 @@ class PersonalizeMiddleware {
|
|
|
97
97
|
// Set browserId cookie on the response
|
|
98
98
|
this.setBrowserId(response, browserId);
|
|
99
99
|
// Share site name with the following executed middlewares
|
|
100
|
-
response.cookies.set(
|
|
100
|
+
response.cookies.set(this.SITE_SYMBOL, site.name);
|
|
101
101
|
sitecore_jss_1.debug.personalize('personalize middleware end: %o', {
|
|
102
102
|
rewritePath,
|
|
103
103
|
browserId,
|
|
@@ -117,7 +117,6 @@ class PersonalizeMiddleware {
|
|
|
117
117
|
});
|
|
118
118
|
return (url, data) => fetcher.fetch(url, data);
|
|
119
119
|
} }));
|
|
120
|
-
this.defaultHostname = config.defaultHostname || 'localhost';
|
|
121
120
|
}
|
|
122
121
|
/**
|
|
123
122
|
* Gets the Next.js middleware handler with error handling
|
|
@@ -159,30 +158,5 @@ class PersonalizeMiddleware {
|
|
|
159
158
|
},
|
|
160
159
|
};
|
|
161
160
|
}
|
|
162
|
-
excludeRoute(pathname) {
|
|
163
|
-
if (pathname.includes('.') || // Ignore files
|
|
164
|
-
pathname.startsWith('/api/') || // Ignore Next.js API calls
|
|
165
|
-
pathname.startsWith('/sitecore/') || // Ignore Sitecore API calls
|
|
166
|
-
pathname.startsWith('/_next') // Ignore next service calls
|
|
167
|
-
) {
|
|
168
|
-
return true;
|
|
169
|
-
}
|
|
170
|
-
return false;
|
|
171
|
-
}
|
|
172
|
-
isPreview(req) {
|
|
173
|
-
var _a, _b;
|
|
174
|
-
return (((_a = req.cookies.get('__prerender_bypass')) === null || _a === void 0 ? void 0 : _a.value) || ((_b = req.cookies.get('__next_preview_data')) === null || _b === void 0 ? void 0 : _b.value));
|
|
175
|
-
}
|
|
176
|
-
/**
|
|
177
|
-
* Safely extract all headers for debug logging
|
|
178
|
-
* Necessary to avoid middleware issue https://github.com/vercel/next.js/issues/39765
|
|
179
|
-
* @param {Headers} incomingHeaders Incoming headers
|
|
180
|
-
* @returns Object with headers as key/value pairs
|
|
181
|
-
*/
|
|
182
|
-
extractDebugHeaders(incomingHeaders) {
|
|
183
|
-
const headers = {};
|
|
184
|
-
incomingHeaders.forEach((value, key) => (headers[key] = value));
|
|
185
|
-
return headers;
|
|
186
|
-
}
|
|
187
161
|
}
|
|
188
162
|
exports.PersonalizeMiddleware = PersonalizeMiddleware;
|
|
@@ -16,29 +16,43 @@ exports.RedirectsMiddleware = void 0;
|
|
|
16
16
|
const regex_parser_1 = __importDefault(require("regex-parser"));
|
|
17
17
|
const server_1 = require("next/server");
|
|
18
18
|
const site_1 = require("@sitecore-jss/sitecore-jss/site");
|
|
19
|
+
const sitecore_jss_1 = require("@sitecore-jss/sitecore-jss");
|
|
20
|
+
const middleware_1 = require("./middleware");
|
|
19
21
|
/**
|
|
20
22
|
* Middleware / handler fetches all redirects from Sitecore instance by grapqhl service
|
|
21
23
|
* compares with current url and redirects to target url
|
|
22
24
|
*/
|
|
23
|
-
class RedirectsMiddleware {
|
|
25
|
+
class RedirectsMiddleware extends middleware_1.MiddlewareBase {
|
|
24
26
|
/**
|
|
25
27
|
* @param {RedirectsMiddlewareConfig} [config] redirects middleware config
|
|
26
28
|
*/
|
|
27
29
|
constructor(config) {
|
|
30
|
+
super(config);
|
|
28
31
|
this.config = config;
|
|
29
32
|
this.handler = (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
30
|
-
|
|
31
|
-
const
|
|
32
|
-
const
|
|
33
|
+
const pathname = req.nextUrl.pathname;
|
|
34
|
+
const language = this.getLanguage(req);
|
|
35
|
+
const hostname = this.getHostHeader(req) || this.defaultHostname;
|
|
36
|
+
let site;
|
|
37
|
+
sitecore_jss_1.debug.redirects('redirects middleware start: %o', {
|
|
38
|
+
pathname,
|
|
39
|
+
language,
|
|
40
|
+
hostname,
|
|
41
|
+
});
|
|
33
42
|
const createResponse = () => __awaiter(this, void 0, void 0, function* () {
|
|
34
|
-
if (
|
|
35
|
-
|
|
36
|
-
|
|
43
|
+
if (this.config.disabled && this.config.disabled(req, server_1.NextResponse.next())) {
|
|
44
|
+
sitecore_jss_1.debug.redirects('skipped (redirects middleware is disabled)');
|
|
45
|
+
return res || server_1.NextResponse.next();
|
|
46
|
+
}
|
|
47
|
+
if (this.isPreview(req) || this.excludeRoute(pathname)) {
|
|
48
|
+
sitecore_jss_1.debug.redirects('skipped (%s)', this.isPreview(req) ? 'preview' : 'route excluded');
|
|
37
49
|
return res || server_1.NextResponse.next();
|
|
38
50
|
}
|
|
51
|
+
site = this.getSite(req, res);
|
|
39
52
|
// Find the redirect from result of RedirectService
|
|
40
|
-
const existsRedirect = yield this.getExistsRedirect(req,
|
|
53
|
+
const existsRedirect = yield this.getExistsRedirect(req, site.name);
|
|
41
54
|
if (!existsRedirect) {
|
|
55
|
+
sitecore_jss_1.debug.redirects('skipped (redirect does not exist)');
|
|
42
56
|
return res || server_1.NextResponse.next();
|
|
43
57
|
}
|
|
44
58
|
const url = req.nextUrl.clone();
|
|
@@ -73,14 +87,20 @@ class RedirectsMiddleware {
|
|
|
73
87
|
});
|
|
74
88
|
const response = yield createResponse();
|
|
75
89
|
// Share site name with the following executed middlewares
|
|
76
|
-
|
|
90
|
+
// Don't need to set when middleware is disabled
|
|
91
|
+
site && response.cookies.set(this.SITE_SYMBOL, site.name);
|
|
92
|
+
sitecore_jss_1.debug.redirects('redirects middleware end: %o', {
|
|
93
|
+
redirected: response.redirected,
|
|
94
|
+
status: response.status,
|
|
95
|
+
url: response.url,
|
|
96
|
+
headers: this.extractDebugHeaders(response.headers),
|
|
97
|
+
});
|
|
77
98
|
return response;
|
|
78
99
|
});
|
|
79
100
|
// NOTE: we provide native fetch for compatibility on Next.js Edge Runtime
|
|
80
101
|
// (underlying default 'cross-fetch' is not currently compatible: https://github.com/lquixada/cross-fetch/issues/78)
|
|
81
102
|
this.redirectsService = new site_1.GraphQLRedirectsService(Object.assign(Object.assign({}, config), { fetch: fetch }));
|
|
82
103
|
this.locales = config.locales;
|
|
83
|
-
this.defaultHostname = config.defaultHostname || 'localhost';
|
|
84
104
|
}
|
|
85
105
|
/**
|
|
86
106
|
* Gets the Next.js API route handler
|
|
@@ -89,21 +109,6 @@ class RedirectsMiddleware {
|
|
|
89
109
|
getHandler() {
|
|
90
110
|
return this.handler;
|
|
91
111
|
}
|
|
92
|
-
excludeRoute(pathname) {
|
|
93
|
-
if (pathname.includes('.') || // Ignore files
|
|
94
|
-
pathname.startsWith('/api/') || // Ignore Next.js API calls
|
|
95
|
-
pathname.startsWith('/sitecore/') || // Ignore Sitecore API calls
|
|
96
|
-
pathname.startsWith('/_next') // Ignore next service calls
|
|
97
|
-
) {
|
|
98
|
-
return true;
|
|
99
|
-
}
|
|
100
|
-
return false;
|
|
101
|
-
}
|
|
102
|
-
getHostname(req) {
|
|
103
|
-
var _a;
|
|
104
|
-
const hostHeader = (_a = req.headers.get('host')) === null || _a === void 0 ? void 0 : _a.split(':')[0];
|
|
105
|
-
return hostHeader || this.defaultHostname;
|
|
106
|
-
}
|
|
107
112
|
/**
|
|
108
113
|
* Method returns RedirectInfo when matches
|
|
109
114
|
* @param {NextRequest} req request
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.BaseGraphQLSitemapService = exports.getSiteEmptyError = exports.siteError = exports.languageError = void 0;
|
|
13
|
+
const graphql_1 = require("@sitecore-jss/sitecore-jss/graphql");
|
|
14
|
+
const sitecore_jss_1 = require("@sitecore-jss/sitecore-jss");
|
|
15
|
+
const personalize_1 = require("@sitecore-jss/sitecore-jss/personalize");
|
|
16
|
+
/** @private */
|
|
17
|
+
exports.languageError = 'The list of languages cannot be empty';
|
|
18
|
+
exports.siteError = 'The service needs a site name';
|
|
19
|
+
/**
|
|
20
|
+
* @param {string} siteName to inject into error text
|
|
21
|
+
* @private
|
|
22
|
+
*/
|
|
23
|
+
function getSiteEmptyError(siteName) {
|
|
24
|
+
return `Site "${siteName}" does not exist or site item tree is missing`;
|
|
25
|
+
}
|
|
26
|
+
exports.getSiteEmptyError = getSiteEmptyError;
|
|
27
|
+
const languageEmptyError = 'The language must be a non-empty string';
|
|
28
|
+
/**
|
|
29
|
+
* GQL query made dynamic based on schema differences between SXP and XM Cloud
|
|
30
|
+
* @param {boolean} usesPersonalize flag to detrmine which variation of a query to run
|
|
31
|
+
* @returns GraphQL query to fetch site paths with
|
|
32
|
+
*/
|
|
33
|
+
const defaultQuery = (usesPersonalize) => /* GraphQL */ `
|
|
34
|
+
query ${usesPersonalize ? 'PersonalizeSitemapQuery' : 'DefaultSitemapQuery'}(
|
|
35
|
+
$siteName: String!
|
|
36
|
+
$language: String!
|
|
37
|
+
$includedPaths: [String]
|
|
38
|
+
$excludedPaths: [String]
|
|
39
|
+
$pageSize: Int = 10
|
|
40
|
+
$after: String
|
|
41
|
+
) {
|
|
42
|
+
site {
|
|
43
|
+
siteInfo(site: $siteName) {
|
|
44
|
+
routes(
|
|
45
|
+
language: $language
|
|
46
|
+
includedPaths: $includedPaths
|
|
47
|
+
excludedPaths: $excludedPaths
|
|
48
|
+
first: $pageSize
|
|
49
|
+
after: $after
|
|
50
|
+
){
|
|
51
|
+
total
|
|
52
|
+
pageInfo {
|
|
53
|
+
endCursor
|
|
54
|
+
hasNext
|
|
55
|
+
}
|
|
56
|
+
results {
|
|
57
|
+
path: routePath
|
|
58
|
+
${usesPersonalize
|
|
59
|
+
? `
|
|
60
|
+
route {
|
|
61
|
+
personalization {
|
|
62
|
+
variantIds
|
|
63
|
+
}
|
|
64
|
+
}`
|
|
65
|
+
: ''}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
`;
|
|
72
|
+
/**
|
|
73
|
+
* Service that fetches the list of site pages using Sitecore's GraphQL API.
|
|
74
|
+
* Used to handle a single site
|
|
75
|
+
* This list is used for SSG and Export functionality.
|
|
76
|
+
* @mixes SearchQueryService<PageListQueryResult>
|
|
77
|
+
*/
|
|
78
|
+
class BaseGraphQLSitemapService {
|
|
79
|
+
/**
|
|
80
|
+
* GraphQL client accessible by descendant classes when needed
|
|
81
|
+
*/
|
|
82
|
+
get graphQLClient() {
|
|
83
|
+
return this._graphQLClient;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Gets the default query used for fetching the list of site pages
|
|
87
|
+
*/
|
|
88
|
+
get query() {
|
|
89
|
+
return defaultQuery(this.options.includePersonalizedRoutes);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Creates an instance of graphQL sitemap service with the provided options
|
|
93
|
+
* @param {GraphQLSitemapServiceConfig} options instance
|
|
94
|
+
*/
|
|
95
|
+
constructor(options) {
|
|
96
|
+
this.options = options;
|
|
97
|
+
this._graphQLClient = this.getGraphQLClient();
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Fetch sitemap which could be used for generation of static pages during `next export`.
|
|
101
|
+
* The `locale` parameter will be used in the item query, but since i18n is not supported,
|
|
102
|
+
* the output paths will not include a `language` property.
|
|
103
|
+
* @param {string} locale which application supports
|
|
104
|
+
* @returns an array of @see StaticPath objects
|
|
105
|
+
*/
|
|
106
|
+
fetchExportSitemap(locale) {
|
|
107
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
108
|
+
const formatPath = (path) => ({
|
|
109
|
+
params: {
|
|
110
|
+
path,
|
|
111
|
+
},
|
|
112
|
+
});
|
|
113
|
+
return this.fetchSitemap([locale], formatPath);
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Fetch sitemap which could be used for generation of static pages using SSG mode
|
|
118
|
+
* @param {string[]} locales locales which application supports
|
|
119
|
+
* @returns an array of @see StaticPath objects
|
|
120
|
+
*/
|
|
121
|
+
fetchSSGSitemap(locales) {
|
|
122
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
123
|
+
const formatPath = (path, locale) => ({
|
|
124
|
+
params: {
|
|
125
|
+
path,
|
|
126
|
+
},
|
|
127
|
+
locale,
|
|
128
|
+
});
|
|
129
|
+
return this.fetchSitemap(locales, formatPath);
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
getTranformedPaths(siteName, languages, formatStaticPath) {
|
|
133
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
134
|
+
const paths = new Array();
|
|
135
|
+
yield Promise.all(languages.map((language) => __awaiter(this, void 0, void 0, function* () {
|
|
136
|
+
if (language === '') {
|
|
137
|
+
throw new RangeError(languageEmptyError);
|
|
138
|
+
}
|
|
139
|
+
sitecore_jss_1.debug.sitemap('fetching sitemap data for %s %s', language, siteName);
|
|
140
|
+
const results = yield this.fetchLanguageSitePaths(language, siteName);
|
|
141
|
+
const transformedPaths = yield this.transformLanguageSitePaths(results, formatStaticPath, language);
|
|
142
|
+
paths.push(...transformedPaths);
|
|
143
|
+
})));
|
|
144
|
+
return paths;
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
transformLanguageSitePaths(sitePaths, formatStaticPath, language) {
|
|
148
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
149
|
+
const formatPath = (path) => formatStaticPath(path.replace(/^\/|\/$/g, '').split('/'), language);
|
|
150
|
+
const aggregatedPaths = [];
|
|
151
|
+
sitePaths.forEach((item) => {
|
|
152
|
+
var _a, _b, _c, _d;
|
|
153
|
+
if (!item)
|
|
154
|
+
return;
|
|
155
|
+
aggregatedPaths.push(formatPath(item.path));
|
|
156
|
+
// check for type safety's sake - personalize may be empty depending on query type
|
|
157
|
+
if ((_b = (_a = item.route) === null || _a === void 0 ? void 0 : _a.personalization) === null || _b === void 0 ? void 0 : _b.variantIds.length) {
|
|
158
|
+
aggregatedPaths.push(...(((_d = (_c = item.route) === null || _c === void 0 ? void 0 : _c.personalization) === null || _d === void 0 ? void 0 : _d.variantIds.map((varId) => formatPath((0, personalize_1.getPersonalizedRewrite)(item.path, { variantId: varId })))) || {}));
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
return aggregatedPaths;
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
fetchLanguageSitePaths(language, siteName) {
|
|
165
|
+
var _a, _b, _c, _d;
|
|
166
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
167
|
+
const args = {
|
|
168
|
+
siteName: siteName,
|
|
169
|
+
language: language,
|
|
170
|
+
pageSize: this.options.pageSize,
|
|
171
|
+
includedPaths: this.options.includedPaths,
|
|
172
|
+
excludedPaths: this.options.excludedPaths,
|
|
173
|
+
};
|
|
174
|
+
let results = [];
|
|
175
|
+
let hasNext = true;
|
|
176
|
+
let after = '';
|
|
177
|
+
while (hasNext) {
|
|
178
|
+
const fetchResponse = yield this.graphQLClient.request(this.query, Object.assign(Object.assign({}, args), { after }));
|
|
179
|
+
if (!((_a = fetchResponse === null || fetchResponse === void 0 ? void 0 : fetchResponse.site) === null || _a === void 0 ? void 0 : _a.siteInfo)) {
|
|
180
|
+
throw new RangeError(getSiteEmptyError(siteName));
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
results = results.concat((_b = fetchResponse.site.siteInfo.routes) === null || _b === void 0 ? void 0 : _b.results);
|
|
184
|
+
hasNext = (_c = fetchResponse.site.siteInfo.routes) === null || _c === void 0 ? void 0 : _c.pageInfo.hasNext;
|
|
185
|
+
after = (_d = fetchResponse.site.siteInfo.routes) === null || _d === void 0 ? void 0 : _d.pageInfo.endCursor;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return results;
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Gets a GraphQL client that can make requests to the API. Uses graphql-request as the default
|
|
193
|
+
* library for fetching graphql data (@see GraphQLRequestClient). Override this method if you
|
|
194
|
+
* want to use something else.
|
|
195
|
+
* @returns {GraphQLClient} implementation
|
|
196
|
+
*/
|
|
197
|
+
getGraphQLClient() {
|
|
198
|
+
return new graphql_1.GraphQLRequestClient(this.options.endpoint, {
|
|
199
|
+
apiKey: this.options.apiKey,
|
|
200
|
+
debugger: sitecore_jss_1.debug.sitemap,
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
exports.BaseGraphQLSitemapService = BaseGraphQLSitemapService;
|