@shop-on/theme-server 0.10.0 → 0.11.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/dist/account-state.js +1 -2
- package/dist/account.d.ts +1 -3
- package/dist/account.js +1 -3
- package/dist/activity.d.ts +1 -3
- package/dist/activity.js +1 -2
- package/dist/analytics-proxy.d.ts +8 -14
- package/dist/analytics-proxy.js +111 -28
- package/dist/analytics-script-proxy.d.ts +1 -5
- package/dist/analytics-script-proxy.js +4 -14
- package/dist/api-contract/src/addresses.js +32 -0
- package/dist/api-contract/src/areas.js +49 -0
- package/dist/api-contract/src/custom-data.d.ts +31 -0
- package/dist/api-contract/src/custom-data.js +303 -0
- package/dist/api-contract/src/ids.js +110 -0
- package/dist/api-contract/src/image.js +65 -0
- package/dist/api-contract/src/limits.js +44 -0
- package/dist/api-contract/src/media.js +13 -0
- package/dist/api-contract/src/pickup.js +11 -0
- package/dist/api-contract/src/recommendations.js +4 -0
- package/dist/api-contract/src/shops.js +27 -0
- package/dist/api-contract/src/storefront/analytics.d.ts +100 -0
- package/dist/api-contract/src/storefront/analytics.js +138 -0
- package/dist/api-contract/src/storefront/areas.d.ts +2 -0
- package/dist/api-contract/src/storefront/cart.d.ts +132 -0
- package/dist/api-contract/src/storefront/cart.js +248 -0
- package/dist/api-contract/src/storefront/catalog.d.ts +3 -0
- package/dist/api-contract/src/storefront/checkout-analytics.d.ts +2 -0
- package/dist/api-contract/src/storefront/checkout.d.ts +2 -0
- package/dist/api-contract/src/storefront/checkout.js +624 -0
- package/dist/api-contract/src/storefront/client.d.ts +3 -0
- package/dist/api-contract/src/storefront/client.js +6 -0
- package/dist/api-contract/src/storefront/content.d.ts +3 -0
- package/dist/api-contract/src/storefront/custom-data.d.ts +2 -0
- package/dist/api-contract/src/storefront/custom-data.js +27 -0
- package/dist/api-contract/src/storefront/customer.d.ts +2 -0
- package/dist/api-contract/src/storefront/customer.js +261 -0
- package/dist/api-contract/src/storefront/downloads.d.ts +2 -0
- package/dist/api-contract/src/storefront/draft-order-invoice.d.ts +2 -0
- package/dist/api-contract/src/storefront/forms.d.ts +2 -0
- package/dist/api-contract/src/storefront/forms.js +82 -0
- package/dist/api-contract/src/storefront/index.d.ts +20956 -0
- package/dist/api-contract/src/storefront/orders.d.ts +2 -0
- package/dist/api-contract/src/storefront/policies.d.ts +2 -0
- package/dist/api-contract/src/storefront/procedure.d.ts +2 -0
- package/dist/api-contract/src/storefront/procedure.js +29 -0
- package/dist/api-contract/src/storefront/reviews.d.ts +20 -0
- package/dist/api-contract/src/storefront/search.d.ts +3 -0
- package/dist/api-contract/src/storefront/seo.d.ts +2 -0
- package/dist/api-contract/src/storefront/seo.js +132 -0
- package/dist/api-contract/src/storefront/shop.d.ts +2 -0
- package/dist/api-contract/src/storefront/shop.js +101 -0
- package/dist/api-contract/src/taxonomy.js +5 -0
- package/dist/api-contract/src/text.js +15 -0
- package/dist/api-contract/src/theme-documents.js +13 -0
- package/dist/api-contract/src/theme-translations.d.ts +14 -0
- package/dist/api-contract/src/theme-translations.js +30 -0
- package/dist/api-contract/src/video.js +19 -0
- package/dist/availability.d.ts +6 -3
- package/dist/availability.js +11 -5
- package/dist/blog.d.ts +53 -59
- package/dist/blog.js +197 -497
- package/dist/cache.d.ts +5 -6
- package/dist/cache.js +1 -2
- package/dist/canonical-host.d.ts +4 -6
- package/dist/canonical-host.js +5 -6
- package/dist/card.js +159 -0
- package/dist/cart-customer.d.ts +1 -3
- package/dist/cart-customer.js +64 -105
- package/dist/cart-mutations.d.ts +43 -77
- package/dist/cart-mutations.js +106 -232
- package/dist/cart-recommendations.d.ts +1 -3
- package/dist/cart-recommendations.js +1 -2
- package/dist/cart-session.d.ts +1 -3
- package/dist/cart-session.js +1 -2
- package/dist/cart.d.ts +63 -224
- package/dist/cart.js +70 -197
- package/dist/checkout.d.ts +13 -17
- package/dist/checkout.js +75 -101
- package/dist/coalesce.js +57 -0
- package/dist/collection.d.ts +89 -298
- package/dist/collection.js +210 -656
- package/dist/contact.d.ts +20 -10
- package/dist/contact.js +36 -30
- package/dist/content-version.d.ts +37 -21
- package/dist/content-version.js +64 -42
- package/dist/contract-kit/src/client.d.ts +12 -0
- package/dist/contract-kit/src/client.js +156 -0
- package/dist/contract-kit/src/errors.d.ts +1 -0
- package/dist/contract-kit/src/errors.js +81 -0
- package/dist/contract-kit/src/ids.d.ts +1 -0
- package/dist/contract-kit/src/ids.js +118 -0
- package/dist/contract-kit/src/index.d.ts +7 -0
- package/dist/contract-kit/src/money.d.ts +1 -0
- package/dist/contract-kit/src/money.js +10 -0
- package/dist/contract-kit/src/pagination.d.ts +1 -0
- package/dist/contract-kit/src/procedure.d.ts +69 -0
- package/dist/contract-kit/src/procedure.js +16 -0
- package/dist/contract-kit/src/realtime.d.ts +1 -0
- package/dist/cookie-secret.js +1 -2
- package/dist/csrf.js +1 -2
- package/dist/customer-handoff.d.ts +1 -3
- package/dist/customer-handoff.js +10 -25
- package/dist/customer-session.d.ts +1 -3
- package/dist/customer-session.js +1 -2
- package/dist/customer.d.ts +2 -3
- package/dist/customer.js +27 -44
- package/dist/files.d.ts +49 -10
- package/dist/files.js +106 -89
- package/dist/fonts.d.ts +3 -5
- package/dist/fonts.js +16 -35
- package/dist/html.d.ts +1 -3
- package/dist/html.js +1 -2
- package/dist/liquid.d.ts +4 -10
- package/dist/liquid.js +25 -32
- package/dist/listing.js +79 -0
- package/dist/marketing-cookies.d.ts +1 -3
- package/dist/marketing-cookies.js +1 -2
- package/dist/menu.d.ts +46 -155
- package/dist/menu.js +51 -61
- package/dist/metafields.d.ts +9 -5
- package/dist/metafields.js +10 -38
- package/dist/metaobject.d.ts +10 -5
- package/dist/metaobject.js +43 -66
- package/dist/money.d.ts +7 -0
- package/dist/money.js +18 -0
- package/dist/newsletter.d.ts +19 -10
- package/dist/newsletter.js +31 -25
- package/dist/page.d.ts +13 -17
- package/dist/page.js +49 -105
- package/dist/password-session.d.ts +1 -3
- package/dist/password-session.js +1 -2
- package/dist/password.d.ts +25 -18
- package/dist/password.js +41 -59
- package/dist/policy.d.ts +7 -7
- package/dist/policy.js +19 -55
- package/dist/product.d.ts +42 -67
- package/dist/product.js +144 -644
- package/dist/read-ahead.d.ts +18 -0
- package/dist/read-ahead.js +32 -0
- package/dist/recommendations.d.ts +6 -12
- package/dist/recommendations.js +36 -60
- package/dist/review.d.ts +134 -94
- package/dist/review.js +257 -469
- package/dist/search.d.ts +59 -64
- package/dist/search.js +170 -469
- package/dist/seo-context.d.ts +1 -3
- package/dist/seo-context.js +4 -5
- package/dist/seo-documents.d.ts +50 -0
- package/dist/seo-documents.js +149 -0
- package/dist/shop-seo.d.ts +3 -5
- package/dist/shop-seo.js +7 -26
- package/dist/shop.d.ts +38 -42
- package/dist/shop.js +37 -51
- package/dist/store-availability.d.ts +12 -14
- package/dist/store-availability.js +18 -56
- package/dist/storefront.js +199 -0
- package/dist/structured-data.d.ts +3 -28
- package/dist/structured-data.js +19 -49
- package/dist/trace.js +1 -2
- package/dist/translations.d.ts +26 -31
- package/dist/translations.js +55 -28
- package/package.json +16 -14
- package/dist/graphql-env.d.ts +0 -22778
- package/dist/graphql.d.ts +0 -64
- package/dist/graphql.js +0 -344
- package/dist/schema.storefront.graphql +0 -10483
- package/dist/seo-proxy.d.ts +0 -11
- package/dist/seo-proxy.js +0 -26
package/dist/collection.js
CHANGED
|
@@ -1,720 +1,274 @@
|
|
|
1
|
+
import { liteId } from "./api-contract/src/ids.js";
|
|
1
2
|
import { memoTTL } from "./cache.js";
|
|
2
|
-
import {
|
|
3
|
+
import { shapeKey, toCard } from "./card.js";
|
|
4
|
+
import { coalescedReads } from "./coalesce.js";
|
|
3
5
|
import { getContentVersion } from "./content-version.js";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
aggregateRating {
|
|
16
|
-
value
|
|
17
|
-
count
|
|
18
|
-
}
|
|
19
|
-
...BadgeTags
|
|
20
|
-
defaultVariant {
|
|
21
|
-
id
|
|
22
|
-
availableForSale
|
|
23
|
-
sku
|
|
24
|
-
priceV2 {
|
|
25
|
-
amount
|
|
26
|
-
currencyCode
|
|
27
|
-
}
|
|
28
|
-
compareAtV2 {
|
|
29
|
-
amount
|
|
30
|
-
currencyCode
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
# Total variant count drives the card quick-add label (1 = "Add to
|
|
34
|
-
# cart", >1 = "Choose options").
|
|
35
|
-
variantsCount
|
|
36
|
-
# Variant options include linked swatch metadata; card grids only
|
|
37
|
-
# need it when a product-card-swatch block is enabled.
|
|
38
|
-
options @include(if: $includeOptions) {
|
|
39
|
-
id
|
|
40
|
-
name
|
|
41
|
-
position
|
|
42
|
-
linkedMetafield {
|
|
43
|
-
namespace
|
|
44
|
-
key
|
|
45
|
-
}
|
|
46
|
-
values {
|
|
47
|
-
id
|
|
48
|
-
name
|
|
49
|
-
position
|
|
50
|
-
hasVariants
|
|
51
|
-
# The variant this value's swatch selects. One object per value, not a
|
|
52
|
-
# per-product connection: a grid of cards stays inside the storefront
|
|
53
|
-
# complexity budget. Its image is only an id into variantMedia.
|
|
54
|
-
firstSelectableVariant @include(if: $includeVariants) {
|
|
55
|
-
id
|
|
56
|
-
availableForSale
|
|
57
|
-
sku
|
|
58
|
-
priceV2 {
|
|
59
|
-
amount
|
|
60
|
-
currencyCode
|
|
61
|
-
}
|
|
62
|
-
compareAtV2 {
|
|
63
|
-
amount
|
|
64
|
-
currencyCode
|
|
65
|
-
}
|
|
66
|
-
featuredMedia {
|
|
67
|
-
id
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
linkedMetaobject {
|
|
71
|
-
id
|
|
72
|
-
fields {
|
|
73
|
-
key
|
|
74
|
-
value
|
|
75
|
-
reference {
|
|
76
|
-
__typename
|
|
77
|
-
... on ShopMediaImage {
|
|
78
|
-
url(transform: { maxWidth: 128, maxHeight: 128 })
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
# Product cards cap the hover gallery at 4 images, so asking for a
|
|
86
|
-
# fifth just burns storefront complexity budget.
|
|
87
|
-
media(first: $mediaFirst) {
|
|
88
|
-
nodes {
|
|
89
|
-
__typename
|
|
90
|
-
id
|
|
91
|
-
alt
|
|
92
|
-
thumbhash
|
|
93
|
-
... on ShopMediaImage {
|
|
94
|
-
url(transform: { maxWidth: 480 })
|
|
95
|
-
url2x: url(transform: { maxWidth: 480, scale: 2 })
|
|
96
|
-
thumbUrl: url(transform: { maxWidth: 80 })
|
|
97
|
-
width
|
|
98
|
-
height
|
|
99
|
-
focalPoint {
|
|
100
|
-
x
|
|
101
|
-
y
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
... on ShopVideo {
|
|
105
|
-
url
|
|
106
|
-
hlsUrl
|
|
107
|
-
preview {
|
|
108
|
-
url(transform: { maxWidth: 480 })
|
|
109
|
-
}
|
|
110
|
-
width
|
|
111
|
-
height
|
|
112
|
-
duration
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
# The images the swatches above switch to, once each.
|
|
117
|
-
variantMedia @include(if: $includeVariants) {
|
|
118
|
-
__typename
|
|
119
|
-
id
|
|
120
|
-
alt
|
|
121
|
-
... on ShopMediaImage {
|
|
122
|
-
url(transform: { maxWidth: 480 })
|
|
123
|
-
url2x: url(transform: { maxWidth: 480, scale: 2 })
|
|
124
|
-
width
|
|
125
|
-
height
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
# Only the metafields a card's bound sources name (one API filter on top
|
|
129
|
-
# of the storefront-visible gate), and nothing unless some are named.
|
|
130
|
-
metafields(first: $metafieldFirst, keys: $metafieldKeys) @include(if: $withMetafields) {
|
|
131
|
-
nodes {
|
|
132
|
-
namespace
|
|
133
|
-
key
|
|
134
|
-
type
|
|
135
|
-
value
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
`, [BadgeTagsFragment]);
|
|
140
|
-
const CollectionImageFragment = graphql(`
|
|
141
|
-
fragment CollectionImage on FileInterface @_unmask {
|
|
142
|
-
__typename
|
|
143
|
-
id
|
|
144
|
-
alt
|
|
145
|
-
thumbhash
|
|
146
|
-
... on ShopMediaImage {
|
|
147
|
-
url(transform: { maxWidth: 640 })
|
|
148
|
-
url1_5x: url(transform: { maxWidth: 960 })
|
|
149
|
-
url2x: url(transform: { maxWidth: 640, scale: 2 })
|
|
150
|
-
url3x: url(transform: { maxWidth: 640, scale: 3 })
|
|
151
|
-
thumbUrl: url(transform: { maxWidth: 80 })
|
|
152
|
-
width
|
|
153
|
-
height
|
|
154
|
-
focalPoint {
|
|
155
|
-
x
|
|
156
|
-
y
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
`);
|
|
161
|
-
/** A `CollectionProductNode` as the card shape every product grid renders. */
|
|
162
|
-
function toCollectionProduct(p) {
|
|
163
|
-
return {
|
|
164
|
-
id: p.id,
|
|
165
|
-
handle: p.handle,
|
|
166
|
-
title: p.title,
|
|
167
|
-
vendor: p.vendor ?? null,
|
|
168
|
-
product_type: p.productType ?? null,
|
|
169
|
-
aggregateRating: p.aggregateRating,
|
|
170
|
-
tags: p.tags,
|
|
171
|
-
available: p.defaultVariant.availableForSale,
|
|
172
|
-
defaultVariantId: p.defaultVariant.id,
|
|
173
|
-
sku: p.defaultVariant.sku ?? null,
|
|
174
|
-
variantsCount: p.variantsCount,
|
|
175
|
-
price: toMoney(p.defaultVariant.priceV2),
|
|
176
|
-
compare_at: p.defaultVariant.compareAtV2 ? toMoney(p.defaultVariant.compareAtV2) : null,
|
|
177
|
-
media: p.media.nodes.map((n) => toFileInfo(n, { baseWidth: 480 })).filter((m) => m !== null),
|
|
178
|
-
...p.metafields ? { metafields: toMetafieldMap(p.metafields.nodes) } : {},
|
|
179
|
-
...p.options ? { options: p.options.map((o) => ({
|
|
180
|
-
id: o.id,
|
|
181
|
-
name: o.name,
|
|
182
|
-
position: o.position,
|
|
183
|
-
linkedMetafield: o.linkedMetafield ? {
|
|
184
|
-
namespace: o.linkedMetafield.namespace,
|
|
185
|
-
key: o.linkedMetafield.key
|
|
186
|
-
} : null,
|
|
187
|
-
values: o.values.map((v) => ({
|
|
188
|
-
id: v.id,
|
|
189
|
-
name: v.name,
|
|
190
|
-
position: v.position,
|
|
191
|
-
hasVariants: v.hasVariants,
|
|
192
|
-
...v.firstSelectableVariant !== void 0 ? { firstSelectableVariant: toCardVariant(v.firstSelectableVariant) } : {},
|
|
193
|
-
linkedMetaobject: v.linkedMetaobject ? {
|
|
194
|
-
id: v.linkedMetaobject.id,
|
|
195
|
-
fields: v.linkedMetaobject.fields.map((f) => ({
|
|
196
|
-
key: f.key,
|
|
197
|
-
value: f.value,
|
|
198
|
-
imageUrl: f.reference?.__typename === "ShopMediaImage" ? f.reference.url : null
|
|
199
|
-
}))
|
|
200
|
-
} : null
|
|
201
|
-
}))
|
|
202
|
-
})) } : {},
|
|
203
|
-
...p.variantMedia ? { variantMedia: swatchMedia(p.variantMedia, p.options) } : {}
|
|
204
|
-
};
|
|
6
|
+
import { imageFileInfo } from "./files.js";
|
|
7
|
+
import { listingCard, listingInput, pageInput, toCollectionListItem, toFacets, toPriceBounds } from "./listing.js";
|
|
8
|
+
import { customDataMap } from "./metafields.js";
|
|
9
|
+
import { orFallback, orNotFound, storefront } from "./storefront.js";
|
|
10
|
+
// src/collection.ts
|
|
11
|
+
/** A collection by handle or id (a `collection_…` TypeID, as a collection
|
|
12
|
+
* metafield's value holds it). Any other id is read as a handle, so a legacy
|
|
13
|
+
* `gid://` matches nothing. */
|
|
14
|
+
function collectionRef(ref) {
|
|
15
|
+
const id = liteId("collection").safeParse(ref);
|
|
16
|
+
return id.success ? { collectionId: id.data } : { handle: ref };
|
|
205
17
|
}
|
|
206
|
-
/**
|
|
207
|
-
*
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
if (!v) return null;
|
|
18
|
+
/** One `collections.get` behind every lean read of a collection: the meta, the
|
|
19
|
+
* description, the liquid drop, the metafields, and the page's own fields.
|
|
20
|
+
* Null while a buyer may not see it (NOT_FOUND) and on a failed read, which is
|
|
21
|
+
* not cached. */
|
|
22
|
+
const fetchCollectionRecord = memoTTL(async (ref, locale) => {
|
|
23
|
+
const c = await orFallback(orNotFound(storefront(locale).collections.get(collectionRef(ref))), null, "fetchCollection");
|
|
24
|
+
if (!c) return null;
|
|
25
|
+
const image = c.image ? imageFileInfo(c.image, 640) : null;
|
|
215
26
|
return {
|
|
216
|
-
id:
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
27
|
+
id: c.id,
|
|
28
|
+
meta: {
|
|
29
|
+
handle: c.handle,
|
|
30
|
+
title: c.title,
|
|
31
|
+
themeTemplate: c.themeTemplate ?? "collection"
|
|
32
|
+
},
|
|
33
|
+
descriptionHtml: c.descriptionHtml.trim() === "" ? "" : c.descriptionHtml,
|
|
34
|
+
descriptionText: c.descriptionText,
|
|
35
|
+
image,
|
|
36
|
+
seo: {
|
|
37
|
+
title: c.seoTitle,
|
|
38
|
+
description: c.seoDescription
|
|
39
|
+
},
|
|
40
|
+
metafields: customDataMap(c.metafields),
|
|
41
|
+
data: {
|
|
42
|
+
title: c.title,
|
|
43
|
+
handle: c.handle,
|
|
44
|
+
description: c.descriptionText,
|
|
45
|
+
image,
|
|
46
|
+
products_count: c.productsCount,
|
|
47
|
+
products: []
|
|
48
|
+
}
|
|
222
49
|
};
|
|
50
|
+
}, {
|
|
51
|
+
key: (ref, locale) => `collection|${ref}|${locale ?? ""}`,
|
|
52
|
+
shouldCache: (v) => v !== null,
|
|
53
|
+
version: getContentVersion
|
|
54
|
+
});
|
|
55
|
+
/** Lean collection lookup for breadcrumbs / contextual labels, and for
|
|
56
|
+
* resolving a collection's assigned theme template. Null for a collection a
|
|
57
|
+
* buyer may not see, and on a failed read. */
|
|
58
|
+
async function fetchCollectionMeta(ref, locale) {
|
|
59
|
+
return (await fetchCollectionRecord(ref, locale))?.meta ?? null;
|
|
223
60
|
}
|
|
224
|
-
/**
|
|
225
|
-
*
|
|
226
|
-
*
|
|
227
|
-
function
|
|
228
|
-
return
|
|
61
|
+
/** The collection's description HTML alone — the `collection_attribute:description`
|
|
62
|
+
* dynamic source; the theme loader calls it only when a template binds the
|
|
63
|
+
* description. null on a miss, a failure, or an empty description. */
|
|
64
|
+
async function fetchCollectionDescriptionHtml(handle, locale) {
|
|
65
|
+
return (await fetchCollectionRecord(handle, locale))?.descriptionHtml || null;
|
|
66
|
+
}
|
|
67
|
+
/** The collection's storefront-visible metafields keyed `"namespace.key"` —
|
|
68
|
+
* the `collection_metafield` dynamic source, off the same record read. `{}`
|
|
69
|
+
* on a miss or a failure, so the literal renders. */
|
|
70
|
+
async function fetchCollectionMetafields(handle, locale) {
|
|
71
|
+
return (await fetchCollectionRecord(handle, locale))?.metafields ?? {};
|
|
229
72
|
}
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
) {
|
|
242
|
-
collection(handle: $handle, id: $id) {
|
|
243
|
-
title
|
|
244
|
-
handle
|
|
245
|
-
# Collection cards line-clamp the description to ~2 lines; cap the
|
|
246
|
-
# projection like descriptionPlain does below instead of dragging the
|
|
247
|
-
# full body copy into every consumer's payload.
|
|
248
|
-
description(truncateAt: 320)
|
|
249
|
-
image {
|
|
250
|
-
...CollectionImage
|
|
251
|
-
}
|
|
252
|
-
productsCount
|
|
253
|
-
products(first: $first) {
|
|
254
|
-
nodes {
|
|
255
|
-
...CollectionProductNode
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
`, [CollectionProductNodeFragment, CollectionImageFragment]);
|
|
261
|
-
function toMoney(m) {
|
|
73
|
+
/** The liquid pass's `collection` drop source. The drop is metadata only
|
|
74
|
+
* (`liquidCollection` maps no products; the grid's own page fetch stays the
|
|
75
|
+
* product source), so it carries none. */
|
|
76
|
+
const fetchLiquidCollection = async (handle, locale) => (await fetchCollectionRecord(handle, locale))?.data ?? null;
|
|
77
|
+
/** The route collection as the theme loader's dynamic sources read it
|
|
78
|
+
* (`DynamicSourceCollection` in `@shop-on/theme-loader`): the title off the
|
|
79
|
+
* lean meta read the theme already made, and a lazy provider for each source
|
|
80
|
+
* the loader fetches only when a template binds it. One builder so a theme
|
|
81
|
+
* passes `routeCollectionSource(meta, locale)` and picks up a new provider on
|
|
82
|
+
* upgrade, instead of every theme re-listing them. */
|
|
83
|
+
function routeCollectionSource(meta, locale) {
|
|
262
84
|
return {
|
|
263
|
-
|
|
264
|
-
|
|
85
|
+
title: meta.title,
|
|
86
|
+
getDescriptionHtml: () => fetchCollectionDescriptionHtml(meta.handle, locale),
|
|
87
|
+
getMetafields: () => fetchCollectionMetafields(meta.handle, locale)
|
|
265
88
|
};
|
|
266
89
|
}
|
|
267
|
-
|
|
268
|
-
|
|
90
|
+
/** Collection card reads of this request's tick in one language share a
|
|
91
|
+
* `collections.cards` call. */
|
|
92
|
+
const coalescedCard = coalescedReads({
|
|
93
|
+
max: 50,
|
|
94
|
+
matches: (card, ref) => {
|
|
95
|
+
const named = collectionRef(ref);
|
|
96
|
+
return "collectionId" in named ? card.id === named.collectionId : card.handle === named.handle;
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
/** One collection's card, in the `collections.cards` call this request's
|
|
100
|
+
* tick shares for its language. Null when a buyer may not see it, and on a
|
|
101
|
+
* failed read (logged under `label`). */
|
|
102
|
+
function readCollectionCard(ref, locale, label) {
|
|
103
|
+
return orFallback(coalescedCard(locale ?? "", ref, async (refs) => {
|
|
104
|
+
return (await storefront(locale).collections.cards({ refs: refs.map(collectionRef) })).items;
|
|
105
|
+
}), null, label);
|
|
269
106
|
}
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
...parseRef(handle),
|
|
273
|
-
first,
|
|
274
|
-
mediaFirst,
|
|
275
|
-
includeOptions,
|
|
276
|
-
...variantsVariable(includeVariants),
|
|
277
|
-
...metafieldKeysVariables(metafieldKeys)
|
|
278
|
-
}, locale ? { acceptLanguage: locale } : void 0)).collection;
|
|
279
|
-
if (!c) return null;
|
|
107
|
+
/** A card as `CollectionData`, with no products, `image` drawn from `image`. */
|
|
108
|
+
function cardData(c, image) {
|
|
280
109
|
return {
|
|
281
110
|
title: c.title,
|
|
282
111
|
handle: c.handle,
|
|
283
|
-
description: c.
|
|
284
|
-
image:
|
|
112
|
+
description: c.descriptionText,
|
|
113
|
+
image: image ? imageFileInfo(image, 640) : null,
|
|
285
114
|
products_count: c.productsCount,
|
|
286
|
-
products:
|
|
115
|
+
products: []
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
/** A collection as a card draws it: meta plus Shopify's `featured_image`,
|
|
119
|
+
* with no products. `image` carries the featured image (its own, else its
|
|
120
|
+
* first visible product's first media), so a reader written as
|
|
121
|
+
* `image ?? products[0].media[0]` (canvas, modern and volt spell it so)
|
|
122
|
+
* resolves the same picture from an empty `products`. */
|
|
123
|
+
const fetchCollectionCard = memoTTL(async (ref, locale) => {
|
|
124
|
+
const c = await readCollectionCard(ref, locale, "fetchCollectionCard");
|
|
125
|
+
return c && cardData(c, c.featuredImage);
|
|
126
|
+
}, {
|
|
127
|
+
key: (h, locale) => `card|${h}|${locale ?? ""}`,
|
|
128
|
+
shouldCache: (v) => v !== null,
|
|
129
|
+
version: getContentVersion
|
|
130
|
+
});
|
|
131
|
+
const fetchCollectionCached = memoTTL(async ({ handle, first, locale, ...card }) => {
|
|
132
|
+
const [c, listing] = await Promise.all([readCollectionCard(handle, locale, "fetchCollection"), first > 0 ? orNotFound(storefront(locale).collections.products({
|
|
133
|
+
...collectionRef(handle),
|
|
134
|
+
limit: Math.min(first, 50),
|
|
135
|
+
card: listingCard(card)
|
|
136
|
+
})) : null]);
|
|
137
|
+
if (!c) return null;
|
|
138
|
+
return {
|
|
139
|
+
...cardData(c, c.image),
|
|
140
|
+
products: listing?.items.map(toCard) ?? []
|
|
287
141
|
};
|
|
288
142
|
}, {
|
|
289
|
-
key: ({ handle, first,
|
|
143
|
+
key: ({ handle, first, locale, ...card }) => `${handle}|${first}|${shapeKey(listingCard(card))}|${locale ?? ""}`,
|
|
144
|
+
shouldCache: (v) => v !== null,
|
|
290
145
|
version: getContentVersion
|
|
291
146
|
});
|
|
147
|
+
/** A collection with its first `first` products in its own order, for the
|
|
148
|
+
* `collection` settings that render a grid of it. Null for a collection a
|
|
149
|
+
* buyer may not see, and on a failed read. */
|
|
292
150
|
const fetchCollection = async (data) => {
|
|
293
151
|
try {
|
|
294
152
|
return await fetchCollectionCached({
|
|
295
153
|
...data,
|
|
296
154
|
mediaFirst: data.mediaFirst ?? 4,
|
|
297
|
-
includeOptions: data.includeOptions ?? true
|
|
298
|
-
includeVariants: data.includeVariants ?? false
|
|
155
|
+
includeOptions: data.includeOptions ?? true
|
|
299
156
|
});
|
|
300
157
|
} catch (err) {
|
|
301
158
|
console.warn("[fetchCollection] failed:", err);
|
|
302
159
|
return null;
|
|
303
160
|
}
|
|
304
161
|
};
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
});
|
|
315
|
-
const FetchCollectionCardQuery = graphql(`
|
|
316
|
-
query FetchCollectionCard($handle: String, $id: ID) {
|
|
317
|
-
collection(handle: $handle, id: $id) {
|
|
318
|
-
title
|
|
319
|
-
handle
|
|
320
|
-
description(truncateAt: 320)
|
|
321
|
-
# The collection's own picture, else its first product's first image.
|
|
322
|
-
# The API loads a product only for a collection without a picture.
|
|
323
|
-
featuredImage {
|
|
324
|
-
...CollectionImage
|
|
325
|
-
}
|
|
326
|
-
productsCount
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
`, [CollectionImageFragment]);
|
|
330
|
-
/** A collection as a card draws it: meta plus Shopify's `featured_image`,
|
|
331
|
-
* with no products. `image` carries the featured image, so a reader written
|
|
332
|
-
* as `image ?? products[0].media[0]` (canvas, modern and volt spell it so)
|
|
333
|
-
* resolves the same picture from an empty `products`. A product page per
|
|
334
|
-
* card was what `fetchCollection` at one product cost for that fallback. */
|
|
335
|
-
const fetchCollectionCard = memoTTL(async (handle, locale) => {
|
|
336
|
-
try {
|
|
337
|
-
const c = (await request(FetchCollectionCardQuery, parseRef(handle), locale ? { acceptLanguage: locale } : void 0)).collection;
|
|
338
|
-
if (!c) return null;
|
|
339
|
-
return {
|
|
340
|
-
title: c.title,
|
|
341
|
-
handle: c.handle,
|
|
342
|
-
description: c.description,
|
|
343
|
-
image: c.featuredImage ? toFileInfo(c.featuredImage, { baseWidth: 640 }) : null,
|
|
344
|
-
products_count: c.productsCount,
|
|
345
|
-
products: []
|
|
346
|
-
};
|
|
347
|
-
} catch (err) {
|
|
348
|
-
console.warn("[fetchCollectionCard] failed:", err);
|
|
349
|
-
return null;
|
|
350
|
-
}
|
|
351
|
-
}, {
|
|
352
|
-
key: (h, locale) => `card|${h}|${locale ?? ""}`,
|
|
353
|
-
shouldCache: (v) => v !== null,
|
|
354
|
-
version: getContentVersion
|
|
355
|
-
});
|
|
356
|
-
/** Maps the GraphQL `ProductPriceRange` (Decimal serialized as string, or
|
|
357
|
-
* null) to numeric bounds. Must guard null explicitly: a bare `Number(null)`
|
|
358
|
-
* is `0`, which is NOT nullish and would defeat the `?? defaultPriceMaxBound`
|
|
359
|
-
* fallback the sections rely on for empty catalogs. */
|
|
360
|
-
function toPriceBounds(range) {
|
|
361
|
-
return {
|
|
362
|
-
min: range.min == null ? null : Number(range.min),
|
|
363
|
-
max: range.max == null ? null : Number(range.max)
|
|
364
|
-
};
|
|
365
|
-
}
|
|
162
|
+
const LISTING_SORT = {
|
|
163
|
+
FEATURED: "featured",
|
|
164
|
+
CREATED_DESC: "newest",
|
|
165
|
+
TITLE_ASC: "title_asc",
|
|
166
|
+
TITLE_DESC: "title_desc",
|
|
167
|
+
PRICE_ASC: "price_asc",
|
|
168
|
+
PRICE_DESC: "price_desc",
|
|
169
|
+
BEST_SELLING: "best_selling"
|
|
170
|
+
};
|
|
366
171
|
const COLLECTION_PAGE_SIZE = 24;
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
$includeOptions: Boolean!
|
|
373
|
-
$includeVariants: Boolean = false
|
|
374
|
-
$withMetafields: Boolean = false
|
|
375
|
-
$metafieldFirst: Int = 0
|
|
376
|
-
$metafieldKeys: [MetafieldKeyInput!] = []
|
|
377
|
-
$after: String
|
|
378
|
-
$filter: CollectionProductFilterInput
|
|
379
|
-
$sort: CollectionProductSortEnum
|
|
380
|
-
) {
|
|
381
|
-
collection(handle: $handle) {
|
|
382
|
-
id
|
|
383
|
-
title
|
|
384
|
-
handle
|
|
385
|
-
themeTemplate
|
|
386
|
-
description
|
|
387
|
-
descriptionHtml
|
|
388
|
-
# Server-side plain-text projection capped at the meta-description
|
|
389
|
-
# storage limit — Rails strip_tags + truncate, word-boundary aware,
|
|
390
|
-
# entity-decoded. Matches the Product PDP query's descriptionPlain.
|
|
391
|
-
descriptionPlain: description(truncateAt: 320)
|
|
392
|
-
seo {
|
|
393
|
-
title
|
|
394
|
-
description
|
|
395
|
-
}
|
|
396
|
-
image {
|
|
397
|
-
...CollectionImage
|
|
398
|
-
}
|
|
399
|
-
productsCount
|
|
400
|
-
products(first: $first, after: $after, filter: $filter, sort: $sort) {
|
|
401
|
-
totalCount
|
|
402
|
-
pageInfo {
|
|
403
|
-
endCursor
|
|
404
|
-
hasNextPage
|
|
405
|
-
}
|
|
406
|
-
nodes {
|
|
407
|
-
...CollectionProductNode
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
productFacets(filter: $filter) {
|
|
411
|
-
namespace
|
|
412
|
-
key
|
|
413
|
-
label
|
|
414
|
-
values {
|
|
415
|
-
value
|
|
416
|
-
label
|
|
417
|
-
count
|
|
418
|
-
selected
|
|
419
|
-
swatch {
|
|
420
|
-
hex
|
|
421
|
-
image {
|
|
422
|
-
__typename
|
|
423
|
-
... on ShopMediaImage {
|
|
424
|
-
url(transform: { maxWidth: 128, maxHeight: 128 })
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
# Present only on the price facet — min/max bounds for the slider.
|
|
430
|
-
range {
|
|
431
|
-
min
|
|
432
|
-
max
|
|
433
|
-
}
|
|
434
|
-
}
|
|
435
|
-
# Unfiltered min/max default-variant price, for seeding the price-filter
|
|
436
|
-
# slider's bounds. Deliberately not passed $filter so the slider
|
|
437
|
-
# endpoints stay stable as the buyer toggles filters.
|
|
438
|
-
priceRange {
|
|
439
|
-
min
|
|
440
|
-
max
|
|
441
|
-
}
|
|
442
|
-
}
|
|
443
|
-
}
|
|
444
|
-
`, [CollectionProductNodeFragment, CollectionImageFragment]);
|
|
445
|
-
/** Server-driven collection listing. Always returns `null` on missing
|
|
446
|
-
* collection / GraphQL error so the route loader can map the failure to
|
|
447
|
-
* `notFound()`. Uncached because filter/sort/cursor permutations would
|
|
448
|
-
* blow the TTL cache; the underlying API is fast. */
|
|
172
|
+
/** Server-driven collection listing: the collection's record and one page
|
|
173
|
+
* of its listing, read together. `null` on a collection a buyer may not see
|
|
174
|
+
* and on a failed read, so the route loader can map it to `notFound()`.
|
|
175
|
+
* Uncached because filter/sort/page permutations would blow the TTL cache;
|
|
176
|
+
* the record under it is cached. */
|
|
449
177
|
const fetchCollectionPage = async (data) => {
|
|
450
|
-
const
|
|
451
|
-
const first = data.first ?? COLLECTION_PAGE_SIZE;
|
|
452
|
-
const mediaFirst = data.mediaFirst ?? 4;
|
|
453
|
-
const includeOptions = data.includeOptions ?? true;
|
|
178
|
+
const input = listingInput(data.filter, data.page ?? 1, data.first ?? COLLECTION_PAGE_SIZE);
|
|
454
179
|
try {
|
|
455
|
-
const
|
|
456
|
-
handle,
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
if (!c) return null;
|
|
180
|
+
const [record, listing] = await Promise.all([fetchCollectionRecord(data.handle, data.locale), orNotFound(storefront(data.locale).collections.products({
|
|
181
|
+
...collectionRef(data.handle),
|
|
182
|
+
...input,
|
|
183
|
+
sort: LISTING_SORT[data.sort ?? "FEATURED"],
|
|
184
|
+
card: listingCard({
|
|
185
|
+
mediaFirst: data.mediaFirst ?? 4,
|
|
186
|
+
includeOptions: data.includeOptions ?? true,
|
|
187
|
+
metafieldKeys: data.metafieldKeys
|
|
188
|
+
})
|
|
189
|
+
}))]);
|
|
190
|
+
if (!record || !listing) return null;
|
|
467
191
|
return {
|
|
468
|
-
id:
|
|
469
|
-
title:
|
|
470
|
-
handle:
|
|
471
|
-
themeTemplate:
|
|
472
|
-
description:
|
|
473
|
-
description_html:
|
|
474
|
-
description_plain:
|
|
475
|
-
seo:
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
page_info: {
|
|
483
|
-
end_cursor: c.products.pageInfo.endCursor ?? null,
|
|
484
|
-
has_next_page: c.products.pageInfo.hasNextPage
|
|
485
|
-
},
|
|
486
|
-
products: c.products.nodes.map(toCollectionProduct),
|
|
487
|
-
facets: c.productFacets.map((f) => ({
|
|
488
|
-
namespace: f.namespace,
|
|
489
|
-
key: f.key,
|
|
490
|
-
label: f.label,
|
|
491
|
-
values: f.values.map((v) => ({
|
|
492
|
-
value: v.value,
|
|
493
|
-
label: v.label,
|
|
494
|
-
count: v.count,
|
|
495
|
-
selected: v.selected,
|
|
496
|
-
swatch: v.swatch ? {
|
|
497
|
-
hex: v.swatch.hex ?? null,
|
|
498
|
-
imageUrl: v.swatch.image?.__typename === "ShopMediaImage" ? v.swatch.image.url : null
|
|
499
|
-
} : null
|
|
500
|
-
})),
|
|
501
|
-
range: f.range ? toPriceBounds(f.range) : null
|
|
502
|
-
})),
|
|
503
|
-
price_bounds: toPriceBounds(c.priceRange)
|
|
192
|
+
id: record.id,
|
|
193
|
+
title: record.meta.title,
|
|
194
|
+
handle: record.meta.handle,
|
|
195
|
+
themeTemplate: record.meta.themeTemplate,
|
|
196
|
+
description: record.descriptionText,
|
|
197
|
+
description_html: record.descriptionHtml,
|
|
198
|
+
description_plain: record.descriptionText,
|
|
199
|
+
seo: record.seo,
|
|
200
|
+
image: record.image,
|
|
201
|
+
products_count: record.data.products_count,
|
|
202
|
+
total_count: listing.total,
|
|
203
|
+
products: listing.items.map(toCard),
|
|
204
|
+
facets: toFacets(listing.facets, input.selections),
|
|
205
|
+
price_bounds: toPriceBounds(listing.priceRange)
|
|
504
206
|
};
|
|
505
207
|
} catch (err) {
|
|
506
208
|
console.warn("[fetchCollectionPage] failed:", err);
|
|
507
209
|
return null;
|
|
508
210
|
}
|
|
509
211
|
};
|
|
510
|
-
const
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
$metafieldKeys: [MetafieldKeyInput!] = []
|
|
519
|
-
) {
|
|
520
|
-
products(first: $first) {
|
|
521
|
-
nodes {
|
|
522
|
-
...CollectionProductNode
|
|
523
|
-
}
|
|
524
|
-
}
|
|
525
|
-
}
|
|
526
|
-
`, [CollectionProductNodeFragment]);
|
|
527
|
-
const loadAllProducts = memoTTL(async (first, mediaFirst, includeOptions, includeVariants, locale) => {
|
|
528
|
-
return (await request(FetchAllProductsQuery, {
|
|
529
|
-
first,
|
|
530
|
-
mediaFirst,
|
|
531
|
-
includeOptions,
|
|
532
|
-
...variantsVariable(includeVariants)
|
|
533
|
-
}, locale ? { acceptLanguage: locale } : void 0)).products.nodes.map(toCollectionProduct);
|
|
212
|
+
const loadAllProducts = memoTTL(async (first, mediaFirst, includeOptions, locale) => {
|
|
213
|
+
return (await storefront(locale).products.list({
|
|
214
|
+
limit: Math.min(first, 50),
|
|
215
|
+
card: listingCard({
|
|
216
|
+
mediaFirst,
|
|
217
|
+
includeOptions
|
|
218
|
+
})
|
|
219
|
+
})).items.map(toCard);
|
|
534
220
|
}, {
|
|
535
|
-
key: (first, mediaFirst, includeOptions,
|
|
221
|
+
key: (first, mediaFirst, includeOptions, locale) => `all|${first}|${mediaFirst}|${includeOptions}|${locale ?? ""}`,
|
|
536
222
|
version: getContentVersion
|
|
537
223
|
});
|
|
538
|
-
/** Shopify's `collections.all` — the shop's whole
|
|
539
|
-
* product list,
|
|
540
|
-
*
|
|
541
|
-
*
|
|
542
|
-
* callers, so it is the same set the automatic "all" collection would hold,
|
|
543
|
-
* newest first.
|
|
224
|
+
/** Shopify's `collections.all` — the shop's whole catalogue a buyer may see
|
|
225
|
+
* as a product list, newest first (`products.list`'s featured order), for
|
|
226
|
+
* the surfaces a theme falls back to when the merchant has picked no
|
|
227
|
+
* collection (Horizon's search empty states).
|
|
544
228
|
*
|
|
545
|
-
* Cached, unlike the collection listing
|
|
546
|
-
*
|
|
547
|
-
*
|
|
229
|
+
* Cached, unlike the collection listing: this read takes no filter, sort or
|
|
230
|
+
* page, so there is exactly one result per (size, locale) — and the search
|
|
231
|
+
* modal's copy of it rides along on every page load. An EMPTY catalogue
|
|
548
232
|
* caches like any other answer; only a thrown read skips the store, which is
|
|
549
|
-
* why the failure is caught out here rather than inside the memo.
|
|
550
|
-
* catalog is genuinely empty would otherwise re-ask on every render, forever.
|
|
233
|
+
* why the failure is caught out here rather than inside the memo.
|
|
551
234
|
*
|
|
552
235
|
* `[]` on a failure, so an empty-state grid renders empty rather than breaking
|
|
553
236
|
* the page it garnishes. */
|
|
554
|
-
async function fetchAllProducts(first, mediaFirst, includeOptions, locale
|
|
237
|
+
async function fetchAllProducts(first, mediaFirst, includeOptions, locale) {
|
|
555
238
|
try {
|
|
556
|
-
return await loadAllProducts(first, mediaFirst, includeOptions,
|
|
239
|
+
return await loadAllProducts(first, mediaFirst, includeOptions, locale);
|
|
557
240
|
} catch (err) {
|
|
558
241
|
console.warn("[fetchAllProducts] failed:", err);
|
|
559
242
|
return [];
|
|
560
243
|
}
|
|
561
244
|
}
|
|
562
245
|
const COLLECTIONS_LIST_PAGE_SIZE = 16;
|
|
563
|
-
const
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
descriptionHtml
|
|
576
|
-
productsCount
|
|
577
|
-
featuredImage {
|
|
578
|
-
...CollectionImage
|
|
579
|
-
}
|
|
580
|
-
}
|
|
581
|
-
}
|
|
582
|
-
}
|
|
583
|
-
`, [CollectionImageFragment]);
|
|
584
|
-
/** Server-driven collection list. Always returns `null` on GraphQL error so
|
|
585
|
-
* the route loader can map the failure to `notFound()`. Uncached because
|
|
586
|
-
* cursor permutations would blow the TTL cache; the underlying API is fast. */
|
|
246
|
+
const COLLECTION_LIST_SORT = {
|
|
247
|
+
TITLE_ASC: "title_asc",
|
|
248
|
+
TITLE_DESC: "title_desc",
|
|
249
|
+
CREATED_DESC: "newest",
|
|
250
|
+
CREATED_ASC: "oldest",
|
|
251
|
+
PRODUCTS_COUNT_DESC: "products_desc",
|
|
252
|
+
PRODUCTS_COUNT_ASC: "products_asc"
|
|
253
|
+
};
|
|
254
|
+
/** Server-driven collection list, newest first unless `sort` names another
|
|
255
|
+
* order (legacy `collections`' default, which a theme asking for no sort got).
|
|
256
|
+
* `null` on a failed read so the route loader can map the failure to
|
|
257
|
+
* `notFound()`. Uncached because page permutations would blow the TTL cache. */
|
|
587
258
|
const fetchCollectionsPage = async (data) => {
|
|
588
|
-
const
|
|
259
|
+
const perPage = Math.min(data.first ?? COLLECTIONS_LIST_PAGE_SIZE, 50);
|
|
589
260
|
try {
|
|
590
|
-
const
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
}, data.locale ? { acceptLanguage: data.locale } : void 0)).collections;
|
|
261
|
+
const answer = await storefront(data.locale).collections.list({
|
|
262
|
+
sort: COLLECTION_LIST_SORT[data.sort ?? "CREATED_DESC"],
|
|
263
|
+
...pageInput(data.page ?? 1, perPage)
|
|
264
|
+
});
|
|
595
265
|
return {
|
|
596
|
-
total_count:
|
|
597
|
-
|
|
598
|
-
end_cursor: conn.pageInfo.endCursor ?? null,
|
|
599
|
-
has_next_page: conn.pageInfo.hasNextPage
|
|
600
|
-
},
|
|
601
|
-
items: conn.nodes.map((c) => ({
|
|
602
|
-
handle: c.handle,
|
|
603
|
-
title: c.title,
|
|
604
|
-
description: c.description,
|
|
605
|
-
description_html: c.descriptionHtml,
|
|
606
|
-
image: c.featuredImage ? toFileInfo(c.featuredImage, { baseWidth: 640 }) : null,
|
|
607
|
-
products_count: c.productsCount
|
|
608
|
-
}))
|
|
266
|
+
total_count: answer.total,
|
|
267
|
+
items: answer.items.map(toCollectionListItem)
|
|
609
268
|
};
|
|
610
269
|
} catch (err) {
|
|
611
270
|
console.warn("[fetchCollectionsPage] failed:", err);
|
|
612
271
|
return null;
|
|
613
272
|
}
|
|
614
273
|
};
|
|
615
|
-
|
|
616
|
-
query FetchCollectionMeta($handle: String, $id: ID) {
|
|
617
|
-
collection(handle: $handle, id: $id) {
|
|
618
|
-
handle
|
|
619
|
-
title
|
|
620
|
-
# Non-null, defaulting to "collection" — the theme picks its template
|
|
621
|
-
# BEFORE it can fetch the listing (the grid section's page size decides
|
|
622
|
-
# how many products to ask for), so the assigned variant has to ride this
|
|
623
|
-
# lean read rather than the page payload that carries it too.
|
|
624
|
-
themeTemplate
|
|
625
|
-
}
|
|
626
|
-
}
|
|
627
|
-
`);
|
|
628
|
-
/** Lean collection lookup for breadcrumbs / contextual labels, and for
|
|
629
|
-
* resolving a collection's assigned theme template. Skips products and image —
|
|
630
|
-
* pays only for `handle` + `title` + `themeTemplate`. */
|
|
631
|
-
const fetchCollectionMeta = memoTTL(async (handle, locale) => {
|
|
632
|
-
try {
|
|
633
|
-
const c = (await request(FetchCollectionMetaQuery, parseRef(handle), locale ? { acceptLanguage: locale } : void 0)).collection;
|
|
634
|
-
if (!c) return null;
|
|
635
|
-
return {
|
|
636
|
-
handle: c.handle,
|
|
637
|
-
title: c.title,
|
|
638
|
-
themeTemplate: c.themeTemplate
|
|
639
|
-
};
|
|
640
|
-
} catch (err) {
|
|
641
|
-
console.warn("[fetchCollectionMeta] failed:", err);
|
|
642
|
-
return null;
|
|
643
|
-
}
|
|
644
|
-
}, {
|
|
645
|
-
key: (h, locale) => `meta|${h}|${locale ?? ""}`,
|
|
646
|
-
shouldCache: (v) => v !== null,
|
|
647
|
-
version: getContentVersion
|
|
648
|
-
});
|
|
649
|
-
const FetchCollectionDescriptionQuery = graphql(`
|
|
650
|
-
query FetchCollectionDescription($handle: String, $id: ID) {
|
|
651
|
-
collection(handle: $handle, id: $id) {
|
|
652
|
-
descriptionHtml
|
|
653
|
-
}
|
|
654
|
-
}
|
|
655
|
-
`);
|
|
656
|
-
/** Cached read behind `fetchCollectionDescriptionHtml`. A blank description is
|
|
657
|
-
* `""` and IS cached (the key is content-versioned, so an edit still lands); only
|
|
658
|
-
* a missing collection or a failed read is `null` and retried. */
|
|
659
|
-
const fetchCollectionDescriptionRaw = memoTTL(async (handle, locale) => {
|
|
660
|
-
try {
|
|
661
|
-
const res = await request(FetchCollectionDescriptionQuery, parseRef(handle), locale ? { acceptLanguage: locale } : void 0);
|
|
662
|
-
if (!res.collection) return null;
|
|
663
|
-
const html = res.collection.descriptionHtml ?? "";
|
|
664
|
-
return html.trim() === "" ? "" : html;
|
|
665
|
-
} catch (err) {
|
|
666
|
-
console.warn("[fetchCollectionDescriptionHtml] failed:", err);
|
|
667
|
-
return null;
|
|
668
|
-
}
|
|
669
|
-
}, {
|
|
670
|
-
key: (h, locale) => `description|${h}|${locale ?? ""}`,
|
|
671
|
-
shouldCache: (v) => v !== null,
|
|
672
|
-
version: getContentVersion
|
|
673
|
-
});
|
|
674
|
-
/** The collection's description HTML alone — the `collection_attribute:description`
|
|
675
|
-
* dynamic source. Kept out of `fetchCollectionMeta` so breadcrumbs and template
|
|
676
|
-
* resolution stay lean; the theme loader calls it only when a template binds the
|
|
677
|
-
* description. null on a miss, a failure, or an empty description. */
|
|
678
|
-
async function fetchCollectionDescriptionHtml(handle, locale) {
|
|
679
|
-
return await fetchCollectionDescriptionRaw(handle, locale) || null;
|
|
680
|
-
}
|
|
681
|
-
const FetchCollectionMetafieldsQuery = graphql(`
|
|
682
|
-
query FetchCollectionMetafields($handle: String, $id: ID) {
|
|
683
|
-
collection(handle: $handle, id: $id) {
|
|
684
|
-
# Storefront-visible only (the API gates on the storefront scope), so the
|
|
685
|
-
# set is a shop's published collection fields; 50 matches the product read.
|
|
686
|
-
metafields(first: 50) {
|
|
687
|
-
nodes {
|
|
688
|
-
namespace
|
|
689
|
-
key
|
|
690
|
-
type
|
|
691
|
-
value
|
|
692
|
-
}
|
|
693
|
-
}
|
|
694
|
-
}
|
|
695
|
-
}
|
|
696
|
-
`);
|
|
697
|
-
/** The collection's storefront metafields keyed `"namespace.key"` — the
|
|
698
|
-
* `collection_metafield` dynamic source. Kept out of `fetchCollectionMeta` for
|
|
699
|
-
* the same reason as the description: that lean read is also what every
|
|
700
|
-
* collection list asks for, so metafields there would be fetched for every
|
|
701
|
-
* listed collection. The theme loader calls this only when a template binds a
|
|
702
|
-
* collection metafield. `{}` on a miss or a failure, so the literal renders. */
|
|
703
|
-
const fetchCollectionMetafields = metafieldsReader("fetchCollectionMetafields", async (handle, locale) => {
|
|
704
|
-
return (await request(FetchCollectionMetafieldsQuery, parseRef(handle), locale ? { acceptLanguage: locale } : void 0)).collection?.metafields.nodes ?? null;
|
|
705
|
-
});
|
|
706
|
-
/** The route collection as the theme loader's dynamic sources read it
|
|
707
|
-
* (`DynamicSourceCollection` in `@shop-on/theme-loader`): the title off the
|
|
708
|
-
* lean meta read the theme already made, and a lazy provider for each source
|
|
709
|
-
* the loader fetches only when a template binds it. One builder so a theme
|
|
710
|
-
* passes `routeCollectionSource(meta, locale)` and picks up a new provider on
|
|
711
|
-
* upgrade, instead of every theme re-listing them. */
|
|
712
|
-
function routeCollectionSource(meta, locale) {
|
|
713
|
-
return {
|
|
714
|
-
title: meta.title,
|
|
715
|
-
getDescriptionHtml: () => fetchCollectionDescriptionHtml(meta.handle, locale),
|
|
716
|
-
getMetafields: () => fetchCollectionMetafields(meta.handle, locale)
|
|
717
|
-
};
|
|
718
|
-
}
|
|
719
|
-
//#endregion
|
|
720
|
-
export { CollectionImageFragment, CollectionProductNodeFragment, fetchAllProducts, fetchCollection, fetchCollectionCard, fetchCollectionDescriptionHtml, fetchCollectionMeta, fetchCollectionMetafields, fetchCollectionPage, fetchCollectionsPage, fetchLiquidCollection, routeCollectionSource, toCollectionProduct, toPriceBounds, variantsVariable };
|
|
274
|
+
export { fetchAllProducts, fetchCollection, fetchCollectionCard, fetchCollectionDescriptionHtml, fetchCollectionMeta, fetchCollectionMetafields, fetchCollectionPage, fetchCollectionsPage, fetchLiquidCollection, routeCollectionSource };
|