@akinon/next 1.48.0-rc.3 → 1.48.0-rc.4
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 +6 -0
- package/middlewares/locale.ts +30 -32
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
package/middlewares/locale.ts
CHANGED
|
@@ -5,6 +5,22 @@ import { LocaleUrlStrategy } from '../localization';
|
|
|
5
5
|
import { urlLocaleMatcherRegex } from '../utils';
|
|
6
6
|
import logger from '../utils/log';
|
|
7
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
|
+
|
|
8
24
|
const withLocale =
|
|
9
25
|
(middleware: NextMiddleware) =>
|
|
10
26
|
async (req: PzNextRequest, event: NextFetchEvent) => {
|
|
@@ -12,12 +28,12 @@ const withLocale =
|
|
|
12
28
|
|
|
13
29
|
try {
|
|
14
30
|
const url = req.nextUrl.clone();
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
31
|
+
const matchedLocale = getMatchedLocale(url.pathname);
|
|
32
|
+
let { localeUrlStrategy, defaultLocaleValue, redirectToDefaultLocale } =
|
|
33
|
+
settings.localization;
|
|
34
|
+
|
|
35
|
+
localeUrlStrategy =
|
|
36
|
+
localeUrlStrategy ?? LocaleUrlStrategy.HideDefaultLocale;
|
|
21
37
|
|
|
22
38
|
if (!defaultLocaleValue) {
|
|
23
39
|
logger.error('Default locale value is not defined in settings.', {
|
|
@@ -29,34 +45,16 @@ const withLocale =
|
|
|
29
45
|
}
|
|
30
46
|
|
|
31
47
|
if (
|
|
32
|
-
|
|
33
|
-
localeUrlStrategy === LocaleUrlStrategy.ShowAllLocales
|
|
48
|
+
!matchedLocale?.length &&
|
|
49
|
+
localeUrlStrategy === LocaleUrlStrategy.ShowAllLocales &&
|
|
50
|
+
redirectToDefaultLocale &&
|
|
51
|
+
req.method === 'GET'
|
|
34
52
|
) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
if (pathParts[0] === singleLocale) {
|
|
39
|
-
url.pathname = '/' + pathParts.slice(1).join('/');
|
|
40
|
-
return NextResponse.redirect(url);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
req.middlewareParams.rewrites.locale = singleLocale;
|
|
44
|
-
} else {
|
|
45
|
-
const matchedLocale =
|
|
46
|
-
url.pathname.match(urlLocaleMatcherRegex)?.[0]?.replace('/', '') ||
|
|
47
|
-
'';
|
|
48
|
-
if (
|
|
49
|
-
!matchedLocale &&
|
|
50
|
-
localeUrlStrategy === LocaleUrlStrategy.ShowAllLocales &&
|
|
51
|
-
redirectToDefaultLocale &&
|
|
52
|
-
req.method === 'GET'
|
|
53
|
-
) {
|
|
54
|
-
url.pathname = `/${defaultLocaleValue}${url.pathname}`;
|
|
55
|
-
return NextResponse.redirect(url);
|
|
56
|
-
}
|
|
57
|
-
req.middlewareParams.rewrites.locale =
|
|
58
|
-
matchedLocale || defaultLocaleValue;
|
|
53
|
+
url.pathname = `/${defaultLocaleValue}${url.pathname}`;
|
|
54
|
+
return NextResponse.redirect(url);
|
|
59
55
|
}
|
|
56
|
+
|
|
57
|
+
req.middlewareParams.rewrites.locale = matchedLocale;
|
|
60
58
|
} catch (error) {
|
|
61
59
|
logger.error('withLocale error', {
|
|
62
60
|
error,
|
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.48.0-rc.
|
|
4
|
+
"version": "1.48.0-rc.4",
|
|
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.48.0-rc.
|
|
33
|
+
"@akinon/eslint-plugin-projectzero": "1.48.0-rc.4",
|
|
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",
|