basklee-react 0.9.11 → 0.9.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/README.md CHANGED
@@ -522,7 +522,7 @@ maintained API client. Page metadata uses those returned records and follows
522
522
  client navigation and language selection. Home-page event clicks open enrollment
523
523
  for the returned event, preserving its return path through sign-in.
524
524
  A product page takes the product's SEO tab: its heading is the page title (the product name without one), its meta description and keywords fill their tags, and its own Open Graph and Twitter Cards tags are added in the page's language only, replacing a default tag of the same name.
525
- Pages with a canonical address also link the same page in the other storefront languages (`hreflang`) and set `og:type` and `og:locale`; a journal article adds its publication date (`article:published_time`) and schema.org `BlogPosting` data. The store icon (`logos.ico`) is the page's `<link rel="icon">`. An unknown `?locale` opens the default language.
525
+ Pages with a canonical address also link the same page in the other storefront languages (`hreflang`) and set `og:type` and `og:locale`; a journal article adds its publication date (`article:published_time`) and schema.org `BlogPosting` data. The store icon (`logos.ico`) is the page's `<link rel="icon">`. An unknown `?locale` opens the language closest to the browser's, else the first one. A first visit without `?locale` opens in the storefront language closest to the browser's languages (`Accept-Language`; `navigator.languages` in the app build): the same code first, then the same language (`ru-RU` finds `ru`, `en` finds `en-GB`); the first available language when none of them is offered.
526
526
 
527
527
  List pages (journal, categories, stores) link their previous and next pages as real links (`?page=2`). Each later page has its own canonical address and a title with its page number; a journal page past the last one answers 404.
528
528
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "basklee-react",
3
- "version": "0.9.11",
3
+ "version": "0.9.12",
4
4
  "description": "Next.js 16 storefront for your Basklee marketplace: setup CLI, server API gateway, typed store and ready-made catalog, cart, checkout and account components in English, Russian and Spanish, plus an Android app build. Powered by Basklee: https://basklee.com",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -40,6 +40,59 @@ export function localesMatch(left: string, right: string): boolean {
40
40
  return normalize(left) === normalize(right);
41
41
  }
42
42
 
43
+ /**
44
+ * The languages a customer's browser asks for, most wanted first: an
45
+ * Accept-Language header («ru-RU,ru;q=0.9,en;q=0.8») or navigator.languages.
46
+ */
47
+ export function wantedLanguages(
48
+ source: string | readonly string[] | null | undefined,
49
+ ): string[] {
50
+ if (!source) {
51
+ return [];
52
+ }
53
+ if (typeof source !== 'string') {
54
+ return source.map((tag) => tag.trim()).filter(Boolean);
55
+ }
56
+ return source
57
+ .split(',')
58
+ .map((part, index) => {
59
+ const [tag = '', ...parameters] = part.split(';');
60
+ const weight = parameters
61
+ .map((parameter) => /^q=([0-9.]+)$/i.exec(parameter.trim()))
62
+ .find(Boolean);
63
+ return { tag: tag.trim(), weight: weight ? Number(weight[1]) : 1, index };
64
+ })
65
+ .filter((item) => item.tag && item.tag !== '*' && item.weight > 0)
66
+ .sort((left, right) => right.weight - left.weight || left.index - right.index)
67
+ .map((item) => item.tag);
68
+ }
69
+
70
+ /**
71
+ * The shop's language closest to what the customer wants, in the customer's
72
+ * order: the same tag first (pt_BR = pt-BR), then the same language (ru-RU
73
+ * finds ru, en finds en-GB). Nothing when no wanted language is offered.
74
+ */
75
+ export function closestLocale(
76
+ available: readonly string[],
77
+ wanted: readonly string[],
78
+ ): string | undefined {
79
+ const language = (tag: string) =>
80
+ tag.trim().toLowerCase().split(/[-_]/)[0] ?? '';
81
+ for (const tag of wanted) {
82
+ const same = available.find((code) => localesMatch(code, tag));
83
+ if (same) {
84
+ return same;
85
+ }
86
+ const kin = language(tag)
87
+ ? available.find((code) => language(code) === language(tag))
88
+ : undefined;
89
+ if (kin) {
90
+ return kin;
91
+ }
92
+ }
93
+ return undefined;
94
+ }
95
+
43
96
  export function getAvailableValueFromObject<T>(
44
97
  values: Record<string, T> | null | undefined,
45
98
  locale: string,
@@ -3,6 +3,7 @@ import { headers } from 'next/headers';
3
3
  import { getBaskleeAppLoad } from '@/lib/basklee/appLoadServer';
4
4
  import { createBaskleeSelectionsStore } from '@/stores/baskleeSelections';
5
5
  import { storefrontErrorLocale } from '@/lib/basklee/errorPageLabels';
6
+ import { closestLocale, wantedLanguages } from '@/lib/basklee/presentation';
6
7
 
7
8
  export const getStorefrontRequestUrl = cache(async () => {
8
9
  const incoming = await headers();
@@ -19,11 +20,21 @@ export const getBaskleeServerLocale = cache(async () => {
19
20
  if (!result) {
20
21
  return storefrontErrorLocale(requested, undefined);
21
22
  }
22
- const selections = createBaskleeSelectionsStore(
23
- result?.response.page_data.init ?? null,
23
+ const init = result.response.page_data.init;
24
+ const selections = createBaskleeSelectionsStore(init ?? null);
25
+ // ?locale= first, then the languages the browser asks for
26
+ // (Accept-Language); the storefront's first language when none is offered.
27
+ const wanted = closestLocale(
28
+ Object.entries(init?.locale ?? {}).map(
29
+ ([key, value]) => value.code?.trim() || key,
30
+ ),
31
+ [
32
+ requested ?? '',
33
+ ...wantedLanguages((await headers()).get('accept-language')),
34
+ ].filter(Boolean),
24
35
  );
25
- if (requested) {
26
- selections.getState().setLocale(requested);
36
+ if (wanted) {
37
+ selections.getState().setLocale(wanted);
27
38
  }
28
39
  return selections.getState().localeCode || 'en';
29
40
  });
@@ -1,5 +1,6 @@
1
1
  import { createStore } from 'zustand/vanilla';
2
2
  import type { IAppLoadCommon } from '@/interfaces/basklee';
3
+ import { closestLocale } from '@/lib/basklee/presentation';
3
4
 
4
5
  export interface BaskleeSelectionsState {
5
6
  localeCode: string;
@@ -96,7 +97,18 @@ export function createBaskleeSelectionsStore(
96
97
  }
97
98
 
98
99
  const previous = get();
100
+ // A language still offered stays; in the browser (the app build loads
101
+ // there) the one closest to navigator.languages; else the first one.
102
+ const browser = typeof window === 'undefined'
103
+ ? undefined
104
+ : closestLocale(
105
+ Object.entries(next.locale).map(
106
+ ([key, value]) => value.code?.trim() || key,
107
+ ),
108
+ window.navigator.languages ?? [],
109
+ );
99
110
  const selected = findLocale(next, previous.localeCode) ??
111
+ (browser ? findLocale(next, browser) : undefined) ??
100
112
  Object.entries(next.locale)[0];
101
113
  const localeCode = selected?.[1].code?.trim() || selected?.[0] || '';
102
114
  const preferredCurrency = next.currencies[previous.currencyId]