orionplace-react 0.5.0 → 0.6.1

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.
Files changed (30) hide show
  1. package/README.md +10 -1
  2. package/bin/orionplace-react.mjs +7 -0
  3. package/package.json +1 -1
  4. package/runtime/marketplace.d.mts +97 -0
  5. package/runtime/marketplace.mjs +519 -3
  6. package/src/project-readme.mjs +23 -2
  7. package/src/setup.mjs +1 -0
  8. package/src/sitemap.mjs +93 -0
  9. package/templates/app/_storefront/CategoryPage.tsx +6 -2
  10. package/templates/app/_storefront/SearchPage.tsx +7 -2
  11. package/templates/app/_storefront/SellersPage.tsx +9 -3
  12. package/templates/app/_storefront/storefrontRoutes.tsx +31 -10
  13. package/templates/app/_storefront/useProductSearch.ts +97 -26
  14. package/templates/app/robots.txt/route.ts +11 -0
  15. package/templates/app/sitemap.xml/route.ts +12 -0
  16. package/templates/components/orionplace/account/ProfileDetailsForm.README.md +4 -0
  17. package/templates/components/orionplace/account/ProfileDetailsForm.tsx +7 -2
  18. package/templates/components/orionplace/cart/CartCheckoutDetails.README.md +4 -0
  19. package/templates/components/orionplace/cart/CartCheckoutDetails.tsx +20 -9
  20. package/templates/interfaces/orionplace/accounts/IProfileData.ts +2 -0
  21. package/templates/interfaces/orionplace/appBase/IApploadUserProfile.ts +2 -0
  22. package/templates/lib/orionplace/cartLabels.ts +2 -0
  23. package/templates/lib/orionplace/pageMetadata.ts +24 -4
  24. package/templates/lib/orionplace/pageSeo.ts +68 -0
  25. package/templates/lib/orionplace/productListLocation.ts +130 -0
  26. package/templates/lib/orionplace/productSearch.ts +48 -0
  27. package/templates/lib/orionplace/profile.ts +12 -0
  28. package/templates/lib/orionplace/profileLabels.ts +2 -0
  29. package/templates/lib/orionplace/serverMetadata.ts +7 -0
  30. package/templates/proxy.ts +8 -2
@@ -282,3 +282,71 @@ export function storefrontPageMetadata(
282
282
  keywords: plainText(values.keywords ?? '') || `${title}, ${copy[2]}`,
283
283
  };
284
284
  }
