@akinon/next 2.0.0-beta.2 → 2.0.0-beta.20

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.
Files changed (189) hide show
  1. package/.eslintrc.js +12 -0
  2. package/CHANGELOG.md +377 -7
  3. package/__tests__/next-config.test.ts +83 -0
  4. package/__tests__/tsconfig.json +23 -0
  5. package/api/auth.ts +133 -44
  6. package/api/barcode-search.ts +59 -0
  7. package/api/cache.ts +41 -5
  8. package/api/client.ts +21 -4
  9. package/api/form.ts +85 -0
  10. package/api/image-proxy.ts +75 -0
  11. package/api/product-categories.ts +53 -0
  12. package/api/similar-product-list.ts +63 -0
  13. package/api/similar-products.ts +111 -0
  14. package/api/virtual-try-on.ts +382 -0
  15. package/assets/styles/index.scss +84 -0
  16. package/babel.config.js +6 -0
  17. package/bin/pz-generate-routes.js +115 -0
  18. package/bin/pz-prebuild.js +1 -0
  19. package/bin/pz-predev.js +1 -0
  20. package/bin/pz-run-tests.js +99 -0
  21. package/bin/run-prebuild-tests.js +46 -0
  22. package/components/accordion.tsx +20 -5
  23. package/components/button.tsx +51 -36
  24. package/components/client-root.tsx +138 -2
  25. package/components/file-input.tsx +65 -3
  26. package/components/index.ts +1 -0
  27. package/components/input.tsx +1 -1
  28. package/components/link.tsx +46 -16
  29. package/components/logger-popup.tsx +213 -0
  30. package/components/modal.tsx +32 -16
  31. package/components/plugin-module.tsx +62 -3
  32. package/components/price.tsx +2 -2
  33. package/components/select.tsx +1 -1
  34. package/components/selected-payment-option-view.tsx +21 -0
  35. package/components/theme-editor/blocks/accordion-block.tsx +136 -0
  36. package/components/theme-editor/blocks/block-renderer-registry.tsx +77 -0
  37. package/components/theme-editor/blocks/button-block.tsx +593 -0
  38. package/components/theme-editor/blocks/counter-block.tsx +348 -0
  39. package/components/theme-editor/blocks/divider-block.tsx +20 -0
  40. package/components/theme-editor/blocks/embed-block.tsx +208 -0
  41. package/components/theme-editor/blocks/group-block.tsx +116 -0
  42. package/components/theme-editor/blocks/hotspot-block.tsx +147 -0
  43. package/components/theme-editor/blocks/icon-block.tsx +230 -0
  44. package/components/theme-editor/blocks/image-block.tsx +137 -0
  45. package/components/theme-editor/blocks/image-gallery-block.tsx +269 -0
  46. package/components/theme-editor/blocks/input-block.tsx +123 -0
  47. package/components/theme-editor/blocks/link-block.tsx +216 -0
  48. package/components/theme-editor/blocks/lottie-block.tsx +325 -0
  49. package/components/theme-editor/blocks/map-block.tsx +89 -0
  50. package/components/theme-editor/blocks/slider-block.tsx +595 -0
  51. package/components/theme-editor/blocks/tab-block.tsx +10 -0
  52. package/components/theme-editor/blocks/text-block.tsx +52 -0
  53. package/components/theme-editor/blocks/video-block.tsx +122 -0
  54. package/components/theme-editor/components/action-toolbar.tsx +305 -0
  55. package/components/theme-editor/components/designer-overlay.tsx +74 -0
  56. package/components/theme-editor/components/with-designer-features.tsx +142 -0
  57. package/components/theme-editor/dynamic-font-loader.tsx +79 -0
  58. package/components/theme-editor/hooks/use-designer-features.tsx +100 -0
  59. package/components/theme-editor/hooks/use-external-designer.tsx +95 -0
  60. package/components/theme-editor/hooks/use-native-widget-data.ts +188 -0
  61. package/components/theme-editor/hooks/use-visibility-context.ts +27 -0
  62. package/components/theme-editor/placeholder-registry.ts +31 -0
  63. package/components/theme-editor/sections/before-after-section.tsx +245 -0
  64. package/components/theme-editor/sections/contact-form-section.tsx +563 -0
  65. package/components/theme-editor/sections/countdown-campaign-banner-section.tsx +433 -0
  66. package/components/theme-editor/sections/coupon-banner-section.tsx +710 -0
  67. package/components/theme-editor/sections/divider-section.tsx +62 -0
  68. package/components/theme-editor/sections/featured-product-spotlight-section.tsx +507 -0
  69. package/components/theme-editor/sections/find-in-store-section.tsx +1995 -0
  70. package/components/theme-editor/sections/hover-showcase-section.tsx +326 -0
  71. package/components/theme-editor/sections/image-hotspot-section.tsx +142 -0
  72. package/components/theme-editor/sections/installment-options-section.tsx +1065 -0
  73. package/components/theme-editor/sections/notification-banner-section.tsx +173 -0
  74. package/components/theme-editor/sections/order-tracking-lookup-section.tsx +1379 -0
  75. package/components/theme-editor/sections/posts-slider-section.tsx +472 -0
  76. package/components/theme-editor/sections/pre-order-launch-banner-section.tsx +663 -0
  77. package/components/theme-editor/sections/section-renderer-registry.tsx +89 -0
  78. package/components/theme-editor/sections/section-wrapper.tsx +135 -0
  79. package/components/theme-editor/sections/shipping-threshold-progress-section.tsx +586 -0
  80. package/components/theme-editor/sections/stats-counter-section.tsx +486 -0
  81. package/components/theme-editor/sections/tabs-section.tsx +578 -0
  82. package/components/theme-editor/theme-block.tsx +102 -0
  83. package/components/theme-editor/theme-placeholder-client.tsx +218 -0
  84. package/components/theme-editor/theme-placeholder-wrapper.tsx +732 -0
  85. package/components/theme-editor/theme-placeholder.tsx +288 -0
  86. package/components/theme-editor/theme-section.tsx +1224 -0
  87. package/components/theme-editor/theme-settings-context.tsx +13 -0
  88. package/components/theme-editor/utils/index.ts +792 -0
  89. package/components/theme-editor/utils/iterator-utils.ts +234 -0
  90. package/components/theme-editor/utils/publish-window.ts +86 -0
  91. package/components/theme-editor/utils/visibility-rules.ts +188 -0
  92. package/data/client/account.ts +17 -2
  93. package/data/client/api.ts +2 -0
  94. package/data/client/basket.ts +66 -5
  95. package/data/client/checkout.ts +391 -99
  96. package/data/client/misc.ts +38 -2
  97. package/data/client/product.ts +19 -2
  98. package/data/client/user.ts +16 -8
  99. package/data/server/category.ts +11 -9
  100. package/data/server/flatpage.ts +11 -4
  101. package/data/server/form.ts +15 -4
  102. package/data/server/landingpage.ts +11 -4
  103. package/data/server/list.ts +5 -4
  104. package/data/server/menu.ts +11 -3
  105. package/data/server/product.ts +111 -55
  106. package/data/server/seo.ts +14 -4
  107. package/data/server/special-page.ts +5 -4
  108. package/data/server/widget.ts +90 -5
  109. package/data/urls.ts +16 -5
  110. package/hocs/client/with-segment-defaults.tsx +2 -2
  111. package/hocs/server/with-segment-defaults.tsx +65 -20
  112. package/hooks/index.ts +4 -0
  113. package/hooks/use-localization.ts +24 -10
  114. package/hooks/use-logger-context.tsx +114 -0
  115. package/hooks/use-logger.ts +92 -0
  116. package/hooks/use-loyalty-availability.ts +21 -0
  117. package/hooks/use-payment-options.ts +2 -1
  118. package/hooks/use-pz-params.ts +37 -0
  119. package/hooks/use-router.ts +51 -14
  120. package/hooks/use-sentry-uncaught-errors.ts +24 -0
  121. package/instrumentation/index.ts +10 -1
  122. package/instrumentation/node.ts +2 -20
  123. package/jest.config.js +25 -0
  124. package/lib/cache-handler.mjs +534 -16
  125. package/lib/cache.ts +272 -37
  126. package/localization/index.ts +2 -1
  127. package/localization/provider.tsx +2 -5
  128. package/middlewares/bfcache-headers.ts +18 -0
  129. package/middlewares/checkout-provider.ts +1 -1
  130. package/middlewares/complete-gpay.ts +32 -26
  131. package/middlewares/complete-masterpass.ts +33 -26
  132. package/middlewares/complete-wallet.ts +182 -0
  133. package/middlewares/default.ts +360 -215
  134. package/middlewares/index.ts +10 -2
  135. package/middlewares/locale.ts +34 -11
  136. package/middlewares/masterpass-rest-callback.ts +230 -0
  137. package/middlewares/oauth-login.ts +200 -57
  138. package/middlewares/pretty-url.ts +21 -8
  139. package/middlewares/redirection-payment.ts +32 -26
  140. package/middlewares/saved-card-redirection.ts +33 -26
  141. package/middlewares/three-d-redirection.ts +32 -26
  142. package/middlewares/url-redirection.ts +11 -1
  143. package/middlewares/wallet-complete-redirection.ts +206 -0
  144. package/package.json +25 -10
  145. package/plugins.d.ts +19 -4
  146. package/plugins.js +10 -1
  147. package/redux/actions.ts +47 -0
  148. package/redux/middlewares/checkout.ts +63 -138
  149. package/redux/middlewares/index.ts +14 -10
  150. package/redux/middlewares/pre-order/address.ts +7 -2
  151. package/redux/middlewares/pre-order/attribute-based-shipping-option.ts +7 -1
  152. package/redux/middlewares/pre-order/data-source-shipping-option.ts +7 -1
  153. package/redux/middlewares/pre-order/delivery-option.ts +7 -1
  154. package/redux/middlewares/pre-order/index.ts +16 -10
  155. package/redux/middlewares/pre-order/installment-option.ts +8 -1
  156. package/redux/middlewares/pre-order/payment-option-reset.ts +37 -0
  157. package/redux/middlewares/pre-order/payment-option.ts +7 -1
  158. package/redux/middlewares/pre-order/pre-order-validation.ts +8 -3
  159. package/redux/middlewares/pre-order/redirection.ts +8 -2
  160. package/redux/middlewares/pre-order/set-pre-order.ts +6 -2
  161. package/redux/middlewares/pre-order/shipping-option.ts +7 -1
  162. package/redux/middlewares/pre-order/shipping-step.ts +5 -1
  163. package/redux/reducers/checkout.ts +23 -3
  164. package/redux/reducers/index.ts +11 -3
  165. package/redux/reducers/root.ts +7 -2
  166. package/redux/reducers/widget.ts +80 -0
  167. package/sentry/index.ts +69 -13
  168. package/tailwind/content.js +16 -0
  169. package/types/commerce/account.ts +5 -1
  170. package/types/commerce/checkout.ts +35 -1
  171. package/types/commerce/widget.ts +33 -0
  172. package/types/index.ts +101 -6
  173. package/types/next-auth.d.ts +2 -2
  174. package/types/widget.ts +80 -0
  175. package/utils/app-fetch.ts +7 -2
  176. package/utils/generate-commerce-search-params.ts +3 -2
  177. package/utils/get-checkout-path.ts +3 -0
  178. package/utils/get-root-hostname.ts +28 -0
  179. package/utils/index.ts +64 -10
  180. package/utils/localization.ts +4 -0
  181. package/utils/mobile-3d-iframe.ts +8 -2
  182. package/utils/override-middleware.ts +7 -12
  183. package/utils/pz-segments.ts +92 -0
  184. package/utils/redirect-ignore.ts +35 -0
  185. package/utils/redirect.ts +9 -3
  186. package/utils/redirection-iframe.ts +8 -2
  187. package/utils/widget-styles.ts +107 -0
  188. package/views/error-page.tsx +93 -0
  189. package/with-pz-config.js +13 -6
