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