285
+
286
+ /*
287
+ * Addresses for search engines and link previews. The public origin is the
288
+ * storefront host the visitor used (the proxy's Host or X-Forwarded-Host),
289
+ * over HTTPS; a local host keeps HTTP for development.
290
+ */
291
+ export function storefrontPublicOrigin(host: string | null | undefined): string {
292
+ const name = (host ?? '').split(',')[0]!.trim().toLowerCase();
293
+ if (!/^(?:[a-z0-9-]+(?:\.[a-z0-9-]+)*|\[[0-9a-f:]+\])(?::\d{1,5})?$/.test(name)) {
294
+ return '';
295
+ }
296
+ const local = /^(?:localhost|127(?:\.\d{1,3}){3}|\[::1\])(?::\d+)?$/.test(name);
297
+ return `${local ? 'http' : 'https'}://${name}`;
298
+ }
299
+
300
+ /**
301
+ * A complete address for a link preview image: a storefront path such as
302
+ * `/ps3/blog/posts/images/5` gets the public origin; without an origin it is
303
+ * left out, since a relative og:image is not loaded by link previews.
304
+ */
305
+ export function storefrontAbsoluteUrl(
306
+ value: string | null | undefined,
307
+ origin: string,
308
+ ): string | undefined {
309
+ if (!value) {
310
+ return undefined;
311
+ }
312
+ try {
313
+ const url = origin ? new URL(value, `${origin}/`) : new URL(value);
314
+ return /^https?:$/.test(url.protocol) ? url.toString() : undefined;
315
+ } catch {
316
+ return undefined;
317
+ }
318
+ }
319
+
320
+ // The pages the sitemap lists (runtime/marketplace.mjs), plus plan pages.
321
+ const indexedPage = /^\/(?:|catalog|journal|tariffs|manufacturers|sellers|stores|contact|privacy|(?:category|product|journal)\/\d+|tariffs\/[^/]+\/\d+)$/;
322
+
323
+ /**
324
+ * The canonical address of a page: the sitemap's address for it, the page in
325
+ * the language it shows (`?locale=`). Pages that are not listed, pages with
326
+ * other query parameters (filters, page numbers, private keys) and noindex
327
+ * pages get none.
328
+ */
329
+ export function storefrontCanonicalUrl(
330
+ origin: string,
331
+ pathname: string,
332
+ search: string,
333
+ locale: string,
334
+ robots?: string,
335
+ ): string | undefined {
336
+ const path = pathname.replace(/\/+$/, '') || '/';
337
+ const query = new URLSearchParams(search);
338
+ query.delete('locale');
339
+ if (
340
+ !origin ||
341
+ /noindex/i.test(robots ?? '') ||
342
+ !indexedPage.test(path) ||
343
+ [...query.keys()].length
344
+ ) {
345
+ return undefined;
346
+ }
347
+ const url = new URL(path, `${origin}/`);
348
+ if (locale) {
349
+ url.searchParams.set('locale', locale);
350
+ }
351
+ return url.toString();
352
+ }
@@ -0,0 +1,130 @@
1
+ import type { ProductFacetSelection } from '@/interfaces/orionplace/products/IProductSearchFacets';
2
+
3
+ // Where a product list is: its result page and the chosen filters. Category,
4
+ // search, seller and featured lists keep it in the URL as `?page&filters`, so
5
+ // a reload and the browser's Back button from a product return to the same
6
+ // list. `filters` is the filter selection as JSON; the address is visitor
7
+ // input, so only plain filter values within the limits below are read.
8
+ export interface ProductListLocation {
9
+ page: number;
10
+ filters: ProductFacetSelection;
11
+ }
12
+
13
+ type QueryValue = string | null | undefined | readonly (string | null)[];
14
+
15
+ const locationKeys = ['page', 'filters'];
16
+ const maxPage = 10000;
17
+ const maxFiltersText = 4000;
18
+ const maxFilterKeys = 40;
19
+ const maxFilterIds = 200;
20
+ const maxFilterString = 100;
21
+ const filterKey = /^[a-z][a-z0-9_]{0,63}$/;
22
+
23
+ function first(value: QueryValue) {
24
+ return Array.isArray(value) ? value[0] : (value as string | null | undefined);
25
+ }
26
+
27
+ function filterValue(value: unknown): ProductFacetSelection[string] {
28
+ if (Array.isArray(value)) {
29
+ const ids = value.filter(
30
+ (id): id is number => Number.isSafeInteger(id) && id >= 0,
31
+ );
32
+
33
+ return ids.length === value.length && ids.length && ids.length <= maxFilterIds
34
+ ? ids
35
+ : undefined;
36
+ }
37
+ if (typeof value === 'string') {
38
+ return value && value.length <= maxFilterString ? value : undefined;
39
+ }
40
+ if (typeof value === 'number') {
41
+ return Number.isFinite(value) ? value : undefined;
42
+ }
43
+
44
+ // `false` chooses nothing, as an empty list or text.
45
+ return value === true ? true : undefined;
46
+ }
47
+
48
+ export function productListFiltersFromText(
49
+ text: string | null | undefined,
50
+ ): ProductFacetSelection {
51
+ if (!text || text.length > maxFiltersText) {
52
+ return {};
53
+ }
54
+ let parsed: unknown;
55
+ try {
56
+ parsed = JSON.parse(text);
57
+ } catch {
58
+ return {};
59
+ }
60
+ if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
61
+ return {};
62
+ }
63
+ const result: ProductFacetSelection = {};
64
+ for (const [key, value] of Object.entries(parsed).slice(0, maxFilterKeys)) {
65
+ const checked = filterKey.test(key) ? filterValue(value) : undefined;
66
+ if (checked !== undefined) {
67
+ result[key] = checked;
68
+ }
69
+ }
70
+
71
+ return result;
72
+ }
73
+
74
+ // The filters as URL text, or undefined when nothing is chosen.
75
+ // Keys are sorted, so the same choice always gives the same text.
76
+ export function productListFiltersText(filters: ProductFacetSelection) {
77
+ const chosen = productListFiltersFromText(JSON.stringify(filters));
78
+ const keys = Object.keys(chosen).sort();
79
+
80
+ return keys.length
81
+ ? JSON.stringify(Object.fromEntries(keys.map((key) => [key, chosen[key]])))
82
+ : undefined;
83
+ }
84
+
85
+ export function productListLocationFromQuery(
86
+ query: Record<string, QueryValue>,
87
+ ): ProductListLocation {
88
+ const page = Number(first(query.page));
89
+
90
+ return {
91
+ page: Number.isSafeInteger(page) && page > 0 && page <= maxPage ? page : 1,
92
+ filters: productListFiltersFromText(first(query.filters)),
93
+ };
94
+ }
95
+
96
+ // Compares the URL form, so the same choice in another key order is equal.
97
+ export function sameProductListLocation(
98
+ left: ProductListLocation,
99
+ right: ProductListLocation,
100
+ ) {
101
+ return (
102
+ left.page === right.page &&
103
+ productListFiltersText(left.filters) === productListFiltersText(right.filters)
104
+ );
105
+ }
106
+
107
+ // The query of `next`: other parameters such as `locale` or `q` stay, and the
108
+ // first page without filters adds nothing.
109
+ export function productListLocationQuery(
110
+ query: Record<string, QueryValue>,
111
+ next: ProductListLocation,
112
+ ) {
113
+ const result: Record<string, string> = {};
114
+
115
+ for (const [key, value] of Object.entries(query)) {
116
+ const text = first(value);
117
+ if (typeof text === 'string' && !locationKeys.includes(key)) {
118
+ result[key] = text;
119
+ }
120
+ }
121
+ if (next.page > 1) {
122
+ result.page = String(next.page);
123
+ }
124
+ const filters = productListFiltersText(next.filters);
125
+ if (filters) {
126
+ result.filters = filters;
127
+ }
128
+
129
+ return result;
130
+ }
@@ -7,6 +7,7 @@ import type {
7
7
  IProductSearchResponse,
8
8
  } from '@/interfaces/orionplace/products/IProductSearchApi';
