@akinon/next 2.0.23-rc.0 → 2.0.23
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/CHANGELOG.md +11 -23
- package/bin/pz-generate-routes.js +1 -4
- package/components/plugin-module.tsx +1 -0
- package/data/client/checkout.ts +1 -0
- package/data/server/category.ts +5 -14
- package/data/server/list.ts +4 -13
- package/data/server/product.ts +4 -11
- package/data/server/special-page.ts +4 -14
- package/data/server/widget.ts +1 -14
- package/data/urls.ts +1 -5
- package/hooks/use-captcha.tsx +1 -1
- package/middlewares/default.ts +249 -254
- package/middlewares/masterpass-rest-callback.ts +202 -89
- package/package.json +2 -2
- package/plugins.d.ts +0 -10
- package/plugins.js +0 -1
- package/redux/middlewares/checkout.ts +3 -45
- package/redux/middlewares/pre-order/installment-option.ts +1 -9
- package/types/index.ts +9 -20
- package/utils/csrf.ts +1 -1
- package/utils/index.ts +1 -0
- package/utils/normalize-search-params.ts +42 -0
- package/with-pz-config.js +1 -2
- package/utils/payload-optimizer.ts +0 -481
package/middlewares/default.ts
CHANGED
|
@@ -18,11 +18,7 @@ import {
|
|
|
18
18
|
withBfcacheHeaders
|
|
19
19
|
} from '.';
|
|
20
20
|
import { getCsrfCookieFlags, urlLocaleMatcherRegex } from '../utils';
|
|
21
|
-
import {
|
|
22
|
-
getPzSegmentsConfig,
|
|
23
|
-
encodePzValue,
|
|
24
|
-
isLegacyMode
|
|
25
|
-
} from '../utils/pz-segments';
|
|
21
|
+
import { getPzSegmentsConfig, encodePzValue, isLegacyMode } from '../utils/pz-segments';
|
|
26
22
|
import withCurrency from './currency';
|
|
27
23
|
import withLocale from './locale';
|
|
28
24
|
import logger from '../utils/log';
|
|
@@ -266,98 +262,98 @@ const withPzDefault =
|
|
|
266
262
|
req: PzNextRequest,
|
|
267
263
|
event: NextFetchEvent
|
|
268
264
|
) => {
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
middlewareResult = (await middleware(
|
|
285
|
-
req,
|
|
286
|
-
event
|
|
287
|
-
)) as NextResponse | void;
|
|
288
|
-
|
|
289
|
-
let customRewriteUrlDiff = '';
|
|
265
|
+
let middlewareResult: NextResponse | void =
|
|
266
|
+
NextResponse.next();
|
|
267
|
+
|
|
268
|
+
try {
|
|
269
|
+
const { locale, prettyUrl, currency } =
|
|
270
|
+
req.middlewareParams.rewrites;
|
|
271
|
+
const { defaultLocaleValue } =
|
|
272
|
+
Settings.localization;
|
|
273
|
+
const url = req.nextUrl.clone();
|
|
274
|
+
const pathnameWithoutLocale =
|
|
275
|
+
url.pathname.replace(
|
|
276
|
+
urlLocaleMatcherRegex,
|
|
277
|
+
''
|
|
278
|
+
);
|
|
290
279
|
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
280
|
+
middlewareResult = (await middleware(
|
|
281
|
+
req,
|
|
282
|
+
event
|
|
283
|
+
)) as NextResponse | void;
|
|
284
|
+
|
|
285
|
+
let customRewriteUrlDiff = '';
|
|
286
|
+
|
|
287
|
+
if (
|
|
288
|
+
middlewareResult instanceof
|
|
289
|
+
NextResponse &&
|
|
290
|
+
middlewareResult.headers.get(
|
|
291
|
+
'pz-override-response'
|
|
292
|
+
) &&
|
|
293
|
+
middlewareResult.headers.get(
|
|
294
|
+
'x-middleware-rewrite'
|
|
295
|
+
)
|
|
296
|
+
) {
|
|
297
|
+
const rewriteUrl = new URL(
|
|
297
298
|
middlewareResult.headers.get(
|
|
298
299
|
'x-middleware-rewrite'
|
|
299
300
|
)
|
|
300
|
-
)
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
301
|
+
);
|
|
302
|
+
const originalUrl = new URL(req.url);
|
|
303
|
+
customRewriteUrlDiff =
|
|
304
|
+
rewriteUrl.pathname.replace(
|
|
305
|
+
originalUrl.pathname,
|
|
306
|
+
''
|
|
305
307
|
);
|
|
306
|
-
|
|
307
|
-
customRewriteUrlDiff =
|
|
308
|
-
rewriteUrl.pathname.replace(
|
|
309
|
-
originalUrl.pathname,
|
|
310
|
-
''
|
|
311
|
-
);
|
|
312
|
-
}
|
|
308
|
+
}
|
|
313
309
|
|
|
314
|
-
|
|
310
|
+
let ordersPrefix: string;
|
|
315
311
|
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
312
|
+
if (isLegacyMode(Settings)) {
|
|
313
|
+
url.basePath = `/${commerceUrl}`;
|
|
314
|
+
url.pathname =
|
|
315
|
+
`/${locale.length ? `${locale}/` : ''}${currency}/${customRewriteUrlDiff}${
|
|
316
|
+
prettyUrl ?? pathnameWithoutLocale
|
|
317
|
+
}`.replace(/\/+/g, '/');
|
|
318
|
+
ordersPrefix = `/${currency}/orders`;
|
|
319
|
+
} else {
|
|
320
|
+
const pzConfig =
|
|
321
|
+
getPzSegmentsConfig(Settings);
|
|
322
|
+
const fullUrlPath =
|
|
323
|
+
`${customRewriteUrlDiff}${
|
|
321
324
|
prettyUrl ?? pathnameWithoutLocale
|
|
322
325
|
}`.replace(/\/+/g, '/');
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
string
|
|
355
|
-
> = {
|
|
356
|
-
locale: resolvedLocale,
|
|
357
|
-
currency,
|
|
358
|
-
url: encodeURIComponent(fullUrl),
|
|
359
|
-
...Object.fromEntries(
|
|
360
|
-
customSegments.map((seg) => [
|
|
326
|
+
const fullUrl = `${req.nextUrl.origin}${fullUrlPath}`;
|
|
327
|
+
const resolvedLocale =
|
|
328
|
+
locale?.length > 0
|
|
329
|
+
? locale
|
|
330
|
+
: Settings.localization
|
|
331
|
+
.defaultLocaleValue;
|
|
332
|
+
const resolveContext = {
|
|
333
|
+
req,
|
|
334
|
+
event,
|
|
335
|
+
url,
|
|
336
|
+
locale: resolvedLocale,
|
|
337
|
+
currency,
|
|
338
|
+
pathname: pathnameWithoutLocale
|
|
339
|
+
};
|
|
340
|
+
const customSegments = pzConfig.segments
|
|
341
|
+
.filter(
|
|
342
|
+
(seg) =>
|
|
343
|
+
seg.name !== 'locale' &&
|
|
344
|
+
seg.name !== 'currency' &&
|
|
345
|
+
seg.name !== 'url'
|
|
346
|
+
);
|
|
347
|
+
const segmentValues: Record<
|
|
348
|
+
string,
|
|
349
|
+
string
|
|
350
|
+
> = {
|
|
351
|
+
locale: resolvedLocale,
|
|
352
|
+
currency,
|
|
353
|
+
url: encodeURIComponent(fullUrl),
|
|
354
|
+
...Object.fromEntries(
|
|
355
|
+
customSegments
|
|
356
|
+
.map((seg) => [
|
|
361
357
|
seg.name,
|
|
362
358
|
req.middlewareParams.rewrites[
|
|
363
359
|
seg.name
|
|
@@ -366,121 +362,105 @@ const withPzDefault =
|
|
|
366
362
|
? seg.resolve(resolveContext)
|
|
367
363
|
: '')
|
|
368
364
|
])
|
|
369
|
-
|
|
370
|
-
|
|
365
|
+
)
|
|
366
|
+
};
|
|
371
367
|
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
368
|
+
const pzValue = encodePzValue(
|
|
369
|
+
segmentValues,
|
|
370
|
+
pzConfig
|
|
371
|
+
);
|
|
372
|
+
|
|
373
|
+
url.pathname =
|
|
374
|
+
`/${pzValue}/${fullUrlPath}`.replace(
|
|
375
|
+
/\/+/g,
|
|
376
|
+
'/'
|
|
375
377
|
);
|
|
378
|
+
ordersPrefix = `/${pzValue}/orders`;
|
|
379
|
+
}
|
|
376
380
|
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
381
|
+
if (
|
|
382
|
+
Settings.usePrettyUrlRoute &&
|
|
383
|
+
url.searchParams.toString().length > 0 &&
|
|
384
|
+
!Object.entries(ROUTES).find(
|
|
385
|
+
([, value]) =>
|
|
386
|
+
new RegExp(`^${value}/?$`).test(
|
|
387
|
+
pathnameWithoutLocale
|
|
388
|
+
)
|
|
389
|
+
)
|
|
390
|
+
) {
|
|
391
|
+
url.pathname =
|
|
392
|
+
url.pathname +
|
|
393
|
+
(/\/$/.test(url.pathname) ? '' : '/') +
|
|
394
|
+
`searchparams|${encodeURIComponent(
|
|
395
|
+
url.searchParams.toString()
|
|
396
|
+
)}`;
|
|
397
|
+
}
|
|
384
398
|
|
|
399
|
+
Settings.rewrites.forEach((rewrite) => {
|
|
400
|
+
url.pathname = url.pathname.replace(
|
|
401
|
+
rewrite.source,
|
|
402
|
+
rewrite.destination
|
|
403
|
+
);
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
// if middleware.ts has a return value for current url
|
|
407
|
+
if (
|
|
408
|
+
middlewareResult instanceof NextResponse
|
|
409
|
+
) {
|
|
410
|
+
// pz-override-response header is used to prevent 404 page for custom responses.
|
|
385
411
|
if (
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
!Object.entries(ROUTES).find(
|
|
390
|
-
([, value]) =>
|
|
391
|
-
new RegExp(`^${value}/?$`).test(
|
|
392
|
-
pathnameWithoutLocale
|
|
393
|
-
)
|
|
394
|
-
)
|
|
412
|
+
middlewareResult.headers.get(
|
|
413
|
+
'pz-override-response'
|
|
414
|
+
) !== 'true'
|
|
395
415
|
) {
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
? ''
|
|
400
|
-
: '/') +
|
|
401
|
-
`searchparams|${encodeURIComponent(
|
|
402
|
-
url.searchParams.toString()
|
|
403
|
-
)}`;
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
Settings.rewrites.forEach((rewrite) => {
|
|
407
|
-
url.pathname = url.pathname.replace(
|
|
408
|
-
rewrite.source,
|
|
409
|
-
rewrite.destination
|
|
416
|
+
middlewareResult.headers.set(
|
|
417
|
+
'x-middleware-rewrite',
|
|
418
|
+
url.href
|
|
410
419
|
);
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
middlewareResult
|
|
420
|
+
} else if (
|
|
421
|
+
middlewareResult.headers.get(
|
|
422
|
+
'x-middleware-rewrite'
|
|
423
|
+
) &&
|
|
424
|
+
middlewareResult.headers.get(
|
|
425
|
+
'pz-override-response'
|
|
426
|
+
) === 'true'
|
|
416
427
|
) {
|
|
417
|
-
// pz-override-response header is used to prevent 404 page for custom responses.
|
|
418
|
-
if (
|
|
419
|
-
middlewareResult.headers.get(
|
|
420
|
-
'pz-override-response'
|
|
421
|
-
) !== 'true'
|
|
422
|
-
) {
|
|
423
|
-
middlewareResult.headers.set(
|
|
424
|
-
'x-middleware-rewrite',
|
|
425
|
-
url.href
|
|
426
|
-
);
|
|
427
|
-
} else if (
|
|
428
|
-
middlewareResult.headers.get(
|
|
429
|
-
'x-middleware-rewrite'
|
|
430
|
-
) &&
|
|
431
|
-
middlewareResult.headers.get(
|
|
432
|
-
'pz-override-response'
|
|
433
|
-
) === 'true'
|
|
434
|
-
) {
|
|
435
|
-
middlewareResult =
|
|
436
|
-
NextResponse.rewrite(url);
|
|
437
|
-
}
|
|
438
|
-
} else {
|
|
439
|
-
// if middleware.ts doesn't have a return value.
|
|
440
|
-
// e.g. NextResponse.next() doesn't exist in middleware.ts
|
|
441
|
-
|
|
442
428
|
middlewareResult =
|
|
443
429
|
NextResponse.rewrite(url);
|
|
444
430
|
}
|
|
431
|
+
} else {
|
|
432
|
+
// if middleware.ts doesn't have a return value.
|
|
433
|
+
// e.g. NextResponse.next() doesn't exist in middleware.ts
|
|
445
434
|
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
const fallbackHost =
|
|
450
|
-
req.headers.get('x-forwarded-host') ||
|
|
451
|
-
req.headers.get('host');
|
|
452
|
-
const hostname =
|
|
453
|
-
process.env.NEXT_PUBLIC_URL ||
|
|
454
|
-
`https://${fallbackHost}`;
|
|
455
|
-
const rootHostname =
|
|
456
|
-
localeUrlStrategy ===
|
|
457
|
-
LocaleUrlStrategy.Subdomain
|
|
458
|
-
? getRootHostname(hostname)
|
|
459
|
-
: null;
|
|
460
|
-
|
|
461
|
-
if (
|
|
462
|
-
!url.pathname.startsWith(ordersPrefix)
|
|
463
|
-
) {
|
|
464
|
-
middlewareResult.cookies.set(
|
|
465
|
-
'pz-locale',
|
|
466
|
-
locale?.length > 0
|
|
467
|
-
? locale
|
|
468
|
-
: defaultLocaleValue,
|
|
469
|
-
{
|
|
470
|
-
domain: rootHostname,
|
|
471
|
-
sameSite: 'none',
|
|
472
|
-
secure: true,
|
|
473
|
-
expires: new Date(
|
|
474
|
-
Date.now() +
|
|
475
|
-
1000 * 60 * 60 * 24 * 7
|
|
476
|
-
) // 7 days
|
|
477
|
-
}
|
|
478
|
-
);
|
|
479
|
-
}
|
|
435
|
+
middlewareResult =
|
|
436
|
+
NextResponse.rewrite(url);
|
|
437
|
+
}
|
|
480
438
|
|
|
439
|
+
const { localeUrlStrategy } =
|
|
440
|
+
Settings.localization;
|
|
441
|
+
|
|
442
|
+
const fallbackHost =
|
|
443
|
+
req.headers.get('x-forwarded-host') ||
|
|
444
|
+
req.headers.get('host');
|
|
445
|
+
const hostname =
|
|
446
|
+
process.env.NEXT_PUBLIC_URL ||
|
|
447
|
+
`https://${fallbackHost}`;
|
|
448
|
+
const rootHostname =
|
|
449
|
+
localeUrlStrategy ===
|
|
450
|
+
LocaleUrlStrategy.Subdomain
|
|
451
|
+
? getRootHostname(hostname)
|
|
452
|
+
: null;
|
|
453
|
+
|
|
454
|
+
if (
|
|
455
|
+
!url.pathname.startsWith(
|
|
456
|
+
ordersPrefix
|
|
457
|
+
)
|
|
458
|
+
) {
|
|
481
459
|
middlewareResult.cookies.set(
|
|
482
|
-
'pz-
|
|
483
|
-
|
|
460
|
+
'pz-locale',
|
|
461
|
+
locale?.length > 0
|
|
462
|
+
? locale
|
|
463
|
+
: defaultLocaleValue,
|
|
484
464
|
{
|
|
485
465
|
domain: rootHostname,
|
|
486
466
|
sameSite: 'none',
|
|
@@ -490,89 +470,104 @@ const withPzDefault =
|
|
|
490
470
|
) // 7 days
|
|
491
471
|
}
|
|
492
472
|
);
|
|
473
|
+
}
|
|
493
474
|
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
});
|
|
475
|
+
middlewareResult.cookies.set(
|
|
476
|
+
'pz-currency',
|
|
477
|
+
currency,
|
|
478
|
+
{
|
|
479
|
+
domain: rootHostname,
|
|
480
|
+
sameSite: 'none',
|
|
481
|
+
secure: true,
|
|
482
|
+
expires: new Date(
|
|
483
|
+
Date.now() + 1000 * 60 * 60 * 24 * 7
|
|
484
|
+
) // 7 days
|
|
505
485
|
}
|
|
486
|
+
);
|
|
487
|
+
|
|
488
|
+
if (
|
|
489
|
+
req.cookies.get('pz-locale') &&
|
|
490
|
+
req.cookies.get('pz-locale').value !==
|
|
491
|
+
locale
|
|
492
|
+
) {
|
|
493
|
+
logger.debug('Locale changed', {
|
|
494
|
+
locale,
|
|
495
|
+
oldLocale:
|
|
496
|
+
req.cookies.get('pz-locale')?.value,
|
|
497
|
+
ip
|
|
498
|
+
});
|
|
499
|
+
}
|
|
506
500
|
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
501
|
+
middlewareResult.headers.set(
|
|
502
|
+
'pz-url',
|
|
503
|
+
req.nextUrl.toString()
|
|
504
|
+
);
|
|
505
|
+
|
|
506
|
+
if (req.cookies.get('pz-set-currency')) {
|
|
507
|
+
middlewareResult.cookies.delete(
|
|
508
|
+
'pz-set-currency'
|
|
510
509
|
);
|
|
510
|
+
}
|
|
511
511
|
|
|
512
|
-
|
|
512
|
+
if (
|
|
513
|
+
req.cookies.get(
|
|
514
|
+
'pz-post-checkout-flow'
|
|
515
|
+
)
|
|
516
|
+
) {
|
|
517
|
+
if (
|
|
518
|
+
pathnameWithoutLocale.startsWith(
|
|
519
|
+
'/orders/completed/'
|
|
520
|
+
) ||
|
|
521
|
+
pathnameWithoutLocale.startsWith(
|
|
522
|
+
'/basket'
|
|
523
|
+
)
|
|
524
|
+
) {
|
|
513
525
|
middlewareResult.cookies.delete(
|
|
514
|
-
'pz-
|
|
526
|
+
'pz-post-checkout-flow'
|
|
515
527
|
);
|
|
516
528
|
}
|
|
529
|
+
}
|
|
517
530
|
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
) ||
|
|
525
|
-
pathnameWithoutLocale.startsWith(
|
|
526
|
-
'/basket'
|
|
527
|
-
)
|
|
528
|
-
) {
|
|
529
|
-
middlewareResult.cookies.delete(
|
|
530
|
-
'pz-post-checkout-flow'
|
|
531
|
-
);
|
|
532
|
-
}
|
|
533
|
-
}
|
|
531
|
+
if (process.env.ACC_APP_VERSION) {
|
|
532
|
+
middlewareResult.headers.set(
|
|
533
|
+
'acc-app-version',
|
|
534
|
+
process.env.ACC_APP_VERSION
|
|
535
|
+
);
|
|
536
|
+
}
|
|
534
537
|
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
process.env.ACC_APP_VERSION
|
|
539
|
-
);
|
|
540
|
-
}
|
|
538
|
+
// Set CSRF token if not set
|
|
539
|
+
try {
|
|
540
|
+
const url = `${Settings.commerceUrl}${user.csrfToken}`;
|
|
541
541
|
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
domain: rootHostname,
|
|
556
|
-
...getCsrfCookieFlags()
|
|
557
|
-
}
|
|
558
|
-
);
|
|
559
|
-
}
|
|
560
|
-
} catch (error) {
|
|
561
|
-
logger.error('CSRF Error', {
|
|
562
|
-
error,
|
|
563
|
-
ip
|
|
564
|
-
});
|
|
542
|
+
if (!req.cookies.get('csrftoken')) {
|
|
543
|
+
const { csrf_token } = await (
|
|
544
|
+
await fetch(url)
|
|
545
|
+
).json();
|
|
546
|
+
middlewareResult.cookies.set(
|
|
547
|
+
'csrftoken',
|
|
548
|
+
csrf_token,
|
|
549
|
+
{
|
|
550
|
+
path: '/',
|
|
551
|
+
domain: rootHostname,
|
|
552
|
+
...getCsrfCookieFlags()
|
|
553
|
+
}
|
|
554
|
+
);
|
|
565
555
|
}
|
|
566
556
|
} catch (error) {
|
|
567
|
-
logger.error('
|
|
557
|
+
logger.error('CSRF Error', {
|
|
568
558
|
error,
|
|
569
559
|
ip
|
|
570
560
|
});
|
|
571
561
|
}
|
|
572
|
-
|
|
573
|
-
|
|
562
|
+
} catch (error) {
|
|
563
|
+
logger.error('withPzDefault Error', {
|
|
564
|
+
error,
|
|
565
|
+
ip
|
|
566
|
+
});
|
|
574
567
|
}
|
|
575
|
-
|
|
568
|
+
|
|
569
|
+
return middlewareResult;
|
|
570
|
+
})
|
|
576
571
|
)
|
|
577
572
|
)
|
|
578
573
|
)
|