@@ -11,14 +11,23 @@ import {
11
11
  withRedirectionPayment,
12
12
  withSavedCardRedirection,
13
13
  withThreeDRedirection,
14
- withUrlRedirection
14
+ withUrlRedirection,
15
+ withCompleteWallet,
16
+ withWalletCompleteRedirection,
17
+ withMasterpassRestCallback,
18
+ withBfcacheHeaders
15
19
  } from '.';
16
20
  import { urlLocaleMatcherRegex } from '../utils';
21
+ import { getPzSegmentsConfig, encodePzValue, isLegacyMode } from '../utils/pz-segments';
17
22
  import withCurrency from './currency';
18
23
  import withLocale from './locale';
19
24
  import logger from '../utils/log';
20
25
  import { user } from '../data/urls';
21
26
  import { getUrlPathWithLocale } from '../utils/localization';
27
+ import getRootHostname from '../utils/get-root-hostname';
28
+ import { LocaleUrlStrategy } from '../localization';
29
+
30
+ const POST_CHECKOUT_COOKIE_MAX_AGE_MS = 1000 * 60 * 30; // 30 minutes
22
31
 
23
32
  const withPzDefault =
24
33
  (middleware: NextMiddleware) =>
@@ -95,7 +104,10 @@ const withPzDefault =
95
104
 
