@bleedingdev/modern-js-i18n-runtime-extensions 3.9.0-ultramodern.2 → 3.9.0-ultramodern.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/dist/cjs/index.js +15 -8
- package/dist/cjs/localeRedirectPolicy.js +96 -0
- package/dist/cjs/paths.js +56 -0
- package/dist/cjs/router-navigation/adapter.js +93 -0
- package/dist/cjs/{localisedUrls/config.js → router-navigation/index.js} +5 -14
- package/dist/esm/index.mjs +2 -1
- package/dist/esm/localeRedirectPolicy.mjs +46 -0
- package/dist/esm/paths.mjs +12 -0
- package/dist/esm/router-navigation/adapter.mjs +55 -0
- package/dist/esm/router-navigation/index.mjs +1 -0
- package/dist/esm-node/index.mjs +2 -1
- package/dist/esm-node/localeRedirectPolicy.mjs +47 -0
- package/dist/esm-node/paths.mjs +13 -0
- package/dist/esm-node/router-navigation/adapter.mjs +56 -0
- package/dist/esm-node/router-navigation/index.mjs +2 -0
- package/dist/types/index.d.ts +2 -1
- package/dist/types/localeRedirectPolicy.d.ts +22 -0
- package/dist/types/paths.d.ts +9 -0
- package/dist/types/router-navigation/adapter.d.ts +23 -0
- package/dist/types/router-navigation/index.d.ts +1 -0
- package/package.json +33 -9
- package/src/index.ts +2 -1
- package/src/localeRedirectPolicy.ts +127 -0
- package/src/paths.ts +37 -0
- package/src/router-navigation/adapter.tsx +120 -0
- package/src/router-navigation/index.ts +6 -0
- package/tests/languageSyncController.test.ts +0 -77
- package/tests/localeRedirectPolicy.test.ts +173 -0
- package/tests/paths.test.ts +24 -0
- package/tests/reactLanguageSync.test.tsx +21 -2
- package/tests/routerNavigation.test.tsx +80 -0
- package/dist/cjs/localisedUrls/index.js +0 -91
- package/dist/cjs/localisedUrls/normalise.js +0 -87
- package/dist/cjs/localisedUrls/pathname.js +0 -68
- package/dist/cjs/localisedUrls/patterns.js +0 -214
- package/dist/cjs/localisedUrls/redirect.js +0 -103
- package/dist/cjs/localisedUrls/resolve.js +0 -78
- package/dist/cjs/localisedUrls/routes.js +0 -202
- package/dist/cjs/localisedUrls/types.js +0 -18
- package/dist/esm/localisedUrls/config.mjs +0 -11
- package/dist/esm/localisedUrls/index.mjs +0 -7
- package/dist/esm/localisedUrls/normalise.mjs +0 -37
- package/dist/esm/localisedUrls/pathname.mjs +0 -24
- package/dist/esm/localisedUrls/patterns.mjs +0 -152
- package/dist/esm/localisedUrls/redirect.mjs +0 -56
- package/dist/esm/localisedUrls/resolve.mjs +0 -37
- package/dist/esm/localisedUrls/routes.mjs +0 -161
- package/dist/esm/localisedUrls/types.mjs +0 -0
- package/dist/esm-node/localisedUrls/config.mjs +0 -12
- package/dist/esm-node/localisedUrls/index.mjs +0 -8
- package/dist/esm-node/localisedUrls/normalise.mjs +0 -38
- package/dist/esm-node/localisedUrls/pathname.mjs +0 -25
- package/dist/esm-node/localisedUrls/patterns.mjs +0 -153
- package/dist/esm-node/localisedUrls/redirect.mjs +0 -57
- package/dist/esm-node/localisedUrls/resolve.mjs +0 -38
- package/dist/esm-node/localisedUrls/routes.mjs +0 -162
- package/dist/esm-node/localisedUrls/types.mjs +0 -1
- package/dist/types/localisedUrls/config.d.ts +0 -9
- package/dist/types/localisedUrls/index.d.ts +0 -9
- package/dist/types/localisedUrls/normalise.d.ts +0 -10
- package/dist/types/localisedUrls/pathname.d.ts +0 -4
- package/dist/types/localisedUrls/patterns.d.ts +0 -11
- package/dist/types/localisedUrls/redirect.d.ts +0 -11
- package/dist/types/localisedUrls/resolve.d.ts +0 -7
- package/dist/types/localisedUrls/routes.d.ts +0 -3
- package/dist/types/localisedUrls/types.d.ts +0 -14
- package/src/localisedUrls/config.ts +0 -18
- package/src/localisedUrls/index.ts +0 -24
- package/src/localisedUrls/normalise.ts +0 -62
- package/src/localisedUrls/pathname.ts +0 -60
- package/src/localisedUrls/patterns.ts +0 -283
- package/src/localisedUrls/redirect.ts +0 -93
- package/src/localisedUrls/resolve.ts +0 -106
- package/src/localisedUrls/routes.ts +0 -422
- package/src/localisedUrls/types.ts +0 -18
- package/tests/clientRedirectIntegration.test.tsx +0 -74
- package/tests/localisedUrls.test.ts +0 -673
package/package.json
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"modern.js",
|
|
18
18
|
"i18n"
|
|
19
19
|
],
|
|
20
|
-
"version": "3.9.0-ultramodern.
|
|
20
|
+
"version": "3.9.0-ultramodern.20",
|
|
21
21
|
"types": "./dist/types/index.d.ts",
|
|
22
22
|
"main": "./dist/cjs/index.js",
|
|
23
23
|
"module": "./dist/esm/index.mjs",
|
|
@@ -30,13 +30,35 @@
|
|
|
30
30
|
},
|
|
31
31
|
"default": "./dist/esm/index.mjs"
|
|
32
32
|
},
|
|
33
|
-
"./
|
|
33
|
+
"./router-navigation": {
|
|
34
|
+
"types": "./dist/types/router-navigation/index.d.ts",
|
|
35
|
+
"node": {
|
|
36
|
+
"import": "./dist/esm-node/router-navigation/index.mjs",
|
|
37
|
+
"require": "./dist/cjs/router-navigation/index.js"
|
|
38
|
+
},
|
|
39
|
+
"default": "./dist/esm/router-navigation/index.mjs"
|
|
40
|
+
},
|
|
41
|
+
"./package.json": "./package.json",
|
|
42
|
+
"./paths": {
|
|
43
|
+
"types": "./dist/types/paths.d.ts",
|
|
44
|
+
"node": {
|
|
45
|
+
"import": "./dist/esm-node/paths.mjs",
|
|
46
|
+
"require": "./dist/cjs/paths.js"
|
|
47
|
+
},
|
|
48
|
+
"default": "./dist/esm/paths.mjs"
|
|
49
|
+
}
|
|
34
50
|
},
|
|
35
51
|
"typesVersions": {
|
|
36
52
|
"*": {
|
|
37
53
|
".": [
|
|
38
54
|
"./dist/types/index.d.ts",
|
|
39
55
|
"./src/index.ts"
|
|
56
|
+
],
|
|
57
|
+
"paths": [
|
|
58
|
+
"./dist/types/paths.d.ts"
|
|
59
|
+
],
|
|
60
|
+
"router-navigation": [
|
|
61
|
+
"./dist/types/router-navigation/index.d.ts"
|
|
40
62
|
]
|
|
41
63
|
}
|
|
42
64
|
},
|
|
@@ -44,20 +66,22 @@
|
|
|
44
66
|
"node": ">=26.7.0"
|
|
45
67
|
},
|
|
46
68
|
"dependencies": {
|
|
69
|
+
"@modern-js/runtime-extensions": "npm:@bleedingdev/modern-js-runtime-extensions@3.9.0-ultramodern.20",
|
|
70
|
+
"@modern-js/runtime-utils": "npm:@bleedingdev/modern-js-runtime-utils@3.9.0-ultramodern.20",
|
|
47
71
|
"i18next-browser-languagedetector": "^8.2.1"
|
|
48
72
|
},
|
|
49
73
|
"peerDependencies": {
|
|
50
|
-
"@modern-js/runtime": "3.9.0-ultramodern.
|
|
74
|
+
"@modern-js/runtime": "3.9.0-ultramodern.20",
|
|
51
75
|
"react": "^19.2.8"
|
|
52
76
|
},
|
|
53
77
|
"devDependencies": {
|
|
54
|
-
"@modern-js/runtime": "npm:@bleedingdev/modern-js-runtime@3.9.0-ultramodern.
|
|
55
|
-
"@rslib/core": "1.0.
|
|
78
|
+
"@modern-js/runtime": "npm:@bleedingdev/modern-js-runtime@3.9.0-ultramodern.20",
|
|
79
|
+
"@rslib/core": "1.0.2",
|
|
56
80
|
"@scripts/rstest-config": "2.66.0",
|
|
57
|
-
"@types/node": "^26.
|
|
81
|
+
"@types/node": "^26.6.2",
|
|
58
82
|
"@typescript/native-preview": "7.0.0-dev.20260707.2",
|
|
59
|
-
"react": "^19.
|
|
60
|
-
"react-dom": "^19.
|
|
83
|
+
"react": "^19.3.0",
|
|
84
|
+
"react-dom": "^19.3.0"
|
|
61
85
|
},
|
|
62
86
|
"sideEffects": false,
|
|
63
87
|
"publishConfig": {
|
|
@@ -66,6 +90,6 @@
|
|
|
66
90
|
"scripts": {
|
|
67
91
|
"dev": "rslib build --watch",
|
|
68
92
|
"build": "rslib build",
|
|
69
|
-
"test": "rstest
|
|
93
|
+
"test": "rstest"
|
|
70
94
|
}
|
|
71
95
|
}
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import type { LocalisedUrlsOption } from '@modern-js/runtime-extensions/localised-urls';
|
|
2
|
+
import {
|
|
3
|
+
isDefaultLocaleRedirectSkipPath,
|
|
4
|
+
localiseTargetPathname,
|
|
5
|
+
matchesPathPrefix,
|
|
6
|
+
shouldSkipLocaleRedirect,
|
|
7
|
+
stripLanguagePrefix,
|
|
8
|
+
} from '@modern-js/runtime-extensions/localised-urls';
|
|
9
|
+
|
|
10
|
+
interface LocaleRedirectRequest {
|
|
11
|
+
url: string;
|
|
12
|
+
header: () => {
|
|
13
|
+
host?: string;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const stripUrlPathPrefix = (pathname: string, urlPath: string): string => {
|
|
18
|
+
const basePath = urlPath.replace('/*', '');
|
|
19
|
+
|
|
20
|
+
if (!basePath || basePath === '/') {
|
|
21
|
+
return pathname;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const remainingPath = pathname.startsWith(basePath)
|
|
25
|
+
? pathname.slice(basePath.length)
|
|
26
|
+
: pathname;
|
|
27
|
+
|
|
28
|
+
return remainingPath || '/';
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Check if pathname should ignore automatic locale redirect.
|
|
33
|
+
*/
|
|
34
|
+
export const shouldIgnoreRedirect = (
|
|
35
|
+
pathname: string,
|
|
36
|
+
urlPath: string,
|
|
37
|
+
ignoreRedirectRoutes?: string[] | ((pathname: string) => boolean),
|
|
38
|
+
): boolean => {
|
|
39
|
+
const remainingPath = stripUrlPathPrefix(pathname, urlPath);
|
|
40
|
+
|
|
41
|
+
return shouldSkipLocaleRedirect(remainingPath, [], ignoreRedirectRoutes);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Check if pathname is a static or federation resource request.
|
|
46
|
+
*
|
|
47
|
+
* This includes configured staticRoutePrefixes, the shared default skip policy
|
|
48
|
+
* from ADR-0002, and language-prefixed variants such as /en/static/app.js.
|
|
49
|
+
*/
|
|
50
|
+
export const isStaticResourceRequest = (
|
|
51
|
+
pathname: string,
|
|
52
|
+
staticRoutePrefixes: string[],
|
|
53
|
+
languages: string[] = [],
|
|
54
|
+
): boolean => {
|
|
55
|
+
if (isDefaultLocaleRedirectSkipPath(pathname, languages)) {
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const matchesStaticRoutePrefix = (targetPathname: string): boolean =>
|
|
60
|
+
staticRoutePrefixes.some(prefix =>
|
|
61
|
+
matchesPathPrefix(targetPathname, prefix),
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
if (matchesStaticRoutePrefix(pathname)) {
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const pathWithoutLanguage = stripLanguagePrefix(pathname, languages);
|
|
69
|
+
|
|
70
|
+
return (
|
|
71
|
+
pathWithoutLanguage !== pathname &&
|
|
72
|
+
matchesStaticRoutePrefix(pathWithoutLanguage)
|
|
73
|
+
);
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export const getLanguageFromPath = (
|
|
77
|
+
req: LocaleRedirectRequest,
|
|
78
|
+
urlPath: string,
|
|
79
|
+
languages: string[],
|
|
80
|
+
): string | null => {
|
|
81
|
+
const url = new URL(req.url, `http://${req.header().host}`);
|
|
82
|
+
const pathname = url.pathname;
|
|
83
|
+
const remainingPath = stripUrlPathPrefix(pathname, urlPath);
|
|
84
|
+
const segments = remainingPath.split('/').filter(Boolean);
|
|
85
|
+
const firstSegment = segments[0];
|
|
86
|
+
|
|
87
|
+
if (languages.includes(firstSegment)) {
|
|
88
|
+
return firstSegment;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return null;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
export const buildLocalizedUrl = (
|
|
95
|
+
req: LocaleRedirectRequest,
|
|
96
|
+
urlPath: string,
|
|
97
|
+
language: string,
|
|
98
|
+
languages: string[],
|
|
99
|
+
localisedUrls?: LocalisedUrlsOption,
|
|
100
|
+
): string => {
|
|
101
|
+
const url = new URL(req.url);
|
|
102
|
+
const pathname = url.pathname;
|
|
103
|
+
const basePath = urlPath.replace('/*', '');
|
|
104
|
+
const remainingPath = stripUrlPathPrefix(pathname, urlPath);
|
|
105
|
+
|
|
106
|
+
const newPathname = localiseTargetPathname(
|
|
107
|
+
remainingPath,
|
|
108
|
+
language,
|
|
109
|
+
languages,
|
|
110
|
+
localisedUrls,
|
|
111
|
+
);
|
|
112
|
+
const suffix = `${url.search}${url.hash}`;
|
|
113
|
+
const localizedUrl =
|
|
114
|
+
basePath === '/' ? newPathname + suffix : basePath + newPathname + suffix;
|
|
115
|
+
|
|
116
|
+
return localizedUrl;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
export const createLocaleRedirectResponse = (location: string): Response =>
|
|
120
|
+
new Response(null, {
|
|
121
|
+
status: 302,
|
|
122
|
+
headers: {
|
|
123
|
+
'Cache-Control': 'private, no-store',
|
|
124
|
+
Location: location,
|
|
125
|
+
Vary: 'Accept-Language, Cookie',
|
|
126
|
+
},
|
|
127
|
+
});
|
package/src/paths.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import {
|
|
2
|
+
canonicalTargetPathname,
|
|
3
|
+
type LocalisedUrlsOption,
|
|
4
|
+
localiseTargetPathname,
|
|
5
|
+
} from '@modern-js/runtime-extensions/localised-urls';
|
|
6
|
+
import { splitUrlTarget } from '@modern-js/runtime-utils/url';
|
|
7
|
+
|
|
8
|
+
export interface LocalizedPathsConfig {
|
|
9
|
+
languages: string[];
|
|
10
|
+
localisedUrls?: LocalisedUrlsOption;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Synchronous mapped URL construction, independent of React/plugin setup. */
|
|
14
|
+
export const buildLocalizedUrl = (
|
|
15
|
+
target: string,
|
|
16
|
+
language: string,
|
|
17
|
+
languages: string[],
|
|
18
|
+
localisedUrls?: LocalisedUrlsOption,
|
|
19
|
+
): string => {
|
|
20
|
+
const { pathname, search, hash } = splitUrlTarget(target);
|
|
21
|
+
return `${localiseTargetPathname(pathname, language, languages, localisedUrls)}${search}${hash}`;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const localizePath = (
|
|
25
|
+
target: string,
|
|
26
|
+
language: string,
|
|
27
|
+
config: LocalizedPathsConfig,
|
|
28
|
+
): string =>
|
|
29
|
+
buildLocalizedUrl(target, language, config.languages, config.localisedUrls);
|
|
30
|
+
|
|
31
|
+
export const canonicalPath = (
|
|
32
|
+
target: string,
|
|
33
|
+
config: LocalizedPathsConfig,
|
|
34
|
+
): string => {
|
|
35
|
+
const { pathname, search, hash } = splitUrlTarget(target);
|
|
36
|
+
return `${canonicalTargetPathname(pathname, config.languages, config.localisedUrls)}${search}${hash}`;
|
|
37
|
+
};
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { RuntimeContext } from '@modern-js/runtime';
|
|
2
|
+
import { InternalRuntimeContext } from '@modern-js/runtime/context';
|
|
3
|
+
import {
|
|
4
|
+
getRouterRuntimeState,
|
|
5
|
+
type RouterLinkTarget,
|
|
6
|
+
type RouterNavigationCapability,
|
|
7
|
+
type RouterNavigationSnapshot,
|
|
8
|
+
subscribeRouterRuntimeState,
|
|
9
|
+
} from '@modern-js/runtime-extensions/router-state';
|
|
10
|
+
import type React from 'react';
|
|
11
|
+
import { useCallback, useContext, useSyncExternalStore } from 'react';
|
|
12
|
+
|
|
13
|
+
export interface I18nRouterNavigationDependencies<
|
|
14
|
+
Adapter extends I18nRouterAdapterShape,
|
|
15
|
+
> {
|
|
16
|
+
I18nNavigationProvider: React.ComponentType<
|
|
17
|
+
React.PropsWithChildren<{ value: Adapter }>
|
|
18
|
+
>;
|
|
19
|
+
useNativeI18nRouterAdapter: () => Adapter;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type I18nRouterLinkTarget = RouterLinkTarget;
|
|
23
|
+
|
|
24
|
+
export interface I18nRouterAdapterShape {
|
|
25
|
+
framework?: string;
|
|
26
|
+
hasRouter: boolean;
|
|
27
|
+
location: RouterNavigationSnapshot['location'] | null;
|
|
28
|
+
navigate: RouterNavigationCapability['navigate'] | null;
|
|
29
|
+
Link: RouterNavigationCapability['Link'] | null;
|
|
30
|
+
params: Record<string, string>;
|
|
31
|
+
createLinkProps?: RouterNavigationCapability['createLinkProps'];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const subscribeNone = () => () => {};
|
|
35
|
+
const getEmptySnapshot = () => null;
|
|
36
|
+
|
|
37
|
+
/** The router provider owns navigation; this seam only observes its capability. */
|
|
38
|
+
export const createI18nRouterNavigation = <
|
|
39
|
+
Adapter extends I18nRouterAdapterShape,
|
|
40
|
+
>({
|
|
41
|
+
I18nNavigationProvider,
|
|
42
|
+
useNativeI18nRouterAdapter,
|
|
43
|
+
}: I18nRouterNavigationDependencies<Adapter>) => {
|
|
44
|
+
// The injected context accepts null to preserve native-router fallback.
|
|
45
|
+
const NavigationProvider = I18nNavigationProvider as React.ComponentType<
|
|
46
|
+
React.PropsWithChildren<{ value: Adapter | null }>
|
|
47
|
+
>;
|
|
48
|
+
const useIntegratedRouterAdapter = (): Adapter => {
|
|
49
|
+
const runtimeContext = useContext(RuntimeContext);
|
|
50
|
+
const internalContext = useContext(InternalRuntimeContext);
|
|
51
|
+
const native = useNativeI18nRouterAdapter();
|
|
52
|
+
const getCapability = useCallback(
|
|
53
|
+
() =>
|
|
54
|
+
getRouterRuntimeState(internalContext)?.navigation ??
|
|
55
|
+
getRouterRuntimeState(runtimeContext)?.navigation,
|
|
56
|
+
[internalContext, runtimeContext],
|
|
57
|
+
);
|
|
58
|
+
const subscribeCapability = useCallback(
|
|
59
|
+
(update: () => void) => {
|
|
60
|
+
const stopInternal = subscribeRouterRuntimeState(
|
|
61
|
+
internalContext,
|
|
62
|
+
update,
|
|
63
|
+
);
|
|
64
|
+
const stopPublic =
|
|
65
|
+
internalContext === runtimeContext
|
|
66
|
+
? undefined
|
|
67
|
+
: subscribeRouterRuntimeState(runtimeContext, update);
|
|
68
|
+
return () => {
|
|
69
|
+
stopInternal();
|
|
70
|
+
stopPublic?.();
|
|
71
|
+
};
|
|
72
|
+
},
|
|
73
|
+
[internalContext, runtimeContext],
|
|
74
|
+
);
|
|
75
|
+
// Separate subscriptions handle both delayed installation/replacement and
|
|
76
|
+
// navigation inside the currently selected provider, without hook guessing.
|
|
77
|
+
const capability = useSyncExternalStore(
|
|
78
|
+
subscribeCapability,
|
|
79
|
+
getCapability,
|
|
80
|
+
getCapability,
|
|
81
|
+
);
|
|
82
|
+
const snapshot = useSyncExternalStore(
|
|
83
|
+
capability?.subscribe ?? subscribeNone,
|
|
84
|
+
capability?.getSnapshot ?? getEmptySnapshot,
|
|
85
|
+
capability?.getSnapshot ?? getEmptySnapshot,
|
|
86
|
+
);
|
|
87
|
+
if (native.hasRouter || !capability || !snapshot) {
|
|
88
|
+
return native;
|
|
89
|
+
}
|
|
90
|
+
const internalState = getRouterRuntimeState(internalContext);
|
|
91
|
+
return {
|
|
92
|
+
framework:
|
|
93
|
+
internalState?.navigation === capability
|
|
94
|
+
? internalState.framework
|
|
95
|
+
: getRouterRuntimeState(runtimeContext)?.framework,
|
|
96
|
+
hasRouter: true,
|
|
97
|
+
...snapshot,
|
|
98
|
+
navigate: capability.navigate,
|
|
99
|
+
Link: capability.Link,
|
|
100
|
+
createLinkProps: capability.createLinkProps,
|
|
101
|
+
} as Adapter;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const I18nRouterNavigationProvider = ({
|
|
105
|
+
children,
|
|
106
|
+
}: React.PropsWithChildren) => {
|
|
107
|
+
const value = useIntegratedRouterAdapter();
|
|
108
|
+
const native = useNativeI18nRouterAdapter();
|
|
109
|
+
// Keep the router subtree mounted when a capability arrives or disappears.
|
|
110
|
+
return (
|
|
111
|
+
<NavigationProvider
|
|
112
|
+
value={value.hasRouter && !native.hasRouter ? value : null}
|
|
113
|
+
>
|
|
114
|
+
{children}
|
|
115
|
+
</NavigationProvider>
|
|
116
|
+
);
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
return { useIntegratedRouterAdapter, I18nRouterNavigationProvider };
|
|
120
|
+
};
|
|
@@ -80,7 +80,6 @@ describe('latest language synchronization coordinator', () => {
|
|
|
80
80
|
|
|
81
81
|
binding.request('cs');
|
|
82
82
|
binding.request('de');
|
|
83
|
-
expect(pending.map(change => change.language)).toEqual(['cs', 'de']);
|
|
84
83
|
|
|
85
84
|
pending[1].resolve();
|
|
86
85
|
await pending[1].promise;
|
|
@@ -91,7 +90,6 @@ describe('latest language synchronization coordinator', () => {
|
|
|
91
90
|
await pending[0].promise;
|
|
92
91
|
await flush();
|
|
93
92
|
expect(target.language).toBe('cs');
|
|
94
|
-
expect(pending.map(change => change.language)).toEqual(['cs', 'de', 'de']);
|
|
95
93
|
|
|
96
94
|
pending[2].resolve();
|
|
97
95
|
await pending[2].promise;
|
|
@@ -100,81 +98,6 @@ describe('latest language synchronization coordinator', () => {
|
|
|
100
98
|
expect(commits.at(-1)).toBe('de');
|
|
101
99
|
});
|
|
102
100
|
|
|
103
|
-
test('repairs a shared singleton after the starting mount leaves', async () => {
|
|
104
|
-
const target = { language: 'en' };
|
|
105
|
-
const { changeLanguage, pending } = deferredChanger(target);
|
|
106
|
-
const commits: string[] = [];
|
|
107
|
-
const first = createLatestLanguageSyncBinding<Target>();
|
|
108
|
-
const second = createLatestLanguageSyncBinding<Target>();
|
|
109
|
-
for (const binding of [first, second]) {
|
|
110
|
-
binding.updateCallbacks({
|
|
111
|
-
changeLanguage,
|
|
112
|
-
commitLanguage: (_target, language) => commits.push(language),
|
|
113
|
-
readLanguage: current => current.language,
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
first.activate(target);
|
|
118
|
-
first.request('cs');
|
|
119
|
-
first.deactivate();
|
|
120
|
-
second.activate(target);
|
|
121
|
-
second.request('de');
|
|
122
|
-
|
|
123
|
-
pending[1].resolve();
|
|
124
|
-
await pending[1].promise;
|
|
125
|
-
await flush();
|
|
126
|
-
pending[0].resolve();
|
|
127
|
-
await pending[0].promise;
|
|
128
|
-
await flush();
|
|
129
|
-
|
|
130
|
-
expect(pending.map(change => change.language)).toEqual(['cs', 'de', 'de']);
|
|
131
|
-
pending[2].resolve();
|
|
132
|
-
await pending[2].promise;
|
|
133
|
-
await flush();
|
|
134
|
-
expect(target.language).toBe('de');
|
|
135
|
-
expect(commits.at(-1)).toBe('de');
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
test('supersedes a hung same-language attempt and repairs it after a later route', async () => {
|
|
139
|
-
rstest.useFakeTimers();
|
|
140
|
-
const target = { language: 'en' };
|
|
141
|
-
const { changeLanguage, pending } = deferredChanger(target);
|
|
142
|
-
const commits: string[] = [];
|
|
143
|
-
const binding = createLatestLanguageSyncBinding<Target>({
|
|
144
|
-
attemptTimeoutMs: 100,
|
|
145
|
-
retryDelayMs: () => 10,
|
|
146
|
-
});
|
|
147
|
-
binding.updateCallbacks({
|
|
148
|
-
changeLanguage,
|
|
149
|
-
commitLanguage: (_target, language) => commits.push(language),
|
|
150
|
-
readLanguage: current => current.language,
|
|
151
|
-
});
|
|
152
|
-
binding.activate(target);
|
|
153
|
-
binding.request('cs');
|
|
154
|
-
|
|
155
|
-
await rstest.advanceTimersByTimeAsync(110);
|
|
156
|
-
expect(pending.map(change => change.language)).toEqual(['cs', 'cs']);
|
|
157
|
-
pending[1].resolve();
|
|
158
|
-
await pending[1].promise;
|
|
159
|
-
await flush();
|
|
160
|
-
expect(commits.at(-1)).toBe('cs');
|
|
161
|
-
|
|
162
|
-
binding.request('de');
|
|
163
|
-
pending[2].resolve();
|
|
164
|
-
await pending[2].promise;
|
|
165
|
-
await flush();
|
|
166
|
-
expect(target.language).toBe('de');
|
|
167
|
-
|
|
168
|
-
pending[0].resolve();
|
|
169
|
-
await pending[0].promise;
|
|
170
|
-
await flush();
|
|
171
|
-
expect(pending.at(-1)?.language).toBe('de');
|
|
172
|
-
pending.at(-1)?.resolve();
|
|
173
|
-
await pending.at(-1)?.promise;
|
|
174
|
-
await flush();
|
|
175
|
-
expect(target.language).toBe('de');
|
|
176
|
-
});
|
|
177
|
-
|
|
178
101
|
test('bounds permanent failures and reports the final consequence once', async () => {
|
|
179
102
|
rstest.useFakeTimers();
|
|
180
103
|
const target = { language: 'en' };
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { describe, expect, test } from '@rstest/core';
|
|
2
|
+
import {
|
|
3
|
+
buildLocalizedUrl,
|
|
4
|
+
createLocaleRedirectResponse,
|
|
5
|
+
getLanguageFromPath,
|
|
6
|
+
isStaticResourceRequest,
|
|
7
|
+
shouldIgnoreRedirect,
|
|
8
|
+
} from '../src/localeRedirectPolicy';
|
|
9
|
+
|
|
10
|
+
const languages = ['en', 'cs'];
|
|
11
|
+
const request = (url: string) => ({
|
|
12
|
+
url,
|
|
13
|
+
header: () => ({ host: 'example.com' }),
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
describe('locale redirect resource policy', () => {
|
|
17
|
+
test('exempts default static and federation routes, including locale prefixes', () => {
|
|
18
|
+
const paths = [
|
|
19
|
+
'/backend-mf-manifest.json',
|
|
20
|
+
'/backendRemoteEntry.cjs',
|
|
21
|
+
'/mf-manifest.json',
|
|
22
|
+
'/mf-stats.json',
|
|
23
|
+
'/remoteEntry.js',
|
|
24
|
+
// A manifest may name a root entry; the federation layer serves these.
|
|
25
|
+
'/remoteEntry.catalog.js',
|
|
26
|
+
'/backendRemoteEntry.catalog-v2.cjs',
|
|
27
|
+
'/static/app.js',
|
|
28
|
+
'/upload/avatar.png',
|
|
29
|
+
];
|
|
30
|
+
|
|
31
|
+
for (const pathname of paths) {
|
|
32
|
+
expect(isStaticResourceRequest(pathname, [], languages)).toBe(true);
|
|
33
|
+
expect(isStaticResourceRequest(`/cs${pathname}`, [], languages)).toBe(
|
|
34
|
+
true,
|
|
35
|
+
);
|
|
36
|
+
expect(shouldIgnoreRedirect(`/store${pathname}`, '/store/*')).toBe(true);
|
|
37
|
+
}
|
|
38
|
+
// Only the entry itself, not a page or asset that merely starts like one.
|
|
39
|
+
for (const pathname of [
|
|
40
|
+
'/remoteEntry.catalog.js.map',
|
|
41
|
+
'/remoteEntry.catalog/js',
|
|
42
|
+
'/products/remoteEntry.js',
|
|
43
|
+
]) {
|
|
44
|
+
expect(isStaticResourceRequest(pathname, [], languages)).toBe(false);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test('configured static prefixes match complete path segments with or without a locale', () => {
|
|
49
|
+
for (const pathname of ['/assets', '/assets/app.js', '/en/assets/app.js']) {
|
|
50
|
+
expect(isStaticResourceRequest(pathname, ['/assets/'], languages)).toBe(
|
|
51
|
+
true,
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
for (const pathname of [
|
|
55
|
+
'/assets-extra/app.js',
|
|
56
|
+
'/cs/assets-extra/app.js',
|
|
57
|
+
]) {
|
|
58
|
+
expect(isStaticResourceRequest(pathname, ['/assets'], languages)).toBe(
|
|
59
|
+
false,
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
expect(
|
|
63
|
+
isStaticResourceRequest('/fr/assets/app.js', ['/assets'], languages),
|
|
64
|
+
).toBe(false);
|
|
65
|
+
expect(
|
|
66
|
+
isStaticResourceRequest('/remoteEntry.js/product', [], languages),
|
|
67
|
+
).toBe(false);
|
|
68
|
+
expect(isStaticResourceRequest('/static-product', [])).toBe(false);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test('custom ignore lists operate after stripping the mount path', () => {
|
|
72
|
+
expect(
|
|
73
|
+
shouldIgnoreRedirect('/store/health/live', '/store/*', ['/health']),
|
|
74
|
+
).toBe(true);
|
|
75
|
+
expect(
|
|
76
|
+
shouldIgnoreRedirect('/store/healthy', '/store/*', ['/health']),
|
|
77
|
+
).toBe(false);
|
|
78
|
+
expect(shouldIgnoreRedirect('/store/products', '/store/*')).toBe(false);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test('custom callbacks receive the remaining pathname and default exemptions take priority', () => {
|
|
82
|
+
const seen: string[] = [];
|
|
83
|
+
const ignore = (pathname: string) => {
|
|
84
|
+
seen.push(pathname);
|
|
85
|
+
return pathname === '/health';
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
expect(shouldIgnoreRedirect('/store/health', '/store/*', ignore)).toBe(
|
|
89
|
+
true,
|
|
90
|
+
);
|
|
91
|
+
expect(shouldIgnoreRedirect('/store/products', '/store/*', ignore)).toBe(
|
|
92
|
+
false,
|
|
93
|
+
);
|
|
94
|
+
expect(
|
|
95
|
+
shouldIgnoreRedirect('/store/static/app.js', '/store/*', ignore),
|
|
96
|
+
).toBe(true);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
describe('locale redirect URL policy', () => {
|
|
101
|
+
test('detects a configured first language segment for relative or absolute URLs', () => {
|
|
102
|
+
expect(
|
|
103
|
+
getLanguageFromPath(
|
|
104
|
+
request('/store/cs/products?x=1'),
|
|
105
|
+
'/store/*',
|
|
106
|
+
languages,
|
|
107
|
+
),
|
|
108
|
+
).toBe('cs');
|
|
109
|
+
expect(
|
|
110
|
+
getLanguageFromPath(
|
|
111
|
+
request('https://example.com/en/products'),
|
|
112
|
+
'/',
|
|
113
|
+
languages,
|
|
114
|
+
),
|
|
115
|
+
).toBe('en');
|
|
116
|
+
expect(getLanguageFromPath(request('/cs/products'), '', languages)).toBe(
|
|
117
|
+
'cs',
|
|
118
|
+
);
|
|
119
|
+
expect(
|
|
120
|
+
getLanguageFromPath(request('/storecs/products'), '/store/*', languages),
|
|
121
|
+
).toBe('cs');
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
test('switches localized dynamic paths while preserving mount, query and hash', () => {
|
|
125
|
+
expect(
|
|
126
|
+
buildLocalizedUrl(
|
|
127
|
+
request(
|
|
128
|
+
'https://example.com/store/en/products/red%20tractor?q=a%2Fb&sort=price#details',
|
|
129
|
+
),
|
|
130
|
+
'/store/*',
|
|
131
|
+
'cs',
|
|
132
|
+
languages,
|
|
133
|
+
{ '/products/:slug': { en: '/products/:slug', cs: '/produkty/:slug' } },
|
|
134
|
+
),
|
|
135
|
+
).toBe('/store/cs/produkty/red%20tractor?q=a%2Fb&sort=price#details');
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
test('supports root and empty mounts and missing locale mapping', () => {
|
|
139
|
+
for (const mount of ['/', '', '/*']) {
|
|
140
|
+
expect(
|
|
141
|
+
buildLocalizedUrl(
|
|
142
|
+
request('https://example.com/products?x=1#top'),
|
|
143
|
+
mount,
|
|
144
|
+
'cs',
|
|
145
|
+
languages,
|
|
146
|
+
),
|
|
147
|
+
).toBe('/cs/products?x=1#top');
|
|
148
|
+
}
|
|
149
|
+
expect(
|
|
150
|
+
buildLocalizedUrl(
|
|
151
|
+
request('https://example.com/store'),
|
|
152
|
+
'/store/*',
|
|
153
|
+
'cs',
|
|
154
|
+
languages,
|
|
155
|
+
false,
|
|
156
|
+
),
|
|
157
|
+
).toBe('/store/cs');
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
test('returns a non-cacheable temporary redirect with language negotiation headers', async () => {
|
|
161
|
+
const response = createLocaleRedirectResponse(
|
|
162
|
+
'/cs/produkty?sort=price#details',
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
expect(response.status).toBe(302);
|
|
166
|
+
expect(response.headers.get('Location')).toBe(
|
|
167
|
+
'/cs/produkty?sort=price#details',
|
|
168
|
+
);
|
|
169
|
+
expect(response.headers.get('Cache-Control')).toBe('private, no-store');
|
|
170
|
+
expect(response.headers.get('Vary')).toBe('Accept-Language, Cookie');
|
|
171
|
+
expect(await response.text()).toBe('');
|
|
172
|
+
});
|
|
173
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { describe, expect, test } from '@rstest/core';
|
|
2
|
+
import { canonicalPath, localizePath } from '../src/paths';
|
|
3
|
+
|
|
4
|
+
const config = {
|
|
5
|
+
languages: ['en', 'cs'],
|
|
6
|
+
localisedUrls: {
|
|
7
|
+
'/products/:slug': { en: '/products/:slug', cs: '/produkty/:slug' },
|
|
8
|
+
},
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
describe('@modern-js/i18n-runtime-extensions/paths', () => {
|
|
12
|
+
test('localises the pathname while leaving query and hash verbatim', () => {
|
|
13
|
+
expect(
|
|
14
|
+
localizePath(
|
|
15
|
+
'/products/red-shoe?tag=boots&tag=sale#details',
|
|
16
|
+
'cs',
|
|
17
|
+
config,
|
|
18
|
+
),
|
|
19
|
+
).toBe('/cs/produkty/red-shoe?tag=boots&tag=sale#details');
|
|
20
|
+
expect(
|
|
21
|
+
canonicalPath('/CS/produkty/red-shoe?tag=boots#details', config),
|
|
22
|
+
).toBe('/products/red-shoe?tag=boots#details');
|
|
23
|
+
});
|
|
24
|
+
});
|