@shop-on/theme-server 0.10.0 → 0.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (168) hide show
  1. package/dist/account-state.js +1 -2
  2. package/dist/account.d.ts +1 -3
  3. package/dist/account.js +1 -3
  4. package/dist/activity.d.ts +1 -3
  5. package/dist/activity.js +1 -2
  6. package/dist/analytics-proxy.d.ts +8 -14
  7. package/dist/analytics-proxy.js +111 -28
  8. package/dist/analytics-script-proxy.d.ts +1 -5
  9. package/dist/analytics-script-proxy.js +4 -14
  10. package/dist/api-contract/src/addresses.js +32 -0
  11. package/dist/api-contract/src/areas.js +49 -0
  12. package/dist/api-contract/src/custom-data.d.ts +31 -0
  13. package/dist/api-contract/src/custom-data.js +303 -0
  14. package/dist/api-contract/src/ids.js +110 -0
  15. package/dist/api-contract/src/image.js +65 -0
  16. package/dist/api-contract/src/limits.js +44 -0
  17. package/dist/api-contract/src/media.js +13 -0
  18. package/dist/api-contract/src/pickup.js +11 -0
  19. package/dist/api-contract/src/recommendations.js +4 -0
  20. package/dist/api-contract/src/shops.js +27 -0
  21. package/dist/api-contract/src/storefront/analytics.d.ts +100 -0
  22. package/dist/api-contract/src/storefront/analytics.js +138 -0
  23. package/dist/api-contract/src/storefront/areas.d.ts +2 -0
  24. package/dist/api-contract/src/storefront/cart.d.ts +132 -0
  25. package/dist/api-contract/src/storefront/cart.js +248 -0
  26. package/dist/api-contract/src/storefront/catalog.d.ts +3 -0
  27. package/dist/api-contract/src/storefront/checkout-analytics.d.ts +2 -0
  28. package/dist/api-contract/src/storefront/checkout.d.ts +2 -0
  29. package/dist/api-contract/src/storefront/checkout.js +624 -0
  30. package/dist/api-contract/src/storefront/client.d.ts +3 -0
  31. package/dist/api-contract/src/storefront/client.js +6 -0
  32. package/dist/api-contract/src/storefront/content.d.ts +3 -0
  33. package/dist/api-contract/src/storefront/custom-data.d.ts +2 -0
  34. package/dist/api-contract/src/storefront/custom-data.js +27 -0
  35. package/dist/api-contract/src/storefront/customer.d.ts +2 -0
  36. package/dist/api-contract/src/storefront/customer.js +261 -0
  37. package/dist/api-contract/src/storefront/downloads.d.ts +2 -0
  38. package/dist/api-contract/src/storefront/draft-order-invoice.d.ts +2 -0
  39. package/dist/api-contract/src/storefront/forms.d.ts +2 -0
  40. package/dist/api-contract/src/storefront/forms.js +82 -0
  41. package/dist/api-contract/src/storefront/index.d.ts +20956 -0
  42. package/dist/api-contract/src/storefront/orders.d.ts +2 -0
  43. package/dist/api-contract/src/storefront/policies.d.ts +2 -0
  44. package/dist/api-contract/src/storefront/procedure.d.ts +2 -0
  45. package/dist/api-contract/src/storefront/procedure.js +29 -0
  46. package/dist/api-contract/src/storefront/reviews.d.ts +20 -0
  47. package/dist/api-contract/src/storefront/search.d.ts +3 -0
  48. package/dist/api-contract/src/storefront/seo.d.ts +2 -0
  49. package/dist/api-contract/src/storefront/seo.js +132 -0
  50. package/dist/api-contract/src/storefront/shop.d.ts +2 -0
  51. package/dist/api-contract/src/storefront/shop.js +101 -0
  52. package/dist/api-contract/src/taxonomy.js +5 -0
  53. package/dist/api-contract/src/text.js +15 -0
  54. package/dist/api-contract/src/theme-documents.js +13 -0
  55. package/dist/api-contract/src/theme-translations.d.ts +14 -0
  56. package/dist/api-contract/src/theme-translations.js +30 -0
  57. package/dist/api-contract/src/video.js +19 -0
  58. package/dist/availability.d.ts +6 -3
  59. package/dist/availability.js +11 -5
  60. package/dist/blog.d.ts +53 -59
  61. package/dist/blog.js +197 -497
  62. package/dist/cache.d.ts +5 -6
  63. package/dist/cache.js +1 -2
  64. package/dist/canonical-host.d.ts +4 -6
  65. package/dist/canonical-host.js +5 -6
  66. package/dist/card.js +159 -0
  67. package/dist/cart-customer.d.ts +1 -3
  68. package/dist/cart-customer.js +64 -105
  69. package/dist/cart-mutations.d.ts +43 -77
  70. package/dist/cart-mutations.js +106 -232
  71. package/dist/cart-recommendations.d.ts +1 -3
  72. package/dist/cart-recommendations.js +1 -2
  73. package/dist/cart-session.d.ts +1 -3
  74. package/dist/cart-session.js +1 -2
  75. package/dist/cart.d.ts +63 -224
  76. package/dist/cart.js +70 -197
  77. package/dist/checkout.d.ts +13 -17
  78. package/dist/checkout.js +75 -101
  79. package/dist/coalesce.js +57 -0
  80. package/dist/collection.d.ts +89 -298
  81. package/dist/collection.js +210 -656
  82. package/dist/contact.d.ts +20 -10
  83. package/dist/contact.js +36 -30
  84. package/dist/content-version.d.ts +37 -21
  85. package/dist/content-version.js +64 -42
  86. package/dist/contract-kit/src/client.d.ts +12 -0
  87. package/dist/contract-kit/src/client.js +156 -0
  88. package/dist/contract-kit/src/errors.d.ts +1 -0
  89. package/dist/contract-kit/src/errors.js +81 -0
  90. package/dist/contract-kit/src/ids.d.ts +1 -0
  91. package/dist/contract-kit/src/ids.js +118 -0
  92. package/dist/contract-kit/src/index.d.ts +7 -0
  93. package/dist/contract-kit/src/money.d.ts +1 -0
  94. package/dist/contract-kit/src/money.js +10 -0
  95. package/dist/contract-kit/src/pagination.d.ts +1 -0
  96. package/dist/contract-kit/src/procedure.d.ts +69 -0
  97. package/dist/contract-kit/src/procedure.js +16 -0
  98. package/dist/contract-kit/src/realtime.d.ts +1 -0
  99. package/dist/cookie-secret.js +1 -2
  100. package/dist/csrf.js +1 -2
  101. package/dist/customer-handoff.d.ts +1 -3
  102. package/dist/customer-handoff.js +10 -25
  103. package/dist/customer-session.d.ts +1 -3
  104. package/dist/customer-session.js +1 -2
  105. package/dist/customer.d.ts +2 -3
  106. package/dist/customer.js +27 -44
  107. package/dist/files.d.ts +49 -10
  108. package/dist/files.js +106 -89
  109. package/dist/fonts.d.ts +3 -5
  110. package/dist/fonts.js +16 -35
  111. package/dist/html.d.ts +1 -3
  112. package/dist/html.js +1 -2
  113. package/dist/liquid.d.ts +4 -10
  114. package/dist/liquid.js +25 -32
  115. package/dist/listing.js +79 -0
  116. package/dist/marketing-cookies.d.ts +1 -3
  117. package/dist/marketing-cookies.js +1 -2
  118. package/dist/menu.d.ts +46 -155
  119. package/dist/menu.js +51 -61
  120. package/dist/metafields.d.ts +9 -5
  121. package/dist/metafields.js +10 -38
  122. package/dist/metaobject.d.ts +10 -5
  123. package/dist/metaobject.js +43 -66
  124. package/dist/money.d.ts +7 -0
  125. package/dist/money.js +18 -0
  126. package/dist/newsletter.d.ts +19 -10
  127. package/dist/newsletter.js +31 -25
  128. package/dist/page.d.ts +13 -17
  129. package/dist/page.js +49 -105
  130. package/dist/password-session.d.ts +1 -3
  131. package/dist/password-session.js +1 -2
  132. package/dist/password.d.ts +25 -18
  133. package/dist/password.js +41 -59
  134. package/dist/policy.d.ts +7 -7
  135. package/dist/policy.js +19 -55
  136. package/dist/product.d.ts +42 -67
  137. package/dist/product.js +144 -644
  138. package/dist/read-ahead.d.ts +18 -0
  139. package/dist/read-ahead.js +32 -0
  140. package/dist/recommendations.d.ts +6 -12
  141. package/dist/recommendations.js +36 -60
  142. package/dist/review.d.ts +134 -94
  143. package/dist/review.js +257 -469
  144. package/dist/search.d.ts +59 -64
  145. package/dist/search.js +170 -469
  146. package/dist/seo-context.d.ts +1 -3
  147. package/dist/seo-context.js +4 -5
  148. package/dist/seo-documents.d.ts +50 -0
  149. package/dist/seo-documents.js +149 -0
  150. package/dist/shop-seo.d.ts +3 -5
  151. package/dist/shop-seo.js +7 -26
  152. package/dist/shop.d.ts +38 -42
  153. package/dist/shop.js +37 -51
  154. package/dist/store-availability.d.ts +12 -14
  155. package/dist/store-availability.js +18 -56
  156. package/dist/storefront.js +199 -0
  157. package/dist/structured-data.d.ts +3 -28
  158. package/dist/structured-data.js +19 -49
  159. package/dist/trace.js +1 -2
  160. package/dist/translations.d.ts +26 -31
  161. package/dist/translations.js +55 -28
  162. package/package.json +16 -14
  163. package/dist/graphql-env.d.ts +0 -22778
  164. package/dist/graphql.d.ts +0 -64
  165. package/dist/graphql.js +0 -344
  166. package/dist/schema.storefront.graphql +0 -10483
  167. package/dist/seo-proxy.d.ts +0 -11
  168. 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 { graphql, request } from "./graphql.js";