96
105
  if (
97
106
  req.nextUrl.pathname.includes('/orders/hooks/') ||
98
- req.nextUrl.pathname.includes('/orders/checkout-with-token/')
107
+ req.nextUrl.pathname.includes('/orders/checkout-with-token/') ||
108
+ req.nextUrl.pathname.includes('/orders/post-checkout-redirect/') ||
109
+ req.nextUrl.pathname.includes('/hooks/cash_register/complete/') ||
110
+ req.nextUrl.pathname.includes('/hooks/cash_register/pre_order/')
99
111
  ) {
100
112
  const queryString = searchParams.toString();
101
113
  const currency = searchParams.get('currency')?.toLowerCase();
@@ -121,8 +133,13 @@ const withPzDefault =
121
133
  }
122
134
 
123
135
  if (req.nextUrl.pathname.startsWith('/orders/redirection/')) {
136
+ const queryString = searchParams.toString();
124
137
  return NextResponse.rewrite(
125
- new URL(`${encodeURI(Settings.commerceUrl)}/orders/redirection/`)
138
+ new URL(
139
+ `${encodeURI(Settings.commerceUrl)}/orders/redirection/${
140
+ queryString ? `?${queryString}` : ''
141
+ }`
142
+ )
126
143
  );
127
144
  }
