@akinon/next 1.48.0 → 1.50.0-rc.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.
Files changed (66) hide show
  1. package/CHANGELOG.md +382 -0
  2. package/api/client.ts +50 -17
  3. package/assets/styles/index.css +49 -0
  4. package/assets/styles/index.css.map +1 -0
  5. package/assets/styles/index.scss +50 -26
  6. package/bin/pz-generate-translations.js +41 -0
  7. package/bin/pz-prebuild.js +1 -0
  8. package/bin/pz-predev.js +1 -0
  9. package/components/file-input.tsx +8 -0
  10. package/components/index.ts +1 -0
  11. package/components/input.tsx +21 -7
  12. package/components/link.tsx +17 -13
  13. package/components/pagination.tsx +1 -2
  14. package/components/price.tsx +11 -4
  15. package/components/pz-root.tsx +15 -3
  16. package/components/selected-payment-option-view.tsx +26 -38
  17. package/data/client/account.ts +10 -9
  18. package/data/client/address.ts +32 -8
  19. package/data/client/api.ts +2 -2
  20. package/data/client/b2b.ts +35 -2
  21. package/data/client/basket.ts +6 -5
  22. package/data/client/checkout.ts +67 -4
  23. package/data/client/user.ts +3 -2
  24. package/data/client/wishlist.ts +8 -1
  25. package/data/server/category.ts +43 -19
  26. package/data/server/flatpage.ts +29 -7
  27. package/data/server/form.ts +29 -11
  28. package/data/server/landingpage.ts +26 -7
  29. package/data/server/list.ts +16 -6
  30. package/data/server/menu.ts +15 -2
  31. package/data/server/product.ts +46 -21
  32. package/data/server/seo.ts +17 -24
  33. package/data/server/special-page.ts +15 -5
  34. package/data/server/widget.ts +14 -7
  35. package/data/urls.ts +12 -2
  36. package/hocs/server/with-segment-defaults.tsx +4 -1
  37. package/hooks/index.ts +2 -1
  38. package/hooks/use-message-listener.ts +24 -0
  39. package/hooks/use-pagination.ts +2 -2
  40. package/hooks/use-payment-options.ts +2 -1
  41. package/lib/cache-handler.mjs +33 -0
  42. package/lib/cache.ts +8 -6
  43. package/middlewares/default.ts +91 -8
  44. package/middlewares/pretty-url.ts +11 -1
  45. package/middlewares/url-redirection.ts +4 -0
  46. package/package.json +5 -4
  47. package/plugins.d.ts +1 -0
  48. package/redux/middlewares/checkout.ts +70 -11
  49. package/redux/reducers/checkout.ts +24 -5
  50. package/redux/reducers/config.ts +2 -0
  51. package/routes/pretty-url.tsx +192 -0
  52. package/types/commerce/account.ts +1 -0
  53. package/types/commerce/address.ts +1 -1
  54. package/types/commerce/b2b.ts +12 -2
  55. package/types/commerce/checkout.ts +33 -0
  56. package/types/commerce/misc.ts +2 -0
  57. package/types/commerce/order.ts +12 -0
  58. package/types/index.ts +37 -3
  59. package/utils/app-fetch.ts +21 -10
  60. package/utils/generate-commerce-search-params.ts +3 -1
  61. package/utils/index.ts +27 -6
  62. package/utils/menu-generator.ts +2 -2
  63. package/utils/redirection-iframe.ts +85 -0
  64. package/utils/server-translation.ts +11 -1
  65. package/utils/server-variables.ts +2 -1
  66. package/with-pz-config.js +13 -2
@@ -1,29 +1,38 @@
1
1
  import Settings from 'settings';
2
- import { ServerVariables } from './server-variables';
3
2
  import logger from '../utils/log';
4
- import { headers } from 'next/headers';
3
+ import { headers, cookies } from 'next/headers';
4
+ import { ServerVariables } from './server-variables';
5
5
 
6
6
  export enum FetchResponseType {
7
7
  JSON = 'json',
8
8
  TEXT = 'text'
9
9
  }
10
10
 