4
+ import { coalescedReads } from "./coalesce.js";
3
5
  import { getContentVersion } from "./content-version.js";
4
- import { toFileInfo } from "./files.js";
5
- import { metafieldsReader } from "./metafields.js";
6
- //#region src/blog.ts
7
- const FetchBlogQuery = graphql(`
8
- query FetchBlog($handle: String!, $first: Int!) {
9
- blog(handle: $handle) {
10
- handle
11
- title
12
- articlesCount
13
- articles(first: $first) {
14
- nodes {
15
- handle
16
- title
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: n.handle,
238
- blogHandle: n.blog.handle,
239
- title: n.title,
240
- summary: n.summary ?? null,
241
- image: toArticleImage(n.image, imageWidth),
242
- tags: [...n.tags],
243
- author: n.author ? { name: n.author.name } : null,
244
- publishedAt: n.publishedAt ?? null,
245
- ...n.bodyExcerpt === void 0 ? {} : { bodyExcerpt: n.bodyExcerpt }
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
- const fetchBlogCached = memoTTL(async ({ handle, first, locale }) => {
249
- const b = (await request(FetchBlogQuery, {
250
- handle,
251
- first
252
- }, locale ? { acceptLanguage: locale } : void 0)).blog;
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
- articles_count: b.articlesCount,
258
- articles: b.articles.nodes.map((node) => toArticleCard(node))
45
+ articlesCount: b.articlesCount,
46
+ tags: b.tags,
47
+ metafields: customDataMap(b.metafields)
259
48
  };
260
49
  }, {
261
- key: ({ handle, first, locale }) => `${handle}|${first}|${locale ?? ""}`,
50
+ key: (ref, locale) => `blog|${ref}|${locale ?? ""}`,
262
51
  shouldCache: (v) => v !== null,
263
52
  version: getContentVersion
264
53
  });
265
- /** Featured-blog / blog-card resolver fetcher. Returns `null` on missing
266
- * blog or GraphQL error; pickers degrade silently. */
267
- const fetchBlog = async (data) => {
268
- try {
269
- return await fetchBlogCached(data);
270
- } catch (err) {
271
- console.warn("[fetchBlog] failed:", err);
272
- return null;
273
- }
274
- };
275
- const BLOG_PAGE_SIZE = 12;
276
- /** Server-driven blog listing. Uncached because cursor + tag permutations would
277
- * blow the TTL cache; the underlying API is fast. Returns `null` on missing
278
- * blog so the route can map to `notFound()`. */
279
- const fetchBlogPage = async (data) => {
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 fetchArticleCached = memoTTL(async (ref, locale) => {
319
- const parts = parseArticleRef(ref);
320
- const a = (await request(FetchArticleQuery, {
321
- handle: parts.handle ?? null,
322
- blogHandle: parts.blogHandle ?? null,
323
- id: parts.id ?? null
324
- }, locale ? { acceptLanguage: locale } : void 0)).article;
325
- if (!a) return null;
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: a.handle,
328
- blogHandle: a.blog.handle,
329
- title: a.title,
330
- bodyHtml: a.bodyHtml,
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: (ref, locale) => `article|${ref}|${locale ?? ""}`,
86
+ key: ({ handle, first, locale }) => `${handle}|${first}|${locale ?? ""}`,
346
87
  shouldCache: (v) => v !== null,
347
88
  version: getContentVersion
348
89
  });
349
- /** Single-article fetch. Used by the `article` and `article_list` resolver
350
- * specs; returns `null` on missing article so the resolver drops the entry. */
351
- const fetchArticleByRef = async (ref, locale) => {
352
- try {
353
- return await fetchArticleCached(ref, locale);
354
- } catch (err) {
355
- console.warn("[fetchArticleByRef] failed:", err);
356
- return null;
357
- }
358
- };
359
- /** Detail-page fetch by `(blogHandle, handle)`. Used by the article route
360
- * loader. */
361
- const fetchArticle = async (params) => {
362
- return await fetchArticleByRef(`${params.blogHandle}/${params.handle}`, params.locale);
363
- };
364
- const FetchArticleCardQuery = graphql(`
365
- query FetchArticleCard(
366
- $handle: String
367
- $blogHandle: String
368
- $id: ID
369
- $imageWidth: Int!
370
- $excerptLength: Int!
371
- $withBodyExcerpt: Boolean!
372
- ) {
373
- article(handle: $handle, blogHandle: $blogHandle, id: $id) {
374
- handle
375
- title
376
- publishedAt
377
- tags
378
- author {
379
- name
380
- }
381
- summary(truncateAt: $excerptLength)
382
- bodyExcerpt: body(truncateAt: $excerptLength) @include(if: $withBodyExcerpt)
383
- image {
384
- __typename
385
- id
386
- alt
387
- ... on ShopMediaImage {
388
- url(transform: { maxWidth: $imageWidth })
389
- url2x: url(transform: { maxWidth: $imageWidth, scale: 2 })
390
- width
391
- height
392
- focalPoint {
393
- x
394
- y
395
- }
396
- }
397
- }
398
- blog {
399
- handle
400
- }
401
- }
402
- }
403
- `);
404
- /** One picked article as a card, sized as `fetchBlogPage` sizes a listing's
405
- * cards — for a picker whose article renders as a card, not a page. `ref` is
406
- * what the `article` setting stores (`parseArticleRef`). `null` on a missing
407
- * article or a failed read, so the card falls back rather than the page. */
408
- const fetchArticleCard = async (params) => {
409
- const imageWidth = params.imageWidth ?? 480;
410
- const { id, handle, blogHandle } = parseArticleRef(params.ref);
411
- try {
412
- const res = await request(FetchArticleCardQuery, {
413
- handle: handle ?? null,
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: (h, locale) => `blog-meta|${h}|${locale ?? ""}`,
156
+ key: (ref, locale) => `article|${ref}|${locale ?? ""}`,
442
157
  shouldCache: (v) => v !== null,
443
158
  version: getContentVersion
444
159
  });
445
- /** The posts either side of one in its blog, by publish date. Named by age
446
- * rather than previous / next because the two conventions disagree: Liquid's
447
- * `blog.previous_article` is the NEWER post and `next_article` the older.
448
- * Null at either end, for a post with no publish date, and on a failed read.
449
- * `imageWidth`, `excerptLength` and `withBodyExcerpt` size the cards as
450
- * `fetchBlogPage`'s do. */
451
- const fetchAdjacentArticles = async (params) => {
452
- if (!params.publishedAt) return {
453
- newer: null,
454
- older: null
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
- const imageWidth = params.imageWidth ?? 480;
457
- try {
458
- const res = await request(FetchAdjacentArticlesQuery, {
459
- blogId: params.blogId,
460
- publishedAt: params.publishedAt,
461
- imageWidth,
462
- excerptLength: params.excerptLength ?? 240,
463
- withBodyExcerpt: params.withBodyExcerpt ?? false
464
- }, params.locale ? { acceptLanguage: params.locale } : void 0);
465
- const other = (nodes) => {
466
- const node = nodes.find((n) => n.handle !== params.handle);
467
- return node ? toArticleCard(node, imageWidth) : null;
468
- };
469
- return {
470
- newer: other(res.newer.nodes),
471
- older: other(res.older.nodes)
472
- };
473
- } catch (err) {
474
- console.warn("[fetchAdjacentArticles] failed:", err);
475
- return {
476
- newer: null,
477
- older: null
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 };