@akinon/next 1.13.1 → 1.14.0
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/.editorconfig +7 -0
- package/.eslintrc.js +40 -40
- package/.prettierrc +13 -13
- package/CHANGELOG.md +13 -0
- package/api/auth.ts +231 -231
- package/api/cache.ts +44 -44
- package/api/client.ts +174 -174
- package/api/logout.ts +42 -42
- package/assets/styles/index.scss +28 -28
- package/bin/pz-check-dependencies.js +98 -98
- package/bin/pz-install-plugins.js +33 -33
- package/bin/pz-install-theme.js +58 -58
- package/bin/pz-postbuild.js +1 -1
- package/bin/pz-postdev.js +1 -1
- package/bin/pz-postinstall.js +6 -6
- package/bin/pz-poststart.js +1 -1
- package/bin/pz-prebuild.js +4 -4
- package/bin/pz-predev.js +4 -4
- package/bin/pz-prestart.js +1 -1
- package/bin/run-script.js +44 -44
- package/components/accordion.tsx +52 -0
- package/components/button.tsx +46 -0
- package/components/client-root.tsx +19 -19
- package/components/icon.tsx +18 -18
- package/components/image.tsx +133 -133
- package/components/index.ts +17 -1
- package/components/input.tsx +110 -0
- package/components/lazy-component.tsx +33 -33
- package/components/loader-spinner.tsx +23 -23
- package/components/mobile-app-toggler.tsx +26 -26
- package/components/oauth-login.tsx +24 -24
- package/components/plugin-module.tsx +11 -8
- package/components/price.tsx +55 -0
- package/components/pz-providers.tsx +24 -24
- package/components/pz-root.tsx +21 -21
- package/components/radio.tsx +18 -18
- package/components/react-portal.tsx +45 -45
- package/components/redirect-three-d/content/index.tsx +74 -74
- package/components/redirect-three-d/index.tsx +17 -17
- package/components/selected-payment-option-view.tsx +7 -0
- package/components/trans.tsx +39 -39
- package/data/client/account.ts +208 -208
- package/data/client/address.ts +107 -107
- package/data/client/api.ts +85 -84
- package/data/client/b2b.ts +106 -106
- package/data/client/basket.ts +82 -82
- package/data/client/checkout.ts +516 -479
- package/data/client/misc.ts +101 -101
- package/data/client/product.ts +89 -89
- package/data/client/user.ts +99 -99
- package/data/client/wishlist.ts +118 -88
- package/data/server/category.ts +132 -128
- package/data/server/flatpage.ts +21 -21
- package/data/server/form.ts +22 -22
- package/data/server/index.ts +10 -10
- package/data/server/landingpage.ts +24 -24
- package/data/server/list.ts +67 -62
- package/data/server/menu.ts +35 -35
- package/data/server/product.ts +86 -86
- package/data/server/seo.ts +48 -48
- package/data/server/special-page.ts +47 -47
- package/data/server/widget.ts +27 -27
- package/data/urls.ts +221 -210
- package/hocs/client/index.ts +1 -1
- package/hocs/client/with-segment-defaults.tsx +25 -25
- package/hocs/server/index.ts +1 -1
- package/hocs/server/with-segment-defaults.tsx +85 -85
- package/hooks/index.ts +10 -10
- package/hooks/use-captcha.tsx +76 -76
- package/hooks/use-common-product-attributes.ts +36 -36
- package/hooks/use-debounce.ts +20 -20
- package/hooks/use-localization.ts +78 -78
- package/hooks/use-media-query.ts +36 -36
- package/hooks/use-mobile-iframe-handler.ts +23 -23
- package/hooks/use-on-click-outside.tsx +28 -28
- package/hooks/use-payment-options.ts +2 -1
- package/hooks/use-router.ts +45 -45
- package/hooks/use-translation.ts +14 -14
- package/lib/cache.ts +215 -215
- package/localization/index.ts +5 -5
- package/localization/provider.tsx +58 -58
- package/middlewares/currency.ts +100 -100
- package/middlewares/default.ts +256 -256
- package/middlewares/index.ts +29 -29
- package/middlewares/locale.ts +68 -68
- package/middlewares/oauth-login.ts +79 -79
- package/middlewares/pretty-url.ts +104 -104
- package/middlewares/redirection-payment.ts +160 -160
- package/middlewares/three-d-redirection.ts +159 -159
- package/middlewares/url-redirection.ts +65 -65
- package/package.json +2 -2
- package/plugins.js +2 -1
- package/redux/hooks.ts +7 -7
- package/redux/middlewares/checkout.ts +265 -260
- package/redux/middlewares/index.ts +50 -50
- package/redux/reducers/checkout.ts +184 -171
- package/redux/reducers/config.ts +28 -28
- package/redux/reducers/header.ts +59 -59
- package/redux/reducers/index.ts +14 -14
- package/redux/reducers/root.ts +61 -61
- package/sentry/index.ts +27 -27
- package/tailwind/rtl.js +137 -137
- package/types/commerce/account.ts +64 -64
- package/types/commerce/address.ts +94 -94
- package/types/commerce/b2b.ts +117 -117
- package/types/commerce/basket.ts +43 -43
- package/types/commerce/category.ts +114 -114
- package/types/commerce/checkout.ts +143 -136
- package/types/commerce/flatpage.ts +7 -7
- package/types/commerce/form.ts +66 -66
- package/types/commerce/index.ts +12 -12
- package/types/commerce/landingpage.ts +7 -7
- package/types/commerce/misc.ts +127 -127
- package/types/commerce/order.ts +119 -119
- package/types/commerce/product.ts +109 -109
- package/types/commerce/widget.ts +28 -28
- package/types/gtm.ts +16 -16
- package/types/index.ts +274 -237
- package/types/metadata.ts +7 -7
- package/types/next-auth.d.ts +24 -24
- package/utils/app-fetch.ts +69 -69
- package/utils/deep-merge.js +24 -24
- package/utils/generate-commerce-search-params.ts +22 -22
- package/utils/get-currency.ts +29 -29
- package/utils/image-loader.ts +31 -31
- package/utils/index.ts +150 -150
- package/utils/localization.ts +29 -29
- package/utils/log.ts +138 -138
- package/utils/menu-generator.ts +27 -27
- package/utils/mobile-3d-iframe.ts +77 -77
- package/utils/server-translation.ts +57 -57
- package/utils/server-variables.ts +9 -9
- package/with-pz-config.js +94 -94
package/middlewares/locale.ts
CHANGED
|
@@ -1,68 +1,68 @@
|
|
|
1
|
-
import { NextFetchEvent, NextMiddleware, NextResponse } from 'next/server';
|
|
2
|
-
import settings from 'settings';
|
|
3
|
-
import { PzNextRequest } from '.';
|
|
4
|
-
import { LocaleUrlStrategy } from '../localization';
|
|
5
|
-
import { urlLocaleMatcherRegex } from '../utils';
|
|
6
|
-
import logger from '../utils/log';
|
|
7
|
-
|
|
8
|
-
const getMatchedLocale = (pathname: string) => {
|
|
9
|
-
let matchedLocale = pathname.match(urlLocaleMatcherRegex)?.[0] ?? '';
|
|
10
|
-
matchedLocale = matchedLocale.replace('/', '');
|
|
11
|
-
|
|
12
|
-
if (!matchedLocale.length) {
|
|
13
|
-
if (
|
|
14
|
-
settings.localization.localeUrlStrategy !==
|
|
15
|
-
LocaleUrlStrategy.ShowAllLocales
|
|
16
|
-
) {
|
|
17
|
-
matchedLocale = settings.localization.defaultLocaleValue;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
return matchedLocale;
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
const withLocale =
|
|
25
|
-
(middleware: NextMiddleware) =>
|
|
26
|
-
async (req: PzNextRequest, event: NextFetchEvent) => {
|
|
27
|
-
const ip = req.headers.get('x-forwarded-for') ?? '';
|
|
28
|
-
|
|
29
|
-
try {
|
|
30
|
-
const url = req.nextUrl.clone();
|
|
31
|
-
const matchedLocale = getMatchedLocale(url.pathname);
|
|
32
|
-
let { localeUrlStrategy, defaultLocaleValue, redirectToDefaultLocale } =
|
|
33
|
-
settings.localization;
|
|
34
|
-
|
|
35
|
-
localeUrlStrategy =
|
|
36
|
-
localeUrlStrategy ?? LocaleUrlStrategy.HideDefaultLocale;
|
|
37
|
-
|
|
38
|
-
if (!defaultLocaleValue) {
|
|
39
|
-
logger.error('Default locale value is not defined in settings.', {
|
|
40
|
-
ip
|
|
41
|
-
});
|
|
42
|
-
throw new Error(
|
|
43
|
-
'Default locale value is not defined. Use `defaultLocaleValue` property in `localization` object in `settings.js` file.'
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
if (
|
|
48
|
-
!matchedLocale?.length &&
|
|
49
|
-
localeUrlStrategy === LocaleUrlStrategy.ShowAllLocales &&
|
|
50
|
-
redirectToDefaultLocale &&
|
|
51
|
-
req.method === 'GET'
|
|
52
|
-
) {
|
|
53
|
-
url.pathname = `/${defaultLocaleValue}${url.pathname}`;
|
|
54
|
-
return NextResponse.redirect(url);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
req.middlewareParams.rewrites.locale = matchedLocale;
|
|
58
|
-
} catch (error) {
|
|
59
|
-
logger.error('withLocale error', {
|
|
60
|
-
error,
|
|
61
|
-
ip
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
return middleware(req, event);
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
export default withLocale;
|
|
1
|
+
import { NextFetchEvent, NextMiddleware, NextResponse } from 'next/server';
|
|
2
|
+
import settings from 'settings';
|
|
3
|
+
import { PzNextRequest } from '.';
|
|
4
|
+
import { LocaleUrlStrategy } from '../localization';
|
|
5
|
+
import { urlLocaleMatcherRegex } from '../utils';
|
|
6
|
+
import logger from '../utils/log';
|
|
7
|
+
|
|
8
|
+
const getMatchedLocale = (pathname: string) => {
|
|
9
|
+
let matchedLocale = pathname.match(urlLocaleMatcherRegex)?.[0] ?? '';
|
|
10
|
+
matchedLocale = matchedLocale.replace('/', '');
|
|
11
|
+
|
|
12
|
+
if (!matchedLocale.length) {
|
|
13
|
+
if (
|
|
14
|
+
settings.localization.localeUrlStrategy !==
|
|
15
|
+
LocaleUrlStrategy.ShowAllLocales
|
|
16
|
+
) {
|
|
17
|
+
matchedLocale = settings.localization.defaultLocaleValue;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return matchedLocale;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const withLocale =
|
|
25
|
+
(middleware: NextMiddleware) =>
|
|
26
|
+
async (req: PzNextRequest, event: NextFetchEvent) => {
|
|
27
|
+
const ip = req.headers.get('x-forwarded-for') ?? '';
|
|
28
|
+
|
|
29
|
+
try {
|
|
30
|
+
const url = req.nextUrl.clone();
|
|
31
|
+
const matchedLocale = getMatchedLocale(url.pathname);
|
|
32
|
+
let { localeUrlStrategy, defaultLocaleValue, redirectToDefaultLocale } =
|
|
33
|
+
settings.localization;
|
|
34
|
+
|
|
35
|
+
localeUrlStrategy =
|
|
36
|
+
localeUrlStrategy ?? LocaleUrlStrategy.HideDefaultLocale;
|
|
37
|
+
|
|
38
|
+
if (!defaultLocaleValue) {
|
|
39
|
+
logger.error('Default locale value is not defined in settings.', {
|
|
40
|
+
ip
|
|
41
|
+
});
|
|
42
|
+
throw new Error(
|
|
43
|
+
'Default locale value is not defined. Use `defaultLocaleValue` property in `localization` object in `settings.js` file.'
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (
|
|
48
|
+
!matchedLocale?.length &&
|
|
49
|
+
localeUrlStrategy === LocaleUrlStrategy.ShowAllLocales &&
|
|
50
|
+
redirectToDefaultLocale &&
|
|
51
|
+
req.method === 'GET'
|
|
52
|
+
) {
|
|
53
|
+
url.pathname = `/${defaultLocaleValue}${url.pathname}`;
|
|
54
|
+
return NextResponse.redirect(url);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
req.middlewareParams.rewrites.locale = matchedLocale;
|
|
58
|
+
} catch (error) {
|
|
59
|
+
logger.error('withLocale error', {
|
|
60
|
+
error,
|
|
61
|
+
ip
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return middleware(req, event);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export default withLocale;
|
|
@@ -1,79 +1,79 @@
|
|
|
1
|
-
import {
|
|
2
|
-
NextFetchEvent,
|
|
3
|
-
NextMiddleware,
|
|
4
|
-
NextRequest,
|
|
5
|
-
NextResponse
|
|
6
|
-
} from 'next/server';
|
|
7
|
-
import Settings from 'settings';
|
|
8
|
-
import { getUrlPathWithLocale } from '../utils/localization';
|
|
9
|
-
|
|
10
|
-
const withOauthLogin =
|
|
11
|
-
(middleware: NextMiddleware) =>
|
|
12
|
-
async (req: NextRequest, event: NextFetchEvent) => {
|
|
13
|
-
const url = req.nextUrl.clone();
|
|
14
|
-
const loginUrlMatcherRegex = new RegExp(/^\/(\w+)\/login\/?$/);
|
|
15
|
-
const loginCallbackUrlMatcherRegex = new RegExp(
|
|
16
|
-
/^\/(\w+)\/login\/callback\/?$/
|
|
17
|
-
);
|
|
18
|
-
|
|
19
|
-
const headers = {
|
|
20
|
-
'x-forwarded-host':
|
|
21
|
-
req.headers.get('x-forwarded-host') || req.headers.get('host') || '',
|
|
22
|
-
'x-currency': req.cookies.get('pz-currency')?.value ?? ''
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
if (loginUrlMatcherRegex.test(url.pathname)) {
|
|
26
|
-
const provider = url.pathname.match(loginUrlMatcherRegex)[1];
|
|
27
|
-
const response = NextResponse.rewrite(
|
|
28
|
-
`${Settings.commerceUrl}/${provider}/login/`,
|
|
29
|
-
{
|
|
30
|
-
headers
|
|
31
|
-
}
|
|
32
|
-
);
|
|
33
|
-
|
|
34
|
-
if (req.headers.get('referer')) {
|
|
35
|
-
response.cookies.set(
|
|
36
|
-
'pz-oauth-callback-url',
|
|
37
|
-
req.headers.get('referer')
|
|
38
|
-
);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
return response;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
if (loginCallbackUrlMatcherRegex.test(url.pathname)) {
|
|
45
|
-
const provider = url.pathname.match(loginCallbackUrlMatcherRegex)[1];
|
|
46
|
-
|
|
47
|
-
return NextResponse.rewrite(
|
|
48
|
-
`${Settings.commerceUrl}/${provider}/login/callback/${url.search}`,
|
|
49
|
-
{
|
|
50
|
-
headers
|
|
51
|
-
}
|
|
52
|
-
);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
if (!url.pathname.startsWith('/baskets/basket')) {
|
|
56
|
-
return middleware(req, event);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if (req.cookies.get('messages')?.value.includes('Successfully signed in')) {
|
|
60
|
-
let redirectUrlWithLocale = `${url.origin}${getUrlPathWithLocale(
|
|
61
|
-
'/auth/oauth-login',
|
|
62
|
-
req.cookies.get('pz-locale')?.value
|
|
63
|
-
)}`;
|
|
64
|
-
let callbackUrl = req.cookies.get('pz-oauth-callback-url')?.value ?? '';
|
|
65
|
-
|
|
66
|
-
if (callbackUrl.length) {
|
|
67
|
-
redirectUrlWithLocale += `?next=${encodeURIComponent(callbackUrl)}`;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const response = NextResponse.redirect(redirectUrlWithLocale);
|
|
71
|
-
response.cookies.delete('messages');
|
|
72
|
-
response.cookies.delete('pz-oauth-callback-url');
|
|
73
|
-
return response;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
return middleware(req, event);
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
export default withOauthLogin;
|
|
1
|
+
import {
|
|
2
|
+
NextFetchEvent,
|
|
3
|
+
NextMiddleware,
|
|
4
|
+
NextRequest,
|
|
5
|
+
NextResponse
|
|
6
|
+
} from 'next/server';
|
|
7
|
+
import Settings from 'settings';
|
|
8
|
+
import { getUrlPathWithLocale } from '../utils/localization';
|
|
9
|
+
|
|
10
|
+
const withOauthLogin =
|
|
11
|
+
(middleware: NextMiddleware) =>
|
|
12
|
+
async (req: NextRequest, event: NextFetchEvent) => {
|
|
13
|
+
const url = req.nextUrl.clone();
|
|
14
|
+
const loginUrlMatcherRegex = new RegExp(/^\/(\w+)\/login\/?$/);
|
|
15
|
+
const loginCallbackUrlMatcherRegex = new RegExp(
|
|
16
|
+
/^\/(\w+)\/login\/callback\/?$/
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
const headers = {
|
|
20
|
+
'x-forwarded-host':
|
|
21
|
+
req.headers.get('x-forwarded-host') || req.headers.get('host') || '',
|
|
22
|
+
'x-currency': req.cookies.get('pz-currency')?.value ?? ''
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
if (loginUrlMatcherRegex.test(url.pathname)) {
|
|
26
|
+
const provider = url.pathname.match(loginUrlMatcherRegex)[1];
|
|
27
|
+
const response = NextResponse.rewrite(
|
|
28
|
+
`${Settings.commerceUrl}/${provider}/login/`,
|
|
29
|
+
{
|
|
30
|
+
headers
|
|
31
|
+
}
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
if (req.headers.get('referer')) {
|
|
35
|
+
response.cookies.set(
|
|
36
|
+
'pz-oauth-callback-url',
|
|
37
|
+
req.headers.get('referer')
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return response;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (loginCallbackUrlMatcherRegex.test(url.pathname)) {
|
|
45
|
+
const provider = url.pathname.match(loginCallbackUrlMatcherRegex)[1];
|
|
46
|
+
|
|
47
|
+
return NextResponse.rewrite(
|
|
48
|
+
`${Settings.commerceUrl}/${provider}/login/callback/${url.search}`,
|
|
49
|
+
{
|
|
50
|
+
headers
|
|
51
|
+
}
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (!url.pathname.startsWith('/baskets/basket')) {
|
|
56
|
+
return middleware(req, event);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (req.cookies.get('messages')?.value.includes('Successfully signed in')) {
|
|
60
|
+
let redirectUrlWithLocale = `${url.origin}${getUrlPathWithLocale(
|
|
61
|
+
'/auth/oauth-login',
|
|
62
|
+
req.cookies.get('pz-locale')?.value
|
|
63
|
+
)}`;
|
|
64
|
+
let callbackUrl = req.cookies.get('pz-oauth-callback-url')?.value ?? '';
|
|
65
|
+
|
|
66
|
+
if (callbackUrl.length) {
|
|
67
|
+
redirectUrlWithLocale += `?next=${encodeURIComponent(callbackUrl)}`;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const response = NextResponse.redirect(redirectUrlWithLocale);
|
|
71
|
+
response.cookies.delete('messages');
|
|
72
|
+
response.cookies.delete('pz-oauth-callback-url');
|
|
73
|
+
return response;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return middleware(req, event);
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
export default withOauthLogin;
|
|
@@ -1,104 +1,104 @@
|
|
|
1
|
-
import { Cache, CacheKey } from '../lib/cache';
|
|
2
|
-
import { NextFetchEvent, NextMiddleware, NextRequest } from 'next/server';
|
|
3
|
-
import { ROUTES } from 'routes';
|
|
4
|
-
import { URLS } from '../data/urls';
|
|
5
|
-
import Settings from 'settings';
|
|
6
|
-
import { urlLocaleMatcherRegex } from '../utils';
|
|
7
|
-
import { PzNextRequest } from '.';
|
|
8
|
-
import logger from '../utils/log';
|
|
9
|
-
|
|
10
|
-
type PrettyUrlResult = {
|
|
11
|
-
matched: boolean;
|
|
12
|
-
path?: string;
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
const resolvePrettyUrlHandler =
|
|
16
|
-
(pathname: string, ip: string | null) => async () => {
|
|
17
|
-
let results = <{ old_path: string }[]>[];
|
|
18
|
-
let prettyUrlResult: PrettyUrlResult = {
|
|
19
|
-
matched: false
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
try {
|
|
23
|
-
const requestUrl = URLS.misc.prettyUrls(pathname);
|
|
24
|
-
|
|
25
|
-
logger.debug(`Resolving pretty url`, { pathname, requestUrl, ip });
|
|
26
|
-
|
|
27
|
-
const apiResponse = await fetch(requestUrl);
|
|
28
|
-
const data = await apiResponse.json();
|
|
29
|
-
({ results } = data);
|
|
30
|
-
|
|
31
|
-
const matched = results.length > 0;
|
|
32
|
-
const [{ old_path: path } = { old_path: '' }] = results;
|
|
33
|
-
prettyUrlResult = {
|
|
34
|
-
matched,
|
|
35
|
-
path
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
logger.trace('Pretty url result', { prettyUrlResult, ip });
|
|
39
|
-
} catch (error) {
|
|
40
|
-
logger.error('Error resolving pretty url', { error, pathname, ip });
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return prettyUrlResult;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
const resolvePrettyUrl = async (pathname: string, ip: string | null) => {
|
|
47
|
-
return Cache.wrap(
|
|
48
|
-
CacheKey.PrettyUrl(pathname),
|
|
49
|
-
resolvePrettyUrlHandler(pathname, ip),
|
|
50
|
-
{
|
|
51
|
-
useProxy: true
|
|
52
|
-
}
|
|
53
|
-
);
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
const withPrettyUrl =
|
|
57
|
-
(middleware: NextMiddleware) =>
|
|
58
|
-
async (req: PzNextRequest, event: NextFetchEvent) => {
|
|
59
|
-
const url = req.nextUrl.clone();
|
|
60
|
-
const matchedLanguagePrefix = url.pathname.match(
|
|
61
|
-
urlLocaleMatcherRegex
|
|
62
|
-
)?.[0];
|
|
63
|
-
const prettyUrlPathname = url.pathname.replace(matchedLanguagePrefix, '');
|
|
64
|
-
const isValidPrettyUrlPath = (pathname: string) => {
|
|
65
|
-
return (
|
|
66
|
-
new RegExp(/^\/[a-zA-Z0-9/]+(?:-[a-zA-Z0-9/]+)*$/).test(pathname) &&
|
|
67
|
-
!Object.entries(ROUTES).find(([, value]) =>
|
|
68
|
-
new RegExp(`^${value}$`).test(pathname)
|
|
69
|
-
)
|
|
70
|
-
);
|
|
71
|
-
};
|
|
72
|
-
const ip = req.headers.get('x-forwarded-for') ?? '';
|
|
73
|
-
|
|
74
|
-
if (
|
|
75
|
-
!isValidPrettyUrlPath(prettyUrlPathname) ||
|
|
76
|
-
Settings.commerceUrl === 'default'
|
|
77
|
-
) {
|
|
78
|
-
return middleware(req, event);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
const prettyUrlResult = await resolvePrettyUrl(
|
|
82
|
-
Settings.localization.localeIncludedPrettyUrlPattern?.test(
|
|
83
|
-
prettyUrlPathname
|
|
84
|
-
)
|
|
85
|
-
? url.pathname
|
|
86
|
-
: prettyUrlPathname,
|
|
87
|
-
ip
|
|
88
|
-
);
|
|
89
|
-
|
|
90
|
-
if (prettyUrlResult.matched) {
|
|
91
|
-
req.middlewareParams.rewrites.prettyUrl = prettyUrlResult.path;
|
|
92
|
-
logger.debug('Resolved pretty url', {
|
|
93
|
-
source: prettyUrlPathname,
|
|
94
|
-
dest: prettyUrlResult.path,
|
|
95
|
-
ip
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
return middleware(req, event);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
return middleware(req, event);
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
export default withPrettyUrl;
|
|
1
|
+
import { Cache, CacheKey } from '../lib/cache';
|
|
2
|
+
import { NextFetchEvent, NextMiddleware, NextRequest } from 'next/server';
|
|
3
|
+
import { ROUTES } from 'routes';
|
|
4
|
+
import { URLS } from '../data/urls';
|
|
5
|
+
import Settings from 'settings';
|
|
6
|
+
import { urlLocaleMatcherRegex } from '../utils';
|
|
7
|
+
import { PzNextRequest } from '.';
|
|
8
|
+
import logger from '../utils/log';
|
|
9
|
+
|
|
10
|
+
type PrettyUrlResult = {
|
|
11
|
+
matched: boolean;
|
|
12
|
+
path?: string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const resolvePrettyUrlHandler =
|
|
16
|
+
(pathname: string, ip: string | null) => async () => {
|
|
17
|
+
let results = <{ old_path: string }[]>[];
|
|
18
|
+
let prettyUrlResult: PrettyUrlResult = {
|
|
19
|
+
matched: false
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
const requestUrl = URLS.misc.prettyUrls(pathname);
|
|
24
|
+
|
|
25
|
+
logger.debug(`Resolving pretty url`, { pathname, requestUrl, ip });
|
|
26
|
+
|
|
27
|
+
const apiResponse = await fetch(requestUrl);
|
|
28
|
+
const data = await apiResponse.json();
|
|
29
|
+
({ results } = data);
|
|
30
|
+
|
|
31
|
+
const matched = results.length > 0;
|
|
32
|
+
const [{ old_path: path } = { old_path: '' }] = results;
|
|
33
|
+
prettyUrlResult = {
|
|
34
|
+
matched,
|
|
35
|
+
path
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
logger.trace('Pretty url result', { prettyUrlResult, ip });
|
|
39
|
+
} catch (error) {
|
|
40
|
+
logger.error('Error resolving pretty url', { error, pathname, ip });
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return prettyUrlResult;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const resolvePrettyUrl = async (pathname: string, ip: string | null) => {
|
|
47
|
+
return Cache.wrap(
|
|
48
|
+
CacheKey.PrettyUrl(pathname),
|
|
49
|
+
resolvePrettyUrlHandler(pathname, ip),
|
|
50
|
+
{
|
|
51
|
+
useProxy: true
|
|
52
|
+
}
|
|
53
|
+
);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const withPrettyUrl =
|
|
57
|
+
(middleware: NextMiddleware) =>
|
|
58
|
+
async (req: PzNextRequest, event: NextFetchEvent) => {
|
|
59
|
+
const url = req.nextUrl.clone();
|
|
60
|
+
const matchedLanguagePrefix = url.pathname.match(
|
|
61
|
+
urlLocaleMatcherRegex
|
|
62
|
+
)?.[0];
|
|
63
|
+
const prettyUrlPathname = url.pathname.replace(matchedLanguagePrefix, '');
|
|
64
|
+
const isValidPrettyUrlPath = (pathname: string) => {
|
|
65
|
+
return (
|
|
66
|
+
new RegExp(/^\/[a-zA-Z0-9/]+(?:-[a-zA-Z0-9/]+)*$/).test(pathname) &&
|
|
67
|
+
!Object.entries(ROUTES).find(([, value]) =>
|
|
68
|
+
new RegExp(`^${value}$`).test(pathname)
|
|
69
|
+
)
|
|
70
|
+
);
|
|
71
|
+
};
|
|
72
|
+
const ip = req.headers.get('x-forwarded-for') ?? '';
|
|
73
|
+
|
|
74
|
+
if (
|
|
75
|
+
!isValidPrettyUrlPath(prettyUrlPathname) ||
|
|
76
|
+
Settings.commerceUrl === 'default'
|
|
77
|
+
) {
|
|
78
|
+
return middleware(req, event);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const prettyUrlResult = await resolvePrettyUrl(
|
|
82
|
+
Settings.localization.localeIncludedPrettyUrlPattern?.test(
|
|
83
|
+
prettyUrlPathname
|
|
84
|
+
)
|
|
85
|
+
? url.pathname
|
|
86
|
+
: prettyUrlPathname,
|
|
87
|
+
ip
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
if (prettyUrlResult.matched) {
|
|
91
|
+
req.middlewareParams.rewrites.prettyUrl = prettyUrlResult.path;
|
|
92
|
+
logger.debug('Resolved pretty url', {
|
|
93
|
+
source: prettyUrlPathname,
|
|
94
|
+
dest: prettyUrlResult.path,
|
|
95
|
+
ip
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
return middleware(req, event);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return middleware(req, event);
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
export default withPrettyUrl;
|