11
- const appFetch = async <T>(
12
- url: RequestInfo,
13
- init: RequestInit = {},
11
+ const appFetch = async <T>({
12
+ url,
13
+ locale,
14
+ currency,
15
+ init = {},
14
16
  responseType = FetchResponseType.JSON
15
- ): Promise<T> => {
17
+ }: {
18
+ url: RequestInfo;
19
+ locale: string;
20
+ currency: string;
21
+ init?: RequestInit;
22
+ responseType?: FetchResponseType;
23
+ }): Promise<T> => {
16
24
  let response: T;
17
25
  let status: number;
18
26
  let ip = '';
19
27
 
20
28
  try {
21
29
  const nextHeaders = headers();
30
+ const nextCookies = cookies();
22
31
  ip = nextHeaders.get('x-forwarded-for') ?? '';
23
32
 
24
33
  const commerceUrl = Settings.commerceUrl;
25
34
  const currentLocale = Settings.localization.locales.find(
26
- (locale) => locale.value === ServerVariables.locale
35
+ (l) => l.value === locale
27
36
  );
28
37
 
29
38
  if (commerceUrl === 'default') {
@@ -35,13 +44,15 @@ const appFetch = async <T>(
35
44
 
36
45
  init.headers = {
37
46
  ...(init.headers ?? {}),
47
+ ...(ServerVariables.globalHeaders ?? {}),
38
48
  'Accept-Language': currentLocale.apiValue,
39
- 'x-currency': ServerVariables.currency,
40
- 'x-forwarded-for': ip
49
+ 'x-currency': currency,
50
+ 'x-forwarded-for': ip,
51
+ cookie: nextCookies.toString()
41
52
  };
42
53
 
43
54
  init.next = {
44
- revalidate: 60
55
+ revalidate: Settings.usePrettyUrlRoute ? 0 : 60
45
56
  };
46
57
 
47
58
  logger.debug(`FETCH START ${url}`, { requestURL, init, ip });
@@ -1,6 +1,8 @@
1
1
  import logger from './log';
2
2
 
3
- export const generateCommerceSearchParams = (searchParams?: URLSearchParams) => {
3
+ export const generateCommerceSearchParams = (
4
+ searchParams?: URLSearchParams
5
+ ) => {
4
6
  if (!searchParams) {
5
7
  return null;
6
8
  }
package/utils/index.ts CHANGED
@@ -63,18 +63,33 @@ export function getTranslateFn(path: string, translations: any) {
63
63
 
64
64
  export function buildClientRequestUrl(
65
65
  path: string,
66
- options?: ClientRequestOptions
66
+ options?: ClientRequestOptions & { cache?: boolean }
67
67
  ) {
68
68
  let url = `/api/client${path}`;
69
69
 
70
+ let hasQuery = url.includes('?');
71
+
70
72
  if (options) {
71
- if (url.includes('?')) {
72
- url += '&';
73
- } else {
74
- url += '?';
73
+ const { cache, ...otherOptions } = options;
74
+
75
+ if (Object.keys(otherOptions).length > 0) {
76
+ if (hasQuery) {
77
+ url += '&';
78
+ } else {
79
+ url += '?';
80
+ hasQuery = true;
81
+ }
82
+ url += `options=${encodeURIComponent(JSON.stringify(otherOptions))}`;
75
83
  }
76
84
 
77
- url += `options=${encodeURIComponent(JSON.stringify(options))}`;
85
+ if (cache === false) {
86
+ if (hasQuery) {
87
+ url += '&';
88
+ } else {
89
+ url += '?';
90
+ }
91
+ url += `t=${Date.now()}`;
92
+ }
78
93
  }
79
94
 
80
95
  return url;
@@ -102,6 +117,12 @@ export function buildCDNUrl(url: string, config?: CDNOptions) {
102
117
  ''
103
118
  );
104
119
 
120
+ const noOptionFileExtension = url?.split('.').pop()?.toLowerCase() ?? '';
121
+
122
+ if (noOptionFileExtension === 'svg' || noOptionFileExtension === 'gif') {
123
+ return url;
124
+ }
125
+
105
126
  let options = '';
106
127
 
107
128
  if (config) {
@@ -13,8 +13,8 @@ export const menuGenerator = (arr: MenuItemType[]) => {
13
13
  });
14
14
 
15
15
  Object.values(data).forEach((item) => {
16
- if (item.parent) {
17
- data[item.parent.pk].children.push(item);
16
+ if (item.parent_pk) {
17
+ data[item.parent_pk].children.push(item);
18
18
  } else {
19
19
  tree.push(item);
20
20
  }
@@ -0,0 +1,85 @@
1
+ const iframeURLChange = (iframe, callback) => {
2
+ iframe.addEventListener('load', () => {
3
+ setTimeout(() => {
4
+ if (iframe?.contentWindow?.location) {
5
+ callback(iframe.contentWindow.location);
6
+ }
7
+ }, 0);
8
+ });
9
+ };
10
+
11
+ const removeIframe = async () => {
12
+ const iframeSelector = document.querySelector(
13
+ '.checkout-payment-redirection-iframe-wrapper'
14
+ );
15
+
16
+ const redirectionPaymentWrapper = document.querySelector(
17
+ '.checkout-redirection-payment-wrapper'
18
+ );
19
+
20
+ const form = redirectionPaymentWrapper.querySelector('form') as HTMLElement;
21
+
22
+ if (!iframeSelector) {
23
+ return;
24
+ }
25
+
26
+ iframeSelector.remove();
27
+
28
+ if (form) {
29
+ form.style.display = 'block';
30
+ }
31
+
32
+ location.reload();
33
+ };
34
+
35
+ export const showRedirectionIframe = (redirectUrl: string) => {
36
+ const iframeWrapper = document.createElement('div');
37
+ const iframe = document.createElement('iframe');
38
+ const closeButton = document.createElement('div');
39
+ const redirectionPaymentWrapper = document.querySelector(
40
+ '.checkout-redirection-payment-wrapper'
41
+ );
42
+ const form = redirectionPaymentWrapper.querySelector('form') as HTMLElement;
43
+
44
+ iframeWrapper.className = 'checkout-payment-redirection-iframe-wrapper';
45
+ closeButton.className = 'close-button';
46
+
47
+ iframe.setAttribute('src', redirectUrl);
48
+ closeButton.innerHTML = '&#x2715';
49
+ closeButton.addEventListener('click', removeIframe);
50
+
51
+ iframeWrapper.append(iframe, closeButton);
52
+
53
+ if (form) {
54
+ form.style.display = 'none';
55
+ }
56
+
57
+ redirectionPaymentWrapper.appendChild(iframeWrapper);
58
+
59
+ iframeURLChange(iframe, (location) => {
60
+ if (location.origin !== window.location.origin) {
61
+ return false;
62
+ }
63
+
64
+ const searchParams = new URLSearchParams(location.search);
65
+ const isOrderCompleted = location.href.includes('/orders/completed');
66
+
67
+ if (isOrderCompleted) {
68
+ (window.parent as any)?.postMessage?.(
69
+ JSON.stringify({
70
+ url: location.pathname
71
+ })
72
+ );
73
+ }
74
+
75
+ if (
76
+ searchParams.has('success') ||
77
+ isOrderCompleted ||
78
+ location.href.includes('/orders/checkout')
79
+ ) {
80
+ setTimeout(() => {
81
+ removeIframe();
82
+ }, 0);
83
+ }
84
+ });
85
+ };
@@ -8,7 +8,6 @@ import logger from './log';
8
8
 
9
9
  export const translations: Translations = {};
10
10
 
11
- // TODO: Read translations from cache
12
11
  export async function getTranslations(locale_: string) {
13
12
  try {
14
13
  const locale = settings.localization.locales.find(
@@ -55,3 +54,14 @@ export async function getTranslations(locale_: string) {
55
54
  export function t(path: string) {
56
55
  return getTranslateFn(path, translations);
57
56
  }
57
+
58
+ export async function t_(path: string, locale?: string) {
59
+ let translations_ = translations;
60
+
61
+ if (settings.useOptimizedTranslations && locale) {
62
+ const { getTranslations } = require('translations');
63
+ translations_ = await getTranslations(locale);
64
+ }
65
+
66
+ return getTranslateFn(path, translations_);
67
+ }
@@ -5,5 +5,6 @@ const { locales, defaultLocaleValue, defaultCurrencyCode } =
5
5
 
6
6
  export const ServerVariables = {
7
7
  locale: locales.find((l) => l.value === defaultLocaleValue)?.value ?? '',
8
- currency: defaultCurrencyCode
8
+ currency: defaultCurrencyCode,
9
+ globalHeaders: {}
9
10
  };
package/with-pz-config.js CHANGED
@@ -8,6 +8,7 @@ const defaultConfig = {
8
8
  transpilePackages: ['@akinon/next', ...pzPlugins.map((p) => `@akinon/${p}`)],
9
9
  skipTrailingSlashRedirect: true,
10
10
  poweredByHeader: false,
11
+ cacheMaxMemorySize: 0,
11
12
  env: {
12
13
  NEXT_PUBLIC_SENTRY_DSN: process.env.SENTRY_DSN
13
14
  },
@@ -56,7 +57,8 @@ const defaultConfig = {
56
57
  ...pzPlugins.reduce((acc, plugin) => {
57
58
  acc[`@akinon/${plugin}`] = false;
58
59
  return acc;
59
- }, {})
60
+ }, {}),
61
+ translations: false
60
62
  };
61
63
  return config;
62
64
  },
@@ -65,7 +67,12 @@ const defaultConfig = {
65
67
  }
66
68
  };
67
69
 
68
- const withPzConfig = (myNextConfig = {}) => {
70
+ const withPzConfig = (
71
+ myNextConfig = {},
72
+ options = {
73
+ useCacheHandler: false
74
+ }
75
+ ) => {
69
76
  let extendedConfig = deepMerge({}, defaultConfig, myNextConfig);
70
77
 
71
78
  const originalPzHeadersFunction = defaultConfig.headers;
@@ -91,6 +98,10 @@ const withPzConfig = (myNextConfig = {}) => {
91
98
  return [...pzRewrites, ...myRewrites];
92
99
  };
93
100
 
101
+ if (options.useCacheHandler && process.env.CACHE_HOST) {
102
+ extendedConfig.cacheHandler = require.resolve('./lib/cache-handler.mjs');
103
+ }
104
+
94
105
  return extendedConfig;
95
106
  };
96
107