@tanqory/theme-kit 0.1.0 → 2.0.0-alpha.19
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/LICENSE +21 -0
- package/README.md +128 -10
- package/dist/abi/canvas-live.d.ts +72 -0
- package/dist/abi/canvas-live.js +159 -0
- package/dist/abi/cart.d.ts +60 -0
- package/dist/abi/cart.js +396 -0
- package/dist/abi/convert.d.ts +360 -0
- package/dist/abi/convert.js +1330 -0
- package/dist/abi/data-api.d.ts +15 -0
- package/dist/abi/data-api.js +213 -0
- package/dist/abi/jcs.d.ts +6 -0
- package/dist/abi/jcs.js +28 -0
- package/dist/abi/labels.d.ts +3 -0
- package/dist/abi/labels.js +34 -0
- package/dist/abi/page-data.d.ts +264 -0
- package/dist/abi/page-data.js +746 -0
- package/dist/abi/page.d.ts +28 -0
- package/dist/abi/page.js +57 -0
- package/dist/abi/referrals.d.ts +23 -0
- package/dist/abi/referrals.js +89 -0
- package/dist/abi/rewards.d.ts +27 -0
- package/dist/abi/rewards.js +82 -0
- package/dist/abi/seo.d.ts +47 -0
- package/dist/abi/seo.js +68 -0
- package/dist/abi/shell.d.ts +23 -0
- package/dist/abi/shell.js +10 -0
- package/dist/abi/state-pack.d.ts +17 -0
- package/dist/abi/state-pack.js +43 -0
- package/dist/abi/wire.d.ts +46 -0
- package/dist/abi/wire.js +114 -0
- package/dist/build.d.ts +121 -0
- package/dist/build.js +401 -0
- package/dist/chunks.d.ts +23 -0
- package/dist/chunks.js +41 -0
- package/dist/client.d.ts +26 -0
- package/dist/client.js +101 -0
- package/dist/contract.d.ts +6 -0
- package/dist/contract.js +6 -0
- package/dist/headless/ai.d.ts +132 -0
- package/dist/headless/ai.js +99 -0
- package/dist/headless/hooks.d.ts +276 -0
- package/dist/headless/hooks.js +544 -0
- package/dist/headless/i18n.d.ts +13 -0
- package/dist/headless/i18n.js +45 -0
- package/dist/headless/index.d.ts +44 -0
- package/dist/headless/index.js +48 -0
- package/dist/headless/lazy-component.d.ts +72 -0
- package/dist/headless/lazy-component.js +225 -0
- package/dist/headless/lazy-section.d.ts +38 -0
- package/dist/headless/lazy-section.js +97 -0
- package/dist/headless/model.d.ts +73 -0
- package/dist/headless/model.js +115 -0
- package/dist/headless/money.d.ts +17 -0
- package/dist/headless/money.js +63 -0
- package/dist/headless/page.d.ts +45 -0
- package/dist/headless/page.js +53 -0
- package/dist/headless/platform.d.ts +55 -0
- package/dist/headless/platform.js +77 -0
- package/dist/headless/primitives/cart.d.ts +90 -0
- package/dist/headless/primitives/cart.js +153 -0
- package/dist/headless/primitives/collection.d.ts +68 -0
- package/dist/headless/primitives/collection.js +80 -0
- package/dist/headless/primitives/product.d.ts +101 -0
- package/dist/headless/primitives/product.js +117 -0
- package/dist/headless/primitives/storefront.d.ts +106 -0
- package/dist/headless/primitives/storefront.js +187 -0
- package/dist/headless/primitives/util.d.ts +19 -0
- package/dist/headless/primitives/util.js +42 -0
- package/dist/headless/strings.d.ts +9 -0
- package/dist/headless/strings.js +198 -0
- package/dist/headless/structured-data.d.ts +31 -0
- package/dist/headless/structured-data.js +57 -0
- package/dist/headless/tanqory-money.generated.d.ts +493 -0
- package/dist/headless/tanqory-money.generated.js +507 -0
- package/dist/headless/theme.d.ts +109 -0
- package/dist/headless/theme.js +62 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +21 -553
- package/dist/runtime/data/StoreProvider.d.ts +84 -0
- package/dist/runtime/data/StoreProvider.js +380 -0
- package/dist/runtime/data/handle.d.ts +14 -0
- package/dist/runtime/data/handle.js +16 -0
- package/dist/runtime/data/routes.d.ts +87 -0
- package/dist/runtime/data/routes.js +158 -0
- package/dist/runtime/data/to-ui.d.ts +286 -0
- package/dist/runtime/data/to-ui.js +1009 -0
- package/dist/runtime/entries/register.d.ts +9 -0
- package/dist/runtime/entries/register.js +9 -0
- package/dist/runtime/entries/sections.d.ts +16 -0
- package/dist/runtime/entries/sections.js +19 -0
- package/dist/{types → runtime/kit}/SectionTree.d.ts +2 -1
- package/dist/runtime/kit/SectionTree.js +100 -0
- package/dist/runtime/kit/analytics.d.ts +63 -0
- package/dist/runtime/kit/analytics.js +250 -0
- package/dist/runtime/kit/cart.d.ts +222 -0
- package/dist/runtime/kit/cart.js +545 -0
- package/dist/{types → runtime/kit}/composition.d.ts +1 -1
- package/dist/runtime/kit/composition.js +3 -0
- package/dist/{types → runtime/kit}/config.d.ts +2 -2
- package/dist/runtime/kit/config.js +6 -0
- package/dist/runtime/kit/consent.d.ts +29 -0
- package/dist/runtime/kit/consent.js +159 -0
- package/dist/runtime/kit/contract/content.d.ts +213 -0
- package/dist/runtime/kit/contract/content.js +119 -0
- package/dist/runtime/kit/contract/field-types.d.ts +360 -0
- package/dist/runtime/kit/contract/field-types.js +216 -0
- package/dist/runtime/kit/contract/groups.d.ts +230 -0
- package/dist/runtime/kit/contract/groups.js +402 -0
- package/dist/runtime/kit/contract/index.d.ts +33 -0
- package/dist/runtime/kit/contract/index.js +33 -0
- package/dist/runtime/kit/contract/transform.d.ts +82 -0
- package/dist/runtime/kit/contract/transform.js +133 -0
- package/dist/runtime/kit/contract/validate.d.ts +77 -0
- package/dist/runtime/kit/contract/validate.js +520 -0
- package/dist/runtime/kit/data.d.ts +732 -0
- package/dist/runtime/kit/data.js +1131 -0
- package/dist/{types → runtime/kit}/defineSection.d.ts +1 -1
- package/dist/runtime/kit/defineSection.js +10 -0
- package/dist/runtime/kit/dynamic-source.d.ts +118 -0
- package/dist/runtime/kit/dynamic-source.js +311 -0
- package/dist/runtime/kit/index.d.ts +24 -0
- package/dist/runtime/kit/index.js +24 -0
- package/dist/{types → runtime/kit}/jsx-to-json.d.ts +1 -1
- package/dist/runtime/kit/jsx-to-json.js +28 -0
- package/dist/{types → runtime/kit}/mount.d.ts +25 -4
- package/dist/runtime/kit/mount.js +124 -0
- package/dist/{types → runtime/kit}/registry.d.ts +1 -1
- package/dist/runtime/kit/registry.js +12 -0
- package/dist/{types → runtime/kit}/ssg.d.ts +9 -3
- package/dist/runtime/kit/ssg.js +56 -0
- package/dist/runtime/kit/storefront.d.ts +652 -0
- package/dist/runtime/kit/storefront.js +723 -0
- package/dist/{types → runtime/kit}/theme-context.d.ts +8 -2
- package/dist/runtime/kit/theme-context.js +25 -0
- package/dist/runtime/kit/types.d.ts +155 -0
- package/dist/runtime/kit/types.js +1 -0
- package/dist/runtime/platform/TrackingPixels.d.ts +14 -0
- package/dist/runtime/platform/TrackingPixels.js +63 -0
- package/dist/runtime/platform/environment.d.ts +68 -0
- package/dist/runtime/platform/environment.js +85 -0
- package/dist/runtime/platform/locale.d.ts +27 -0
- package/dist/runtime/platform/locale.js +42 -0
- package/dist/runtime/platform/route-analytics.d.ts +15 -0
- package/dist/runtime/platform/route-analytics.js +72 -0
- package/dist/runtime/platform/safe-href.d.ts +25 -0
- package/dist/runtime/platform/safe-href.js +69 -0
- package/dist/runtime/platform/safe-values.d.ts +37 -0
- package/dist/runtime/platform/safe-values.js +140 -0
- package/dist/runtime/render/SectionRenderer.d.ts +4 -0
- package/dist/runtime/render/SectionRenderer.js +199 -0
- package/dist/runtime/render/listing-query.d.ts +52 -0
- package/dist/runtime/render/listing-query.js +122 -0
- package/dist/runtime/render/localized-copy.d.ts +86 -0
- package/dist/runtime/render/localized-copy.js +129 -0
- package/dist/runtime/render/product-listing.d.ts +11 -0
- package/dist/runtime/render/product-listing.js +243 -0
- package/dist/runtime/render/resolved.d.ts +47 -0
- package/dist/runtime/render/resolved.js +80 -0
- package/dist/runtime/render/section-actions.d.ts +67 -0
- package/dist/runtime/render/section-actions.js +205 -0
- package/dist/runtime/render/starter.d.ts +11 -0
- package/dist/runtime/render/starter.js +96 -0
- package/dist/runtime/seo/head.d.ts +115 -0
- package/dist/runtime/seo/head.js +281 -0
- package/dist/runtime/seo/structured-data.d.ts +83 -0
- package/dist/runtime/seo/structured-data.js +252 -0
- package/dist/runtime/shell/PageShell.d.ts +18 -0
- package/dist/runtime/shell/PageShell.js +118 -0
- package/dist/runtime/shell/RuntimeBridge.d.ts +18 -0
- package/dist/runtime/shell/RuntimeBridge.js +124 -0
- package/dist/runtime/shell/overlay-state.d.ts +31 -0
- package/dist/runtime/shell/overlay-state.js +59 -0
- package/dist/runtime/shell/preview-mode.d.ts +2 -0
- package/dist/runtime/shell/preview-mode.js +11 -0
- package/dist/runtime/shell/spa-router.d.ts +14 -0
- package/dist/runtime/shell/spa-router.js +76 -0
- package/dist/runtime/shell/url-redirects.d.ts +6 -0
- package/dist/runtime/shell/url-redirects.js +37 -0
- package/dist/runtime/slots.d.ts +74 -0
- package/dist/runtime/slots.js +16 -0
- package/dist/runtime.d.ts +34 -0
- package/dist/runtime.js +40 -0
- package/dist/sdk/app-block.d.ts +29 -0
- package/dist/sdk/app-block.js +22 -0
- package/dist/sdk/font-hooks.d.ts +7 -0
- package/dist/sdk/font-hooks.js +21 -0
- package/dist/sdk/fonts.d.ts +144 -0
- package/dist/sdk/fonts.js +241 -0
- package/dist/sdk/index.d.ts +32 -0
- package/dist/sdk/index.js +36 -0
- package/dist/sdk/motion-hooks.d.ts +23 -0
- package/dist/sdk/motion-hooks.js +46 -0
- package/dist/sdk/motion.d.ts +78 -0
- package/dist/sdk/motion.js +292 -0
- package/dist/sdk/referrals.d.ts +62 -0
- package/dist/sdk/referrals.js +20 -0
- package/dist/sdk/rewards.d.ts +85 -0
- package/dist/sdk/rewards.js +29 -0
- package/dist/sdk/runtime-context.d.ts +230 -0
- package/dist/sdk/runtime-context.js +73 -0
- package/dist/sdk/schema.d.ts +249 -0
- package/dist/sdk/schema.js +168 -0
- package/dist/sdk/server-render.d.ts +8 -0
- package/dist/sdk/server-render.js +12 -0
- package/dist/sdk/store-lookup.d.ts +32 -0
- package/dist/sdk/store-lookup.js +48 -0
- package/dist/sdk/store.d.ts +164 -0
- package/dist/sdk/store.js +5 -0
- package/dist/sdk/sx.d.ts +45 -0
- package/dist/sdk/sx.js +117 -0
- package/dist/sdk/theme.d.ts +212 -0
- package/dist/sdk/theme.js +27 -0
- package/dist/sdk/types.d.ts +789 -0
- package/dist/sdk/types.js +7 -0
- package/dist/server.d.ts +112 -0
- package/dist/server.js +589 -0
- package/dist/vite-chunks.d.ts +10 -0
- package/dist/vite-chunks.js +44 -0
- package/docs/headless-api.md +250 -0
- package/package.json +72 -26
- package/dist/index.js.map +0 -1
- package/dist/ssg-BZ5FYTw7.js +0 -1802
- package/dist/ssg-BZ5FYTw7.js.map +0 -1
- package/dist/ssg.js +0 -8
- package/dist/ssg.js.map +0 -1
- package/dist/types/cart.d.ts +0 -86
- package/dist/types/data.d.ts +0 -357
- package/dist/types/dynamic-source.d.ts +0 -84
- package/dist/types/editor.d.ts +0 -5
- package/dist/types/index.d.ts +0 -20
- package/dist/types/preview-bridge.d.ts +0 -9
- package/dist/types/storefront.d.ts +0 -333
- package/dist/types/types.d.ts +0 -44
|
@@ -0,0 +1,746 @@
|
|
|
1
|
+
import * as convert from './convert.js';
|
|
2
|
+
import { isBoundSource, resolveBoundSource } from '../runtime/kit/dynamic-source.js';
|
|
3
|
+
import { searchOrder } from './data-api.js';
|
|
4
|
+
import { filterRequests, listingQueryOf, stateFromQuery } from '../runtime/render/listing-query.js';
|
|
5
|
+
/** Hard limits (theme-abi-v2.md §8). The host enforces them; the theme plans inside them. */
|
|
6
|
+
const MAX_DATA_CALLS = 4;
|
|
7
|
+
/** `snapshot.menus` holds every menu of the store up to this many, by handle order (storefront-query-v1 §9.1). */
|
|
8
|
+
const SNAPSHOT_MENUS_MAX = 20;
|
|
9
|
+
/** Products per listing request — the page size platform/runtime/render/product-listing.ts asks for. */
|
|
10
|
+
const LISTING_FIRST = 24;
|
|
11
|
+
/** Product tags are the merchant's internal labels, never a shopper filter (product-listing.ts). */
|
|
12
|
+
const HIDDEN_FACETS = new Set(['filter.tag']);
|
|
13
|
+
/**
|
|
14
|
+
* The listing state of this request (`?sort=` and `?filter.*=`, platform/runtime/render/listing-query.ts):
|
|
15
|
+
* only on the collection route. `sort` may also arrive as the ABI's query-derived route param.
|
|
16
|
+
* Cache-safe for the same reason as the search query: a request with a query string is neither served
|
|
17
|
+
* from nor stored in the shared cache (§5.1).
|
|
18
|
+
*/
|
|
19
|
+
export function listingQueryOfRequest(ctx) {
|
|
20
|
+
if (ctx.route.kind !== 'collection')
|
|
21
|
+
return '';
|
|
22
|
+
let search;
|
|
23
|
+
try {
|
|
24
|
+
search = new URL(ctx.request.url).searchParams;
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
search = new URLSearchParams();
|
|
28
|
+
}
|
|
29
|
+
const sort = ctx.route.params['sort'];
|
|
30
|
+
if (typeof sort === 'string' && sort && !search.has('sort'))
|
|
31
|
+
search.set('sort', sort);
|
|
32
|
+
return listingQueryOf(search);
|
|
33
|
+
}
|
|
34
|
+
/** Results per type the search page loads — the same page size sections/SearchResults.tsx asks the browser wire for. */
|
|
35
|
+
const SEARCH_FIRST = 48;
|
|
36
|
+
const normQuery = (q) => q.trim().toLowerCase();
|
|
37
|
+
/**
|
|
38
|
+
* The search query of this request: the `q` route param (theme-abi-v2.md §2.1), else — for a
|
|
39
|
+
* gateway that does not forward it yet — the `q` of the request's own query string. Only on the
|
|
40
|
+
* search route. Shopper-typed text: every place that prints it escapes it (React does).
|
|
41
|
+
* Reading the raw query is cache-safe: a request with any query string is neither served from nor
|
|
42
|
+
* stored in the shared cache (§5.1).
|
|
43
|
+
*/
|
|
44
|
+
export function searchQueryOf(ctx) {
|
|
45
|
+
if (ctx.route.kind !== 'search')
|
|
46
|
+
return null;
|
|
47
|
+
const param = ctx.route.params['q'];
|
|
48
|
+
if (typeof param === 'string')
|
|
49
|
+
return param;
|
|
50
|
+
try {
|
|
51
|
+
return new URL(ctx.request.url).searchParams.get('q');
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The search page's sort (`?sort=`, the ABI's query-derived `sort` route param on the search kind):
|
|
59
|
+
* a sort id lib/abi/data-api.ts `searchOrder` knows, else null (relevance). Only on the search route.
|
|
60
|
+
*/
|
|
61
|
+
export function searchSortOf(ctx) {
|
|
62
|
+
if (ctx.route.kind !== 'search')
|
|
63
|
+
return null;
|
|
64
|
+
let sort = ctx.route.params['sort'];
|
|
65
|
+
if (typeof sort !== 'string') {
|
|
66
|
+
try {
|
|
67
|
+
sort = new URL(ctx.request.url).searchParams.get('sort') ?? undefined;
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
sort = undefined;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return searchOrder(sort) ? sort : null;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The variant a product URL names (`/products/<handle>?variant=<id>`): a shared link, or checkout's way
|
|
77
|
+
* back to the product. Only on the product route; the section opens on it only when it is a variant
|
|
78
|
+
* of that product (ProductDetails ignores any other id). Read from the request's own query string:
|
|
79
|
+
* a request with a query is never served from or stored in the shared cache (§5.1).
|
|
80
|
+
*/
|
|
81
|
+
export function variantOf(ctx) {
|
|
82
|
+
if (ctx.route.kind !== 'product')
|
|
83
|
+
return null;
|
|
84
|
+
let v = ctx.route.params['variant'];
|
|
85
|
+
if (typeof v !== 'string') {
|
|
86
|
+
try {
|
|
87
|
+
v = new URL(ctx.request.url).searchParams.get('variant');
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
v = null;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const id = typeof v === 'string' ? v.trim() : '';
|
|
94
|
+
return id && id.length <= 200 && /^[A-Za-z0-9_:/.\-]+$/.test(id) ? id : null;
|
|
95
|
+
}
|
|
96
|
+
/** The reasons the platform router sends a cart permalink it could not open to `/cart?cart_link=` with. */
|
|
97
|
+
const CART_LINK_REASONS = new Set(['unavailable', 'failed']);
|
|
98
|
+
/**
|
|
99
|
+
* Why a cart permalink (`/cart/<variant>:<qty>`) landed on the cart page: the router's
|
|
100
|
+
* `?cart_link=` reason (apps/storefront/router/cart-permalink.mjs), else null. Only on the cart
|
|
101
|
+
* route, and only a reason the router sends. The cart route is a private response already (§5).
|
|
102
|
+
*/
|
|
103
|
+
export function cartLinkOf(ctx) {
|
|
104
|
+
if (ctx.route.kind !== 'cart')
|
|
105
|
+
return null;
|
|
106
|
+
try {
|
|
107
|
+
const reason = new URL(ctx.request.url).searchParams.get('cart_link');
|
|
108
|
+
return reason && CART_LINK_REASONS.has(reason) ? reason : null;
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* The route params the page renders with: the route's own, plus `q` (`searchQueryOf`), its `sort`
|
|
116
|
+
* (`searchSortOf`) and `cart_link` (`cartLinkOf`) when only the request's query string has them.
|
|
117
|
+
* They travel in the page state, so the hydrating browser builds the same location.
|
|
118
|
+
*/
|
|
119
|
+
export function pageParamsOf(ctx, searchQuery, searchSort = null) {
|
|
120
|
+
const params = { ...ctx.route.params };
|
|
121
|
+
if (searchQuery !== null && params['q'] === undefined)
|
|
122
|
+
params['q'] = searchQuery;
|
|
123
|
+
// the sort the page was loaded with — the same id the browser's store keys its search on
|
|
124
|
+
if (searchSort !== null && params['sort'] === undefined)
|
|
125
|
+
params['sort'] = searchSort;
|
|
126
|
+
const cartLink = cartLinkOf(ctx);
|
|
127
|
+
if (cartLink !== null)
|
|
128
|
+
params['cart_link'] = cartLink;
|
|
129
|
+
const variant = variantOf(ctx);
|
|
130
|
+
if (variant !== null)
|
|
131
|
+
params['variant'] = variant;
|
|
132
|
+
return params;
|
|
133
|
+
}
|
|
134
|
+
const RESOURCE_KINDS = new Set(['collection', 'product', 'menu', 'page', 'blog']);
|
|
135
|
+
/** Capability each route kind's snapshot resource needs (capability-security §2 / render-context Capability). */
|
|
136
|
+
const RESOURCE_CAPABILITY = {
|
|
137
|
+
product: 'data:catalog', collection: 'data:catalog', collections: 'data:catalog',
|
|
138
|
+
page: 'data:content', blog: 'data:content', article: 'data:content', policy: 'data:content', metaobject: 'data:content',
|
|
139
|
+
search: 'data:search', cart: 'data:cart', customer: 'data:customer',
|
|
140
|
+
};
|
|
141
|
+
/** Capability each round-2 call needs. */
|
|
142
|
+
const NEED_CAPABILITY = {
|
|
143
|
+
collection: 'data:catalog', product: 'data:catalog', page: 'data:content', blog: 'data:content', menu: 'data:shop',
|
|
144
|
+
};
|
|
145
|
+
/** Sections that call `allCollections()`. */
|
|
146
|
+
/** Sections that show recommendations for the route's product. */
|
|
147
|
+
const SHOWS_RECOMMENDATIONS = new Set(['product-recommendations']);
|
|
148
|
+
const LISTS_ALL_COLLECTIONS = new Set(['collection-list', 'collection-links', 'not-found', 'search-results', 'cart-items']);
|
|
149
|
+
function walk(nodes, visit) {
|
|
150
|
+
for (const n of nodes) {
|
|
151
|
+
visit(n);
|
|
152
|
+
if (Array.isArray(n.blocks))
|
|
153
|
+
walk(n.blocks, visit);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Resource settings on this page (effective value = node setting, else the attribute default). A resource list
|
|
158
|
+
* (`{ type: 'list', items: { type: <kind> } }`) names one need per non-empty entry, keyed
|
|
159
|
+
* `${nodeId}.${settingKey}.${index}` exactly as the host keys its snapshot refs (storefront-edge core/snapshot-refs.ts):
|
|
160
|
+
* round 1 answers what fits in the snapshot, round 2 fetches the rest inside the call limit.
|
|
161
|
+
*/
|
|
162
|
+
export function pageNeeds(nodes, defs) {
|
|
163
|
+
const needs = [];
|
|
164
|
+
/** The menu handles the header shows: when every one is empty the drawer lists the collections instead. */
|
|
165
|
+
const headerMenus = [];
|
|
166
|
+
let listsCollections = false;
|
|
167
|
+
let showsLocale = false;
|
|
168
|
+
let showsAccount = false;
|
|
169
|
+
let showsRecommendations = false;
|
|
170
|
+
walk(nodes, (n) => {
|
|
171
|
+
const def = defs(n.type);
|
|
172
|
+
if (!def)
|
|
173
|
+
return;
|
|
174
|
+
if (LISTS_ALL_COLLECTIONS.has(n.type))
|
|
175
|
+
listsCollections = true;
|
|
176
|
+
if (SHOWS_RECOMMENDATIONS.has(n.type))
|
|
177
|
+
showsRecommendations = true;
|
|
178
|
+
const settings = (n.settings ?? {});
|
|
179
|
+
// The header's locale control and the footer's region control (on by default) both list countries.
|
|
180
|
+
if ((n.type === 'header' || n.type === 'footer') && (settings.showLocale ?? def.attributes?.showLocale?.default) === true)
|
|
181
|
+
showsLocale = true;
|
|
182
|
+
if (n.type === 'header' && (settings.showAccount ?? def.attributes?.showAccount?.default) !== false)
|
|
183
|
+
showsAccount = true;
|
|
184
|
+
for (const [key, spec] of Object.entries(def.attributes ?? {})) {
|
|
185
|
+
if (spec.type === 'list') {
|
|
186
|
+
const kind = spec.items?.type;
|
|
187
|
+
const list = settings[key] ?? spec.default;
|
|
188
|
+
if (typeof kind !== 'string' || !RESOURCE_KINDS.has(kind) || !Array.isArray(list))
|
|
189
|
+
continue;
|
|
190
|
+
list.forEach((h, i) => {
|
|
191
|
+
if (typeof h === 'string' && h.trim())
|
|
192
|
+
needs.push({ kind: kind, handle: h.trim(), ref: `${n.id}.${key}.${i}` });
|
|
193
|
+
});
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
if (!RESOURCE_KINDS.has(spec.type))
|
|
197
|
+
continue;
|
|
198
|
+
const value = settings[key] ?? spec.default;
|
|
199
|
+
if (typeof value !== 'string' || !value.trim())
|
|
200
|
+
continue;
|
|
201
|
+
needs.push({ kind: spec.type, handle: value.trim(), ref: `${n.id}.${key}` });
|
|
202
|
+
if (n.type === 'header' && spec.type === 'menu')
|
|
203
|
+
headerMenus.push(value.trim());
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
return { needs, listsCollections, showsLocale, showsAccount, showsRecommendations, headerMenus };
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Picker settings connected to a dynamic source (E1 slice 3): a product, collection or page setting bound to a
|
|
210
|
+
* metafield of the route's resource or the shop (`{ '@source': 'product.metafields.custom.related' }`) names the
|
|
211
|
+
* handle that metafield holds (the projector writes a reference as `{id, handle}`). Those handles are needs like a
|
|
212
|
+
* saved handle, fetched in round 2. A bound value that resolves to nothing is no need: the setting's default renders.
|
|
213
|
+
*/
|
|
214
|
+
export function boundResourceNeeds(nodes, defs, sources) {
|
|
215
|
+
const out = [];
|
|
216
|
+
walk(nodes, (n) => {
|
|
217
|
+
const def = defs(n.type);
|
|
218
|
+
if (!def)
|
|
219
|
+
return;
|
|
220
|
+
const settings = (n.settings ?? {});
|
|
221
|
+
for (const [key, spec] of Object.entries(def.attributes ?? {})) {
|
|
222
|
+
const kind = spec.type;
|
|
223
|
+
if (kind !== 'product' && kind !== 'collection' && kind !== 'page')
|
|
224
|
+
continue;
|
|
225
|
+
const v = settings[key];
|
|
226
|
+
if (!isBoundSource(v))
|
|
227
|
+
continue;
|
|
228
|
+
const handle = (resolveBoundSource(v['@source'], sources) ?? '').trim();
|
|
229
|
+
if (handle && !handle.startsWith('gid://') && !handle.includes(','))
|
|
230
|
+
out.push({ kind, handle, ref: `${n.id}.${key}` });
|
|
231
|
+
}
|
|
232
|
+
});
|
|
233
|
+
return out;
|
|
234
|
+
}
|
|
235
|
+
function languageName(code, inLanguage) {
|
|
236
|
+
try {
|
|
237
|
+
return new Intl.DisplayNames([inLanguage], { type: 'language' }).of(code) ?? code;
|
|
238
|
+
}
|
|
239
|
+
catch {
|
|
240
|
+
return code;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
function intlRegionName(code, inLanguage) {
|
|
244
|
+
try {
|
|
245
|
+
return new Intl.DisplayNames([inLanguage], { type: 'region' }).of(code) ?? code;
|
|
246
|
+
}
|
|
247
|
+
catch {
|
|
248
|
+
return code;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* `ctx.locale` → Nova's Localization. `locale.country` is a personalising input (§2): it is read
|
|
253
|
+
* only when the page shows the country switcher, and the caller hints `vary: ['x-tq-country']`
|
|
254
|
+
* for exactly that case. Otherwise the country stays blank and nothing on the page depends on it.
|
|
255
|
+
*/
|
|
256
|
+
export function localizationOf(ctx, shipsTo, readCountry) {
|
|
257
|
+
const lang = ctx.locale.language;
|
|
258
|
+
const currency = { isoCode: ctx.locale.currency };
|
|
259
|
+
const market = { id: ctx.locale.market, handle: ctx.locale.market, name: ctx.locale.market };
|
|
260
|
+
const country = readCountry ? ctx.locale.country : '';
|
|
261
|
+
const languages = [...new Set(ctx.locale.availableLanguages)];
|
|
262
|
+
// theme-abi Locale-2: the platform's CLDR names in the page's language, when it sends them. The isolate's own
|
|
263
|
+
// Intl.DisplayNames has no data for some languages (Burmese: English names), so it is only the fallback.
|
|
264
|
+
const names = displayNamesOf(ctx);
|
|
265
|
+
const language = (l) => {
|
|
266
|
+
const n = names?.languages[l];
|
|
267
|
+
return n ? { isoCode: l, name: n.name, endonymName: n.endonymName } : { isoCode: l, name: languageName(l, l) };
|
|
268
|
+
};
|
|
269
|
+
const regionName = (iso, inLanguage) => names?.countries[iso] ?? intlRegionName(iso, inLanguage);
|
|
270
|
+
// The store's active markets, when the platform names them (`ctx.locale.markets`, optional): each
|
|
271
|
+
// country gets its own market, currency and languages. Absent ⇒ one market for every country.
|
|
272
|
+
const byCountry = new Map();
|
|
273
|
+
for (const m of marketsOf(ctx)) {
|
|
274
|
+
const own = { id: m.id, handle: m.id, name: m.id };
|
|
275
|
+
const langs = [...new Set(m.languages)].filter((l) => languages.length === 0 || languages.includes(l));
|
|
276
|
+
for (const c of m.countries) {
|
|
277
|
+
const iso = c.toUpperCase();
|
|
278
|
+
if (!/^[A-Z]{2}$/.test(iso) || byCountry.has(iso))
|
|
279
|
+
continue;
|
|
280
|
+
byCountry.set(iso, { isoCode: iso, name: regionName(iso, lang), currency: { isoCode: m.currency }, market: own, ...(langs.length ? { availableLanguages: langs.map(language) } : {}) });
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
const countryOf = (c) => byCountry.get(c) ?? { isoCode: c, name: c ? regionName(c, lang) : '', currency, market };
|
|
284
|
+
const countries = byCountry.size ? [...byCountry.keys()] : shipsTo;
|
|
285
|
+
return {
|
|
286
|
+
country: country ? countryOf(country) : { isoCode: '', name: '', currency, market },
|
|
287
|
+
availableCountries: readCountry ? countries.map(countryOf) : [],
|
|
288
|
+
language: language(lang),
|
|
289
|
+
availableLanguages: languages.map(language),
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
/** `ctx.locale.displayNames` (theme-abi Locale-2, optional), read defensively: only string names; null when absent. */
|
|
293
|
+
export function displayNamesOf(ctx) {
|
|
294
|
+
const raw = ctx.locale.displayNames;
|
|
295
|
+
if (!raw || typeof raw !== 'object')
|
|
296
|
+
return null;
|
|
297
|
+
const countries = {};
|
|
298
|
+
const languages = {};
|
|
299
|
+
if (raw.countries && typeof raw.countries === 'object') {
|
|
300
|
+
for (const [k, v] of Object.entries(raw.countries))
|
|
301
|
+
if (/^[A-Z]{2}$/.test(k) && typeof v === 'string' && v)
|
|
302
|
+
countries[k] = v;
|
|
303
|
+
}
|
|
304
|
+
if (raw.languages && typeof raw.languages === 'object') {
|
|
305
|
+
for (const [k, v] of Object.entries(raw.languages)) {
|
|
306
|
+
const n = v;
|
|
307
|
+
if (n && typeof n.name === 'string' && n.name && typeof n.endonymName === 'string' && n.endonymName)
|
|
308
|
+
languages[k] = { name: n.name, endonymName: n.endonymName };
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
return { countries, languages };
|
|
312
|
+
}
|
|
313
|
+
/** `ctx.locale.markets`, read defensively: only well-formed entries; [] when the platform sends none. */
|
|
314
|
+
export function marketsOf(ctx) {
|
|
315
|
+
const raw = ctx.locale.markets;
|
|
316
|
+
if (!Array.isArray(raw))
|
|
317
|
+
return [];
|
|
318
|
+
const strings = (v) => (Array.isArray(v) ? v.filter((x) => typeof x === 'string' && x.length > 0) : []);
|
|
319
|
+
return raw.flatMap((m) => {
|
|
320
|
+
const r = m;
|
|
321
|
+
if (!r || typeof r.id !== 'string' || !r.id || typeof r.currency !== 'string' || !/^[A-Z]{3}$/.test(r.currency))
|
|
322
|
+
return [];
|
|
323
|
+
return [{ id: r.id, currency: r.currency, countries: strings(r.countries), languages: strings(r.languages) }];
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
/** Load a page's data through `ctx.data`. */
|
|
327
|
+
export async function loadPageData(ctx, nodes, defs,
|
|
328
|
+
/** Menu handles named by GLOBAL settings (header/footer menu pickers), not by a node. */
|
|
329
|
+
settingMenus = [],
|
|
330
|
+
/**
|
|
331
|
+
* The search query this page renders (`searchQueryOf`). When the snapshot answered a different
|
|
332
|
+
* query — a gateway that does not forward `q` as a route param answers the empty one — the
|
|
333
|
+
* search is run once in round 2, so the server HTML holds the results the browser would show.
|
|
334
|
+
*/
|
|
335
|
+
searchQuery = null,
|
|
336
|
+
/** The search sort of this request (`searchSortOf`); null = relevance. */
|
|
337
|
+
searchSort = null,
|
|
338
|
+
/** The collection listing's URL state (`listingQueryOfRequest`); '' = none. */
|
|
339
|
+
listingQuery = '',
|
|
340
|
+
/**
|
|
341
|
+
* What the theme asks the plan for regardless of its section types (`RenderOptions.storefront.plan`), and the
|
|
342
|
+
* data calls the caller makes beside this plan in round 1 (`reservedCalls`). Absent: the plan reads only the
|
|
343
|
+
* section conventions (`pageNeeds`), exactly as before.
|
|
344
|
+
*/
|
|
345
|
+
plan) {
|
|
346
|
+
const found = pageNeeds(nodes, defs);
|
|
347
|
+
const { needs, headerMenus } = found;
|
|
348
|
+
const listsAll = found.listsCollections || plan?.collections === true;
|
|
349
|
+
const showsLocale = found.showsLocale || plan?.localization === true;
|
|
350
|
+
const showsAccount = found.showsAccount || plan?.account === true;
|
|
351
|
+
const showsRecommendations = found.showsRecommendations || plan?.recommendations === true;
|
|
352
|
+
for (const handle of settingMenus)
|
|
353
|
+
needs.push({ kind: 'menu', handle, ref: `settings.${handle}` });
|
|
354
|
+
let calls = plan?.reservedCalls ?? 0;
|
|
355
|
+
const can = (cap) => ctx.capabilities.has(cap);
|
|
356
|
+
// §3 (rev 2026-10-07, IA-2): snapshot() never throws for a denied capability — the resource comes
|
|
357
|
+
// back null and `denied` names the capability. Hosts built before that revision (conformance
|
|
358
|
+
// runner 1966cca among them) still throw, so when the route's resource capability is not in
|
|
359
|
+
// ctx.capabilities Nova does not ask for it at all: shop + menus in one round, and the template
|
|
360
|
+
// renders its "nothing here" state. Either way nothing outside the grant is ever called.
|
|
361
|
+
const resourceCap = RESOURCE_CAPABILITY[ctx.route.kind];
|
|
362
|
+
// Round 1 carries what the plan knows it needs before the snapshot answers (TS-10, 2026-11-19): the store's
|
|
363
|
+
// collection list for a page that lists collections, beside the snapshot, so such a page takes one round instead of
|
|
364
|
+
// two. Each extra round is a cross-region hop (theme-abi §8 `sequentialRounds`); calls inside one round run together.
|
|
365
|
+
// Dev, 2026-11-19 (Tanqory Demo Fashion, server-timing `render`, 12 runs each): a one-round page (/collections)
|
|
366
|
+
// rendered in 75 ms median, 100 ms p90. A route whose own resource is the list (`collections`) needs none of it.
|
|
367
|
+
let early = null;
|
|
368
|
+
if (listsAll && ctx.route.kind !== 'collections' && can('data:catalog') && calls + 2 <= MAX_DATA_CALLS) {
|
|
369
|
+
calls++;
|
|
370
|
+
early = ctx.data.catalog.collections({ first: 50 });
|
|
371
|
+
early.catch(() => undefined); // a refusal is read where it is awaited, never an unhandled rejection
|
|
372
|
+
}
|
|
373
|
+
// The product page's recommendations need only the route's handle, known before the snapshot answers: they ride in
|
|
374
|
+
// round 1 too (TS-10, 2026-11-20). A product that does not exist answers none; the page renders its own empty state.
|
|
375
|
+
const recRoute = ctx.route.kind === 'product' ? ctx.route.handle : undefined;
|
|
376
|
+
let earlyRecs = null;
|
|
377
|
+
if (showsRecommendations && recRoute && can('data:products.recommendations') && calls + 2 <= MAX_DATA_CALLS) {
|
|
378
|
+
calls++;
|
|
379
|
+
earlyRecs = ctx.data.catalog.recommendations(recRoute, 'related', 8);
|
|
380
|
+
earlyRecs.catch(() => undefined); // a refusal is read where it is awaited, never an unhandled rejection
|
|
381
|
+
}
|
|
382
|
+
let snapshot;
|
|
383
|
+
if (!resourceCap || can(resourceCap)) {
|
|
384
|
+
calls++;
|
|
385
|
+
// Opt-in answers (Theme ABI, storefront-query rev 2.19): the collection page's facets, and the
|
|
386
|
+
// product page's collections (its breadcrumb). Asked for only on the route that shows them.
|
|
387
|
+
const optIn = ctx.route.kind === 'collection' ? { facets: true } : ctx.route.kind === 'product' ? { productCollections: true } : {};
|
|
388
|
+
// `policyLinks` (theme-abi alpha.14): the store's published policies on every page — the footer lists
|
|
389
|
+
// them when its legal menu is empty, and the cookie banner links the privacy policy only when published.
|
|
390
|
+
// `reviewsFirst` (storefront-query rev 2.38, NEW-PROD-10): the product page's newest published reviews.
|
|
391
|
+
// A data plane from before either member refuses an unknown option (strict `opts`): ask again without the
|
|
392
|
+
// newest member, then without both, and the page keeps the older behaviour (no review list; no policy links).
|
|
393
|
+
const ask = (extra) => ctx.data.snapshot({ resolve: 'all', ...optIn, ...extra });
|
|
394
|
+
const attempts = ctx.route.kind === 'product' ? [{ policyLinks: true, reviewsFirst: convert.REVIEWS_FIRST }, { policyLinks: true }, {}] : [{ policyLinks: true }, {}];
|
|
395
|
+
let answered = null;
|
|
396
|
+
for (const [i, extra] of attempts.entries()) {
|
|
397
|
+
try {
|
|
398
|
+
answered = await ask(extra);
|
|
399
|
+
break;
|
|
400
|
+
}
|
|
401
|
+
catch (e) {
|
|
402
|
+
const name = e?.name;
|
|
403
|
+
if (name === 'DataLimitError' || name === 'CapabilityError' || i === attempts.length - 1)
|
|
404
|
+
throw e;
|
|
405
|
+
calls++;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
snapshot = answered;
|
|
409
|
+
}
|
|
410
|
+
else {
|
|
411
|
+
calls += 2;
|
|
412
|
+
const [shopRecord, menuList] = await Promise.all([ctx.data.shop.get(), ctx.data.shop.menus()]);
|
|
413
|
+
snapshot = {
|
|
414
|
+
shop: shopRecord,
|
|
415
|
+
menus: Object.fromEntries(menuList.map((m) => [m.handle, m])),
|
|
416
|
+
resource: { kind: 'not_found' },
|
|
417
|
+
refs: {},
|
|
418
|
+
pixels: [],
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
const products = {};
|
|
422
|
+
const collections = {};
|
|
423
|
+
const pages = {};
|
|
424
|
+
const blogs = {};
|
|
425
|
+
const articles = {};
|
|
426
|
+
const menus = {};
|
|
427
|
+
const policies = [];
|
|
428
|
+
let collectionList = null;
|
|
429
|
+
let search = null;
|
|
430
|
+
let policyHandle = null;
|
|
431
|
+
let cart = null;
|
|
432
|
+
let metaobject = null;
|
|
433
|
+
for (const m of Object.values(snapshot.menus ?? {}))
|
|
434
|
+
menus[m.handle] = convert.menu(m);
|
|
435
|
+
const putProduct = (p) => { if (p)
|
|
436
|
+
products[p.handle] = convert.product(p); };
|
|
437
|
+
const putCollection = (c) => {
|
|
438
|
+
if (!c)
|
|
439
|
+
return;
|
|
440
|
+
const nc = convert.collection(c);
|
|
441
|
+
collections[c.handle] = nc;
|
|
442
|
+
for (const p of c.products?.nodes ?? [])
|
|
443
|
+
if (!products[p.handle])
|
|
444
|
+
products[p.handle] = convert.product(p);
|
|
445
|
+
};
|
|
446
|
+
// A `catalog.collections()` entry is a CARD: the platform sends it with an empty product page
|
|
447
|
+
// (`products.nodes: []`, the real `totalCount`). It must never replace a collection a section
|
|
448
|
+
// loaded with its products — that emptied the featured collection on every page that also lists
|
|
449
|
+
// collections (home with a collection list, the cart), and the section then rendered nothing.
|
|
450
|
+
const putCard = (c) => { if (!collections[c.handle])
|
|
451
|
+
collections[c.handle] = convert.collection(c); };
|
|
452
|
+
/** A listed card answers a collection need only when it carries the products (or there are none). */
|
|
453
|
+
const cardAnswers = (handle) => collectionList?.some((c) => c.handle === handle && ((c.products?.length ?? 0) > 0 || c.productsCount === 0)) ?? false;
|
|
454
|
+
const putPage = (p) => { if (p)
|
|
455
|
+
pages[p.handle] = convert.page(p); };
|
|
456
|
+
const putBlog = (b) => {
|
|
457
|
+
if (!b)
|
|
458
|
+
return;
|
|
459
|
+
blogs[b.handle] = convert.blog(b);
|
|
460
|
+
for (const a of b.articles?.nodes ?? [])
|
|
461
|
+
articles[`${b.handle}/${a.handle}`] = { ...convert.article(a), blogHandle: b.handle };
|
|
462
|
+
};
|
|
463
|
+
const putArticle = (a, blogHandle) => { if (a)
|
|
464
|
+
articles[`${blogHandle}/${a.handle}`] = { ...convert.article(a), blogHandle }; };
|
|
465
|
+
const r = snapshot.resource;
|
|
466
|
+
switch (r.kind) {
|
|
467
|
+
case 'product':
|
|
468
|
+
putProduct(r.product);
|
|
469
|
+
break;
|
|
470
|
+
case 'collection':
|
|
471
|
+
putCollection(r.collection);
|
|
472
|
+
break;
|
|
473
|
+
case 'collections':
|
|
474
|
+
if (r.collections) {
|
|
475
|
+
collectionList = r.collections.nodes.map(convert.collection);
|
|
476
|
+
r.collections.nodes.forEach(putCard);
|
|
477
|
+
}
|
|
478
|
+
break;
|
|
479
|
+
case 'page':
|
|
480
|
+
putPage(r.page);
|
|
481
|
+
break;
|
|
482
|
+
case 'blog':
|
|
483
|
+
putBlog(r.blog);
|
|
484
|
+
break;
|
|
485
|
+
case 'article':
|
|
486
|
+
putBlog(r.blog);
|
|
487
|
+
if (r.article)
|
|
488
|
+
putArticle(r.article, r.blog?.handle ?? ctx.route.blogHandle ?? '');
|
|
489
|
+
break;
|
|
490
|
+
case 'policy':
|
|
491
|
+
if (r.policy) {
|
|
492
|
+
policies.push(r.policy);
|
|
493
|
+
policyHandle = r.policy.handle;
|
|
494
|
+
}
|
|
495
|
+
break;
|
|
496
|
+
case 'metaobject':
|
|
497
|
+
metaobject = metaobjectPageOf(r.metaobject, ctx.route.handle);
|
|
498
|
+
break;
|
|
499
|
+
// The snapshot applies the route's `sort` param (rev 2.19). When the sort came only from the URL (a gateway
|
|
500
|
+
// that does not forward it), the snapshot's order is relevance: it is not used, and round 2 asks for the sort.
|
|
501
|
+
case 'search':
|
|
502
|
+
if (r.result && (!searchSort || typeof ctx.route.params['sort'] === 'string')) {
|
|
503
|
+
search = { query: r.result.query, ...(searchSort ? { sort: searchSort } : {}), result: convert.searchResults(r.result) };
|
|
504
|
+
r.result.products.nodes.forEach(putProduct);
|
|
505
|
+
}
|
|
506
|
+
break;
|
|
507
|
+
case 'cart':
|
|
508
|
+
cart = r.cart;
|
|
509
|
+
break;
|
|
510
|
+
default: break;
|
|
511
|
+
}
|
|
512
|
+
if (early) {
|
|
513
|
+
try {
|
|
514
|
+
const c = await early;
|
|
515
|
+
collectionList = c.nodes.map(convert.collection);
|
|
516
|
+
c.nodes.forEach(putCard);
|
|
517
|
+
}
|
|
518
|
+
catch (e) {
|
|
519
|
+
const name = e?.name;
|
|
520
|
+
if (name === 'DataLimitError' || name === 'CapabilityError')
|
|
521
|
+
throw e;
|
|
522
|
+
ctx.log.warn('page data: collection list unavailable', { error: String(e?.message ?? e) });
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
// Picker settings bound to a metafield of the route's resource or the shop: their handles join the needs.
|
|
526
|
+
const routeSources = {
|
|
527
|
+
shop: { ...snapshot.shop, ...convert.metafieldsOf(snapshot.shop) },
|
|
528
|
+
...(r.kind === 'product' && r.product ? { product: products[r.product.handle] } : {}),
|
|
529
|
+
...(r.kind === 'collection' && r.collection ? { collection: collections[r.collection.handle] } : {}),
|
|
530
|
+
...(r.kind === 'page' && r.page ? { page: pages[r.page.handle] } : {}),
|
|
531
|
+
...(metaobject ? { metaobjectPage: metaobject } : {}),
|
|
532
|
+
};
|
|
533
|
+
for (const b of boundResourceNeeds(nodes, defs, routeSources))
|
|
534
|
+
needs.push(b);
|
|
535
|
+
// refs the platform resolved (`${nodeId}.${settingKey}`)
|
|
536
|
+
const missing = [];
|
|
537
|
+
for (const need of needs) {
|
|
538
|
+
if (need.kind === 'menu' && menus[need.handle])
|
|
539
|
+
continue;
|
|
540
|
+
const has = Object.prototype.hasOwnProperty.call(snapshot.refs ?? {}, need.ref);
|
|
541
|
+
if (has) {
|
|
542
|
+
const v = snapshot.refs[need.ref];
|
|
543
|
+
if (v && need.kind === 'collection')
|
|
544
|
+
putCollection(v);
|
|
545
|
+
else if (v && need.kind === 'product')
|
|
546
|
+
putProduct(v);
|
|
547
|
+
else if (v && need.kind === 'menu')
|
|
548
|
+
menus[need.handle] = convert.menu(v);
|
|
549
|
+
else if (v && need.kind === 'page')
|
|
550
|
+
putPage(v);
|
|
551
|
+
else if (v && need.kind === 'blog')
|
|
552
|
+
putBlog(v);
|
|
553
|
+
continue;
|
|
554
|
+
}
|
|
555
|
+
if (need.kind === 'collection' && collections[need.handle])
|
|
556
|
+
continue;
|
|
557
|
+
if (need.kind === 'product' && products[need.handle])
|
|
558
|
+
continue;
|
|
559
|
+
if (need.kind === 'page' && pages[need.handle])
|
|
560
|
+
continue;
|
|
561
|
+
if (need.kind === 'blog' && blogs[need.handle])
|
|
562
|
+
continue;
|
|
563
|
+
if (!missing.some((m) => m.kind === need.kind && m.handle === need.handle))
|
|
564
|
+
missing.push(need);
|
|
565
|
+
}
|
|
566
|
+
// A header whose menu has no items (a new store's empty `main-menu`) lists the store's collections in
|
|
567
|
+
// its mobile drawer, so they are loaded too. Decided on what round 1 answered: a menu it did not
|
|
568
|
+
// answer is fetched in round 2 alongside, and may well be empty.
|
|
569
|
+
const headerMenuEmpty = headerMenus.length > 0 && headerMenus.every((h) => !(menus[h]?.items?.length));
|
|
570
|
+
const listsCollections = listsAll || headerMenuEmpty;
|
|
571
|
+
// round 2 — started together (one round), never past the hard call limit
|
|
572
|
+
const round = [];
|
|
573
|
+
const unresolved = [];
|
|
574
|
+
const take = () => (calls < MAX_DATA_CALLS ? (calls++, true) : false);
|
|
575
|
+
// The snapshot's `menus` is every menu of the store, up to SNAPSHOT_MENUS_MAX (storefront-query §9.1): when it holds
|
|
576
|
+
// fewer, a menu it does not list does not exist, and asking again in round 2 only costs a round (TS-10: Nova's three
|
|
577
|
+
// footer menus did that on every page of a store that has none of them).
|
|
578
|
+
const snapshotListsEveryMenu = Object.keys(snapshot.menus ?? {}).length < SNAPSHOT_MENUS_MAX;
|
|
579
|
+
const missingMenus = snapshotListsEveryMenu ? [] : missing.filter((m) => m.kind === 'menu').map((m) => m.handle);
|
|
580
|
+
if (missingMenus.length) {
|
|
581
|
+
if (take())
|
|
582
|
+
round.push(ctx.data.shop.menus(missingMenus).then((list) => { for (const m of list)
|
|
583
|
+
menus[m.handle] = convert.menu(m); }));
|
|
584
|
+
else
|
|
585
|
+
unresolved.push(...missingMenus.map((h) => `menu:${h}`));
|
|
586
|
+
}
|
|
587
|
+
const wantQuery = ctx.route.kind === 'search' ? (searchQuery ?? '').trim() : '';
|
|
588
|
+
const order = searchOrder(searchSort);
|
|
589
|
+
if (wantQuery && normQuery(search?.query ?? '') !== normQuery(wantQuery) && can('data:search')) {
|
|
590
|
+
if (take()) {
|
|
591
|
+
round.push(ctx.data.catalog.search({ query: wantQuery, first: SEARCH_FIRST, types: ['product', 'page', 'article'], ...(order ? { sortKey: order.sortKey, ...(order.reverse ? { reverse: true } : {}) } : {}) }).then((r) => {
|
|
592
|
+
// Keyed by the query (and sort) the page asked for, so `searchSync(q, sort)` matches on server and browser alike.
|
|
593
|
+
search = { query: wantQuery, ...(searchSort ? { sort: searchSort } : {}), result: convert.searchResults(r) };
|
|
594
|
+
r.products.nodes.forEach(putProduct);
|
|
595
|
+
}).catch((e) => {
|
|
596
|
+
// A breach or a missing capability is the host's to report (entry.abi.tsx). Anything else —
|
|
597
|
+
// a shopper query the data plane refuses (over 1,024 code units ⇒ invalid_argument) — must
|
|
598
|
+
// not take the page down: the search renders its "no results" state. The empty answer is
|
|
599
|
+
// recorded for this query so the browser's first render is the same "no results", not a
|
|
600
|
+
// skeleton that differs from the server HTML.
|
|
601
|
+
const name = e?.name;
|
|
602
|
+
if (name === 'DataLimitError' || name === 'CapabilityError')
|
|
603
|
+
throw e;
|
|
604
|
+
ctx.log.warn('page data: search unavailable', { error: String(e?.message ?? e) });
|
|
605
|
+
const none = { nodes: [], pageInfo: { hasNextPage: false } };
|
|
606
|
+
search = { query: wantQuery, ...(searchSort ? { sort: searchSort } : {}), result: convert.searchResults({ query: wantQuery, products: none, pages: none, articles: none, collections: none }) };
|
|
607
|
+
}));
|
|
608
|
+
}
|
|
609
|
+
else
|
|
610
|
+
unresolved.push('search');
|
|
611
|
+
}
|
|
612
|
+
// A shared filtered collection URL: the first filtered page, when one request answers it.
|
|
613
|
+
let listingSeed = null;
|
|
614
|
+
const listed = ctx.route.kind === 'collection' && ctx.route.handle ? collections[ctx.route.handle] : undefined;
|
|
615
|
+
if (listingQuery && listed && can('data:catalog')) {
|
|
616
|
+
const facets = (listed.filters ?? []).filter((f) => !HIDDEN_FACETS.has(f.id));
|
|
617
|
+
const requests = filterRequests(stateFromQuery(listingQuery, facets), facets, 2);
|
|
618
|
+
const only = requests.length === 1 ? requests[0] : null;
|
|
619
|
+
const filters = only ? convert.abiFilters(only) : [];
|
|
620
|
+
if (only && filters.length) {
|
|
621
|
+
const handle = listed.handle;
|
|
622
|
+
if (take()) {
|
|
623
|
+
round.push(ctx.data.catalog.collection(handle, { productsFirst: LISTING_FIRST, filters }).then((c) => {
|
|
624
|
+
if (!c)
|
|
625
|
+
return;
|
|
626
|
+
listingSeed = { handle, key: JSON.stringify(only), products: (c.products?.nodes ?? []).map(convert.product), pageInfo: { hasNextPage: Boolean(c.products?.pageInfo?.hasNextPage), endCursor: c.products?.pageInfo?.endCursor ?? null } };
|
|
627
|
+
}));
|
|
628
|
+
}
|
|
629
|
+
else
|
|
630
|
+
unresolved.push(`listing:${handle}`);
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
const recommendations = {};
|
|
634
|
+
const recFor = ctx.route.kind === 'product' ? ctx.route.handle : undefined;
|
|
635
|
+
if (earlyRecs && recFor) {
|
|
636
|
+
try {
|
|
637
|
+
const list = await earlyRecs;
|
|
638
|
+
if (products[recFor])
|
|
639
|
+
recommendations[recFor] = list.map(convert.product);
|
|
640
|
+
}
|
|
641
|
+
catch (e) {
|
|
642
|
+
const name = e?.name;
|
|
643
|
+
if (name === 'DataLimitError' || name === 'CapabilityError')
|
|
644
|
+
throw e;
|
|
645
|
+
ctx.log.warn('page data: recommendations unavailable', { error: String(e?.message ?? e) });
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
else if (showsRecommendations && recFor && products[recFor] && can('data:products.recommendations')) {
|
|
649
|
+
if (take())
|
|
650
|
+
round.push(ctx.data.catalog.recommendations(recFor, 'related', 8).then((list) => { recommendations[recFor] = list.map(convert.product); }));
|
|
651
|
+
else
|
|
652
|
+
unresolved.push(`recommendations:${recFor}`);
|
|
653
|
+
}
|
|
654
|
+
if (listsCollections && !collectionList && can('data:catalog')) {
|
|
655
|
+
if (take())
|
|
656
|
+
round.push(ctx.data.catalog.collections({ first: 50 }).then((c) => { collectionList = c.nodes.map(convert.collection); c.nodes.forEach(putCard); }));
|
|
657
|
+
else
|
|
658
|
+
unresolved.push('collections');
|
|
659
|
+
}
|
|
660
|
+
for (const need of missing) {
|
|
661
|
+
if (need.kind === 'menu')
|
|
662
|
+
continue;
|
|
663
|
+
if (need.kind === 'collection' && cardAnswers(need.handle))
|
|
664
|
+
continue;
|
|
665
|
+
const call = need.kind === 'collection' ? () => ctx.data.catalog.collection(need.handle).then(putCollection)
|
|
666
|
+
: need.kind === 'product' ? () => ctx.data.catalog.product(need.handle).then(putProduct)
|
|
667
|
+
: need.kind === 'page' ? () => ctx.data.content.page(need.handle).then(putPage)
|
|
668
|
+
: () => ctx.data.content.blog(need.handle).then(putBlog);
|
|
669
|
+
if (!can(NEED_CAPABILITY[need.kind]))
|
|
670
|
+
unresolved.push(`${need.kind}:${need.handle}`);
|
|
671
|
+
else if (take())
|
|
672
|
+
round.push(call());
|
|
673
|
+
else
|
|
674
|
+
unresolved.push(`${need.kind}:${need.handle}`);
|
|
675
|
+
}
|
|
676
|
+
if (round.length)
|
|
677
|
+
await Promise.all(round);
|
|
678
|
+
const denied = snapshot.denied ?? [];
|
|
679
|
+
if (denied.length)
|
|
680
|
+
ctx.log.info('page data: capabilities denied by the store grant', { denied });
|
|
681
|
+
if (unresolved.length)
|
|
682
|
+
ctx.log.warn('page data: resources left unresolved inside the data-call limit', { unresolved });
|
|
683
|
+
return {
|
|
684
|
+
route: ctx.route.kind,
|
|
685
|
+
shop: convert.shop(snapshot.shop, policies, ctx.locale.country ?? ''),
|
|
686
|
+
menus,
|
|
687
|
+
products,
|
|
688
|
+
collections,
|
|
689
|
+
collectionList,
|
|
690
|
+
pages,
|
|
691
|
+
blogs,
|
|
692
|
+
articles,
|
|
693
|
+
...(ctx.route.kind === 'metaobject' ? { metaobject } : {}),
|
|
694
|
+
search,
|
|
695
|
+
listingSeed,
|
|
696
|
+
recommendations,
|
|
697
|
+
policyHandle,
|
|
698
|
+
accountUrls: ctx.route.kind === 'customer'
|
|
699
|
+
? { orders: ctx.data.customer.accountUrl('orders'), addresses: ctx.data.customer.accountUrl('addresses'), profile: ctx.data.customer.accountUrl('profile') }
|
|
700
|
+
: null,
|
|
701
|
+
cart,
|
|
702
|
+
localization: localizationOf(ctx, snapshot.shop.shipsToCountries ?? [], showsLocale),
|
|
703
|
+
// A personalising input (§2): read only when the account menu renders; the caller varies on it.
|
|
704
|
+
signedIn: showsAccount ? ctx.request.isSignedIn : null,
|
|
705
|
+
unresolved,
|
|
706
|
+
routeMissing: routeMissingOf(ctx.route, { products, collections, pages, blogs, articles, policies, metaobject }),
|
|
707
|
+
};
|
|
708
|
+
}
|
|
709
|
+
/**
|
|
710
|
+
* The snapshot's entry of a metaobject page → the record dynamic sources read (`metaobject.<field>`). `routeHandle` is
|
|
711
|
+
* the platform's `route.handle`, `<definition url handle>/<entry handle>`: the definition's half names the page's URL.
|
|
712
|
+
* Exported for tests.
|
|
713
|
+
*/
|
|
714
|
+
export function metaobjectPageOf(m, routeHandle) {
|
|
715
|
+
if (!m || typeof m.handle !== 'string' || typeof m.type !== 'string')
|
|
716
|
+
return null;
|
|
717
|
+
const cut = typeof routeHandle === 'string' ? routeHandle.indexOf('/') : -1;
|
|
718
|
+
const definitionHandle = cut > 0 ? routeHandle.slice(0, cut) : '';
|
|
719
|
+
const fields = m.fields && typeof m.fields === 'object' && !Array.isArray(m.fields) ? m.fields : {};
|
|
720
|
+
// storefront-query rev 2.62: `Metaobject.seo` — strings kept, anything else dropped
|
|
721
|
+
const s = m.seo && typeof m.seo === 'object' && !Array.isArray(m.seo) ? m.seo : null;
|
|
722
|
+
const seo = s ? { title: typeof s['title'] === 'string' ? s['title'] : null, description: typeof s['description'] === 'string' ? s['description'] : null } : null;
|
|
723
|
+
return { handle: m.handle, type: m.type, definitionHandle, url: definitionHandle ? `/pages/${encodeURIComponent(definitionHandle)}/${encodeURIComponent(m.handle)}` : '', fields, ...(seo && (seo.title || seo.description) ? { seo } : {}) };
|
|
724
|
+
}
|
|
725
|
+
/** An unknown (or not yet published) resource on a resource route is a real 404. Exported for tests. */
|
|
726
|
+
export function routeMissingOf(route, found) {
|
|
727
|
+
const h = route.handle;
|
|
728
|
+
if (!h)
|
|
729
|
+
return false;
|
|
730
|
+
switch (route.kind) {
|
|
731
|
+
case 'product': return !found.products[h];
|
|
732
|
+
case 'collection': return !found.collections[h];
|
|
733
|
+
case 'page': return !found.pages[h];
|
|
734
|
+
case 'blog': return !found.blogs[h];
|
|
735
|
+
// Articles are keyed `<blog>/<article>`; without the blog handle any article with this handle counts.
|
|
736
|
+
case 'article': {
|
|
737
|
+
const blog = route.blogHandle;
|
|
738
|
+
return blog ? !found.articles[`${blog}/${h}`] : !Object.values(found.articles).some((a) => a?.handle === h);
|
|
739
|
+
}
|
|
740
|
+
// The snapshot answers a policy route with the store's PUBLISHED policy for the handle, else null.
|
|
741
|
+
case 'policy': return found.policies.length === 0;
|
|
742
|
+
// A metaobject page: the snapshot answers the entry of the route's definition and handle, else null.
|
|
743
|
+
case 'metaobject': return !found.metaobject;
|
|
744
|
+
default: return false;
|
|
745
|
+
}
|
|
746
|
+
}
|