@revenexx/cover 0.13.0 → 0.14.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.
- package/app/api/product.ts +1 -0
- package/app/components/layout/header/LocaleSwitcher.vue +18 -1
- package/app/composables/useProductLocales.ts +56 -0
- package/app/layouts/default.vue +10 -1
- package/app/shared/productLocales.ts +109 -0
- package/i18n/locales/de/common.json +2 -1
- package/i18n/locales/en/common.json +2 -1
- package/package.json +1 -1
- package/server/api/product/[id]/locales.get.ts +57 -0
- package/server/interfaces/product.ts +8 -0
- package/server/services/ApiProductService.ts +4 -0
- package/server/utils/liveCatalog.ts +69 -0
package/app/api/product.ts
CHANGED
|
@@ -6,10 +6,16 @@
|
|
|
6
6
|
* carry region + currency; languages show their native full names (never
|
|
7
7
|
* flags or codes) and are limited to the locales the selected market
|
|
8
8
|
* offers. The trigger shows the current combination prominently.
|
|
9
|
+
*
|
|
10
|
+
* On a product page a language the product has no page in (PE-859 hides
|
|
11
|
+
* untranslated products) still switches the language, but lands on the
|
|
12
|
+
* product's category instead of a 404; a hint under the select says so.
|
|
9
13
|
*/
|
|
10
14
|
const { icon } = useIcons();
|
|
11
15
|
const { t, locale, locales } = useI18n();
|
|
12
16
|
const switchLocalePath = useSwitchLocalePath();
|
|
17
|
+
const localePath = useLocalePath();
|
|
18
|
+
const { hasPage, fallbackPath } = useProductLocales();
|
|
13
19
|
const { markets } = useMarkets();
|
|
14
20
|
const { market, setMarket, region, currencies, currency, currencySymbol, setCurrency } = useLocalePreferences(markets);
|
|
15
21
|
|
|
@@ -29,11 +35,16 @@ const languageItems = computed(() => {
|
|
|
29
35
|
code: entry.code,
|
|
30
36
|
/** Native full name ("Deutsch", "English") — never flags or codes. */
|
|
31
37
|
name: entry.name ?? entry.code,
|
|
32
|
-
to: switchLocalePath(entry.code),
|
|
38
|
+
to: hasPage(entry.code) ? switchLocalePath(entry.code) : localePath(fallbackPath.value, entry.code),
|
|
33
39
|
active: entry.code === locale.value,
|
|
40
|
+
hasPage: hasPage(entry.code),
|
|
34
41
|
}));
|
|
35
42
|
});
|
|
36
43
|
|
|
44
|
+
/** Languages the open product is missing in, by name — empty off a product page. */
|
|
45
|
+
const languagesWithoutProduct = computed(() =>
|
|
46
|
+
languageItems.value.filter(item => !item.hasPage && !item.active).map(item => item.name));
|
|
47
|
+
|
|
37
48
|
const currentLanguageName = computed(() =>
|
|
38
49
|
languageItems.value.find(item => item.active)?.name ?? locale.value);
|
|
39
50
|
|
|
@@ -121,6 +132,12 @@ const triggerLabel = computed(() =>
|
|
|
121
132
|
class="w-full"
|
|
122
133
|
@update:model-value="(code: string) => onLanguageChange(code)"
|
|
123
134
|
/>
|
|
135
|
+
<template
|
|
136
|
+
v-if="languagesWithoutProduct.length"
|
|
137
|
+
#help
|
|
138
|
+
>
|
|
139
|
+
{{ t('topbar.localePanel.productNotTranslated', { languages: languagesWithoutProduct.join(', ') }) }}
|
|
140
|
+
</template>
|
|
124
141
|
</UFormField>
|
|
125
142
|
|
|
126
143
|
<!-- Currency: chosen within the currencies the market trades in -->
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { productApi } from "../api/product";
|
|
2
|
+
import { normalizeLocaleCode } from "../shared/locale";
|
|
3
|
+
import { productFallbackPath, productRouteCategorySegments } from "../shared/productLocales";
|
|
4
|
+
import { routeSegments, useRouteProductSubject } from "./useRouteSubject";
|
|
5
|
+
|
|
6
|
+
interface ProductLocalesResponse {
|
|
7
|
+
locales: string[] | null;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Which storefront locales the product on this page has a page in.
|
|
12
|
+
*
|
|
13
|
+
* With `search_untranslated=hide` (PE-859) a product exists only in the
|
|
14
|
+
* languages it is translated into, so the language switcher and the hreflang
|
|
15
|
+
* alternates must not link it into the others. Both call this; they share one
|
|
16
|
+
* request through the `product-locales:<id>` key, and the BFF caches the
|
|
17
|
+
* answer per product.
|
|
18
|
+
*
|
|
19
|
+
* `available` is `null` off a product page or when the lookup failed: every
|
|
20
|
+
* locale is linked then, exactly as before. Only a product page narrows.
|
|
21
|
+
*/
|
|
22
|
+
export function useProductLocales() {
|
|
23
|
+
const route = useRoute();
|
|
24
|
+
const { id } = useRouteProductSubject();
|
|
25
|
+
const { locales } = useI18n();
|
|
26
|
+
const requestFetch = useRequestFetch();
|
|
27
|
+
|
|
28
|
+
const codes = computed(() => locales.value.map(entry => entry.code).join(","));
|
|
29
|
+
|
|
30
|
+
const { data } = useAsyncData<ProductLocalesResponse | null>(
|
|
31
|
+
() => `product-locales:${id.value}`,
|
|
32
|
+
() => (id.value && codes.value
|
|
33
|
+
? requestFetch<ProductLocalesResponse>(productApi.locales(id.value), { query: { locales: codes.value } })
|
|
34
|
+
.catch(() => null)
|
|
35
|
+
: Promise.resolve(null)),
|
|
36
|
+
{ default: () => null },
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
const available = computed<string[] | null>(() => {
|
|
40
|
+
const list = id.value ? data.value?.locales : null;
|
|
41
|
+
return list ? list.map(normalizeLocaleCode) : null;
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
/** Whether the product has a page in this locale (always true off a product page). */
|
|
45
|
+
function hasPage(code: string): boolean {
|
|
46
|
+
return available.value?.includes(normalizeLocaleCode(code)) ?? true;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Where a locale without the product goes instead: its category listing, or home. */
|
|
50
|
+
const fallbackPath = computed(() => productFallbackPath(productRouteCategorySegments(
|
|
51
|
+
routeSegments(route.params.category),
|
|
52
|
+
routeSegments(route.params.rest),
|
|
53
|
+
)));
|
|
54
|
+
|
|
55
|
+
return { available, hasPage, fallbackPath };
|
|
56
|
+
}
|
package/app/layouts/default.vue
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
<script setup lang="ts">
|
|
2
|
-
|
|
2
|
+
import { filterLocaleHeadAlternates } from "../shared/productLocales";
|
|
3
|
+
|
|
4
|
+
const localeHead = useLocaleHead({ seo: true });
|
|
5
|
+
// A product page links only the languages the product has a page in
|
|
6
|
+
// (PE-859 hides untranslated products); every other page is unchanged.
|
|
7
|
+
const { available: productLocales } = useProductLocales();
|
|
8
|
+
const head = computed(() => ({
|
|
9
|
+
...localeHead.value,
|
|
10
|
+
...filterLocaleHeadAlternates(localeHead.value, productLocales.value),
|
|
11
|
+
}));
|
|
3
12
|
const { navigateToSearch } = useSearch();
|
|
4
13
|
const route = useRoute();
|
|
5
14
|
const cart = useCartStore();
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which storefront languages a product has a page in, and what that does to
|
|
3
|
+
* the links pointing at those pages.
|
|
4
|
+
*
|
|
5
|
+
* With `search_untranslated=hide` (baseline PE-859) a product only has search
|
|
6
|
+
* documents in the languages it is translated into; the tenant's main
|
|
7
|
+
* language is always there. The product page looks its document up under the
|
|
8
|
+
* request language's keys (`searchLocaleKeys`), so a language without such a
|
|
9
|
+
* document is a 404. The language switcher and the hreflang alternates must
|
|
10
|
+
* not send anyone there.
|
|
11
|
+
*
|
|
12
|
+
* Pure functions, shared by the BFF (which reads the documents) and the
|
|
13
|
+
* client (which filters the links), so both decide availability the same way.
|
|
14
|
+
*/
|
|
15
|
+
import { localeLanguage, normalizeLocaleCode, searchLocaleKeys } from "./locale";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The languages (of `languages`, in their order) that have a document among
|
|
19
|
+
* `docLocales`. A language counts as available exactly when the product page
|
|
20
|
+
* would find a document for it: one whose locale is one of the language's
|
|
21
|
+
* search keys (`de` → `de-DE` or `de`). Both spellings match in both
|
|
22
|
+
* directions, so this holds before, during and after the full-code migration
|
|
23
|
+
* (PE-851). A `de-AT` document does not make `de` available, because the
|
|
24
|
+
* page would not find it either.
|
|
25
|
+
*/
|
|
26
|
+
export function languagesWithDocuments(
|
|
27
|
+
docLocales: readonly string[],
|
|
28
|
+
languages: readonly string[],
|
|
29
|
+
): string[] {
|
|
30
|
+
const present = new Set(docLocales.filter(Boolean).map(normalizeLocaleCode));
|
|
31
|
+
return languages.filter(language =>
|
|
32
|
+
searchLocaleKeys(language).some(key => present.has(key)));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The listing a language without the product sends the buyer to: the
|
|
37
|
+
* product's own category, as deep as a listing URL goes
|
|
38
|
+
* (`/category/<category>/<subcategory>`). The `_` placeholder of an
|
|
39
|
+
* uncategorised product is not a category, so that case (and any URL without
|
|
40
|
+
* a category) goes to the home page instead. Category codes are not
|
|
41
|
+
* translated, so the same path exists in every language.
|
|
42
|
+
*/
|
|
43
|
+
export function productFallbackPath(categorySegments: readonly string[]): string {
|
|
44
|
+
const path = categorySegments.filter(segment => segment && segment !== "_").slice(0, 2);
|
|
45
|
+
return path.length ? `/category/${path.map(encodeURIComponent).join("/")}` : "/";
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** The category segments of a `/category/<…>/product/<slug>` route. */
|
|
49
|
+
export function productRouteCategorySegments(category: readonly string[], rest: readonly string[]): string[] {
|
|
50
|
+
const marker = rest.lastIndexOf("product");
|
|
51
|
+
return [...category, ...(marker >= 0 ? rest.slice(0, marker) : [])];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The parts of a `useLocaleHead()` result this module touches. */
|
|
55
|
+
export interface LocaleHeadLink {
|
|
56
|
+
id?: string;
|
|
57
|
+
rel?: string;
|
|
58
|
+
href?: string;
|
|
59
|
+
hreflang?: string;
|
|
60
|
+
[key: string]: unknown;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface LocaleHeadMeta {
|
|
64
|
+
id?: string;
|
|
65
|
+
property?: string;
|
|
66
|
+
content?: string;
|
|
67
|
+
[key: string]: unknown;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The locale head with alternates only for the languages that have the page.
|
|
72
|
+
*
|
|
73
|
+
* `available` is the list of available storefront locale codes, or `null`
|
|
74
|
+
* when it is not known (not a product page, or the lookup failed) — then the
|
|
75
|
+
* head is returned unchanged, which is what every other page gets.
|
|
76
|
+
*
|
|
77
|
+
* Dropped: `rel="alternate"` links whose hreflang names an unavailable
|
|
78
|
+
* language, `x-default` unless its URL is one of the alternates that stay
|
|
79
|
+
* (it points at the default locale's URL, which may be one of the dropped
|
|
80
|
+
* ones), and `og:locale:alternate` metas for unavailable languages. Canonical
|
|
81
|
+
* and `og:locale` describe the current page and stay. Matching is by language
|
|
82
|
+
* subtag, the same unit availability is decided in.
|
|
83
|
+
*/
|
|
84
|
+
export function filterLocaleHeadAlternates<L extends LocaleHeadLink, M extends LocaleHeadMeta>(
|
|
85
|
+
head: { link?: L[]; meta?: M[] },
|
|
86
|
+
available: readonly string[] | null,
|
|
87
|
+
): { link: L[]; meta: M[] } {
|
|
88
|
+
const link = head.link ?? [];
|
|
89
|
+
const meta = head.meta ?? [];
|
|
90
|
+
if (!available) {
|
|
91
|
+
return { link, meta };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const languages = new Set(available.map(localeLanguage));
|
|
95
|
+
const offered = (code: string): boolean => languages.has(localeLanguage(code));
|
|
96
|
+
const isAlternate = (entry: L): boolean => entry.rel === "alternate" && Boolean(entry.hreflang);
|
|
97
|
+
|
|
98
|
+
const kept = link.filter(entry =>
|
|
99
|
+
!isAlternate(entry) || entry.hreflang === "x-default" || offered(entry.hreflang as string));
|
|
100
|
+
const keptHrefs = new Set(kept
|
|
101
|
+
.filter(entry => isAlternate(entry) && entry.hreflang !== "x-default")
|
|
102
|
+
.map(entry => entry.href));
|
|
103
|
+
|
|
104
|
+
return {
|
|
105
|
+
link: kept.filter(entry => !isAlternate(entry) || entry.hreflang !== "x-default" || keptHrefs.has(entry.href)),
|
|
106
|
+
meta: meta.filter(entry =>
|
|
107
|
+
entry.property !== "og:locale:alternate" || !entry.content || offered(entry.content)),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
@@ -81,7 +81,8 @@
|
|
|
81
81
|
"region": "Land",
|
|
82
82
|
"currency": "Währung",
|
|
83
83
|
"regionPlaceholder": "Land suchen …",
|
|
84
|
-
"hint": "Währung und Sortiment richten sich nach dem gewählten Land."
|
|
84
|
+
"hint": "Währung und Sortiment richten sich nach dem gewählten Land.",
|
|
85
|
+
"productNotTranslated": "Diesen Artikel gibt es nicht auf {languages}. Ein Wechsel dorthin führt zu seiner Kategorie oder, wenn er keine hat, zur Startseite."
|
|
85
86
|
}
|
|
86
87
|
},
|
|
87
88
|
"header": {
|
|
@@ -81,7 +81,8 @@
|
|
|
81
81
|
"region": "Country",
|
|
82
82
|
"currency": "Currency",
|
|
83
83
|
"regionPlaceholder": "Search country…",
|
|
84
|
-
"hint": "Currency and catalog follow the selected country."
|
|
84
|
+
"hint": "Currency and catalog follow the selected country.",
|
|
85
|
+
"productNotTranslated": "This product is not available in {languages}. Switching there takes you to its category, or to the home page if it has none."
|
|
85
86
|
}
|
|
86
87
|
},
|
|
87
88
|
"header": {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@revenexx/cover",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"description": "Cover — revenexx design system for Nuxt. Distributed as a Nuxt layer: generic UI components, theming tokens and stores shared by the demo shop, custom storefronts and the Blokkli theme.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./nuxt.config.ts",
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { normalizeLocaleCode } from "../../../../app/shared/locale";
|
|
2
|
+
|
|
3
|
+
/** What a product page may link to in other languages. */
|
|
4
|
+
export interface ProductLocalesResponse {
|
|
5
|
+
/**
|
|
6
|
+
* The requested storefront locales the product has a page in, or `null`
|
|
7
|
+
* when that is not known — the storefront then links every language, as
|
|
8
|
+
* on any other page.
|
|
9
|
+
*/
|
|
10
|
+
locales: string[] | null;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** A locale code as the storefront writes it (`de`, `en-US`). */
|
|
14
|
+
const LOCALE_CODE = /^[a-z]{2,3}(-[A-Za-z0-9]{2,8})*$/;
|
|
15
|
+
|
|
16
|
+
/** More languages than any storefront offers: a bound on the lookup, not a limit. */
|
|
17
|
+
const MAX_LOCALES = 20;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The storefront locales `?locales=de,en` (the shop's own i18n locales) that
|
|
21
|
+
* this product has a page in. The language switcher and the hreflang
|
|
22
|
+
* alternates use it so a product is never linked into a language it has no
|
|
23
|
+
* document in (`search_untranslated=hide`, PE-859).
|
|
24
|
+
*
|
|
25
|
+
* Soft-failing: the answer only narrows links, so a failed lookup degrades to
|
|
26
|
+
* `null` (link everything, as before) instead of breaking the product page.
|
|
27
|
+
*/
|
|
28
|
+
export default defineEventHandler(async (event): Promise<ProductLocalesResponse> => {
|
|
29
|
+
const id = getRouterParam(event, "id") ?? "";
|
|
30
|
+
if (!id) {
|
|
31
|
+
throw createError({ statusCode: 400, message: "Invalid product id" });
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const raw = getQuery(event).locales;
|
|
35
|
+
const requested = [...new Set(
|
|
36
|
+
(Array.isArray(raw) ? raw.join(",") : String(raw ?? ""))
|
|
37
|
+
.split(",")
|
|
38
|
+
.map(code => normalizeLocaleCode(code))
|
|
39
|
+
.filter(code => LOCALE_CODE.test(code)),
|
|
40
|
+
)].slice(0, MAX_LOCALES);
|
|
41
|
+
if (!requested.length) {
|
|
42
|
+
throw createError({ statusCode: 400, message: "No locales given" });
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const service = getProductService(event);
|
|
46
|
+
if (!service.getProductLanguages) {
|
|
47
|
+
return { locales: null };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
try {
|
|
51
|
+
return { locales: await service.getProductLanguages(id, requested) };
|
|
52
|
+
}
|
|
53
|
+
catch (err) {
|
|
54
|
+
getLogService().warn("Product locales lookup failed", { id, ...toErrorContext(err) });
|
|
55
|
+
return { locales: null };
|
|
56
|
+
}
|
|
57
|
+
});
|
|
@@ -32,4 +32,12 @@ export interface IProductService {
|
|
|
32
32
|
* renders no associations section.
|
|
33
33
|
*/
|
|
34
34
|
getProductAssociations(id: string, locale?: Locale): Promise<ProductAssociations | null>;
|
|
35
|
+
/**
|
|
36
|
+
* The storefront languages (of `languages`) this product has a page in. A
|
|
37
|
+
* catalogue that hides untranslated products (`search_untranslated=hide`)
|
|
38
|
+
* has none in a language the product is not translated into. Optional: a
|
|
39
|
+
* service without it has the product in every language, which is what the
|
|
40
|
+
* storefront assumes when it is missing.
|
|
41
|
+
*/
|
|
42
|
+
getProductLanguages?(id: string, languages: readonly string[]): Promise<string[]>;
|
|
35
43
|
}
|
|
@@ -51,6 +51,10 @@ export class ApiProductService implements IProductService {
|
|
|
51
51
|
return doc ? mapLiveDocToDetail(doc, locale) : null;
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
+
async getProductLanguages(id: string, languages: readonly string[]): Promise<string[]> {
|
|
55
|
+
return getLiveProductLanguages(id, languages);
|
|
56
|
+
}
|
|
57
|
+
|
|
54
58
|
/**
|
|
55
59
|
* Model siblings from the products app's REST rows — the search index has
|
|
56
60
|
* no parent link, so this is the one read that bypasses it.
|
|
@@ -14,6 +14,7 @@ import type {
|
|
|
14
14
|
ProductVariants,
|
|
15
15
|
} from "../../app/interfaces/product-variants";
|
|
16
16
|
import { pickLocalizedLabel, searchLocaleKeys } from "../../app/shared/locale";
|
|
17
|
+
import { languagesWithDocuments } from "../../app/shared/productLocales";
|
|
17
18
|
import type { Locale } from "./locale";
|
|
18
19
|
|
|
19
20
|
/**
|
|
@@ -73,6 +74,8 @@ export interface LiveSearchParams {
|
|
|
73
74
|
perPage?: number;
|
|
74
75
|
/** Facet values per field. Typesense returns only 10 when unset. */
|
|
75
76
|
maxFacetValues?: number;
|
|
77
|
+
/** Only these document fields in the hits (`include_fields`). */
|
|
78
|
+
includeFields?: string;
|
|
76
79
|
}
|
|
77
80
|
|
|
78
81
|
/** Where the platform's CDN is mounted, on every storefront domain. */
|
|
@@ -208,6 +211,7 @@ export async function searchLiveProductsMulti(
|
|
|
208
211
|
...(p.facetBy ? { facet_by: p.facetBy } : {}),
|
|
209
212
|
...(p.maxFacetValues ? { max_facet_values: p.maxFacetValues } : {}),
|
|
210
213
|
...(p.sortBy ? { sort_by: p.sortBy } : {}),
|
|
214
|
+
...(p.includeFields ? { include_fields: p.includeFields } : {}),
|
|
211
215
|
page: p.page ?? 1,
|
|
212
216
|
per_page: p.perPage ?? 24,
|
|
213
217
|
})),
|
|
@@ -393,6 +397,71 @@ export async function getLiveProductDoc(entityId: string, locale: Locale): Promi
|
|
|
393
397
|
return preferLocaleDocs(docs, locale)[0] ?? null;
|
|
394
398
|
}
|
|
395
399
|
|
|
400
|
+
/**
|
|
401
|
+
* `id:=[…]` over every document id one product may carry in any of these
|
|
402
|
+
* locales, each id once: `42` in `de`, `en` → `42:de-DE`, `42:de`, `42:en-US`,
|
|
403
|
+
* `42:en`.
|
|
404
|
+
*/
|
|
405
|
+
export function liveDocIdClauseForLocales(entityId: string, locales: readonly string[]): string {
|
|
406
|
+
const keys = [...new Set(locales.flatMap(locale => searchLocaleKeys(locale)))];
|
|
407
|
+
return `id:=[${keys.map(key => tsFilterValue(`${entityId}:${key}`)).join(",")}]`;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
const PRODUCT_LANGUAGES_TTL_MS = 5 * 60 * 1000;
|
|
411
|
+
const PRODUCT_LANGUAGES_MAX_ENTRIES = 5000;
|
|
412
|
+
|
|
413
|
+
// Partitioned by tenant like the category cache. Translations change rarely
|
|
414
|
+
// and every product page asks (switcher and hreflang), so a short TTL keeps
|
|
415
|
+
// it to one search per product and tenant every few minutes.
|
|
416
|
+
const productLanguagesCache = new Map<string, { languages: string[]; loadedAt: number }>();
|
|
417
|
+
|
|
418
|
+
/** Clears the per-product language cache — tests only. */
|
|
419
|
+
export function resetProductLanguagesCache(): void {
|
|
420
|
+
productLanguagesCache.clear();
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* The storefront languages (of `languages`) this product has a page in.
|
|
425
|
+
*
|
|
426
|
+
* ONE request across every language: a lookup by document id over all their
|
|
427
|
+
* keys. It goes through multi_search, which does not pin a locale (the query
|
|
428
|
+
* endpoint does, via X-Revenexx-Locale, and so could only answer for one
|
|
429
|
+
* language per request). Only `id` and `locale` come back.
|
|
430
|
+
*/
|
|
431
|
+
export async function getLiveProductLanguages(entityId: string, languages: readonly string[]): Promise<string[]> {
|
|
432
|
+
if (!entityId || !languages.length) {
|
|
433
|
+
return [];
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
const cacheKey = `${revenexxTenantKey()}|${entityId}|${languages.join(",")}`;
|
|
437
|
+
const cached = productLanguagesCache.get(cacheKey);
|
|
438
|
+
if (cached && Date.now() - cached.loadedAt < PRODUCT_LANGUAGES_TTL_MS) {
|
|
439
|
+
return cached.languages;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
const keyCount = new Set(languages.flatMap(language => searchLocaleKeys(language))).size;
|
|
443
|
+
const result = await searchLiveProducts({
|
|
444
|
+
q: "*",
|
|
445
|
+
queryBy: "name",
|
|
446
|
+
filterBy: liveDocIdClauseForLocales(entityId, languages),
|
|
447
|
+
includeFields: "id,locale",
|
|
448
|
+
perPage: keyCount,
|
|
449
|
+
});
|
|
450
|
+
const docLocales = (result.hits ?? []).map(({ document }) =>
|
|
451
|
+
document.locale || document.id.slice(document.id.lastIndexOf(":") + 1));
|
|
452
|
+
const available = languagesWithDocuments(docLocales, languages);
|
|
453
|
+
|
|
454
|
+
if (productLanguagesCache.size >= PRODUCT_LANGUAGES_MAX_ENTRIES) {
|
|
455
|
+
// Oldest first (insertion order) — a bound, not an LRU.
|
|
456
|
+
const oldest = productLanguagesCache.keys().next().value;
|
|
457
|
+
if (oldest !== undefined) {
|
|
458
|
+
productLanguagesCache.delete(oldest);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
productLanguagesCache.set(cacheKey, { languages: available, loadedAt: Date.now() });
|
|
462
|
+
return available;
|
|
463
|
+
}
|
|
464
|
+
|
|
396
465
|
// ---------------------------------------------------------------------------
|
|
397
466
|
// Labels — every localized name the catalogue carries (categories, attributes,
|
|
398
467
|
// select options, association types) arrives as the same `labels` map, so it is
|