@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.
- package/.eslintrc.js +12 -0
- package/CHANGELOG.md +377 -7
- package/__tests__/next-config.test.ts +83 -0
- package/__tests__/tsconfig.json +23 -0
- package/api/auth.ts +133 -44
- package/api/barcode-search.ts +59 -0
- package/api/cache.ts +41 -5
- package/api/client.ts +21 -4
- package/api/form.ts +85 -0
- package/api/image-proxy.ts +75 -0
- package/api/product-categories.ts +53 -0
- package/api/similar-product-list.ts +63 -0
- package/api/similar-products.ts +111 -0
- package/api/virtual-try-on.ts +382 -0
- package/assets/styles/index.scss +84 -0
- package/babel.config.js +6 -0
- package/bin/pz-generate-routes.js +115 -0
- package/bin/pz-prebuild.js +1 -0
- package/bin/pz-predev.js +1 -0
- package/bin/pz-run-tests.js +99 -0
- package/bin/run-prebuild-tests.js +46 -0
- package/components/accordion.tsx +20 -5
- package/components/button.tsx +51 -36
- package/components/client-root.tsx +138 -2
- package/components/file-input.tsx +65 -3
- package/components/index.ts +1 -0
- package/components/input.tsx +1 -1
- package/components/link.tsx +46 -16
- package/components/logger-popup.tsx +213 -0
- package/components/modal.tsx +32 -16
- package/components/plugin-module.tsx +62 -3
- package/components/price.tsx +2 -2
- package/components/select.tsx +1 -1
- package/components/selected-payment-option-view.tsx +21 -0
- package/components/theme-editor/blocks/accordion-block.tsx +136 -0
- package/components/theme-editor/blocks/block-renderer-registry.tsx +77 -0
- package/components/theme-editor/blocks/button-block.tsx +593 -0
- package/components/theme-editor/blocks/counter-block.tsx +348 -0
- package/components/theme-editor/blocks/divider-block.tsx +20 -0
- package/components/theme-editor/blocks/embed-block.tsx +208 -0
- package/components/theme-editor/blocks/group-block.tsx +116 -0
- package/components/theme-editor/blocks/hotspot-block.tsx +147 -0
- package/components/theme-editor/blocks/icon-block.tsx +230 -0
- package/components/theme-editor/blocks/image-block.tsx +137 -0
- package/components/theme-editor/blocks/image-gallery-block.tsx +269 -0
- package/components/theme-editor/blocks/input-block.tsx +123 -0
- package/components/theme-editor/blocks/link-block.tsx +216 -0
- package/components/theme-editor/blocks/lottie-block.tsx +325 -0
- package/components/theme-editor/blocks/map-block.tsx +89 -0
- package/components/theme-editor/blocks/slider-block.tsx +595 -0
- package/components/theme-editor/blocks/tab-block.tsx +10 -0
- package/components/theme-editor/blocks/text-block.tsx +52 -0
- package/components/theme-editor/blocks/video-block.tsx +122 -0
- package/components/theme-editor/components/action-toolbar.tsx +305 -0
- package/components/theme-editor/components/designer-overlay.tsx +74 -0
- package/components/theme-editor/components/with-designer-features.tsx +142 -0
- package/components/theme-editor/dynamic-font-loader.tsx +79 -0
- package/components/theme-editor/hooks/use-designer-features.tsx +100 -0
- package/components/theme-editor/hooks/use-external-designer.tsx +95 -0
- package/components/theme-editor/hooks/use-native-widget-data.ts +188 -0
- package/components/theme-editor/hooks/use-visibility-context.ts +27 -0
- package/components/theme-editor/placeholder-registry.ts +31 -0
- package/components/theme-editor/sections/before-after-section.tsx +245 -0
- package/components/theme-editor/sections/contact-form-section.tsx +563 -0
- package/components/theme-editor/sections/countdown-campaign-banner-section.tsx +433 -0
- package/components/theme-editor/sections/coupon-banner-section.tsx +710 -0
- package/components/theme-editor/sections/divider-section.tsx +62 -0
- package/components/theme-editor/sections/featured-product-spotlight-section.tsx +507 -0
- package/components/theme-editor/sections/find-in-store-section.tsx +1995 -0
- package/components/theme-editor/sections/hover-showcase-section.tsx +326 -0
- package/components/theme-editor/sections/image-hotspot-section.tsx +142 -0
- package/components/theme-editor/sections/installment-options-section.tsx +1065 -0
- package/components/theme-editor/sections/notification-banner-section.tsx +173 -0
- package/components/theme-editor/sections/order-tracking-lookup-section.tsx +1379 -0
- package/components/theme-editor/sections/posts-slider-section.tsx +472 -0
- package/components/theme-editor/sections/pre-order-launch-banner-section.tsx +663 -0
- package/components/theme-editor/sections/section-renderer-registry.tsx +89 -0
- package/components/theme-editor/sections/section-wrapper.tsx +135 -0
- package/components/theme-editor/sections/shipping-threshold-progress-section.tsx +586 -0
- package/components/theme-editor/sections/stats-counter-section.tsx +486 -0
- package/components/theme-editor/sections/tabs-section.tsx +578 -0
- package/components/theme-editor/theme-block.tsx +102 -0
- package/components/theme-editor/theme-placeholder-client.tsx +218 -0
- package/components/theme-editor/theme-placeholder-wrapper.tsx +732 -0
- package/components/theme-editor/theme-placeholder.tsx +288 -0
- package/components/theme-editor/theme-section.tsx +1224 -0
- package/components/theme-editor/theme-settings-context.tsx +13 -0
- package/components/theme-editor/utils/index.ts +792 -0
- package/components/theme-editor/utils/iterator-utils.ts +234 -0
- package/components/theme-editor/utils/publish-window.ts +86 -0
- package/components/theme-editor/utils/visibility-rules.ts +188 -0
- package/data/client/account.ts +17 -2
- package/data/client/api.ts +2 -0
- package/data/client/basket.ts +66 -5
- package/data/client/checkout.ts +391 -99
- package/data/client/misc.ts +38 -2
- package/data/client/product.ts +19 -2
- package/data/client/user.ts +16 -8
- package/data/server/category.ts +11 -9
- package/data/server/flatpage.ts +11 -4
- package/data/server/form.ts +15 -4
- package/data/server/landingpage.ts +11 -4
- package/data/server/list.ts +5 -4
- package/data/server/menu.ts +11 -3
- package/data/server/product.ts +111 -55
- package/data/server/seo.ts +14 -4
- package/data/server/special-page.ts +5 -4
- package/data/server/widget.ts +90 -5
- package/data/urls.ts +16 -5
- package/hocs/client/with-segment-defaults.tsx +2 -2
- package/hocs/server/with-segment-defaults.tsx +65 -20
- package/hooks/index.ts +4 -0
- package/hooks/use-localization.ts +24 -10
- package/hooks/use-logger-context.tsx +114 -0
- package/hooks/use-logger.ts +92 -0
- package/hooks/use-loyalty-availability.ts +21 -0
- package/hooks/use-payment-options.ts +2 -1
- package/hooks/use-pz-params.ts +37 -0
- package/hooks/use-router.ts +51 -14
- package/hooks/use-sentry-uncaught-errors.ts +24 -0
- package/instrumentation/index.ts +10 -1
- package/instrumentation/node.ts +2 -20
- package/jest.config.js +25 -0
- package/lib/cache-handler.mjs +534 -16
- package/lib/cache.ts +272 -37
- package/localization/index.ts +2 -1
- package/localization/provider.tsx +2 -5
- package/middlewares/bfcache-headers.ts +18 -0
- package/middlewares/checkout-provider.ts +1 -1
- package/middlewares/complete-gpay.ts +32 -26
- package/middlewares/complete-masterpass.ts +33 -26
- package/middlewares/complete-wallet.ts +182 -0
- package/middlewares/default.ts +360 -215
- package/middlewares/index.ts +10 -2
- package/middlewares/locale.ts +34 -11
- package/middlewares/masterpass-rest-callback.ts +230 -0
- package/middlewares/oauth-login.ts +200 -57
- package/middlewares/pretty-url.ts +21 -8
- package/middlewares/redirection-payment.ts +32 -26
- package/middlewares/saved-card-redirection.ts +33 -26
- package/middlewares/three-d-redirection.ts +32 -26
- package/middlewares/url-redirection.ts +11 -1
- package/middlewares/wallet-complete-redirection.ts +206 -0
- package/package.json +25 -10
- package/plugins.d.ts +19 -4
- package/plugins.js +10 -1
- package/redux/actions.ts +47 -0
- package/redux/middlewares/checkout.ts +63 -138
- package/redux/middlewares/index.ts +14 -10
- package/redux/middlewares/pre-order/address.ts +7 -2
- package/redux/middlewares/pre-order/attribute-based-shipping-option.ts +7 -1
- package/redux/middlewares/pre-order/data-source-shipping-option.ts +7 -1
- package/redux/middlewares/pre-order/delivery-option.ts +7 -1
- package/redux/middlewares/pre-order/index.ts +16 -10
- package/redux/middlewares/pre-order/installment-option.ts +8 -1
- package/redux/middlewares/pre-order/payment-option-reset.ts +37 -0
- package/redux/middlewares/pre-order/payment-option.ts +7 -1
- package/redux/middlewares/pre-order/pre-order-validation.ts +8 -3
- package/redux/middlewares/pre-order/redirection.ts +8 -2
- package/redux/middlewares/pre-order/set-pre-order.ts +6 -2
- package/redux/middlewares/pre-order/shipping-option.ts +7 -1
- package/redux/middlewares/pre-order/shipping-step.ts +5 -1
- package/redux/reducers/checkout.ts +23 -3
- package/redux/reducers/index.ts +11 -3
- package/redux/reducers/root.ts +7 -2
- package/redux/reducers/widget.ts +80 -0
- package/sentry/index.ts +69 -13
- package/tailwind/content.js +16 -0
- package/types/commerce/account.ts +5 -1
- package/types/commerce/checkout.ts +35 -1
- package/types/commerce/widget.ts +33 -0
- package/types/index.ts +101 -6
- package/types/next-auth.d.ts +2 -2
- package/types/widget.ts +80 -0
- package/utils/app-fetch.ts +7 -2
- package/utils/generate-commerce-search-params.ts +3 -2
- package/utils/get-checkout-path.ts +3 -0
- package/utils/get-root-hostname.ts +28 -0
- package/utils/index.ts +64 -10
- package/utils/localization.ts +4 -0
- package/utils/mobile-3d-iframe.ts +8 -2
- package/utils/override-middleware.ts +7 -12
- package/utils/pz-segments.ts +92 -0
- package/utils/redirect-ignore.ts +35 -0
- package/utils/redirect.ts +9 -3
- package/utils/redirection-iframe.ts +8 -2
- package/utils/widget-styles.ts +107 -0
- package/views/error-page.tsx +93 -0
- package/with-pz-config.js +13 -6
package/middlewares/default.ts
CHANGED
|
@@ -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(
|
|
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.
|
|
146
|
-
req.nextUrl.
|
|
147
|
-
|
|
148
|
-
|
|
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
|
|
153
|
-
|
|
154
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
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
|
-
|
|
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
|
-
|
|
358
|
-
|
|
359
|
-
|
|
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
|
)
|