9
9
  import type { OrionplaceApi } from '@/lib/orionplace/api';
10
+ import { initialSearchFacets } from '@/lib/orionplace/searchFilters';
10
11
 
11
12
  export type ProductSearchPage = NonNullable<
12
13
  IProductSearchResponse['page_data']
@@ -230,3 +231,50 @@ export async function loadProductSearch(
230
231
  signal,
231
232
  );
232
233
  }
234
+
235
+ /**
236
+ * A result page with `fullFacets`: the filter choices of the unfiltered
237
+ * first page, when the page was a later one or loaded with filters.
238
+ */
239
+ export type ProductListPage = ProductSearchPage & {
240
+ fullFacets?: IProductSearchFacets | null;
241
+ };
242
+
243
+ /**
244
+ * A list opened from its URL (Back, a reload) on a later page or with
245
+ * filters gets only the filtered choices, or none after the first page.
246
+ * With `withFullFacets` this also asks for the unfiltered first page, so the
247
+ * filter panel keeps every seller, price and option to choose from. The
248
+ * extra request is only for the panel: when it fails the panel shows the
249
+ * choices of the result.
250
+ */
251
+ export async function loadProductListPage(
252
+ api: OrionplaceApi,
253
+ scope: ProductSearchScope,
254
+ filters: ProductFacetSelection = {},
255
+ page = 1,
256
+ signal?: AbortSignal,
257
+ withFullFacets = true,
258
+ ): Promise<ProductListPage> {
259
+ // Both requests run together.
260
+ const [data, full] = await Promise.all([
261
+ loadProductSearch(api, scope, filters, page, signal),
262
+ withFullFacets && (page > 1 || Object.keys(filters).length > 0)
263
+ ? loadProductSearch(api, scope, {}, 1, signal).catch(() => undefined)
264
+ : undefined,
265
+ ]);
266
+
267
+ return full ? { ...data, fullFacets: productSearchFacetData(full) } : data;
268
+ }
269
+
270
+ /** The filter choices of a list page: the full ones first, as kept by the store. */
271
+ export function productListFacetData(
272
+ page: ProductListPage | undefined,
273
+ previous: IProductSearchFacets | null = null,
274
+ ) {
275
+ return [page?.fullFacets ?? null, productSearchFacetData(page)].reduce(
276
+ (current, incoming) =>
277
+ incoming ? initialSearchFacets(current, incoming) : current,
278
+ previous,
279
+ );
280
+ }
@@ -34,6 +34,18 @@ export function profileDate(value: string | null | undefined) {
34
34
  : '';
35
35
  }
