orionplace-svelte 0.6.2 → 0.7.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/README.md CHANGED
@@ -480,7 +480,9 @@ The corresponding `.cfg` is read on every request. Backend URLs must use HTTPS,
480
480
  end in `/api`, and belong to `*.orionplace.app`. API methods, query strings, bodies,
481
481
  authorization and cookies are forwarded. Backend statuses are retained and cookie
482
482
  Domain attributes are removed to bind cookies to the storefront. Gateway responses
483
- use `Cache-Control: no-store`. Network failures return `502`.
483
+ use `Cache-Control: no-store`, except public blog images and videos: they keep the
484
+ backend's `private, max-age` rule, so the browser does not download them again on
485
+ every page. Network failures return `502`.
484
486
 
485
487
  A changing request (POST, PUT, PATCH, DELETE) from another site is rejected with
486
488
  `403`. A request from the page's own host or the storefront domain passes, and the
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "orionplace-svelte",
3
- "version": "0.6.2",
3
+ "version": "0.7.0",
4
4
  "description": "SvelteKit 2 storefront for your Orionplace marketplace: setup CLI, server API gateway, typed store and ready-made catalog, cart, checkout and account components in English and Russian, plus an Android app build.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -37,6 +37,11 @@ export declare function marketplacePublicOrigin(
37
37
  tenantDomain?: string,
38
38
  ): string;
39
39
 
40
+ export declare function marketplaceCacheControl(
41
+ media: boolean,
42
+ upstream: Response,
43
+ ): string;
44
+
40
45
  export declare function noindexPathPrefixes(value?: string): string[];
41
46
 
