nuxtseo-shared 5.3.14 → 5.3.15

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.
@@ -64,12 +64,30 @@ interface RouteLocaleInfo {
64
64
  /** Route with locale prefix stripped (e.g. /fr/about → /about). For no_prefix this equals route. */
65
65
  basePath: string;
66
66
  }
67
- interface RuntimeRouteContext {
68
- /** Request host, used to resolve locales under domain-based strategies. */
69
- host?: string;
70
- /** Known current locale, required to translate no_prefix routes without a locale domain. */
67
+ type RuntimeRouteContext = {
68
+ /** Known current locale, required for no-prefix routes without a locale domain. */
71
69
  locale?: string;
72
- }
70
+ } & ({
71
+ host?: string;
72
+ domainMode?: 'canonical';
73
+ } | {
74
+ host: string;
75
+ domainMode: 'request';
76
+ });
77
+ type LocaleDomainConfig = Pick<RuntimeLocale, 'code' | 'domain' | 'domains' | 'defaultForDomains'>;
78
+ /** Resolve the host default and available locales. Unknown hosts keep the configured fallback. */
79
+ declare function resolveI18nDomain<T extends LocaleDomainConfig>(host: string | undefined, i18n: {
80
+ defaultLocale: string;
81
+ locales: T[];
82
+ }): {
83
+ _tag: "unknown";
84
+ defaultLocale: string;
85
+ locales: T[];
86
+ } | {
87
+ _tag: "known";
88
+ defaultLocale: string;
89
+ locales: T[];
90
+ };
73
91
  /**
74
92
  * Resolve the canonical domain for a locale. Domainless locales are served on
75
93
  * every domain, so their stable canonical is the default locale's domain.
@@ -94,5 +112,5 @@ declare function localePath(basePath: string, locale: string, i18n: RuntimeI18nC
94
112
  declare function resolveLocaleAlternates(route: string, i18n: RuntimeI18nConfig, context?: RuntimeRouteContext): LocaleAlternateResolution;
95
113
  declare function computeLocaleAlternates(route: string, i18n: RuntimeI18nConfig, context?: RuntimeRouteContext): LocaleAlternate[];
96
114
 
97
- export { computeLocaleAlternates, localePath, resolveCanonicalLocaleDomain, resolveLocaleAlternates, resolveLocaleFromRoute };
115
+ export { computeLocaleAlternates, localePath, resolveCanonicalLocaleDomain, resolveI18nDomain, resolveLocaleAlternates, resolveLocaleFromRoute };
98
116
  export type { LocaleAlternate, LocaleAlternateResolution, LocalePagePaths, LocalePages, RouteLocaleInfo, RuntimeI18nConfig, RuntimeLocale, RuntimeRouteContext, UnlocalizedLocalePage };
@@ -64,12 +64,30 @@ interface RouteLocaleInfo {
64
64
  /** Route with locale prefix stripped (e.g. /fr/about → /about). For no_prefix this equals route. */
65
65
  basePath: string;
66
66
  }