128
145
 
@@ -140,21 +157,35 @@ const withPzDefault =
140
157
  );
141
158
  }
142
159
 
143
- // If commerce redirects to /orders/checkout/ without locale
160
+ // If commerce redirects to /orders/checkout/ or /orders/post-checkout/ without locale
161
+ const isPostCheckout = !!req.nextUrl.pathname.match(
162
+ new RegExp('^/orders/post-checkout/$')
163
+ );
164
+ const checkoutLocalePath = getUrlPathWithLocale(
165
+ '/orders/checkout/',
166
+ req.cookies.get('pz-locale')?.value
167
+ );
144
168
  if (
145
- req.nextUrl.pathname.match(new RegExp('^/orders/checkout/$')) &&
146
- req.nextUrl.searchParams.size === 0 &&
147
- getUrlPathWithLocale(
148
- '/orders/checkout/',
149
- req.cookies.get('pz-locale')?.value
150
- ) !== req.nextUrl.pathname
169
+ (isPostCheckout && req.nextUrl.searchParams.size === 0) ||
170
+ (req.nextUrl.pathname.match(new RegExp('^/orders/checkout/$')) &&
171
+ req.nextUrl.searchParams.size === 0 &&
172
+ checkoutLocalePath !== req.nextUrl.pathname)
151
173
  ) {
152
- const redirectUrlWithLocale = `${url.origin}${getUrlPathWithLocale(
153
- '/orders/checkout/',
154
- req.cookies.get('pz-locale')?.value
155
- )}`;
174
+ const response = NextResponse.redirect(
175
+ `${url.origin}${checkoutLocalePath}`,
176
+ 303
177
+ );
178
+
179
+ if (isPostCheckout) {
180
+ response.cookies.set('pz-post-checkout-flow', 'true', {
181
+ path: '/',
182
+ sameSite: 'none',
183
+ secure: true,
184
+ expires: new Date(Date.now() + POST_CHECKOUT_COOKIE_MAX_AGE_MS)
185
+ });
186
+ }
156
187
 
157
- return NextResponse.redirect(redirectUrlWithLocale, 303);
188
+ return response;
158
189
  }