36
36
 
37
+ /** The name of a phone country code (`RU` → «Россия» / «Russia»); the code itself when the browser has no name. */
38
+ export function profileCountryName(code: string | null | undefined, locale: string) {
39
+ if (!code) {
40
+ return '';
41
+ }
42
+ try {
43
+ return new Intl.DisplayNames([locale], { type: 'region' }).of(code) || code;
44
+ } catch {
45
+ return code;
46
+ }
47
+ }
48
+
37
49
  export class ProfileRequestError extends Error {
38
50
  readonly kind = 'profile-request';
39
51
  readonly fields: Record<string, string>;
@@ -15,6 +15,7 @@ export function profileLabels(locale: string) {
15
15
  lastName: 'Фамилия',
16
16
  phone: 'Номер телефона',
17
17
  phoneHint: 'Номер телефона для входа.',
18
+ phoneCountryHint: 'Номер телефона для входа. Его страна, {country}, определяет доступные способы оплаты.',
18
19
  dob: 'Дата рождения',
19
20
  save: 'Сохранить',
20
21
  pending: 'Сохраняем…',
@@ -89,6 +90,7 @@ export function profileLabels(locale: string) {
89
90
  lastName: 'Last name',
90
91
  phone: 'Phone number',
91
92
  phoneHint: 'Your sign-in phone number.',
93
+ phoneCountryHint: 'Your sign-in phone number. Its country, {country}, decides which payment methods you can use.',
92
94
  dob: 'Date of birth',
93
95
  save: 'Save',
94
96
  pending: 'Saving…',
@@ -1,4 +1,6 @@
1
1
  import { cache } from 'react';
2
+ import { headers } from 'next/headers';
3
+ import { storefrontPublicOrigin } from '@/lib/orionplace/pageSeo';
2
4
  import { getOrionplaceAppLoad } from '@/lib/orionplace/appLoadServer';
3
5
  import {
4
6
  getOrionplaceServerLocale,
@@ -73,11 +75,16 @@ export const getStorefrontMetadataContext = cache(async () => {
73
75
  } catch {
74
76
  // The page retains its request error and offers Retry.
75
77
  }
78
+ const incoming = await headers();
76
79
  return storefrontMetadata(
77
80
  result?.response.page_data.init ?? null,
78
81
  locale,
79
82
  url,
80
83
  { product, post, tariff, promo, pageKind },
84
+ // The address the visitor used: the proxy's host.
85
+ storefrontPublicOrigin(
86
+ incoming.get('x-forwarded-host') ?? incoming.get('host'),
87
+ ),
81
88
  );
82
89
  });
83
90
 
@@ -1,4 +1,5 @@
1
1
  import { NextResponse, type NextRequest } from 'next/server';
2
+ import { marketplaceNoindexPath } from '@orionplace-runtime/marketplace.mjs';
2
3
  import {
3
4
  APP_LOAD_HANDOFF_HEADER,
4
5
  startAppLoad,
@@ -47,10 +48,15 @@ export async function proxy(request: NextRequest) {
47
48
  ...(unavailable ? { status: 503 } : {}),
48
49
  });
49
50
  response.headers.set('Cache-Control', 'private, no-store');
51
+ // Pages under ORIONPLACE_NOINDEX_PATHS stay out of search engines.
52
+ if (marketplaceNoindexPath(request.nextUrl.pathname)) {
53
+ response.headers.set('X-Robots-Tag', 'noindex, follow');
54
+ }
50
55
  return response;
51
56
  }
52
57
 
53
- // The gateway sets its own headers for /api; static assets keep their caching.
58
+ // The gateway sets its own headers for /api, robots.txt and the sitemap;
59
+ // static assets keep their caching.
54
60
  export const config = {
55
- matcher: ['/((?!api/|_next/static|_next/image|favicon.ico).*)'],
61
+ matcher: ['/((?!api/|_next/static|_next/image|favicon.ico|robots.txt|sitemap.xml).*)'],
56
62
  };