67
- interface RuntimeRouteContext {
68
- /** Request host, used to resolve locales under domain-based strategies. */
69
- host?: string;
70
- /** Known current locale, required to translate no_prefix routes without a locale domain. */
67
+ type RuntimeRouteContext = {
68
+ /** Known current locale, required for no-prefix routes without a locale domain. */
71
69
  locale?: string;
72
- }
70
+ } & ({
71
+ host?: string;
72
+ domainMode?: 'canonical';
73
+ } | {
74
+ host: string;
75
+ domainMode: 'request';
76
+ });
77
+ type LocaleDomainConfig = Pick<RuntimeLocale, 'code' | 'domain' | 'domains' | 'defaultForDomains'>;
78
+ /** Resolve the host default and available locales. Unknown hosts keep the configured fallback. */
79
+ declare function resolveI18nDomain<T extends LocaleDomainConfig>(host: string | undefined, i18n: {
80
+ defaultLocale: string;
81
+ locales: T[];
82
+ }): {
83
+ _tag: "unknown";
84
+ defaultLocale: string;
85
+ locales: T[];
86
+ } | {
87
+ _tag: "known";
88
+ defaultLocale: string;
89
+ locales: T[];
90
+ };
73
91
  /**
74
92
  * Resolve the canonical domain for a locale. Domainless locales are served on
75
93
  * every domain, so their stable canonical is the default locale's domain.
@@ -94,5 +112,5 @@ declare function localePath(basePath: string, locale: string, i18n: RuntimeI18nC
94
112
  declare function resolveLocaleAlternates(route: string, i18n: RuntimeI18nConfig, context?: RuntimeRouteContext): LocaleAlternateResolution;
95
113
  declare function computeLocaleAlternates(route: string, i18n: RuntimeI18nConfig, context?: RuntimeRouteContext): LocaleAlternate[];
96
114
 
97
- export { computeLocaleAlternates, localePath, resolveCanonicalLocaleDomain, resolveLocaleAlternates, resolveLocaleFromRoute };
115
+ export { computeLocaleAlternates, localePath, resolveCanonicalLocaleDomain, resolveI18nDomain, resolveLocaleAlternates, resolveLocaleFromRoute };
98
116
  export type { LocaleAlternate, LocaleAlternateResolution, LocalePagePaths, LocalePages, RouteLocaleInfo, RuntimeI18nConfig, RuntimeLocale, RuntimeRouteContext, UnlocalizedLocalePage };
@@ -1,3 +1,5 @@
1
+ import { parseURL } from 'ufo';
2
+
1
3
  function isUnlocalizedLocalePage(page) {
2
4
  return page._tag === "unlocalized" && typeof page.path === "string";
3
5
  }
@@ -10,24 +12,28 @@ function matchesUnlocalizedLocalePage(page, path) {
10
12
  return !!matchPagePattern(subtreePattern, path);
11
13
  }
12
14
  function normalizeHost(value) {
13
- return value.trim().toLowerCase().replace(/^[a-z][a-z\d+.-]*:\/\//, "").split("/")[0];
15
+ const input = value.trim();
16
+ return parseURL(input.startsWith("//") ? `https:${input}` : input.includes("://") ? input : `https://${input}`).host?.toLowerCase() || "";
14
17
  }
15
18
  function localeDomains(locale) {
16
- return locale.domains?.length ? locale.domains : locale.domain ? [locale.domain] : [];
19
+ return [...locale.domains || [], ...locale.domain ? [locale.domain] : []];
17
20
  }
18
- function resolveLocaleFromHost(host, i18n) {
19
- if (!host)
20
- return void 0;
21
- const normalizedHost = normalizeHost(host);
22
- const domainDefault = i18n.locales.find(
23
- (locale) => locale.defaultForDomains?.some((domain) => normalizeHost(domain) === normalizedHost)
24
- );
25
- if (domainDefault)
26
- return domainDefault;
27
- const matches = i18n.locales.filter(
28
- (locale) => localeDomains(locale).some((domain) => normalizeHost(domain) === normalizedHost)
29
- );
30
- return matches.length === 1 ? matches[0] : matches.find((locale) => locale.code === i18n.defaultLocale);
21
+ function resolveI18nDomain(host, i18n) {
22
+ const normalizedHost = host ? normalizeHost(host) : "";
23
+ const domains = i18n.locales.map((locale) => ({
24
+ locale,
25
+ hosts: localeDomains(locale).map(normalizeHost),
26
+ defaults: locale.defaultForDomains?.map(normalizeHost) || []
27
+ }));
28
+ const matches = normalizedHost ? domains.filter((entry) => entry.hosts.includes(normalizedHost) || entry.defaults.includes(normalizedHost)) : [];
29
+ if (!matches.length)
30
+ return { _tag: "unknown", defaultLocale: i18n.defaultLocale, locales: i18n.locales };
31
+ const defaultLocale = matches.find((entry) => entry.defaults.includes(normalizedHost))?.locale || (matches.length === 1 ? matches[0].locale : void 0);
32
+ return {
33
+ _tag: "known",
34
+ defaultLocale: defaultLocale?.code || i18n.defaultLocale,
35
+ locales: domains.filter((entry) => !entry.hosts.length || entry.hosts.includes(normalizedHost)).map((entry) => entry.locale)
36
+ };
31
37
  }
32
38
  function firstLocaleDomain(locale) {
33
39
  return locale?.defaultForDomains?.[0] || locale?.domain || locale?.domains?.[0];
@@ -56,19 +62,19 @@ function resolveLocaleFromRoute(route, i18n, context = {}) {
56
62
  }
57
63
  }
58
64
  const contextLocale = context.locale ? i18n.locales.find((locale) => locale.code === context.locale) : void 0;
59
- const domainLocale = resolveLocaleFromHost(context.host, i18n);
60
- return { locale: contextLocale?.code || domainLocale?.code || i18n.defaultLocale, basePath: `${pathname}${suffix}` };
65
+ const domain = resolveI18nDomain(context.host, i18n);
66
+ const hostLocales = domain._tag === "known" ? domain.locales.filter((locale) => [...localeDomains(locale), ...locale.defaultForDomains || []].some((host) => normalizeHost(host) === normalizeHost(context.host))) : [];
67
+ const hostLocale = hostLocales.find((locale) => locale.code === domain.defaultLocale) || hostLocales[0];
68
+ return { locale: contextLocale?.code || hostLocale?.code || domain.defaultLocale, basePath: `${pathname}${suffix}` };
61
69
  }
62
70
  function localePath(basePath, locale, i18n, context = {}) {
63
71
  const { pathname, suffix } = splitRouteSuffix(basePath);
64
72
  if (i18n.strategy === "no_prefix")
65
73
  return `${pathname}${suffix}`;
66
- const isDefault = locale === i18n.defaultLocale;
67
- const localeConfig = i18n.locales.find((item) => item.code === locale);
68
- const normalizedHost = context.host ? normalizeHost(context.host) : void 0;
69
- const matchesDomainDefault = !!normalizedHost && !!localeConfig?.defaultForDomains?.some((domain) => normalizeHost(domain) === normalizedHost);
70
- const isDomainDefault = i18n.differentDomains || matchesDomainDefault;
71
- if (i18n.strategy === "prefix_except_default" && (isDefault || isDomainDefault))
74
+ const domain = resolveI18nDomain(context.host, i18n);
75
+ const isDefault = locale === domain.defaultLocale;
76
+ const isDomainDefault = i18n.differentDomains || domain._tag === "known" && isDefault;
77
+ if (i18n.strategy === "prefix_except_default" && (isDefault || i18n.differentDomains))
72
78
  return `${pathname}${suffix}`;
73
79
  if (i18n.strategy === "prefix_and_default" && isDomainDefault)
74
80
  return `${pathname}${suffix}`;
@@ -241,7 +247,8 @@ function alternatesForEntry(localePaths, params, i18n, context) {
241
247
  const untranslated = localePaths[i18n.defaultLocale];
242
248
  const alternates = [];
243
249
  const defaultLocale = i18n.locales.find((locale) => locale.code === i18n.defaultLocale);
244
- for (const l of i18n.locales) {
250
+ const locales = context.domainMode === "request" ? resolveI18nDomain(context.host, i18n).locales : i18n.locales;
251
+ for (const l of locales) {
245
252
  const pattern = localePaths[l.code] ?? untranslated;
246
253
  if (localePaths[l.code] === false)
247
254
  continue;
@@ -250,7 +257,7 @@ function alternatesForEntry(localePaths, params, i18n, context) {
250
257
  const path = fillPagePattern(pattern, params);
251
258
  if (path === null)
252
259
  continue;
253
- const domain = resolveCanonicalLocaleDomain(l, defaultLocale);
260
+ const domain = context.domainMode === "request" ? void 0 : resolveCanonicalLocaleDomain(l, defaultLocale);
254
261
  alternates.push({
255
262
  code: l.code,
256
263
  hreflang: l.hreflang || l.code,
@@ -258,7 +265,7 @@ function alternatesForEntry(localePaths, params, i18n, context) {
258
265
  ...domain ? { domain } : {}
259
266
  });
260
267
  }
261
- return alternates.length ? alternates : null;
268
+ return alternates;
262
269
  }
263
270
  function alternatesFromPages(basePath, routePath, locale, i18n, context) {
264
271
  const pages = i18n.pages;
@@ -288,9 +295,10 @@ function alternatesFromPages(basePath, routePath, locale, i18n, context) {
288
295
  matches.sort((a, b) => compareSpecificity(a.ranks, b.ranks));
289
296
  for (const match of matches) {
290
297
  if (match._tag === "unlocalized") {
291
- const defaultLocale = i18n.locales.find((locale2) => locale2.code === i18n.defaultLocale);
298
+ const defaultCode = context.domainMode === "request" ? resolveI18nDomain(context.host, i18n).defaultLocale : i18n.defaultLocale;
299
+ const defaultLocale = i18n.locales.find((locale2) => locale2.code === defaultCode);
292
300
  if (defaultLocale) {
293
- const domain = resolveCanonicalLocaleDomain(defaultLocale);
301
+ const domain = context.domainMode === "request" ? void 0 : resolveCanonicalLocaleDomain(defaultLocale);
294
302
  return [{
295
303
  code: defaultLocale.code,
296
304
  hreflang: defaultLocale.hreflang || defaultLocale.code,
@@ -300,9 +308,7 @@ function alternatesFromPages(basePath, routePath, locale, i18n, context) {
300
308
  }
301
309
  continue;
302
310
  }
303
- const alternates = alternatesForEntry(match.localePaths, match.params, i18n, context);
304
- if (alternates)
305
- return alternates;
311
+ return alternatesForEntry(match.localePaths, match.params, i18n, context);
306
312
  }
307
313
  return null;
308
314
  }
@@ -320,8 +326,8 @@ function resolveLocaleAlternates(route, i18n, context = {}) {
320
326
  const defaultLocale = i18n.locales.find((locale2) => locale2.code === i18n.defaultLocale);
321
327
  return {
322
328
  _tag: "strategy",
323
- alternates: i18n.locales.map((l) => {
324
- const domain = resolveCanonicalLocaleDomain(l, defaultLocale);
329
+ alternates: (context.domainMode === "request" ? resolveI18nDomain(context.host, i18n).locales : i18n.locales).map((l) => {
330
+ const domain = context.domainMode === "request" ? void 0 : resolveCanonicalLocaleDomain(l, defaultLocale);
325
331
  return {
326
332
  code: l.code,
327
333
  hreflang: l.hreflang || l.code,
@@ -335,4 +341,4 @@ function computeLocaleAlternates(route, i18n, context = {}) {
335
341
  return resolveLocaleAlternates(route, i18n, context).alternates;
336
342
  }
337
343
 
338
- export { computeLocaleAlternates, localePath, resolveCanonicalLocaleDomain, resolveLocaleAlternates, resolveLocaleFromRoute };
344
+ export { computeLocaleAlternates, localePath, resolveCanonicalLocaleDomain, resolveI18nDomain, resolveLocaleAlternates, resolveLocaleFromRoute };
package/dist/module.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "compatibility": {
5
5
  "nuxt": ">=3.16.0"
6
6
  },
7
- "version": "5.3.14",
7
+ "version": "5.3.15",
8
8
  "builder": {
9
9
  "@nuxt/module-builder": "1.0.3",
10
10
  "unbuild": "3.6.1"
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nuxtseo-shared",
3
3
  "type": "module",
4
- "version": "5.3.14",
4
+ "version": "5.3.15",
5
5
  "description": "Shared utilities for Nuxt SEO modules.",
6
6
  "author": {
7
7
  "name": "Harlan Wilton",