159
190
 
160
191
  // Dynamically handle any payment gateway without specifying names
@@ -209,7 +240,6 @@ const withPzDefault =
209
240
 
210
241
  req.middlewareParams = {
211
242
  commerceUrl,
212
- found: true,
213
243
  rewrites: {}
214
244
  };
215
245
 
@@ -224,206 +254,321 @@ const withPzDefault =
224
254
  withCompleteGpay(
225
255
  withCompleteMasterpass(
226
256
  withSavedCardRedirection(
227
- async (req: PzNextRequest, event: NextFetchEvent) => {
228
- let middlewareResult: NextResponse | void =
229
- NextResponse.next();
230
-
231
- try {
232
- const { locale, prettyUrl, currency } =
233
- req.middlewareParams.rewrites;
234
- const { defaultLocaleValue } =
235
- Settings.localization;
236
- const url = req.nextUrl.clone();
237
- const pathnameWithoutLocale =
238
- url.pathname.replace(urlLocaleMatcherRegex, '');
239
-
240
- middlewareResult = (await middleware(
241
- req,
242
- event
243
- )) as NextResponse | void;
244
-
245
- let customRewriteUrlDiff = '';
246
-
247
- if (
248
- middlewareResult instanceof NextResponse &&
249
- middlewareResult.headers.get(
250
- 'pz-override-response'
251
- ) &&
252
- middlewareResult.headers.get(
253
- 'x-middleware-rewrite'
254
- )
255
- ) {
256
- const rewriteUrl = new URL(
257
- middlewareResult.headers.get(
258
- 'x-middleware-rewrite'
259
- )
260
- );
261
- const originalUrl = new URL(req.url);
262
- customRewriteUrlDiff =
263
- rewriteUrl.pathname.replace(
264
- originalUrl.pathname,
265
- ''
266
- );
267
- }
268
-
269
- url.basePath = `/${commerceUrl}`;
270
- url.pathname = `/${
271
- locale.length ? `${locale}/` : ''
272
- }${currency}/${customRewriteUrlDiff}${
273
- prettyUrl ?? pathnameWithoutLocale
274
- }`.replace(/\/+/g, '/');
275
-
276
- if (
277
- Settings.usePrettyUrlRoute &&
278
- url.searchParams.toString().length > 0 &&
279
- !Object.entries(ROUTES).find(([, value]) =>
280
- new RegExp(`^${value}/?$`).test(
281
- pathnameWithoutLocale
282
- )
283
- )
284
- ) {
285
- url.pathname =
286
- url.pathname +
287
- (/\/$/.test(url.pathname) ? '' : '/') +
288
- `searchparams|${url.searchParams.toString()}`;
289
- }
290
-
291
- if (
292
- !req.middlewareParams.found &&
293
- Settings.customNotFoundEnabled
294
- ) {
295
- let pathname = url.pathname
296
- .replace(/\/+$/, '')
297
- .split('/');
298
- url.pathname = url.pathname.replace(
299
- pathname.pop(),
300
- 'pz-not-found'
301
- );
302
- }
303
-
304
- Settings.rewrites.forEach((rewrite) => {
305
- url.pathname = url.pathname.replace(
306
- rewrite.source,
307
- rewrite.destination
308
- );
309
- });
310
-
311
- // if middleware.ts has a return value for current url
312
- if (middlewareResult instanceof NextResponse) {
313
- // pz-override-response header is used to prevent 404 page for custom responses.
314
- if (
315
- middlewareResult.headers.get(
316
- 'pz-override-response'
317
- ) !== 'true'
318
- ) {
319
- middlewareResult.headers.set(
320
- 'x-middleware-rewrite',
321
- url.href
322
- );
323
- } else if (
324
- middlewareResult.headers.get(
325
- 'x-middleware-rewrite'
326
- ) &&
327
- middlewareResult.headers.get(
328
- 'pz-override-response'
329
- ) === 'true'
330
- ) {
331
- middlewareResult = NextResponse.rewrite(url);
332
- }
333
- } else {
334
- // if middleware.ts doesn't have a return value.
335
- // e.g. NextResponse.next() doesn't exist in middleware.ts
336
-
337
- middlewareResult = NextResponse.rewrite(url);
338
- }
339
-
340
- if (
341
- !url.pathname.startsWith(`/${currency}/orders`)
342
- ) {
343
- middlewareResult.cookies.set(
344
- 'pz-locale',
345
- locale?.length > 0
346
- ? locale
347
- : defaultLocaleValue,
348
- {
349
- sameSite: 'none',
350
- secure: true,
351
- expires: new Date(
352
- Date.now() + 1000 * 60 * 60 * 24 * 7
353
- ) // 7 days
257
+ withCompleteWallet(
258
+ withWalletCompleteRedirection(
259
+ withMasterpassRestCallback(
260
+ withBfcacheHeaders(
261
+ async (
262
+ req: PzNextRequest,
263
+ event: NextFetchEvent
264
+ ) => {
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
+ );
279
+
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(
298
+ middlewareResult.headers.get(
299
+ 'x-middleware-rewrite'
300
+ )
301
+ );
302
+ const originalUrl = new URL(req.url);
303
+ customRewriteUrlDiff =
304
+ rewriteUrl.pathname.replace(
305
+ originalUrl.pathname,
306
+ ''
307
+ );
308
+ }
309
+
310
+ let ordersPrefix: string;
311
+
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}${
324
+ prettyUrl ?? pathnameWithoutLocale
325
+ }`.replace(/\/+/g, '/');
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) => [
357
+ seg.name,
358
+ req.middlewareParams.rewrites[
359
+ seg.name
360
+ ] ??
361
+ (seg.resolve
362
+ ? seg.resolve(resolveContext)
363
+ : '')
364
+ ])
365
+ )
366
+ };
367
+
368
+ const pzValue = encodePzValue(
369
+ segmentValues,
370
+ pzConfig
371
+ );
372
+
373
+ url.pathname =
374
+ `/${pzValue}/${fullUrlPath}`.replace(
375
+ /\/+/g,
376
+ '/'
377
+ );
378
+ ordersPrefix = `/${pzValue}/orders`;
379
+ }
380
+
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
+ }
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.
411
+ if (
412
+ middlewareResult.headers.get(
413
+ 'pz-override-response'
414
+ ) !== 'true'
415
+ ) {
416
+ middlewareResult.headers.set(
417
+ 'x-middleware-rewrite',
418
+ url.href
419
+ );
420
+ } else if (
421
+ middlewareResult.headers.get(
422
+ 'x-middleware-rewrite'
423
+ ) &&
424
+ middlewareResult.headers.get(
425
+ 'pz-override-response'
426
+ ) === 'true'
427
+ ) {
428
+ middlewareResult =
429
+ NextResponse.rewrite(url);
430
+ }
431
+ } else {
432
+ // if middleware.ts doesn't have a return value.
433
+ // e.g. NextResponse.next() doesn't exist in middleware.ts
434
+
435
+ middlewareResult =
436
+ NextResponse.rewrite(url);
437
+ }
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
+ ) {
459
+ middlewareResult.cookies.set(
460
+ 'pz-locale',
461
+ locale?.length > 0
462
+ ? locale
463
+ : defaultLocaleValue,
464
+ {
465
+ domain: rootHostname,
466
+ sameSite: 'none',
467
+ secure: true,
468
+ expires: new Date(
469
+ Date.now() + 1000 * 60 * 60 * 24 * 7
470
+ ) // 7 days
471
+ }
472
+ );
473
+ }
474
+
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
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
+ }
500
+
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'
509
+ );
510
+ }
511
+
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
+ ) {
525
+ middlewareResult.cookies.delete(
526
+ 'pz-post-checkout-flow'
527
+ );
528
+ }
529
+ }
530
+
531
+ if (process.env.ACC_APP_VERSION) {
532
+ middlewareResult.headers.set(
533
+ 'acc-app-version',
534
+ process.env.ACC_APP_VERSION
535
+ );
536
+ }
537
+
538
+ // Set CSRF token if not set
539
+ try {
540
+ const url = `${Settings.commerceUrl}${user.csrfToken}`;
541
+
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
+ domain: rootHostname
551
+ }
552
+ );
553
+ }
554
+ } catch (error) {
555
+ logger.error('CSRF Error', {
556
+ error,
557
+ ip
558
+ });
559
+ }
560
+ } catch (error) {
561
+ logger.error('withPzDefault Error', {
562
+ error,
563
+ ip
564
+ });
354
565
  }
355
- );
356
- }
357
- middlewareResult.cookies.set(
358
- 'pz-currency',
359
- currency,
360
- {
361
- sameSite: 'none',
362
- secure: true,
363
- expires: new Date(
364
- Date.now() + 1000 * 60 * 60 * 24 * 7
365
- ) // 7 days
366
- }
367
- );
368
-
369
- if (
370
- req.cookies.get('pz-locale') &&
371
- req.cookies.get('pz-locale').value !== locale
372
- ) {
373
- logger.debug('Locale changed', {
374
- locale,
375
- oldLocale:
376
- req.cookies.get('pz-locale')?.value,
377
- ip
378
- });
379
- }
380
-
381
- middlewareResult.headers.set(
382
- 'pz-url',
383
- req.nextUrl.toString()
384
- );
385
-
386
- if (req.cookies.get('pz-set-currency')) {
387
- middlewareResult.cookies.delete(
388
- 'pz-set-currency'
389
- );
390
- }
391
-
392
- if (process.env.ACC_APP_VERSION) {
393
- middlewareResult.headers.set(
394
- 'acc-app-version',
395
- process.env.ACC_APP_VERSION
396
- );
397
- }
398
-
399
- // Set CSRF token if not set
400
- try {
401
- const url = `${Settings.commerceUrl}${user.csrfToken}`;
402
-
403
- if (!req.cookies.get('csrftoken')) {
404
- const { csrf_token } = await (
405
- await fetch(url)
406
- ).json();
407
- middlewareResult.cookies.set(
408
- 'csrftoken',
409
- csrf_token
410
- );
411
- }
412
- } catch (error) {
413
- logger.error('CSRF Error', {
414
- error,
415
- ip
416
- });
417
- }
418
- } catch (error) {
419
- logger.error('withPzDefault Error', {
420
- error,
421
- ip
422
- });
423
- }
424
-
425
- return middlewareResult;
426
- }
566
+
567
+ return middlewareResult;
568
+ })
569
+ )
570
+ )
571
+ )
427
572
  )
428
573
  )
429
574
  )