-
Notifications
You must be signed in to change notification settings - Fork 279
/
Copy pathpersonalize-middleware.ts
296 lines (262 loc) · 10.1 KB
/
personalize-middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import { NextResponse, NextRequest, userAgent } from 'next/server';
import {
GraphQLPersonalizeService,
GraphQLPersonalizeServiceConfig,
CdpService,
CdpServiceConfig,
ExperienceParams,
getPersonalizedRewrite,
} from '@sitecore-jss/sitecore-jss/personalize';
import { SiteInfo, SiteResolver } from '@sitecore-jss/sitecore-jss/site';
import { debug, NativeDataFetcher } from '@sitecore-jss/sitecore-jss';
import { resolvePointOfSale } from '../utils';
export type PersonalizeMiddlewareConfig = {
/**
* Function used to determine if route should be excluded from personalization.
* By default, files (pathname.includes('.')), Next.js API routes (pathname.startsWith('/api/')), and Sitecore API routes (pathname.startsWith('/sitecore/')) are ignored.
* This is an important performance consideration since Next.js Edge middleware runs on every request.
* @param {string} pathname The pathname
* @returns {boolean} Whether to exclude the route from personalization
*/
excludeRoute?: (pathname: string) => boolean;
/**
* Configuration for your Sitecore Experience Edge endpoint
*/
edgeConfig: Omit<GraphQLPersonalizeServiceConfig, 'fetch'>;
/**
* Configuration for your Sitecore CDP endpoint
*/
cdpConfig: Omit<CdpServiceConfig, 'dataFetcherResolver'>;
/**
* function, determines if middleware should be turned off, based on cookie, header, or other considerations
* @param {NextRequest} [req] optional: request object from middleware handler
* @param {NextResponse} [res] optional: response object from middleware handler
* @returns {boolean} false by default
*/
disabled?: (req?: NextRequest, res?: NextResponse) => boolean;
/**
* Site resolution implementation by name/hostname
*/
siteResolver: SiteResolver;
/**
* fallback hostname in case `host` header is not present
* @default localhost
*/
defaultHostname?: string;
/**
* function to resolve point of sale endpoint for a site
* @param {Siteinfo} site to get info from
* @param {string} language to get info for
* @returns point of sale value for site/language combination
*/
resolvePointOfSale?: (site?: SiteInfo, language?: string) => string;
};
/**
* Middleware / handler to support Sitecore Personalize
*/
export class PersonalizeMiddleware {
private personalizeService: GraphQLPersonalizeService;
private cdpService: CdpService;
private defaultHostname: string;
/**
* @param {PersonalizeMiddlewareConfig} [config] Personalize middleware config
*/
constructor(protected config: PersonalizeMiddlewareConfig) {
// NOTE: we provide native fetch for compatibility on Next.js Edge Runtime
// (underlying default 'cross-fetch' is not currently compatible: https://github.com/lquixada/cross-fetch/issues/78)
this.personalizeService = new GraphQLPersonalizeService({
...config.edgeConfig,
fetch: fetch,
});
// NOTE: same here, we provide NativeDataFetcher for compatibility on Next.js Edge Runtime
this.cdpService = new CdpService({
...config.cdpConfig,
dataFetcherResolver: <T>({
timeout,
headers,
}: {
timeout: number;
headers?: Record<string, string>;
}) => {
const fetcher = new NativeDataFetcher({
debugger: debug.personalize,
timeout,
headers,
});
return (url: string, data?: unknown) => fetcher.fetch<T>(url, data);
},
});
this.defaultHostname = config.defaultHostname || 'localhost';
}
/**
* Gets the Next.js middleware handler with error handling
* @returns middleware handler
*/
public getHandler(): (req: NextRequest, res?: NextResponse) => Promise<NextResponse> {
return async (req, res) => {
try {
return await this.handler(req, res);
} catch (error) {
console.log('Personalize middleware failed:');
console.log(error);
return res || NextResponse.next();
}
};
}
protected get browserIdCookieName(): string {
// Each user should have saved identifier to connect between session, CDP uses bid cookies + local storage
return `BID_${this.config.cdpConfig.clientKey}`;
}
protected getBrowserId(req: NextRequest): string | undefined {
return req.cookies.get(this.browserIdCookieName)?.value || undefined;
}
protected setBrowserId(res: NextResponse, browserId: string) {
const expiryDate = new Date(new Date().setFullYear(new Date().getFullYear() + 2));
const options = { expires: expiryDate, secure: true };
res.cookies.set(this.browserIdCookieName, browserId, options);
}
protected getExperienceParams(req: NextRequest): ExperienceParams {
return {
referrer: req.referrer,
utm: {
campaign: req.nextUrl.searchParams.get('utm_campaign'),
content: req.nextUrl.searchParams.get('utm_content'),
medium: req.nextUrl.searchParams.get('utm_medium'),
source: req.nextUrl.searchParams.get('utm_source'),
},
};
}
protected excludeRoute(pathname: string) {
if (
pathname.includes('.') || // Ignore files
pathname.startsWith('/api/') || // Ignore Next.js API calls
pathname.startsWith('/sitecore/') || // Ignore Sitecore API calls
pathname.startsWith('/_next') // Ignore next service calls
) {
return true;
}
return false;
}
protected isPreview(req: NextRequest) {
return (
req.cookies.get('__prerender_bypass')?.value || req.cookies.get('__next_preview_data')?.value
);
}
/**
* Safely extract all headers for debug logging
* Necessary to avoid middleware issue https://github.com/vercel/next.js/issues/39765
* @param {Headers} incomingHeaders Incoming headers
* @returns Object with headers as key/value pairs
*/
protected extractDebugHeaders(incomingHeaders: Headers) {
const headers = {} as { [key: string]: string };
incomingHeaders.forEach((value, key) => (headers[key] = value));
return headers;
}
private handler = async (req: NextRequest, res?: NextResponse): Promise<NextResponse> => {
const hostHeader = req.headers.get('host')?.split(':')[0];
const hostname = hostHeader || this.defaultHostname;
const pathname = req.nextUrl.pathname;
const language = req.nextUrl.locale || req.nextUrl.defaultLocale || 'en';
let siteName = res?.cookies.get('sc_site')?.value;
let browserId = this.getBrowserId(req);
debug.personalize('personalize middleware start: %o', {
pathname,
language,
});
if (!hostHeader) {
debug.personalize(`host header is missing, default ${hostname} is used`);
}
// Response will be provided if other middleware is run before us (e.g. redirects)
let response = res || NextResponse.next();
if (this.config.disabled && this.config.disabled(req, response)) {
debug.personalize('skipped (personalize middleware is disabled)');
return response;
}
if (
response.redirected || // Don't attempt to personalize a redirect
this.isPreview(req) || // No need to personalize for preview (layout data is already prepared for preview)
this.excludeRoute(pathname) ||
(this.config.excludeRoute && this.config.excludeRoute(pathname))
) {
debug.personalize(
'skipped (%s)',
response.redirected ? 'redirected' : this.isPreview(req) ? 'preview' : 'route excluded'
);
return response;
}
let site;
if (!siteName) {
site = this.config.siteResolver.getByHost(hostname);
siteName = site.name;
} else {
site = this.config.siteResolver.getByName(siteName);
}
// Get personalization info from Experience Edge
const personalizeInfo = await this.personalizeService.getPersonalizeInfo(
pathname,
language,
siteName
);
if (!personalizeInfo) {
// Likely an invalid route / language
debug.personalize('skipped (personalize info not found)');
return response;
}
if (personalizeInfo.variantIds.length === 0) {
debug.personalize('skipped (no personalization configured)');
return response;
}
if (!browserId) {
browserId = await this.cdpService.generateBrowserId();
if (!browserId) {
debug.personalize('skipped (browser id generation failed)');
return response;
}
}
// Execute targeted experience in CDP
const { ua } = userAgent(req);
const params = this.getExperienceParams(req);
const pointOfSale = this.config.resolvePointOfSale
? this.config.resolvePointOfSale(site, language)
: resolvePointOfSale(site, language);
const variantId = await this.cdpService.executeExperience(
personalizeInfo.contentId,
browserId,
ua,
pointOfSale,
params
);
if (!variantId) {
debug.personalize('skipped (no variant identified)');
return response;
}
if (!personalizeInfo.variantIds.includes(variantId)) {
debug.personalize('skipped (invalid variant)');
return response;
}
// Path can be rewritten by previously executed middleware
const basePath = res?.headers.get('x-sc-rewrite') || pathname;
// Rewrite to persononalized path
const rewritePath = getPersonalizedRewrite(basePath, { variantId });
// Note an absolute URL is required: https://nextjs.org/docs/messages/middleware-relative-urls
const rewriteUrl = req.nextUrl.clone();
rewriteUrl.pathname = rewritePath;
response = NextResponse.rewrite(rewriteUrl);
// Disable preflight caching to force revalidation on client-side navigation (personalization may be influenced)
// See https://github.com/vercel/next.js/issues/32727
response.headers.set('x-middleware-cache', 'no-cache');
// Share rewrite path with following executed middlewares
response.headers.set('x-sc-rewrite', rewritePath);
// Set browserId cookie on the response
this.setBrowserId(response, browserId);
// Share site name with the following executed middlewares
response.cookies.set('sc_site', siteName);
debug.personalize('personalize middleware end: %o', {
rewritePath,
browserId,
headers: this.extractDebugHeaders(response.headers),
});
return response;
};
}