42
47
  export declare function marketplaceNoindexPath(
@@ -41,6 +41,18 @@ const hostnamePattern =
41
41
  const blogMediaPattern =
42
42
  /^\/ps3\/blog\/posts\/(?:images\/\d+|videos\/\d+(?:\/preview)?)$/;
43
43
 
44
+ /**
45
+ * API answers are never stored. A media file keeps the backend's own browser
46
+ * cache rule when the backend allows it (a published public blog image:
47
+ * "private, max-age=…"); "private" keeps it out of shared caches.
48
+ */
49
+ export function marketplaceCacheControl(media, upstream) {
50
+ const value = upstream.headers.get('cache-control') ?? '';
51
+ return media && upstream.ok && /\bprivate\b/i.test(value) && /\bmax-age=\d+/i.test(value) && !/no-store|no-cache/i.test(value)
52
+ ? value
53
+ : 'no-store';
54
+ }
55
+
44
56
  /** @param {string} pathname */
45
57
  export function isMarketplaceMediaPath(pathname) {
46
58
  return blogMediaPattern.test(pathname);
@@ -307,7 +319,7 @@ export async function forwardMarketplace(request, configuration = {}) {
307
319
  : destination.href,
308
320
  );
309
321
  }
310
- responseHeaders.set('Cache-Control', 'no-store');
322
+ responseHeaders.set('Cache-Control', marketplaceCacheControl(media, upstream));
311
323
  responseHeaders.set('Vary', 'Host, X-Forwarded-Host, X-Front-Domain');
312
324
  // fetch decodes compressed bodies: do not reuse upstream Content-Length/Encoding.
313
325
  return new Response(upstream.body, {
@@ -403,6 +403,14 @@ for example \`ORIONPLACE_NOINDEX_PATHS="/catalog /category /product"\`. Those
403
403
  pages answer with \`X-Robots-Tag: noindex, follow\` and the sitemap leaves them
404
404
  out; the journal and other pages stay indexable. Leave it empty on a live shop.
405
405
 
406
+ Every product page carries schema.org product data for Google and Yandex: the
407
+ price, the crossed-out old price, stock, photos, brand, seller, rating and the
408
+ return terms the page shows. To include the return terms, set
409
+ \`ORIONPLACE_SHIPPING_COUNTRIES\` in \`.env\` to the countries you ship to, as
410
+ ISO codes separated by spaces, for example
411
+ \`ORIONPLACE_SHIPPING_COUNTRIES="RU BY"\`; without it the return terms are left
412
+ out. The delivery cost is not included: it depends on the buyer's address.
413
+
406
414
  This setup does not add analytics.
407
415
 
408
416
  ## Android app
@@ -12,10 +12,12 @@ function escapeAttribute(value: string) {
12
12
 
13
13
  /**
14
14
  * Personalized pages are never shared between visitors or storefront domains.
15
- * The API gateway, robots.txt and the sitemap set their own headers, so those
16
- * requests are left alone.
15
+ * The API gateway (/api, /ps3 media), robots.txt and the sitemap set their own
16
+ * headers, so those requests are left alone: public blog images keep the
17
+ * backend's browser cache rule.
17
18
  */
18
19
  const ownHeaders = ['/robots.txt', '/sitemap.xml'];
20
+ const gatewayPaths = ['/api/', '/ps3/'];
19
21
 
20
22
  export const handle: Handle = async ({ event, resolve }) => {
21
23
  const response = await resolve(event, {
@@ -26,7 +28,7 @@ export const handle: Handle = async ({ event, resolve }) => {
26
28
  ),
27
29
  });
28
30
  if (
29
- !event.url.pathname.startsWith('/api/') &&
31
+ !gatewayPaths.some((path) => event.url.pathname.startsWith(path)) &&
30
32
  !ownHeaders.includes(event.url.pathname)
31
33
  ) {
32
34
  response.headers.set('Cache-Control', 'private, no-store');
@@ -10,6 +10,7 @@ import {
10
10
  storefrontPublicOrigin,
11
11
  } from '@/lib/utils/orionplace/pageSeo';
12
12
  import type { StorefrontSeoPage } from '@/lib/utils/orionplace/pageSeo';
13
+ import { structuredDataJson } from '@/lib/utils/orionplace/productStructuredData';
13
14
 
14
15
  export function useOrionplacePageSeo(
15
16
  page: MaybeRefOrGetter<StorefrontSeoPage>,
@@ -20,6 +21,12 @@ export function useOrionplacePageSeo(
20
21
  keywords?: MaybeRefOrGetter<string | undefined>;
21
22
  robots?: MaybeRefOrGetter<string | undefined>;
22
23
  image?: MaybeRefOrGetter<string | undefined>;
24
+ /**
25
+ * The page's schema.org data (a record page, e.g. the product), built
26
+ * from its canonical address and the visitor's origin. A page without a
27
+ * canonical address (preview, filtered, noindex) gets none.
28
+ */
29
+ structuredData?: (canonical: string, origin: string) => unknown;
23
30
  } = {},
24
31
  ) {
25
32
  // The address the visitor used: the proxy's host during the server render.
@@ -56,8 +63,14 @@ export function useOrionplacePageSeo(
56
63
  ogImage: () => storefrontAbsoluteUrl(toValue(values.image), origin),
57
64
  ogUrl: () => canonical.value,
58
65
  };
66
+ const structured = computed(() =>
67
+ canonical.value && values.structuredData
68
+ ? structuredDataJson(values.structuredData(canonical.value, origin))
69
+ : '',
70
+ );
59
71
  useSeoMeta(seo);
60
72
  useHead(() => ({
61
73
  link: canonical.value ? [{ rel: 'canonical', href: canonical.value }] : [],
74
+ jsonLd: structured.value || undefined,
62
75
  }));
63
76
  }
@@ -2,6 +2,10 @@
2
2
  import type { PageHead } from '@/lib/orionplace/navigation.svelte';
3
3
 
4
4
  let { metadata }: { metadata: PageHead } = $props();
5
+
6
+ // The JSON is escaped by structuredDataJson, so it cannot close the tag.
7
+ const scriptOpen = '<script type="application/ld+json">';
8
+ const scriptClose = '</' + 'script>';
5
9
  </script>
6
10
 
7
11
  <svelte:head>
@@ -23,6 +27,10 @@
23
27
  {/if}
24
28
  {/each}
25
29
 
30
+ {#if metadata.jsonLd}
31
+ {@html scriptOpen + metadata.jsonLd + scriptClose}
32
+ {/if}
33
+
26
34
  {#each metadata.link ?? [] as link (link.rel)}
27
35
  {#if link.href}
28
36
  <link
@@ -105,16 +105,20 @@ export interface PageHead {
105
105
  meta?: Record<string, any>[];
106
106
  // <link> tags, one per `rel` (the canonical address).
107
107
  link?: { rel: string; href: string }[];
108
+ // The page's schema.org data as JSON (structuredDataJson), if it has any.
109
+ jsonLd?: string;
108
110
  }
109
111
  type HeadReader = () => PageHead;
110
112
 
111
113
  function collectHead(readers: HeadReader[]): PageHead {
112
114
  let title: string | undefined;
115
+ let jsonLd: string | undefined;
113
116
  const meta = new Map<string, Record<string, any>>();
114
117
  const link = new Map<string, { rel: string; href: string }>();
115
118
  for (const read of readers) {
116
119
  const head = read();
117
120
  title = head.title ?? title;
121
+ jsonLd = head.jsonLd || jsonLd;
118
122
  for (const tag of head.meta ?? []) {
119
123
  const key = tag.property ?? tag.name;
120
124
  if (key) {
@@ -125,7 +129,12 @@ function collectHead(readers: HeadReader[]): PageHead {
125
129
  link.set(tag.rel, tag);
126
130
  }
127
131
  }
128
- return { title, meta: [...meta.values()], link: [...link.values()] };
132
+ return {
133
+ title,
134
+ meta: [...meta.values()],
135
+ link: [...link.values()],
136
+ ...(jsonLd ? { jsonLd } : {}),
137
+ };
129
138
  }
130
139
 
131
140
  interface HeadRegistry {
@@ -0,0 +1,220 @@
1
+ // The product page's schema.org data for search engines: Google merchant
2
+ // listings and Yandex product snippets read the same block. Every value
3
+ // repeats what the page shows: the price and the crossed-out old price,
4
+ // stock, return terms, photos, brand, seller and rating. The delivery cost is
5
+ // left out: it depends on the buyer's address.
6
+ // The same file is in the three packages; it has no imports so that it stays
7
+ // identical.
8
+
9
+ const SCHEMA_ORG = 'https://schema.org/';
10
+ const MAX_IMAGES = 10;
11
+ // EProductStatus.PUBLISHED and PUBLISHED_AS_OPTION: the product is on sale.
12
+ const ON_SALE_STATUSES = [2, 1];
13
+
14
+ interface StructuredPrice {
15
+ transform_value: number;
16
+ }
17
+
18
+ /** The product fields the data uses (IProduct has them all). */
19
+ export interface StorefrontStructuredProduct {
20
+ id: number;
21
+ is_digital?: boolean;
22
+ discount: number;
23
+ base_price_dict: Record<string, StructuredPrice>;
24
+ product_price_dict: Record<string, StructuredPrice>;
25
+ images?: { full?: string[] | null; small?: string[] | null } | null;
26
+ status: { status: number };
27
+ c_real_left: number;
28
+ seller?: { name?: string | null } | null;
29
+ manufacture?: { name?: string | null } | null;
30
+ refunds?: {
31
+ customer_max_refund_days: number;
32
+ max_refund_days_with_defect: number;
33
+ } | null;
34
+ feedback_stat?: {
35
+ c_feedbacks?: number | null;
36
+ avg_raiting?: number | string | null;
37
+ } | null;
38
+ }
39
+
40
+ export interface StorefrontProductDataOptions {
41
+ /** The page's canonical address; without one the page gets no data. */
42
+ url: string | undefined;
43
+ name: string | undefined;
44
+ description?: string;
45
+ /** The currency the page shows prices in (its ID and code). */
46
+ currencyId: string;
47
+ currencyCode: string | undefined;
48
+ /** ORIONPLACE_SHIPPING_COUNTRIES, parsed by shippingCountryCodes(). */
49
+ shippingCountries: string[];
50
+ /** False when the page hides the seller (single-seller storefront). */
51
+ showSeller: boolean;
52
+ /** Makes an image address complete (storefrontAbsoluteUrl). */
53
+ absoluteUrl: (value: string | undefined) => string | undefined;
54
+ }
55
+
56
+ /**
57
+ * The countries the shop ships to, from ORIONPLACE_SHIPPING_COUNTRIES:
58
+ * ISO 3166-1 alpha-2 codes separated by spaces or commas, e.g. "RU BY".
59
+ */
60
+ export function shippingCountryCodes(value: unknown): string[] {
61
+ const codes = String(value ?? '')
62
+ .split(/[\s,;]+/)
63
+ .map((code) => code.trim().toUpperCase())
64
+ .filter((code) => /^[A-Z]{2}$/.test(code));
65
+
66
+ return [...new Set(codes)];
67
+ }
68
+
69
+ function wholeDays(value: unknown) {
70
+ const days = Math.floor(Number(value));
71
+
72
+ return Number.isFinite(days) && days > 0 ? days : 0;
73
+ }
74
+
75
+ /**
76
+ * The return terms the page shows: the days for a product without a defect
77
+ * are the return window (sent back by mail at the buyer's cost); a defective
78
+ * product goes back at the shop's cost. A digital product or one without
79
+ * days cannot be returned. Without a shipping country there is no policy:
80
+ * search engines require the country.
81
+ */
82
+ export function productReturnPolicy(
83
+ product: Pick<StorefrontStructuredProduct, 'is_digital' | 'refunds'>,
84
+ countries: string[],
85
+ ) {
86
+ if (!countries.length) {
87
+ return undefined;
88
+ }
89
+
90
+ const applicableCountry = countries.length === 1 ? countries[0] : countries;
91
+ const returnDays = product.is_digital
92
+ ? 0
93
+ : wholeDays(product.refunds?.customer_max_refund_days);
94
+ const defectDays = product.is_digital
95
+ ? 0
96
+ : wholeDays(product.refunds?.max_refund_days_with_defect);
97
+
98
+ if (!returnDays) {
99
+ return {
100
+ '@type': 'MerchantReturnPolicy',
101
+ applicableCountry,
102
+ 'returnPolicyCategory': `${SCHEMA_ORG}MerchantReturnNotPermitted`,
103
+ };
104
+ }
105
+
106
+ return {
107
+ '@type': 'MerchantReturnPolicy',
108
+ applicableCountry,
109
+ 'returnPolicyCategory': `${SCHEMA_ORG}MerchantReturnFiniteReturnWindow`,
110
+ 'merchantReturnDays': returnDays,
111
+ 'returnMethod': `${SCHEMA_ORG}ReturnByMail`,
112
+ 'returnFees': `${SCHEMA_ORG}ReturnFeesCustomerResponsibility`,
113
+ ...(defectDays ? { itemDefectReturnFees: `${SCHEMA_ORG}FreeReturn` } : {}),
114
+ };
115
+ }
116
+
117
+ function priceValue(
118
+ prices: Record<string, StructuredPrice> | undefined,
119
+ currencyId: string,
120
+ ) {
121
+ const value = Number(prices?.[currencyId]?.transform_value);
122
+
123
+ return prices?.[currencyId] && Number.isFinite(value) ? value : undefined;
124
+ }
125
+
126
+ /** The schema.org Product of the page, or undefined when it cannot be built. */
127
+ export function storefrontProductStructuredData(
128
+ product: StorefrontStructuredProduct | undefined,
129
+ options: StorefrontProductDataOptions,
130
+ ) {
131
+ const name = options.name?.trim();
132
+ const currencyCode = options.currencyCode?.trim().toUpperCase() ?? '';
133
+
134
+ if (!product || !options.url || !name) {
135
+ return undefined;
136
+ }
137
+
138
+ // The price the page shows, as ProductCardPrice picks it.
139
+ const currencyId =
140
+ options.currencyId ||
141
+ Object.keys(product.product_price_dict ?? {})[0] ||
142
+ Object.keys(product.base_price_dict ?? {})[0] ||
143
+ '';
144
+ const basePrice = priceValue(product.base_price_dict, currencyId);
145
+ const price =
146
+ priceValue(product.product_price_dict, currencyId) ?? basePrice;
147
+ const images = [
148
+ ...(product.images?.full ?? []),
149
+ ...(product.images?.small ?? []),
150
+ ]
151
+ .map((image) => options.absoluteUrl(image))
152
+ .filter((image, index, list): image is string =>
153
+ Boolean(image) && list.indexOf(image) === index,
154
+ )
155
+ .slice(0, MAX_IMAGES);
156
+ const inStock =
157
+ ON_SALE_STATUSES.includes(product.status?.status) &&
158
+ product.c_real_left > 0;
159
+ const returns = productReturnPolicy(product, options.shippingCountries);
160
+ const brand = product.manufacture?.name?.trim();
161
+ const seller = options.showSeller ? product.seller?.name?.trim() : '';
162
+ const reviews = Number(product.feedback_stat?.c_feedbacks ?? 0);
163
+ const rating = Number(product.feedback_stat?.avg_raiting ?? 0);
164
+ const offer =
165
+ price !== undefined && /^[A-Z]{3}$/.test(currencyCode)
166
+ ? {
167
+ '@type': 'Offer',
168
+ 'url': options.url,
169
+ price,
170
+ 'priceCurrency': currencyCode,
171
+ // The crossed-out old price, when the page shows one.
172
+ ...(basePrice !== undefined && price < basePrice
173
+ ? {
174
+ priceSpecification: {
175
+ '@type': 'UnitPriceSpecification',
176
+ 'priceType': `${SCHEMA_ORG}StrikethroughPrice`,
177
+ 'price': basePrice,
178
+ 'priceCurrency': currencyCode,
179
+ },
180
+ }
181
+ : {}),
182
+ 'availability': `${SCHEMA_ORG}${inStock ? 'InStock' : 'OutOfStock'}`,
183
+ 'itemCondition': `${SCHEMA_ORG}NewCondition`,
184
+ ...(returns ? { hasMerchantReturnPolicy: returns } : {}),
185
+ ...(seller
186
+ ? { seller: { '@type': 'Organization', 'name': seller } }
187
+ : {}),
188
+ }
189
+ : undefined;
190
+
191
+ return {
192
+ '@context': 'https://schema.org',
193
+ '@type': 'Product',
194
+ name,
195
+ 'url': options.url,
196
+ 'sku': String(product.id),
197
+ ...(options.description?.trim()
198
+ ? { description: options.description.trim() }
199
+ : {}),
200
+ ...(images.length ? { image: images } : {}),
201
+ ...(brand ? { brand: { '@type': 'Brand', 'name': brand } } : {}),
202
+ ...(offer ? { offers: offer } : {}),
203
+ ...(reviews > 0 && Number.isFinite(rating) && rating > 0
204
+ ? {
205
+ aggregateRating: {
206
+ '@type': 'AggregateRating',
207
+ 'ratingValue': Math.round(rating * 10) / 10,
208
+ 'reviewCount': reviews,
209
+ },
210
+ }
211
+ : {}),
212
+ };
213
+ }
214
+
215
+ /** The data as the text of a <script type="application/ld+json"> tag. */
216
+ export function structuredDataJson(value: unknown): string {
217
+ return value === undefined
218
+ ? ''
219
+ : JSON.stringify(value).replace(/</g, '\\u003c');
220
+ }
@@ -1,3 +1,4 @@
1
+ import { env } from '$env/dynamic/private';
1
2
  import { loadOrionplaceAppLoad } from '@/lib/orionplace/appLoadServer';
2
3
  import { OrionplaceStore } from '@/lib/orionplace/store.svelte';
3
4
  import { storefrontErrorLocale } from '@/lib/utils/orionplace/errorPageLabels';
@@ -33,5 +34,8 @@ export const load: LayoutServerLoad = async (event) => {
33
34
  return {
34
35
  orionplace: { result, failure },
35
36
  orionplaceLocale: store.localeCode,
37
+ // The countries the shop ships to, for the return terms in the product
38
+ // page's schema.org data.
39
+ orionplaceShippingCountries: env.ORIONPLACE_SHIPPING_COUNTRIES ?? '',
36
40
  };
37
41
  };
@@ -1,4 +1,5 @@
1
1
  <script lang="ts">
2
+ import { page } from '$app/state';
2
3
  import { useOrionplaceStore } from '@/lib/stores/orionplace.svelte';
3
4
  import { computed, ref, watch } from '@/lib/orionplace/reactivity.svelte';
4
5
  import { navigateTo, useRoute } from '@/lib/orionplace/navigation.svelte';
@@ -6,6 +7,11 @@
6
7
  import { useOrionplaceMissingPage } from '@/lib/composables/useOrionplaceMissingPage.svelte';
7
8
  import { useOrionplaceServerData } from '@/lib/composables/useOrionplaceServerData.svelte';
8
9
  import { requestProductPage } from '@/lib/utils/orionplace/productPage';
10
+ import { storefrontAbsoluteUrl } from '@/lib/utils/orionplace/pageSeo';
11
+ import {
12
+ shippingCountryCodes,
13
+ storefrontProductStructuredData,
14
+ } from '@/lib/utils/orionplace/productStructuredData';
9
15
  import {
10
16
  getAvailableNestedValueFromObject,
11
17
  plainText,
@@ -72,28 +78,49 @@
72
78
  return images?.full?.[0] ?? images?.small?.[0];
73
79
  });
74
80
 
75
- useOrionplacePageSeo('product', () => store.localeCode, {
76
- title: () =>
81
+ // ORIONPLACE_SHIPPING_COUNTRIES from the server's .env (+layout.server.ts),
82
+ // for the return terms in the page's schema.org data.
83
+ const shippingCountries = computed(() =>
84
+ shippingCountryCodes(page.data.orionplaceShippingCountries),
85
+ );
86
+ const productName = computed(() =>
87
+ getAvailableNestedValueFromObject(
88
+ product.value?.locale,
89
+ store.localeCode,
90
+ 'product_name',
91
+ ),
92
+ );
93
+ const productSummary = computed(() =>
94
+ plainText(
77
95
  getAvailableNestedValueFromObject(
78
96
  product.value?.locale,
79
97
  store.localeCode,
80
- 'product_name',
81
- ),
82
- description: () =>
83
- plainText(
98
+ 'meta_description',
99
+ ) ||
84
100
  getAvailableNestedValueFromObject(
85
101
  product.value?.locale,
86
102
  store.localeCode,
87
- 'meta_description',
88
- ) ||
89
- getAvailableNestedValueFromObject(
90
- product.value?.locale,
91
- store.localeCode,
92
- 'short_description',
93
- ),
94
- ).slice(0, 160),
103
+ 'short_description',
104
+ ),
105
+ ).slice(0, 160),
106
+ );
107
+
108
+ useOrionplacePageSeo('product', () => store.localeCode, {
109
+ title: () => productName.value,
110
+ description: () => productSummary.value,
95
111
  image: () => image.value,
96
112
  robots: () => (previewKey.value ? 'noindex, nofollow' : undefined),
113
+ structuredData: (canonical, origin) =>
114
+ storefrontProductStructuredData(product.value, {
115
+ url: canonical,
116
+ name: productName.value,
117
+ description: productSummary.value,
118
+ currencyId: store.currencyId,
119
+ currencyCode: store.currentCurrency?.currency_code,
120
+ shippingCountries: shippingCountries.value,
121
+ showSeller: !store.init?.visual_settings?.single_seller_mode,
122
+ absoluteUrl: (value) => storefrontAbsoluteUrl(value, origin),
123
+ }),
97
124
  });
98
125
 
99
126
  // An invalid ID, or a product that the selected endpoint does not return,