@akinon/next 1.82.0-rc.10 → 1.82.0-rc.12

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 CHANGED
@@ -1,5 +1,13 @@
1
1
  # @akinon/next
2
2
 
3
+ ## 1.82.0-rc.12
4
+
5
+ ### Minor Changes
6
+
7
+ - ac65ca9: ZERO-3269: Enhance locale handling by adding Subdomain strategy and updating related functions
8
+
9
+ ## 1.82.0-rc.11
10
+
3
11
  ## 1.82.0-rc.10
4
12
 
5
13
  ## 1.82.0-rc.9
@@ -27,8 +27,11 @@ export const useRouter = () => {
27
27
  );
28
28
 
29
29
  url.pathname = `${
30
- locale === defaultLocale?.value &&
31
- localeUrlStrategy === LocaleUrlStrategy.HideDefaultLocale
30
+ localeUrlStrategy === LocaleUrlStrategy.Subdomain ||
31
+ (locale === defaultLocale?.value &&
32
+ [LocaleUrlStrategy.HideDefaultLocale].includes(
33
+ localeUrlStrategy as LocaleUrlStrategy
34
+ ))
32
35
  ? ''
33
36
  : `/${locale}`
34
37
  }${pathnameWithoutLocale}`;
@@ -1,5 +1,6 @@
1
1
  export enum LocaleUrlStrategy {
2
2
  HideAllLocales = 'hide-all-locales',
3
3
  HideDefaultLocale = 'hide-default-locale',
4
- ShowAllLocales = 'show-all-locales'
4
+ ShowAllLocales = 'show-all-locales',
5
+ Subdomain = 'subdomain'
5
6
  }
@@ -4,17 +4,32 @@ import { PzNextRequest } from '.';
4
4
  import { LocaleUrlStrategy } from '../localization';
5
5
  import { urlLocaleMatcherRegex } from '../utils';
6
6
  import logger from '../utils/log';
7
+ import { getUrlPathWithLocale } from '../utils/localization';
7
8
 
8
- const getMatchedLocale = (pathname: string) => {
9
+ const getMatchedLocale = (pathname: string, req: PzNextRequest) => {
9
10
  let matchedLocale = pathname.match(urlLocaleMatcherRegex)?.[0] ?? '';
10
11
  matchedLocale = matchedLocale.replace('/', '');
11
12
 
13
+ const { localeUrlStrategy, defaultLocaleValue } = settings.localization;
14
+
15
+ if (localeUrlStrategy === LocaleUrlStrategy.Subdomain) {
16
+ const host = req.headers.get('x-forwarded-host');
17
+
18
+ if (host) {
19
+ const subDomain = host.split('.')[0] || '';
20
+ const subDomainLocaleMatched = `/${subDomain}`.match(
21
+ urlLocaleMatcherRegex
22
+ );
23
+
24
+ if (subDomainLocaleMatched && subDomainLocaleMatched[0]) {
25
+ matchedLocale = subDomainLocaleMatched[0].slice(1);
26
+ }
27
+ }
28
+ }
29
+
12
30
  if (!matchedLocale.length) {
13
- if (
14
- settings.localization.localeUrlStrategy !==
15
- LocaleUrlStrategy.ShowAllLocales
16
- ) {
17
- matchedLocale = settings.localization.defaultLocaleValue;
31
+ if (localeUrlStrategy !== LocaleUrlStrategy.ShowAllLocales) {
32
+ matchedLocale = defaultLocaleValue;
18
33
  }
19
34
  }
20
35
 
@@ -28,7 +43,7 @@ const withLocale =
28
43
 
29
44
  try {
30
45
  const url = req.nextUrl.clone();
31
- const matchedLocale = getMatchedLocale(url.pathname);
46
+ const matchedLocale = getMatchedLocale(url.pathname, req);
32
47
  let { localeUrlStrategy, defaultLocaleValue, redirectToDefaultLocale } =
33
48
  settings.localization;
34
49
 
@@ -50,8 +65,15 @@ const withLocale =
50
65
  redirectToDefaultLocale &&
51
66
  req.method === 'GET'
52
67
  ) {
53
- url.pathname = `/${defaultLocaleValue}${url.pathname}`;
54
- return NextResponse.redirect(url);
68
+ // Redirect to existing or default locale
69
+
70
+ url.pathname = getUrlPathWithLocale(
71
+ url.pathname,
72
+ req.cookies.get('pz-locale')?.value
73
+ );
74
+
75
+ // Use 303 for POST requests
76
+ return NextResponse.redirect(url, 303);
55
77
  }
56
78
 
57
79
  req.middlewareParams.rewrites.locale = matchedLocale;
@@ -61,7 +83,6 @@ const withLocale =
61
83
  ip
62
84
  });
63
85
  }
64
-
65
86
  return middleware(req, event);
66
87
  };
67
88
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@akinon/next",
3
3
  "description": "Core package for Project Zero Next",
4
- "version": "1.82.0-rc.10",
4
+ "version": "1.82.0-rc.12",
5
5
  "private": false,
6
6
  "license": "MIT",
7
7
  "bin": {
@@ -30,7 +30,7 @@
30
30
  "set-cookie-parser": "2.6.0"
31
31
  },
32
32
  "devDependencies": {
33
- "@akinon/eslint-plugin-projectzero": "1.82.0-rc.10",
33
+ "@akinon/eslint-plugin-projectzero": "1.82.0-rc.12",
34
34
  "@types/react-redux": "7.1.30",
35
35
  "@types/set-cookie-parser": "2.4.7",
36
36
  "@typescript-eslint/eslint-plugin": "6.7.4",
package/utils/index.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import settings from 'settings';
2
2
  import { LocaleUrlStrategy } from '../localization';
3
- import { CDNOptions, ClientRequestOptions } from '../types';
3
+ import { CDNOptions, ClientRequestOptions, Locale } from '../types';
4
4
 
5
5
  export * from './get-currency';
6
6
  export * from './menu-generator';
@@ -152,14 +152,17 @@ export function buildCDNUrl(url: string, config?: CDNOptions) {
152
152
  return `${rootWithoutOptions}${options}${fileExtension}`;
153
153
  }
154
154
 
155
+ const { locales, localeUrlStrategy, defaultLocaleValue } =
156
+ settings.localization;
157
+
158
+ const isLocaleExcluded = (locale: Locale) =>
159
+ ![LocaleUrlStrategy.ShowAllLocales, LocaleUrlStrategy.Subdomain].includes(
160
+ localeUrlStrategy
161
+ ) && locale.value !== defaultLocaleValue;
162
+
155
163
  export const urlLocaleMatcherRegex = new RegExp(
156
- `^/(${settings.localization.locales
157
- .filter((l) =>
158
- settings.localization.localeUrlStrategy !==
159
- LocaleUrlStrategy.ShowAllLocales
160
- ? l.value !== settings.localization.defaultLocaleValue
161
- : l
162
- )
164
+ `^/(${locales
165
+ .filter((l) => !isLocaleExcluded(l))
163
166
  .map((l) => l.value)
164
167
  .join('|')})(?=/|$)`
165
168
  );
@@ -11,6 +11,10 @@ export const getUrlPathWithLocale = (
11
11
  currentLocale = defaultLocaleValue;
12
12
  }
13
13
 
14
+ if (localeUrlStrategy === LocaleUrlStrategy.Subdomain) {
15
+ return pathname;
16
+ }
17
+
14
18
  if (localeUrlStrategy === LocaleUrlStrategy.HideAllLocales) {
15
19
  return pathname;
16
20
  }