@sitecore-jss/sitecore-jss-nextjs 21.7.0-canary.81 → 21.7.0-canary.83
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/index.js +1 -0
- package/dist/esm/index.js +3 -1
- package/package.json +6 -6
- package/types/graphql/index.d.ts +1 -1
- package/types/index.d.ts +3 -1
- package/dist/cjs/revalidate/index.js +0 -5
- package/dist/cjs/revalidate/revalidate-middleware.js +0 -216
- package/dist/esm/revalidate/index.js +0 -1
- package/dist/esm/revalidate/revalidate-middleware.js +0 -212
- package/revalidate.d.ts +0 -1
- package/revalidate.js +0 -1
- package/types/revalidate/index.d.ts +0 -1
- package/types/revalidate/revalidate-middleware.d.ts +0 -115
package/dist/cjs/index.js
CHANGED
|
@@ -28,6 +28,7 @@ exports.withDatasourceCheck = exports.withPlaceholder = exports.withEditorChrome
|
|
|
28
28
|
var sitecore_jss_1 = require("@sitecore-jss/sitecore-jss");
|
|
29
29
|
Object.defineProperty(exports, "constants", { enumerable: true, get: function () { return sitecore_jss_1.constants; } });
|
|
30
30
|
Object.defineProperty(exports, "AxiosDataFetcher", { enumerable: true, get: function () { return sitecore_jss_1.AxiosDataFetcher; } });
|
|
31
|
+
/** @deprecated use import 'GraphQLClientError' from '@sitecore-jss/sitecore-jss-nextjs/graphql' instead */
|
|
31
32
|
Object.defineProperty(exports, "ClientError", { enumerable: true, get: function () { return sitecore_jss_1.ClientError; } });
|
|
32
33
|
Object.defineProperty(exports, "NativeDataFetcher", { enumerable: true, get: function () { return sitecore_jss_1.NativeDataFetcher; } });
|
|
33
34
|
Object.defineProperty(exports, "enableDebug", { enumerable: true, get: function () { return sitecore_jss_1.enableDebug; } });
|
package/dist/esm/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
export { constants, AxiosDataFetcher,
|
|
1
|
+
export { constants, AxiosDataFetcher,
|
|
2
|
+
/** @deprecated use import 'GraphQLClientError' from '@sitecore-jss/sitecore-jss-nextjs/graphql' instead */
|
|
3
|
+
ClientError, NativeDataFetcher, enableDebug, debug, } from '@sitecore-jss/sitecore-jss';
|
|
2
4
|
// we will remove the root exports for these later
|
|
3
5
|
// we cannot mark exports as deprected directly, so we're using this hack instead
|
|
4
6
|
import { GraphQLRequestClient as GraphQLRequestClientDep } from './graphql';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sitecore-jss/sitecore-jss-nextjs",
|
|
3
|
-
"version": "21.7.0-canary.
|
|
3
|
+
"version": "21.7.0-canary.83",
|
|
4
4
|
"main": "dist/cjs/index.js",
|
|
5
5
|
"module": "dist/esm/index.js",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"test": "mocha --require ./test/setup.js \"./src/**/*.test.ts\" \"./src/**/*.test.tsx\" --exit",
|
|
12
12
|
"prepublishOnly": "npm run build",
|
|
13
13
|
"coverage": "nyc npm test",
|
|
14
|
-
"generate-docs": "npx typedoc --plugin typedoc-plugin-markdown --readme none --out ../../ref-docs/sitecore-jss-nextjs --entryPoints src/index.ts --entryPoints src/monitoring/index.ts --entryPoints src/editing/index.ts --entryPoints src/middleware/index.ts --entryPoints src/context/index.ts --entryPoints src/utils/index.ts --entryPoints src/site/index.ts --entryPoints src/graphql/index.ts --
|
|
14
|
+
"generate-docs": "npx typedoc --plugin typedoc-plugin-markdown --readme none --out ../../ref-docs/sitecore-jss-nextjs --entryPoints src/index.ts --entryPoints src/monitoring/index.ts --entryPoints src/editing/index.ts --entryPoints src/middleware/index.ts --entryPoints src/context/index.ts --entryPoints src/utils/index.ts --entryPoints src/site/index.ts --entryPoints src/graphql/index.ts --githubPages false"
|
|
15
15
|
},
|
|
16
16
|
"engines": {
|
|
17
17
|
"node": ">=18"
|
|
@@ -72,9 +72,9 @@
|
|
|
72
72
|
"react-dom": "^18.2.0"
|
|
73
73
|
},
|
|
74
74
|
"dependencies": {
|
|
75
|
-
"@sitecore-jss/sitecore-jss": "^21.7.0-canary.
|
|
76
|
-
"@sitecore-jss/sitecore-jss-dev-tools": "^21.7.0-canary.
|
|
77
|
-
"@sitecore-jss/sitecore-jss-react": "^21.7.0-canary.
|
|
75
|
+
"@sitecore-jss/sitecore-jss": "^21.7.0-canary.83",
|
|
76
|
+
"@sitecore-jss/sitecore-jss-dev-tools": "^21.7.0-canary.83",
|
|
77
|
+
"@sitecore-jss/sitecore-jss-react": "^21.7.0-canary.83",
|
|
78
78
|
"@vercel/kv": "^0.2.1",
|
|
79
79
|
"node-html-parser": "^6.1.4",
|
|
80
80
|
"prop-types": "^15.8.1",
|
|
@@ -83,7 +83,7 @@
|
|
|
83
83
|
},
|
|
84
84
|
"description": "",
|
|
85
85
|
"types": "types/index.d.ts",
|
|
86
|
-
"gitHead": "
|
|
86
|
+
"gitHead": "11783a4710054cabfcc9b8e383c06682a0e92c2b",
|
|
87
87
|
"files": [
|
|
88
88
|
"dist",
|
|
89
89
|
"types",
|
package/types/graphql/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { RetryStrategy, DefaultRetryStrategy, GraphQLRequestClient, GraphQLRequestClientFactory, GraphQLRequestClientFactoryConfig, getEdgeProxyContentUrl, } from '@sitecore-jss/sitecore-jss/graphql';
|
|
1
|
+
export { GraphQLClientError, RetryStrategy, DefaultRetryStrategy, GraphQLRequestClient, GraphQLRequestClientFactory, GraphQLRequestClientFactoryConfig, getEdgeProxyContentUrl, } from '@sitecore-jss/sitecore-jss/graphql';
|
package/types/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
export { constants, HttpDataFetcher, HttpResponse, AxiosResponse, AxiosDataFetcher, AxiosDataFetcherConfig,
|
|
1
|
+
export { constants, HttpDataFetcher, HttpResponse, AxiosResponse, AxiosDataFetcher, AxiosDataFetcherConfig,
|
|
2
|
+
/** @deprecated use import 'GraphQLClientError' from '@sitecore-jss/sitecore-jss-nextjs/graphql' instead */
|
|
3
|
+
ClientError, NativeDataFetcher, NativeDataFetcherConfig, HTMLLink, enableDebug, debug, } from '@sitecore-jss/sitecore-jss';
|
|
2
4
|
import { GraphQLRequestClient as GraphQLRequestClientDep } from './graphql';
|
|
3
5
|
import { resolveUrl as resolveUrlDep } from '@sitecore-jss/sitecore-jss/utils';
|
|
4
6
|
/** @deprecated use import from '@sitecore-jss/sitecore-jss-nextjs/utils' instead */
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RevalidateMiddleware = void 0;
|
|
4
|
-
var revalidate_middleware_1 = require("./revalidate-middleware");
|
|
5
|
-
Object.defineProperty(exports, "RevalidateMiddleware", { enumerable: true, get: function () { return revalidate_middleware_1.RevalidateMiddleware; } });
|
|
@@ -1,216 +0,0 @@
|
|
|
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.RevalidateMiddleware = void 0;
|
|
13
|
-
// import { I18NConfig } from 'next/dist/server/config-shared';
|
|
14
|
-
const personalize_1 = require("@sitecore-jss/sitecore-jss/personalize");
|
|
15
|
-
const site_1 = require("@sitecore-jss/sitecore-jss/site");
|
|
16
|
-
const sitecore_jss_1 = require("@sitecore-jss/sitecore-jss");
|
|
17
|
-
var EntityDefinition;
|
|
18
|
-
(function (EntityDefinition) {
|
|
19
|
-
EntityDefinition["LayoutData"] = "LayoutData";
|
|
20
|
-
EntityDefinition["Item"] = "Item";
|
|
21
|
-
})(EntityDefinition || (EntityDefinition = {}));
|
|
22
|
-
/**
|
|
23
|
-
* Middleware / handler for on-demand ISR (e.g. '/api/revalidate').
|
|
24
|
-
*/
|
|
25
|
-
class RevalidateMiddleware {
|
|
26
|
-
constructor(config) {
|
|
27
|
-
this.config = config;
|
|
28
|
-
this.handler = (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
29
|
-
// filter out updated paths and language from request.body
|
|
30
|
-
const filteredUpdates = this.getFilteredUpdates(req);
|
|
31
|
-
if (this.isEmpty(filteredUpdates)) {
|
|
32
|
-
// nothing to revalidate
|
|
33
|
-
return res.status(204).json({ message: 'No updates to revalidate' });
|
|
34
|
-
}
|
|
35
|
-
// extract only paths from filtered updates object
|
|
36
|
-
const paths = this.extractPaths(filteredUpdates);
|
|
37
|
-
const pathsToRevalidate = [];
|
|
38
|
-
// when personalization is configured and when both multiSite and personalization are configured
|
|
39
|
-
if (this.config.personalize) {
|
|
40
|
-
const personalizeInfo = yield this.getPersonalizedResults(filteredUpdates);
|
|
41
|
-
if (this.config.multiSite) {
|
|
42
|
-
this.handleMultiSitePersonalization(personalizeInfo, pathsToRevalidate, this.getPathName, this.getSiteName);
|
|
43
|
-
}
|
|
44
|
-
else {
|
|
45
|
-
this.handleNonMultiSitePersonalization(personalizeInfo, pathsToRevalidate, this.getPathName);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
// when only multiSite is configured
|
|
49
|
-
if (this.config.multiSite && !this.config.personalize) {
|
|
50
|
-
const multiSitePaths = paths.map((path) => (0, site_1.getSiteRewrite)(this.getPathName(path), { siteName: this.getSiteName(path) }));
|
|
51
|
-
pathsToRevalidate.push(...multiSitePaths);
|
|
52
|
-
}
|
|
53
|
-
// when both multiSite and personalization are not configured
|
|
54
|
-
if (!this.config.multiSite && !this.config.personalize) {
|
|
55
|
-
const defaultPaths = paths.map((path) => this.getPathName(path));
|
|
56
|
-
pathsToRevalidate.push(...defaultPaths);
|
|
57
|
-
}
|
|
58
|
-
// when other locales are configured besides defaultLocale
|
|
59
|
-
if (!this.isEmpty(filteredUpdates)) {
|
|
60
|
-
const filteredLanguage = [...new Set(filteredUpdates.map(({ language }) => language))].join(',');
|
|
61
|
-
if (this.config.localePrefix) {
|
|
62
|
-
const language = this.config.localePrefix(filteredLanguage);
|
|
63
|
-
if (language) {
|
|
64
|
-
yield Promise.all(pathsToRevalidate.map((path) => res.revalidate(`/${language}` + path)));
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
yield Promise.all(pathsToRevalidate.map((path) => res.revalidate(path)));
|
|
69
|
-
sitecore_jss_1.debug.revalidate(`revalidated paths: ${pathsToRevalidate.join(', ')}`);
|
|
70
|
-
});
|
|
71
|
-
this.personalizeService = new personalize_1.GraphQLPersonalizeService({
|
|
72
|
-
clientFactory: config.clientFactory,
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Generates a Next.js API route handler that executes a revalidation process.
|
|
77
|
-
* @returns The route handler function for handling Next.js API requests.
|
|
78
|
-
*/
|
|
79
|
-
getHandler() {
|
|
80
|
-
return (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
81
|
-
try {
|
|
82
|
-
yield this.handler(req, res);
|
|
83
|
-
return res.status(200).json({ revalidated: true });
|
|
84
|
-
}
|
|
85
|
-
catch (error) {
|
|
86
|
-
console.log('Error Revalidating:');
|
|
87
|
-
console.log(error);
|
|
88
|
-
return res.status(500).json({ revalidated: false });
|
|
89
|
-
}
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Gets personalized results for the updated paths
|
|
94
|
-
* @param {UpdatedPaths[]} filteredUpdates Updated paths
|
|
95
|
-
*/
|
|
96
|
-
getPersonalizedResults(filteredUpdates) {
|
|
97
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
98
|
-
const personalizedResults = [];
|
|
99
|
-
const nonPersonalizedResults = [];
|
|
100
|
-
yield Promise.all(filteredUpdates.map((update) => __awaiter(this, void 0, void 0, function* () {
|
|
101
|
-
const siteName = this.getSiteName(update.path);
|
|
102
|
-
const pathName = this.getPathName(update.path);
|
|
103
|
-
const personalizeInfo = yield this.personalizeService.getPersonalizeInfo(pathName, update.language, siteName);
|
|
104
|
-
if (personalizeInfo && personalizeInfo.variantIds.length > 0) {
|
|
105
|
-
personalizeInfo.variantIds.forEach((variantId) => {
|
|
106
|
-
personalizedResults.push({
|
|
107
|
-
path: update.path,
|
|
108
|
-
variantId,
|
|
109
|
-
});
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
else {
|
|
113
|
-
// Collect paths without personalized info
|
|
114
|
-
nonPersonalizedResults.push({
|
|
115
|
-
path: update.path,
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
})));
|
|
119
|
-
return {
|
|
120
|
-
personalized: personalizedResults,
|
|
121
|
-
nonPersonalized: nonPersonalizedResults,
|
|
122
|
-
};
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
isEmpty(data) {
|
|
126
|
-
return data.length === 0;
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* Extracts the paths from the updated paths
|
|
130
|
-
* @param {UpdatedPaths[]} filteredUpdates Updated paths
|
|
131
|
-
* @returns {string[]} paths
|
|
132
|
-
*/
|
|
133
|
-
extractPaths(filteredUpdates) {
|
|
134
|
-
return filteredUpdates.map((update) => update.path);
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* Gets the site name from the path name
|
|
138
|
-
* @param {string} pathname Path name
|
|
139
|
-
* @returns {string} site name
|
|
140
|
-
*/
|
|
141
|
-
getSiteName(pathname) {
|
|
142
|
-
let siteName = '';
|
|
143
|
-
const path = pathname.endsWith('/') ? pathname : pathname + '/';
|
|
144
|
-
const result = path.match('(.*?)\\/');
|
|
145
|
-
if (result && result[1] !== '') {
|
|
146
|
-
siteName = result[1];
|
|
147
|
-
}
|
|
148
|
-
return siteName;
|
|
149
|
-
}
|
|
150
|
-
/**
|
|
151
|
-
* Gets the path name from the full path
|
|
152
|
-
* @param {string} fullPath Full path
|
|
153
|
-
* @returns {string} path name
|
|
154
|
-
*/
|
|
155
|
-
getPathName(fullPath) {
|
|
156
|
-
const pathParts = fullPath.split('/').filter((part) => part !== '');
|
|
157
|
-
if (pathParts.length >= 2) {
|
|
158
|
-
const siteName = `/${pathParts[0]}/`;
|
|
159
|
-
const path = `/${pathParts.slice(1).join('/')}`;
|
|
160
|
-
return path.startsWith(siteName) ? path.slice(siteName.length) : path;
|
|
161
|
-
}
|
|
162
|
-
return '/';
|
|
163
|
-
}
|
|
164
|
-
extractSiteName(path) {
|
|
165
|
-
const siteName = path.split('/')[0];
|
|
166
|
-
return siteName;
|
|
167
|
-
}
|
|
168
|
-
/**
|
|
169
|
-
* Filters out the updated paths and language from the request body
|
|
170
|
-
* @param {NextApiRequest} req Next.js API request
|
|
171
|
-
* @returns {UpdatedPaths[]} updated paths
|
|
172
|
-
*/
|
|
173
|
-
getFilteredUpdates(req) {
|
|
174
|
-
var _a, _b;
|
|
175
|
-
if (!((_a = req.body) === null || _a === void 0 ? void 0 : _a.updates) || this.isEmpty(req.body.updates)) {
|
|
176
|
-
return [];
|
|
177
|
-
}
|
|
178
|
-
return (_b = req.body) === null || _b === void 0 ? void 0 : _b.updates.filter((update) => update.entity_definition === EntityDefinition.LayoutData && update.entity_culture).map((update) => {
|
|
179
|
-
if (update.identifier === 'website/') {
|
|
180
|
-
return null;
|
|
181
|
-
}
|
|
182
|
-
return {
|
|
183
|
-
path: update.identifier,
|
|
184
|
-
language: update.entity_culture,
|
|
185
|
-
};
|
|
186
|
-
}).filter(Boolean);
|
|
187
|
-
}
|
|
188
|
-
handleMultiSitePersonalization(personalizeInfo, pathsToRevalidate, getPathName, getSiteName) {
|
|
189
|
-
if (personalizeInfo.personalized.length > 0) {
|
|
190
|
-
const personalizedRewrite = personalizeInfo.personalized.map((info) => {
|
|
191
|
-
return (0, personalize_1.getPersonalizedRewrite)((0, site_1.getSiteRewrite)(getPathName(info.path), { siteName: getSiteName(info.path) }), {
|
|
192
|
-
variantId: info.variantId,
|
|
193
|
-
});
|
|
194
|
-
});
|
|
195
|
-
pathsToRevalidate.push(...personalizedRewrite);
|
|
196
|
-
}
|
|
197
|
-
if (personalizeInfo.nonPersonalized.length > 0) {
|
|
198
|
-
const nonPersonalizedRewrite = personalizeInfo.nonPersonalized.map((info) => {
|
|
199
|
-
return (0, site_1.getSiteRewrite)(getPathName(info.path), {
|
|
200
|
-
siteName: getSiteName(info.path),
|
|
201
|
-
});
|
|
202
|
-
});
|
|
203
|
-
pathsToRevalidate.push(...nonPersonalizedRewrite);
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
handleNonMultiSitePersonalization(personalizeInfo, pathsToRevalidate, getPathName) {
|
|
207
|
-
const nonMultiSitePersonalizedRewrite = personalizeInfo.personalized.map((info) => {
|
|
208
|
-
return (0, personalize_1.getPersonalizedRewrite)(getPathName(info.path), { variantId: info.variantId });
|
|
209
|
-
});
|
|
210
|
-
const nonMultiSiteNonPersonalizedRewrite = personalizeInfo.nonPersonalized.map((info) => {
|
|
211
|
-
return this.getPathName(info.path);
|
|
212
|
-
});
|
|
213
|
-
pathsToRevalidate.push(...nonMultiSitePersonalizedRewrite, ...nonMultiSiteNonPersonalizedRewrite);
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
exports.RevalidateMiddleware = RevalidateMiddleware;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { RevalidateMiddleware } from './revalidate-middleware';
|
|
@@ -1,212 +0,0 @@
|
|
|
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 { I18NConfig } from 'next/dist/server/config-shared';
|
|
11
|
-
import { GraphQLPersonalizeService, getPersonalizedRewrite, } from '@sitecore-jss/sitecore-jss/personalize';
|
|
12
|
-
import { getSiteRewrite } from '@sitecore-jss/sitecore-jss/site';
|
|
13
|
-
import { debug } from '@sitecore-jss/sitecore-jss';
|
|
14
|
-
var EntityDefinition;
|
|
15
|
-
(function (EntityDefinition) {
|
|
16
|
-
EntityDefinition["LayoutData"] = "LayoutData";
|
|
17
|
-
EntityDefinition["Item"] = "Item";
|
|
18
|
-
})(EntityDefinition || (EntityDefinition = {}));
|
|
19
|
-
/**
|
|
20
|
-
* Middleware / handler for on-demand ISR (e.g. '/api/revalidate').
|
|
21
|
-
*/
|
|
22
|
-
export class RevalidateMiddleware {
|
|
23
|
-
constructor(config) {
|
|
24
|
-
this.config = config;
|
|
25
|
-
this.handler = (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
26
|
-
// filter out updated paths and language from request.body
|
|
27
|
-
const filteredUpdates = this.getFilteredUpdates(req);
|
|
28
|
-
if (this.isEmpty(filteredUpdates)) {
|
|
29
|
-
// nothing to revalidate
|
|
30
|
-
return res.status(204).json({ message: 'No updates to revalidate' });
|
|
31
|
-
}
|
|
32
|
-
// extract only paths from filtered updates object
|
|
33
|
-
const paths = this.extractPaths(filteredUpdates);
|
|
34
|
-
const pathsToRevalidate = [];
|
|
35
|
-
// when personalization is configured and when both multiSite and personalization are configured
|
|
36
|
-
if (this.config.personalize) {
|
|
37
|
-
const personalizeInfo = yield this.getPersonalizedResults(filteredUpdates);
|
|
38
|
-
if (this.config.multiSite) {
|
|
39
|
-
this.handleMultiSitePersonalization(personalizeInfo, pathsToRevalidate, this.getPathName, this.getSiteName);
|
|
40
|
-
}
|
|
41
|
-
else {
|
|
42
|
-
this.handleNonMultiSitePersonalization(personalizeInfo, pathsToRevalidate, this.getPathName);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
// when only multiSite is configured
|
|
46
|
-
if (this.config.multiSite && !this.config.personalize) {
|
|
47
|
-
const multiSitePaths = paths.map((path) => getSiteRewrite(this.getPathName(path), { siteName: this.getSiteName(path) }));
|
|
48
|
-
pathsToRevalidate.push(...multiSitePaths);
|
|
49
|
-
}
|
|
50
|
-
// when both multiSite and personalization are not configured
|
|
51
|
-
if (!this.config.multiSite && !this.config.personalize) {
|
|
52
|
-
const defaultPaths = paths.map((path) => this.getPathName(path));
|
|
53
|
-
pathsToRevalidate.push(...defaultPaths);
|
|
54
|
-
}
|
|
55
|
-
// when other locales are configured besides defaultLocale
|
|
56
|
-
if (!this.isEmpty(filteredUpdates)) {
|
|
57
|
-
const filteredLanguage = [...new Set(filteredUpdates.map(({ language }) => language))].join(',');
|
|
58
|
-
if (this.config.localePrefix) {
|
|
59
|
-
const language = this.config.localePrefix(filteredLanguage);
|
|
60
|
-
if (language) {
|
|
61
|
-
yield Promise.all(pathsToRevalidate.map((path) => res.revalidate(`/${language}` + path)));
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
yield Promise.all(pathsToRevalidate.map((path) => res.revalidate(path)));
|
|
66
|
-
debug.revalidate(`revalidated paths: ${pathsToRevalidate.join(', ')}`);
|
|
67
|
-
});
|
|
68
|
-
this.personalizeService = new GraphQLPersonalizeService({
|
|
69
|
-
clientFactory: config.clientFactory,
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Generates a Next.js API route handler that executes a revalidation process.
|
|
74
|
-
* @returns The route handler function for handling Next.js API requests.
|
|
75
|
-
*/
|
|
76
|
-
getHandler() {
|
|
77
|
-
return (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
78
|
-
try {
|
|
79
|
-
yield this.handler(req, res);
|
|
80
|
-
return res.status(200).json({ revalidated: true });
|
|
81
|
-
}
|
|
82
|
-
catch (error) {
|
|
83
|
-
console.log('Error Revalidating:');
|
|
84
|
-
console.log(error);
|
|
85
|
-
return res.status(500).json({ revalidated: false });
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* Gets personalized results for the updated paths
|
|
91
|
-
* @param {UpdatedPaths[]} filteredUpdates Updated paths
|
|
92
|
-
*/
|
|
93
|
-
getPersonalizedResults(filteredUpdates) {
|
|
94
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
95
|
-
const personalizedResults = [];
|
|
96
|
-
const nonPersonalizedResults = [];
|
|
97
|
-
yield Promise.all(filteredUpdates.map((update) => __awaiter(this, void 0, void 0, function* () {
|
|
98
|
-
const siteName = this.getSiteName(update.path);
|
|
99
|
-
const pathName = this.getPathName(update.path);
|
|
100
|
-
const personalizeInfo = yield this.personalizeService.getPersonalizeInfo(pathName, update.language, siteName);
|
|
101
|
-
if (personalizeInfo && personalizeInfo.variantIds.length > 0) {
|
|
102
|
-
personalizeInfo.variantIds.forEach((variantId) => {
|
|
103
|
-
personalizedResults.push({
|
|
104
|
-
path: update.path,
|
|
105
|
-
variantId,
|
|
106
|
-
});
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
else {
|
|
110
|
-
// Collect paths without personalized info
|
|
111
|
-
nonPersonalizedResults.push({
|
|
112
|
-
path: update.path,
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
})));
|
|
116
|
-
return {
|
|
117
|
-
personalized: personalizedResults,
|
|
118
|
-
nonPersonalized: nonPersonalizedResults,
|
|
119
|
-
};
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
isEmpty(data) {
|
|
123
|
-
return data.length === 0;
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Extracts the paths from the updated paths
|
|
127
|
-
* @param {UpdatedPaths[]} filteredUpdates Updated paths
|
|
128
|
-
* @returns {string[]} paths
|
|
129
|
-
*/
|
|
130
|
-
extractPaths(filteredUpdates) {
|
|
131
|
-
return filteredUpdates.map((update) => update.path);
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* Gets the site name from the path name
|
|
135
|
-
* @param {string} pathname Path name
|
|
136
|
-
* @returns {string} site name
|
|
137
|
-
*/
|
|
138
|
-
getSiteName(pathname) {
|
|
139
|
-
let siteName = '';
|
|
140
|
-
const path = pathname.endsWith('/') ? pathname : pathname + '/';
|
|
141
|
-
const result = path.match('(.*?)\\/');
|
|
142
|
-
if (result && result[1] !== '') {
|
|
143
|
-
siteName = result[1];
|
|
144
|
-
}
|
|
145
|
-
return siteName;
|
|
146
|
-
}
|
|
147
|
-
/**
|
|
148
|
-
* Gets the path name from the full path
|
|
149
|
-
* @param {string} fullPath Full path
|
|
150
|
-
* @returns {string} path name
|
|
151
|
-
*/
|
|
152
|
-
getPathName(fullPath) {
|
|
153
|
-
const pathParts = fullPath.split('/').filter((part) => part !== '');
|
|
154
|
-
if (pathParts.length >= 2) {
|
|
155
|
-
const siteName = `/${pathParts[0]}/`;
|
|
156
|
-
const path = `/${pathParts.slice(1).join('/')}`;
|
|
157
|
-
return path.startsWith(siteName) ? path.slice(siteName.length) : path;
|
|
158
|
-
}
|
|
159
|
-
return '/';
|
|
160
|
-
}
|
|
161
|
-
extractSiteName(path) {
|
|
162
|
-
const siteName = path.split('/')[0];
|
|
163
|
-
return siteName;
|
|
164
|
-
}
|
|
165
|
-
/**
|
|
166
|
-
* Filters out the updated paths and language from the request body
|
|
167
|
-
* @param {NextApiRequest} req Next.js API request
|
|
168
|
-
* @returns {UpdatedPaths[]} updated paths
|
|
169
|
-
*/
|
|
170
|
-
getFilteredUpdates(req) {
|
|
171
|
-
var _a, _b;
|
|
172
|
-
if (!((_a = req.body) === null || _a === void 0 ? void 0 : _a.updates) || this.isEmpty(req.body.updates)) {
|
|
173
|
-
return [];
|
|
174
|
-
}
|
|
175
|
-
return (_b = req.body) === null || _b === void 0 ? void 0 : _b.updates.filter((update) => update.entity_definition === EntityDefinition.LayoutData && update.entity_culture).map((update) => {
|
|
176
|
-
if (update.identifier === 'website/') {
|
|
177
|
-
return null;
|
|
178
|
-
}
|
|
179
|
-
return {
|
|
180
|
-
path: update.identifier,
|
|
181
|
-
language: update.entity_culture,
|
|
182
|
-
};
|
|
183
|
-
}).filter(Boolean);
|
|
184
|
-
}
|
|
185
|
-
handleMultiSitePersonalization(personalizeInfo, pathsToRevalidate, getPathName, getSiteName) {
|
|
186
|
-
if (personalizeInfo.personalized.length > 0) {
|
|
187
|
-
const personalizedRewrite = personalizeInfo.personalized.map((info) => {
|
|
188
|
-
return getPersonalizedRewrite(getSiteRewrite(getPathName(info.path), { siteName: getSiteName(info.path) }), {
|
|
189
|
-
variantId: info.variantId,
|
|
190
|
-
});
|
|
191
|
-
});
|
|
192
|
-
pathsToRevalidate.push(...personalizedRewrite);
|
|
193
|
-
}
|
|
194
|
-
if (personalizeInfo.nonPersonalized.length > 0) {
|
|
195
|
-
const nonPersonalizedRewrite = personalizeInfo.nonPersonalized.map((info) => {
|
|
196
|
-
return getSiteRewrite(getPathName(info.path), {
|
|
197
|
-
siteName: getSiteName(info.path),
|
|
198
|
-
});
|
|
199
|
-
});
|
|
200
|
-
pathsToRevalidate.push(...nonPersonalizedRewrite);
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
handleNonMultiSitePersonalization(personalizeInfo, pathsToRevalidate, getPathName) {
|
|
204
|
-
const nonMultiSitePersonalizedRewrite = personalizeInfo.personalized.map((info) => {
|
|
205
|
-
return getPersonalizedRewrite(getPathName(info.path), { variantId: info.variantId });
|
|
206
|
-
});
|
|
207
|
-
const nonMultiSiteNonPersonalizedRewrite = personalizeInfo.nonPersonalized.map((info) => {
|
|
208
|
-
return this.getPathName(info.path);
|
|
209
|
-
});
|
|
210
|
-
pathsToRevalidate.push(...nonMultiSitePersonalizedRewrite, ...nonMultiSiteNonPersonalizedRewrite);
|
|
211
|
-
}
|
|
212
|
-
}
|
package/revalidate.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './types/revalidate/index';
|
package/revalidate.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
module.exports = require('./dist/cjs/revalidate/index');
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { RevalidateMiddleware } from './revalidate-middleware';
|
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
import { NextApiResponse, NextApiRequest } from 'next';
|
|
2
|
-
import { GraphQLRequestClientFactory } from '@sitecore-jss/sitecore-jss/graphql';
|
|
3
|
-
declare enum EntityDefinition {
|
|
4
|
-
LayoutData = "LayoutData",
|
|
5
|
-
Item = "Item"
|
|
6
|
-
}
|
|
7
|
-
/**
|
|
8
|
-
* Object model of each updated entity returned from the webhook payload
|
|
9
|
-
*/
|
|
10
|
-
export type Entity = {
|
|
11
|
-
identifier: string;
|
|
12
|
-
entity_definition: EntityDefinition;
|
|
13
|
-
operation: string;
|
|
14
|
-
entity_culture: string;
|
|
15
|
-
};
|
|
16
|
-
/**
|
|
17
|
-
* Object model for updated paths returned from the webhook payload
|
|
18
|
-
*/
|
|
19
|
-
export type UpdatedPaths = {
|
|
20
|
-
path: string;
|
|
21
|
-
language: string;
|
|
22
|
-
};
|
|
23
|
-
/**
|
|
24
|
-
* Object model for personalized results from GraphqlPersonalizeService
|
|
25
|
-
*/
|
|
26
|
-
export type PersonalizedResult = {
|
|
27
|
-
path: string;
|
|
28
|
-
variantId: string;
|
|
29
|
-
};
|
|
30
|
-
export type RevalidateConfig = {
|
|
31
|
-
/**
|
|
32
|
-
* A GraphQL Request Client Factory is a function that accepts configuration and returns an instance of a GraphQLRequestClient.
|
|
33
|
-
* This factory function is used to create and configure GraphQL clients for making GraphQL API requests.
|
|
34
|
-
*/
|
|
35
|
-
clientFactory: GraphQLRequestClientFactory;
|
|
36
|
-
/**
|
|
37
|
-
* Indicates whether multisite functionality is enabled.
|
|
38
|
-
* Default is false
|
|
39
|
-
*/
|
|
40
|
-
multiSite?: boolean;
|
|
41
|
-
/**
|
|
42
|
-
* Indicates whether personalization is enabled.
|
|
43
|
-
* Default is false
|
|
44
|
-
*/
|
|
45
|
-
personalize?: boolean;
|
|
46
|
-
/**
|
|
47
|
-
* Function to handle language prefixes for different locales.
|
|
48
|
-
* @param language - The language to generate the prefix for.
|
|
49
|
-
* @returns The language prefix or null.
|
|
50
|
-
*/
|
|
51
|
-
localePrefix?: (language: string) => string | null;
|
|
52
|
-
};
|
|
53
|
-
/**
|
|
54
|
-
* Middleware / handler for on-demand ISR (e.g. '/api/revalidate').
|
|
55
|
-
*/
|
|
56
|
-
export declare class RevalidateMiddleware {
|
|
57
|
-
protected config: RevalidateConfig;
|
|
58
|
-
private personalizeService;
|
|
59
|
-
constructor(config: RevalidateConfig);
|
|
60
|
-
/**
|
|
61
|
-
* Generates a Next.js API route handler that executes a revalidation process.
|
|
62
|
-
* @returns The route handler function for handling Next.js API requests.
|
|
63
|
-
*/
|
|
64
|
-
getHandler(): (req: NextApiRequest, res: NextApiResponse) => Promise<void>;
|
|
65
|
-
/**
|
|
66
|
-
* Gets personalized results for the updated paths
|
|
67
|
-
* @param {UpdatedPaths[]} filteredUpdates Updated paths
|
|
68
|
-
*/
|
|
69
|
-
getPersonalizedResults(filteredUpdates: UpdatedPaths[]): Promise<{
|
|
70
|
-
personalized: PersonalizedResult[];
|
|
71
|
-
nonPersonalized: {
|
|
72
|
-
path: string;
|
|
73
|
-
}[];
|
|
74
|
-
}>;
|
|
75
|
-
protected isEmpty(data: UpdatedPaths[]): boolean;
|
|
76
|
-
/**
|
|
77
|
-
* Extracts the paths from the updated paths
|
|
78
|
-
* @param {UpdatedPaths[]} filteredUpdates Updated paths
|
|
79
|
-
* @returns {string[]} paths
|
|
80
|
-
*/
|
|
81
|
-
protected extractPaths(filteredUpdates: UpdatedPaths[]): string[];
|
|
82
|
-
/**
|
|
83
|
-
* Gets the site name from the path name
|
|
84
|
-
* @param {string} pathname Path name
|
|
85
|
-
* @returns {string} site name
|
|
86
|
-
*/
|
|
87
|
-
protected getSiteName(pathname: string): string;
|
|
88
|
-
/**
|
|
89
|
-
* Gets the path name from the full path
|
|
90
|
-
* @param {string} fullPath Full path
|
|
91
|
-
* @returns {string} path name
|
|
92
|
-
*/
|
|
93
|
-
protected getPathName(fullPath: string): string;
|
|
94
|
-
protected extractSiteName(path: string): string;
|
|
95
|
-
/**
|
|
96
|
-
* Filters out the updated paths and language from the request body
|
|
97
|
-
* @param {NextApiRequest} req Next.js API request
|
|
98
|
-
* @returns {UpdatedPaths[]} updated paths
|
|
99
|
-
*/
|
|
100
|
-
protected getFilteredUpdates(req: NextApiRequest): UpdatedPaths[];
|
|
101
|
-
protected handleMultiSitePersonalization(personalizeInfo: {
|
|
102
|
-
personalized: PersonalizedResult[];
|
|
103
|
-
nonPersonalized: {
|
|
104
|
-
path: string;
|
|
105
|
-
}[];
|
|
106
|
-
}, pathsToRevalidate: string[], getPathName: (x: string) => string, getSiteName: (x: string) => string): void;
|
|
107
|
-
protected handleNonMultiSitePersonalization(personalizeInfo: {
|
|
108
|
-
personalized: PersonalizedResult[];
|
|
109
|
-
nonPersonalized: {
|
|
110
|
-
path: string;
|
|
111
|
-
}[];
|
|
112
|
-
}, pathsToRevalidate: string[], getPathName: (x: string) => string): void;
|
|
113
|
-
private handler;
|
|
114
|
-
}
|
|
115
|
-
export {};
|