@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/blog.js
CHANGED
|
@@ -1,528 +1,229 @@
|
|
|
1
|
+
import { liteId } from "./api-contract/src/ids.js";
|
|
2
|
+
import { LISTING_OFFSET_MAX } from "./api-contract/src/limits.js";
|
|
1
3
|
import { memoTTL } from "./cache.js";
|
|
2
|
-
import {
|
|
4
|
+
import { coalescedReads } from "./coalesce.js";
|
|
3
5
|
import { getContentVersion } from "./content-version.js";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
publishedAt
|
|
18
|
-
tags
|
|
19
|
-
author {
|
|
20
|
-
name
|
|
21
|
-
}
|
|
22
|
-
summary(truncateAt: 240)
|
|
23
|
-
image {
|
|
24
|
-
__typename
|
|
25
|
-
id
|
|
26
|
-
alt
|
|
27
|
-
... on ShopMediaImage {
|
|
28
|
-
url(transform: { maxWidth: 480 })
|
|
29
|
-
url2x: url(transform: { maxWidth: 480, scale: 2 })
|
|
30
|
-
width
|
|
31
|
-
height
|
|
32
|
-
focalPoint {
|
|
33
|
-
x
|
|
34
|
-
y
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
blog {
|
|
39
|
-
handle
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
`);
|
|
46
|
-
const FetchBlogPageQuery = graphql(`
|
|
47
|
-
query FetchBlogPage(
|
|
48
|
-
$handle: String!
|
|
49
|
-
$first: Int!
|
|
50
|
-
$after: String
|
|
51
|
-
$tag: String
|
|
52
|
-
$imageWidth: Int!
|
|
53
|
-
$excerptLength: Int!
|
|
54
|
-
$withBodyExcerpt: Boolean!
|
|
55
|
-
) {
|
|
56
|
-
blog(handle: $handle) {
|
|
57
|
-
handle
|
|
58
|
-
title
|
|
59
|
-
articlesCount
|
|
60
|
-
tags
|
|
61
|
-
articles(first: $first, after: $after, tag: $tag) {
|
|
62
|
-
totalCount
|
|
63
|
-
pageInfo {
|
|
64
|
-
endCursor
|
|
65
|
-
hasNextPage
|
|
66
|
-
}
|
|
67
|
-
nodes {
|
|
68
|
-
handle
|
|
69
|
-
title
|
|
70
|
-
publishedAt
|
|
71
|
-
tags
|
|
72
|
-
author {
|
|
73
|
-
name
|
|
74
|
-
}
|
|
75
|
-
summary(truncateAt: $excerptLength)
|
|
76
|
-
bodyExcerpt: body(truncateAt: $excerptLength) @include(if: $withBodyExcerpt)
|
|
77
|
-
image {
|
|
78
|
-
__typename
|
|
79
|
-
id
|
|
80
|
-
alt
|
|
81
|
-
... on ShopMediaImage {
|
|
82
|
-
url(transform: { maxWidth: $imageWidth })
|
|
83
|
-
url2x: url(transform: { maxWidth: $imageWidth, scale: 2 })
|
|
84
|
-
width
|
|
85
|
-
height
|
|
86
|
-
focalPoint {
|
|
87
|
-
x
|
|
88
|
-
y
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
blog {
|
|
93
|
-
handle
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
`);
|
|
100
|
-
const FetchArticleQuery = graphql(`
|
|
101
|
-
query FetchArticle($handle: String, $blogHandle: String, $id: ID) {
|
|
102
|
-
article(handle: $handle, blogHandle: $blogHandle, id: $id) {
|
|
103
|
-
handle
|
|
104
|
-
title
|
|
105
|
-
bodyHtml
|
|
106
|
-
summaryHtml
|
|
107
|
-
summary(truncateAt: 240)
|
|
108
|
-
# Plain-text body capped at the meta-description storage limit —
|
|
109
|
-
# Rails strip_tags + entity decode + word-boundary truncate.
|
|
110
|
-
bodyPlain: body(truncateAt: 320)
|
|
111
|
-
publishedAt
|
|
112
|
-
updatedAt
|
|
113
|
-
tags
|
|
114
|
-
author {
|
|
115
|
-
name
|
|
116
|
-
}
|
|
117
|
-
seo {
|
|
118
|
-
title
|
|
119
|
-
description
|
|
120
|
-
}
|
|
121
|
-
image {
|
|
122
|
-
__typename
|
|
123
|
-
id
|
|
124
|
-
alt
|
|
125
|
-
... on ShopMediaImage {
|
|
126
|
-
url(transform: { maxWidth: 1200 })
|
|
127
|
-
url2x: url(transform: { maxWidth: 1200, scale: 2 })
|
|
128
|
-
width
|
|
129
|
-
height
|
|
130
|
-
focalPoint {
|
|
131
|
-
x
|
|
132
|
-
y
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
blog {
|
|
137
|
-
handle
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
`);
|
|
142
|
-
const FetchBlogMetaQuery = graphql(`
|
|
143
|
-
query FetchBlogMeta($handle: String!) {
|
|
144
|
-
blog(handle: $handle) {
|
|
145
|
-
id
|
|
146
|
-
handle
|
|
147
|
-
title
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
`);
|
|
151
|
-
const FetchAdjacentArticlesQuery = graphql(`
|
|
152
|
-
query FetchAdjacentArticles(
|
|
153
|
-
$blogId: ID!
|
|
154
|
-
$publishedAt: ISO8601DateTime!
|
|
155
|
-
$imageWidth: Int!
|
|
156
|
-
$excerptLength: Int!
|
|
157
|
-
$withBodyExcerpt: Boolean!
|
|
158
|
-
) {
|
|
159
|
-
older: articles(
|
|
160
|
-
first: 2
|
|
161
|
-
filter: { blogIdEq: $blogId, publishedAt: { lteq: $publishedAt } }
|
|
162
|
-
sort: { field: "publishedAt", direction: desc }
|
|
163
|
-
) {
|
|
164
|
-
nodes {
|
|
165
|
-
handle
|
|
166
|
-
title
|
|
167
|
-
publishedAt
|
|
168
|
-
tags
|
|
169
|
-
author {
|
|
170
|
-
name
|
|
171
|
-
}
|
|
172
|
-
summary(truncateAt: $excerptLength)
|
|
173
|
-
bodyExcerpt: body(truncateAt: $excerptLength) @include(if: $withBodyExcerpt)
|
|
174
|
-
image {
|
|
175
|
-
__typename
|
|
176
|
-
id
|
|
177
|
-
alt
|
|
178
|
-
... on ShopMediaImage {
|
|
179
|
-
url(transform: { maxWidth: $imageWidth })
|
|
180
|
-
url2x: url(transform: { maxWidth: $imageWidth, scale: 2 })
|
|
181
|
-
width
|
|
182
|
-
height
|
|
183
|
-
focalPoint {
|
|
184
|
-
x
|
|
185
|
-
y
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
blog {
|
|
190
|
-
handle
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
newer: articles(
|
|
195
|
-
first: 2
|
|
196
|
-
filter: { blogIdEq: $blogId, publishedAt: { gteq: $publishedAt } }
|
|
197
|
-
sort: { field: "publishedAt", direction: asc }
|
|
198
|
-
) {
|
|
199
|
-
nodes {
|
|
200
|
-
handle
|
|
201
|
-
title
|
|
202
|
-
publishedAt
|
|
203
|
-
tags
|
|
204
|
-
author {
|
|
205
|
-
name
|
|
206
|
-
}
|
|
207
|
-
summary(truncateAt: $excerptLength)
|
|
208
|
-
bodyExcerpt: body(truncateAt: $excerptLength) @include(if: $withBodyExcerpt)
|
|
209
|
-
image {
|
|
210
|
-
__typename
|
|
211
|
-
id
|
|
212
|
-
alt
|
|
213
|
-
... on ShopMediaImage {
|
|
214
|
-
url(transform: { maxWidth: $imageWidth })
|
|
215
|
-
url2x: url(transform: { maxWidth: $imageWidth, scale: 2 })
|
|
216
|
-
width
|
|
217
|
-
height
|
|
218
|
-
focalPoint {
|
|
219
|
-
x
|
|
220
|
-
y
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
blog {
|
|
225
|
-
handle
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
`);
|
|
231
|
-
function toArticleImage(node, baseWidth) {
|
|
232
|
-
const f = toFileInfo(node, { baseWidth });
|
|
233
|
-
return f && f.kind === "image" ? f : null;
|
|
234
|
-
}
|
|
235
|
-
function toArticleCard(n, imageWidth = 480) {
|
|
6
|
+
import { imageFileInfo } from "./files.js";
|
|
7
|
+
import { customDataMap } from "./metafields.js";
|
|
8
|
+
import { orFallback, orNotFound, storefront } from "./storefront.js";
|
|
9
|
+
// src/blog.ts
|
|
10
|
+
/** The width a card's cover is drawn at unless the theme sizes it. */
|
|
11
|
+
const CARD_IMAGE_WIDTH = 480;
|
|
12
|
+
/** The width an article page's cover is drawn at. */
|
|
13
|
+
const ARTICLE_IMAGE_WIDTH = 1200;
|
|
14
|
+
/** Where a summary is cut unless the theme says otherwise. */
|
|
15
|
+
const EXCERPT_LENGTH = 240;
|
|
16
|
+
const BLOG_PAGE_SIZE = 12;
|
|
17
|
+
const excerptLengthOf = (length) => Math.min(Math.max(1, length ?? EXCERPT_LENGTH), 320);
|
|
18
|
+
function toArticleCard(card, imageWidth) {
|
|
236
19
|
return {
|
|
237
|
-
handle:
|
|
238
|
-
blogHandle:
|
|
239
|
-
title:
|
|
240
|
-
summary:
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
20
|
+
handle: card.handle,
|
|
21
|
+
blogHandle: card.blog.handle,
|
|
22
|
+
title: card.title,
|
|
23
|
+
summary: card.summaryText,
|
|
24
|
+
excerpt: card.excerpt,
|
|
25
|
+
image: card.image ? imageFileInfo(card.image, imageWidth) : null,
|
|
26
|
+
tags: [...card.tags],
|
|
27
|
+
author: card.authorName === null ? null : { name: card.authorName },
|
|
28
|
+
publishedAt: card.publishedAt
|
|
246
29
|
};
|
|
247
30
|
}
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
31
|
+
/** A blog by handle, or by id when the ref is a blog id. */
|
|
32
|
+
function blogRef(ref) {
|
|
33
|
+
const id = liteId("blog").safeParse(ref);
|
|
34
|
+
return id.success ? { blogId: id.data } : { handle: ref };
|
|
35
|
+
}
|
|
36
|
+
/** One `blogs.get` behind every lean read of a blog: its meta, its tags and
|
|
37
|
+
* count, and its metafields. Null while the shop lacks it (NOT_FOUND) and on
|
|
38
|
+
* a failed read, which is not cached. */
|
|
39
|
+
const fetchBlogRecord = memoTTL(async (ref, locale) => {
|
|
40
|
+
const b = await orFallback(orNotFound(storefront(locale).blogs.get(blogRef(ref))), null, "fetchBlog");
|
|
253
41
|
if (!b) return null;
|
|
254
42
|
return {
|
|
255
43
|
handle: b.handle,
|
|
256
44
|
title: b.title,
|
|
257
|
-
|
|
258
|
-
|
|
45
|
+
articlesCount: b.articlesCount,
|
|
46
|
+
tags: b.tags,
|
|
47
|
+
metafields: customDataMap(b.metafields)
|
|
259
48
|
};
|
|
260
49
|
}, {
|
|
261
|
-
key: (
|
|
50
|
+
key: (ref, locale) => `blog|${ref}|${locale ?? ""}`,
|
|
262
51
|
shouldCache: (v) => v !== null,
|
|
263
52
|
version: getContentVersion
|
|
264
53
|
});
|
|
265
|
-
/**
|
|
266
|
-
*
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
};
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
const first = data.first ?? BLOG_PAGE_SIZE;
|
|
281
|
-
const imageWidth = data.imageWidth ?? 480;
|
|
282
|
-
try {
|
|
283
|
-
const b = (await request(FetchBlogPageQuery, {
|
|
284
|
-
handle: data.handle,
|
|
285
|
-
first,
|
|
286
|
-
after: data.after ?? null,
|
|
287
|
-
tag: data.tag ?? null,
|
|
288
|
-
imageWidth,
|
|
289
|
-
excerptLength: data.excerptLength ?? 240,
|
|
290
|
-
withBodyExcerpt: data.withBodyExcerpt ?? false
|
|
291
|
-
}, data.locale ? { acceptLanguage: data.locale } : void 0)).blog;
|
|
292
|
-
if (!b) return null;
|
|
293
|
-
return {
|
|
294
|
-
handle: b.handle,
|
|
295
|
-
title: b.title,
|
|
296
|
-
articles_count: b.articlesCount,
|
|
297
|
-
tags: [...b.tags],
|
|
298
|
-
total_count: b.articles.totalCount,
|
|
299
|
-
page_info: {
|
|
300
|
-
end_cursor: b.articles.pageInfo.endCursor ?? null,
|
|
301
|
-
has_next_page: b.articles.pageInfo.hasNextPage
|
|
302
|
-
},
|
|
303
|
-
articles: b.articles.nodes.map((node) => toArticleCard(node, imageWidth))
|
|
304
|
-
};
|
|
305
|
-
} catch (err) {
|
|
306
|
-
console.warn("[fetchBlogPage] failed:", err);
|
|
307
|
-
return null;
|
|
308
|
-
}
|
|
309
|
-
};
|
|
310
|
-
function parseArticleRef(ref) {
|
|
311
|
-
const slash = ref.indexOf("/");
|
|
312
|
-
if (slash > 0 && !ref.startsWith("gid://")) return {
|
|
313
|
-
blogHandle: ref.slice(0, slash),
|
|
314
|
-
handle: ref.slice(slash + 1)
|
|
54
|
+
/** A page of a blog's articles as cards; null for a blog the shop lacks, and
|
|
55
|
+
* on a failed read. */
|
|
56
|
+
async function readArticles(input) {
|
|
57
|
+
const answer = await orFallback(orNotFound(storefront(input.locale).blogs.articles({
|
|
58
|
+
...blogRef(input.handle),
|
|
59
|
+
offset: Math.min(input.offset, LISTING_OFFSET_MAX),
|
|
60
|
+
limit: Math.min(Math.max(1, input.limit), 50),
|
|
61
|
+
tag: input.tag?.trim() || void 0,
|
|
62
|
+
excerptLength: excerptLengthOf(input.excerptLength)
|
|
63
|
+
})), null, "fetchBlogArticles");
|
|
64
|
+
if (!answer) return null;
|
|
65
|
+
const width = input.imageWidth ?? CARD_IMAGE_WIDTH;
|
|
66
|
+
return {
|
|
67
|
+
items: answer.items.map((c) => toArticleCard(c, width)),
|
|
68
|
+
total: answer.total
|
|
315
69
|
};
|
|
316
|
-
return { id: ref };
|
|
317
70
|
}
|
|
318
|
-
const
|
|
319
|
-
const
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
}
|
|
325
|
-
if (!
|
|
71
|
+
const fetchBlogCached = memoTTL(async ({ handle, first, locale }) => {
|
|
72
|
+
const [blog, articles] = await Promise.all([fetchBlogRecord(handle, locale), readArticles({
|
|
73
|
+
handle,
|
|
74
|
+
offset: 0,
|
|
75
|
+
limit: first,
|
|
76
|
+
locale
|
|
77
|
+
})]);
|
|
78
|
+
if (!blog || !articles) return null;
|
|
326
79
|
return {
|
|
327
|
-
handle:
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
summaryHtml: a.summaryHtml ?? null,
|
|
332
|
-
summary: a.summary ?? null,
|
|
333
|
-
image: toArticleImage(a.image, 1200),
|
|
334
|
-
tags: [...a.tags],
|
|
335
|
-
author: a.author ? { name: a.author.name } : null,
|
|
336
|
-
publishedAt: a.publishedAt ?? null,
|
|
337
|
-
updatedAt: a.updatedAt,
|
|
338
|
-
bodyPlain: a.bodyPlain,
|
|
339
|
-
seo: {
|
|
340
|
-
title: a.seo.title,
|
|
341
|
-
description: a.seo.description
|
|
342
|
-
}
|
|
80
|
+
handle: blog.handle,
|
|
81
|
+
title: blog.title,
|
|
82
|
+
articles_count: blog.articlesCount,
|
|
83
|
+
articles: articles.items
|
|
343
84
|
};
|
|
344
85
|
}, {
|
|
345
|
-
key: (
|
|
86
|
+
key: ({ handle, first, locale }) => `${handle}|${first}|${locale ?? ""}`,
|
|
346
87
|
shouldCache: (v) => v !== null,
|
|
347
88
|
version: getContentVersion
|
|
348
89
|
});
|
|
349
|
-
/**
|
|
350
|
-
*
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
const
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
/** One
|
|
405
|
-
*
|
|
406
|
-
*
|
|
407
|
-
|
|
408
|
-
const
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
blogHandle: blogHandle ?? null,
|
|
415
|
-
id: id ?? null,
|
|
416
|
-
imageWidth,
|
|
417
|
-
excerptLength: params.excerptLength ?? 240,
|
|
418
|
-
withBodyExcerpt: params.withBodyExcerpt ?? false
|
|
419
|
-
}, params.locale ? { acceptLanguage: params.locale } : void 0);
|
|
420
|
-
return res.article ? toArticleCard(res.article, imageWidth) : null;
|
|
421
|
-
} catch (err) {
|
|
422
|
-
console.warn("[fetchArticleCard] failed:", err);
|
|
423
|
-
return null;
|
|
424
|
-
}
|
|
425
|
-
};
|
|
426
|
-
/** Lean blog lookup for breadcrumbs / contextual labels. Skips articles. */
|
|
427
|
-
const fetchBlogMeta = memoTTL(async (handle, locale) => {
|
|
428
|
-
try {
|
|
429
|
-
const b = (await request(FetchBlogMetaQuery, { handle }, locale ? { acceptLanguage: locale } : void 0)).blog;
|
|
430
|
-
if (!b) return null;
|
|
431
|
-
return {
|
|
432
|
-
id: b.id,
|
|
433
|
-
handle: b.handle,
|
|
434
|
-
title: b.title
|
|
435
|
-
};
|
|
436
|
-
} catch (err) {
|
|
437
|
-
console.warn("[fetchBlogMeta] failed:", err);
|
|
438
|
-
return null;
|
|
439
|
-
}
|
|
90
|
+
/** A blog with its newest `first` articles, for a blog setting (featured
|
|
91
|
+
* blog, blog cards). Null for a missing blog or a failed read; pickers
|
|
92
|
+
* degrade silently. */
|
|
93
|
+
async function fetchBlog(data) {
|
|
94
|
+
return fetchBlogCached(data);
|
|
95
|
+
}
|
|
96
|
+
/** A blog's listing page: the blog, its tags, and page `page` of its
|
|
97
|
+
* (tag-filtered) articles. Uncached because page + tag permutations would
|
|
98
|
+
* blow the TTL cache; the blog itself rides the cached record. Null for a
|
|
99
|
+
* missing blog so the route can map to `notFound()`. */
|
|
100
|
+
async function fetchBlogPage(data) {
|
|
101
|
+
const first = data.first ?? BLOG_PAGE_SIZE;
|
|
102
|
+
const [blog, articles] = await Promise.all([fetchBlogRecord(data.handle, data.locale), readArticles({
|
|
103
|
+
handle: data.handle,
|
|
104
|
+
offset: (Math.max(1, data.page ?? 1) - 1) * first,
|
|
105
|
+
limit: first,
|
|
106
|
+
tag: data.tag,
|
|
107
|
+
excerptLength: data.excerptLength,
|
|
108
|
+
imageWidth: data.imageWidth,
|
|
109
|
+
locale: data.locale
|
|
110
|
+
})]);
|
|
111
|
+
if (!blog || !articles) return null;
|
|
112
|
+
return {
|
|
113
|
+
handle: blog.handle,
|
|
114
|
+
title: blog.title,
|
|
115
|
+
articles_count: blog.articlesCount,
|
|
116
|
+
tags: [...blog.tags],
|
|
117
|
+
total_count: articles.total,
|
|
118
|
+
articles: articles.items
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
/** Lean blog lookup for breadcrumbs / contextual labels, off the blog record. */
|
|
122
|
+
async function fetchBlogMeta(handle, locale) {
|
|
123
|
+
const blog = await fetchBlogRecord(handle, locale);
|
|
124
|
+
return blog && {
|
|
125
|
+
handle: blog.handle,
|
|
126
|
+
title: blog.title
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
/** The blog's storefront metafields keyed `"namespace.key"`: the
|
|
130
|
+
* `blog_metafield` dynamic source, off the blog record. `{}` on a miss or a
|
|
131
|
+
* failure, so the literal renders. */
|
|
132
|
+
async function fetchBlogMetafields(handle, locale) {
|
|
133
|
+
return (await fetchBlogRecord(handle, locale))?.metafields ?? {};
|
|
134
|
+
}
|
|
135
|
+
function articleRef(ref) {
|
|
136
|
+
const id = liteId("article").safeParse(ref);
|
|
137
|
+
if (id.success) return { articleId: id.data };
|
|
138
|
+
const [blogHandle, handle, ...rest] = ref.split("/");
|
|
139
|
+
if (!blogHandle || !handle || rest.length > 0) return null;
|
|
140
|
+
return {
|
|
141
|
+
blogHandle,
|
|
142
|
+
handle
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
/** One `articles.get` behind every read of an article: the page, a setting's
|
|
146
|
+
* article, its neighbours, its metafields. Null while a buyer may not see it
|
|
147
|
+
* (NOT_FOUND) and on a failed read, which is not cached. */
|
|
148
|
+
const fetchArticleRecord = memoTTL(async (ref, locale) => {
|
|
149
|
+
const named = articleRef(ref);
|
|
150
|
+
if (!named) return null;
|
|
151
|
+
return orFallback(orNotFound(storefront(locale).articles.get({
|
|
152
|
+
...named,
|
|
153
|
+
excerptLength: EXCERPT_LENGTH
|
|
154
|
+
})), null, "fetchArticle");
|
|
440
155
|
}, {
|
|
441
|
-
key: (
|
|
156
|
+
key: (ref, locale) => `article|${ref}|${locale ?? ""}`,
|
|
442
157
|
shouldCache: (v) => v !== null,
|
|
443
158
|
version: getContentVersion
|
|
444
159
|
});
|
|
445
|
-
/**
|
|
446
|
-
*
|
|
447
|
-
*
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
160
|
+
/** One article as its page renders it, by `ref`: an article id (an `article`
|
|
161
|
+
* setting's value) or `<blog-handle>/<article-handle>`. Null for a missing
|
|
162
|
+
* article or a failed read, so a resolver drops the entry. */
|
|
163
|
+
async function fetchArticleByRef(ref, locale) {
|
|
164
|
+
const a = await fetchArticleRecord(ref, locale);
|
|
165
|
+
if (!a) return null;
|
|
166
|
+
return {
|
|
167
|
+
...toArticleCard(a, ARTICLE_IMAGE_WIDTH),
|
|
168
|
+
bodyHtml: a.bodyHtml,
|
|
169
|
+
summaryHtml: a.summaryHtml,
|
|
170
|
+
updatedAt: a.updatedAt,
|
|
171
|
+
bodyPlain: a.bodyText,
|
|
172
|
+
seo: {
|
|
173
|
+
title: a.seoTitle,
|
|
174
|
+
description: a.seoDescription
|
|
175
|
+
}
|
|
455
176
|
};
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
177
|
+
}
|
|
178
|
+
/** The article route's read, by `(blogHandle, handle)`. */
|
|
179
|
+
async function fetchArticle(params) {
|
|
180
|
+
return fetchArticleByRef(`${params.blogHandle}/${params.handle}`, params.locale);
|
|
181
|
+
}
|
|
182
|
+
/** The posts either side of one in its blog, by publish date, off the same
|
|
183
|
+
* read as the article. Named by age rather than previous / next because the
|
|
184
|
+
* two conventions disagree: Liquid's `blog.previous_article` is the NEWER
|
|
185
|
+
* post and `next_article` the older. Null at either end and on a failed read.
|
|
186
|
+
* `imageWidth` sizes the cards as `fetchBlogPage`'s do. */
|
|
187
|
+
async function fetchAdjacentArticles(params) {
|
|
188
|
+
const a = await fetchArticleRecord(`${params.blogHandle}/${params.handle}`, params.locale);
|
|
189
|
+
const width = params.imageWidth ?? CARD_IMAGE_WIDTH;
|
|
190
|
+
return {
|
|
191
|
+
newer: a?.newer ? toArticleCard(a.newer, width) : null,
|
|
192
|
+
older: a?.older ? toArticleCard(a.older, width) : null
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
/** The article's storefront metafields: the `article_metafield` dynamic
|
|
196
|
+
* source, off the same read as the article. `ref` is what every article read
|
|
197
|
+
* takes. `{}` on a miss or a failure, so the literal renders. */
|
|
198
|
+
async function fetchArticleMetafields(ref, locale) {
|
|
199
|
+
const a = await fetchArticleRecord(ref, locale);
|
|
200
|
+
return a ? customDataMap(a.metafields) : {};
|
|
201
|
+
}
|
|
202
|
+
/** Article card reads of this request's tick in one language and at one
|
|
203
|
+
* excerpt length share an `articles.cards` call. */
|
|
204
|
+
const coalescedArticleCard = coalescedReads({
|
|
205
|
+
max: 50,
|
|
206
|
+
matches: (card, ref) => {
|
|
207
|
+
const named = articleRef(ref);
|
|
208
|
+
if (!named) return false;
|
|
209
|
+
return "articleId" in named ? card.id === named.articleId : card.blog.handle === named.blogHandle && card.handle === named.handle;
|
|
479
210
|
}
|
|
480
|
-
};
|
|
481
|
-
const FetchBlogMetafieldsQuery = graphql(`
|
|
482
|
-
query FetchBlogMetafields($handle: String, $id: ID) {
|
|
483
|
-
blog(handle: $handle, id: $id) {
|
|
484
|
-
metafields(first: 50) {
|
|
485
|
-
nodes {
|
|
486
|
-
namespace
|
|
487
|
-
key
|
|
488
|
-
type
|
|
489
|
-
value
|
|
490
|
-
}
|
|
491
|
-
}
|
|
492
|
-
}
|
|
493
|
-
}
|
|
494
|
-
`);
|
|
495
|
-
const FetchArticleMetafieldsQuery = graphql(`
|
|
496
|
-
query FetchArticleMetafields($blogHandle: String, $handle: String, $id: ID) {
|
|
497
|
-
article(blogHandle: $blogHandle, handle: $handle, id: $id) {
|
|
498
|
-
metafields(first: 50) {
|
|
499
|
-
nodes {
|
|
500
|
-
namespace
|
|
501
|
-
key
|
|
502
|
-
type
|
|
503
|
-
value
|
|
504
|
-
}
|
|
505
|
-
}
|
|
506
|
-
}
|
|
507
|
-
}
|
|
508
|
-
`);
|
|
509
|
-
/** The blog's storefront metafields keyed `"namespace.key"` — the
|
|
510
|
-
* `blog_metafield` dynamic source, read only when a template binds one. `{}`
|
|
511
|
-
* on a miss or a failure, so the literal renders. */
|
|
512
|
-
const fetchBlogMetafields = metafieldsReader("fetchBlogMetafields", async (handle, locale) => {
|
|
513
|
-
return (await request(FetchBlogMetafieldsQuery, handle.startsWith("gid://") ? { id: handle } : { handle }, locale ? { acceptLanguage: locale } : void 0)).blog?.metafields.nodes ?? null;
|
|
514
|
-
});
|
|
515
|
-
/** The article's storefront metafields — the `article_metafield` dynamic
|
|
516
|
-
* source. `ref` is what every article read takes (`parseArticleRef`): the
|
|
517
|
-
* route's `<blog-handle>/<article-handle>` composite, or a node id. */
|
|
518
|
-
const fetchArticleMetafields = metafieldsReader("fetchArticleMetafields", async (ref, locale) => {
|
|
519
|
-
const parts = parseArticleRef(ref);
|
|
520
|
-
return (await request(FetchArticleMetafieldsQuery, {
|
|
521
|
-
blogHandle: parts.blogHandle ?? null,
|
|
522
|
-
handle: parts.handle ?? null,
|
|
523
|
-
id: parts.id ?? null
|
|
524
|
-
}, locale ? { acceptLanguage: locale } : void 0)).article?.metafields.nodes ?? null;
|
|
525
211
|
});
|
|
212
|
+
/** One picked article as a card, sized as `fetchBlogPage` sizes a listing's
|
|
213
|
+
* cards: for a picker whose article renders as a card, not a page. `ref` is
|
|
214
|
+
* what the `article` setting stores. Null for a missing article, a ref that
|
|
215
|
+
* names none, or a failed read, so the card falls back rather than the page. */
|
|
216
|
+
async function fetchArticleCard(params) {
|
|
217
|
+
if (!articleRef(params.ref)) return null;
|
|
218
|
+
const excerptLength = excerptLengthOf(params.excerptLength);
|
|
219
|
+
const card = await orFallback(coalescedArticleCard(`${params.locale ?? ""}|${excerptLength}`, params.ref, async (refs) => {
|
|
220
|
+
return (await storefront(params.locale).articles.cards({
|
|
221
|
+
refs: refs.flatMap((ref) => articleRef(ref) ?? []),
|
|
222
|
+
excerptLength
|
|
223
|
+
})).items;
|
|
224
|
+
}), null, "fetchArticleCard");
|
|
225
|
+
return card && toArticleCard(card, params.imageWidth ?? CARD_IMAGE_WIDTH);
|
|
226
|
+
}
|
|
526
227
|
/** The route blog as the theme loader's dynamic sources read it
|
|
527
228
|
* (`DynamicSourceBlog` in `@shop-on/theme-loader`): the title off the blog read
|
|
528
229
|
* the theme already made, and a lazy provider the loader calls only when a
|
|
@@ -541,5 +242,4 @@ function routeArticleSource(article, locale) {
|
|
|
541
242
|
getMetafields: () => fetchArticleMetafields(`${article.blogHandle}/${article.handle}`, locale)
|
|
542
243
|
};
|
|
543
244
|
}
|
|
544
|
-
//#endregion
|
|
545
245
|
export { fetchAdjacentArticles, fetchArticle, fetchArticleByRef, fetchArticleCard, fetchArticleMetafields, fetchBlog, fetchBlogMeta, fetchBlogMetafields, fetchBlogPage, routeArticleSource, routeBlogSource };
|