@tanqory/theme-kit 0.1.0 → 2.0.0-alpha.17

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 (210) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +125 -10
  3. package/dist/abi/canvas-live.d.ts +67 -0
  4. package/dist/abi/canvas-live.js +154 -0
  5. package/dist/abi/cart.d.ts +59 -0
  6. package/dist/abi/cart.js +387 -0
  7. package/dist/abi/convert.d.ts +344 -0
  8. package/dist/abi/convert.js +1278 -0
  9. package/dist/abi/data-api.d.ts +15 -0
  10. package/dist/abi/data-api.js +209 -0
  11. package/dist/abi/jcs.d.ts +6 -0
  12. package/dist/abi/jcs.js +28 -0
  13. package/dist/abi/labels.d.ts +3 -0
  14. package/dist/abi/labels.js +34 -0
  15. package/dist/abi/page-data.d.ts +256 -0
  16. package/dist/abi/page-data.js +678 -0
  17. package/dist/abi/page.d.ts +28 -0
  18. package/dist/abi/page.js +57 -0
  19. package/dist/abi/referrals.d.ts +23 -0
  20. package/dist/abi/referrals.js +89 -0
  21. package/dist/abi/rewards.d.ts +27 -0
  22. package/dist/abi/rewards.js +82 -0
  23. package/dist/abi/seo.d.ts +47 -0
  24. package/dist/abi/seo.js +68 -0
  25. package/dist/abi/shell.d.ts +23 -0
  26. package/dist/abi/shell.js +10 -0
  27. package/dist/abi/state-pack.d.ts +17 -0
  28. package/dist/abi/state-pack.js +43 -0
  29. package/dist/abi/wire.d.ts +38 -0
  30. package/dist/abi/wire.js +84 -0
  31. package/dist/build.d.ts +116 -0
  32. package/dist/build.js +310 -0
  33. package/dist/client.d.ts +19 -0
  34. package/dist/client.js +87 -0
  35. package/dist/contract.d.ts +6 -0
  36. package/dist/contract.js +6 -0
  37. package/dist/headless/ai.d.ts +132 -0
  38. package/dist/headless/ai.js +99 -0
  39. package/dist/headless/hooks.d.ts +276 -0
  40. package/dist/headless/hooks.js +540 -0
  41. package/dist/headless/i18n.d.ts +13 -0
  42. package/dist/headless/i18n.js +45 -0
  43. package/dist/headless/index.d.ts +37 -0
  44. package/dist/headless/index.js +38 -0
  45. package/dist/headless/money.d.ts +25 -0
  46. package/dist/headless/money.js +61 -0
  47. package/dist/headless/page.d.ts +43 -0
  48. package/dist/headless/page.js +53 -0
  49. package/dist/headless/platform.d.ts +55 -0
  50. package/dist/headless/platform.js +77 -0
  51. package/dist/headless/primitives/cart.d.ts +90 -0
  52. package/dist/headless/primitives/cart.js +153 -0
  53. package/dist/headless/primitives/collection.d.ts +68 -0
  54. package/dist/headless/primitives/collection.js +80 -0
  55. package/dist/headless/primitives/product.d.ts +101 -0
  56. package/dist/headless/primitives/product.js +117 -0
  57. package/dist/headless/primitives/storefront.d.ts +106 -0
  58. package/dist/headless/primitives/storefront.js +186 -0
  59. package/dist/headless/primitives/util.d.ts +19 -0
  60. package/dist/headless/primitives/util.js +42 -0
  61. package/dist/headless/strings.d.ts +9 -0
  62. package/dist/headless/strings.js +190 -0
  63. package/dist/headless/structured-data.d.ts +31 -0
  64. package/dist/headless/structured-data.js +57 -0
  65. package/dist/headless/theme.d.ts +99 -0
  66. package/dist/headless/theme.js +54 -0
  67. package/dist/index.d.ts +20 -0
  68. package/dist/index.js +18 -553
  69. package/dist/runtime/data/StoreProvider.d.ts +84 -0
  70. package/dist/runtime/data/StoreProvider.js +373 -0
  71. package/dist/runtime/data/handle.d.ts +14 -0
  72. package/dist/runtime/data/handle.js +16 -0
  73. package/dist/runtime/data/routes.d.ts +87 -0
  74. package/dist/runtime/data/routes.js +158 -0
  75. package/dist/runtime/data/to-ui.d.ts +281 -0
  76. package/dist/runtime/data/to-ui.js +977 -0
  77. package/dist/runtime/entries/register.d.ts +9 -0
  78. package/dist/runtime/entries/register.js +9 -0
  79. package/dist/runtime/entries/sections.d.ts +16 -0
  80. package/dist/runtime/entries/sections.js +19 -0
  81. package/dist/{types → runtime/kit}/SectionTree.d.ts +2 -1
  82. package/dist/runtime/kit/SectionTree.js +100 -0
  83. package/dist/runtime/kit/analytics.d.ts +63 -0
  84. package/dist/runtime/kit/analytics.js +250 -0
  85. package/dist/runtime/kit/cart.d.ts +215 -0
  86. package/dist/runtime/kit/cart.js +545 -0
  87. package/dist/{types → runtime/kit}/composition.d.ts +1 -1
  88. package/dist/runtime/kit/composition.js +3 -0
  89. package/dist/{types → runtime/kit}/config.d.ts +2 -2
  90. package/dist/runtime/kit/config.js +6 -0
  91. package/dist/runtime/kit/consent.d.ts +29 -0
  92. package/dist/runtime/kit/consent.js +159 -0
  93. package/dist/runtime/kit/contract/content.d.ts +199 -0
  94. package/dist/runtime/kit/contract/content.js +106 -0
  95. package/dist/runtime/kit/contract/field-types.d.ts +337 -0
  96. package/dist/runtime/kit/contract/field-types.js +206 -0
  97. package/dist/runtime/kit/contract/groups.d.ts +230 -0
  98. package/dist/runtime/kit/contract/groups.js +402 -0
  99. package/dist/runtime/kit/contract/index.d.ts +33 -0
  100. package/dist/runtime/kit/contract/index.js +33 -0
  101. package/dist/runtime/kit/contract/transform.d.ts +82 -0
  102. package/dist/runtime/kit/contract/transform.js +133 -0
  103. package/dist/runtime/kit/contract/validate.d.ts +75 -0
  104. package/dist/runtime/kit/contract/validate.js +480 -0
  105. package/dist/runtime/kit/data.d.ts +720 -0
  106. package/dist/runtime/kit/data.js +1117 -0
  107. package/dist/{types → runtime/kit}/defineSection.d.ts +1 -1
  108. package/dist/runtime/kit/defineSection.js +10 -0
  109. package/dist/{types → runtime/kit}/dynamic-source.d.ts +40 -18
  110. package/dist/runtime/kit/dynamic-source.js +294 -0
  111. package/dist/runtime/kit/index.d.ts +24 -0
  112. package/dist/runtime/kit/index.js +24 -0
  113. package/dist/{types → runtime/kit}/jsx-to-json.d.ts +1 -1
  114. package/dist/runtime/kit/jsx-to-json.js +28 -0
  115. package/dist/{types → runtime/kit}/mount.d.ts +25 -4
  116. package/dist/runtime/kit/mount.js +124 -0
  117. package/dist/{types → runtime/kit}/registry.d.ts +1 -1
  118. package/dist/runtime/kit/registry.js +12 -0
  119. package/dist/{types → runtime/kit}/ssg.d.ts +9 -3
  120. package/dist/runtime/kit/ssg.js +56 -0
  121. package/dist/runtime/kit/storefront.d.ts +639 -0
  122. package/dist/runtime/kit/storefront.js +723 -0
  123. package/dist/{types → runtime/kit}/theme-context.d.ts +8 -2
  124. package/dist/runtime/kit/theme-context.js +25 -0
  125. package/dist/runtime/kit/types.d.ts +147 -0
  126. package/dist/runtime/kit/types.js +1 -0
  127. package/dist/runtime/platform/TrackingPixels.d.ts +14 -0
  128. package/dist/runtime/platform/TrackingPixels.js +63 -0
  129. package/dist/runtime/platform/environment.d.ts +68 -0
  130. package/dist/runtime/platform/environment.js +85 -0
  131. package/dist/runtime/platform/locale.d.ts +27 -0
  132. package/dist/runtime/platform/locale.js +42 -0
  133. package/dist/runtime/platform/route-analytics.d.ts +15 -0
  134. package/dist/runtime/platform/route-analytics.js +72 -0
  135. package/dist/runtime/platform/safe-href.d.ts +25 -0
  136. package/dist/runtime/platform/safe-href.js +69 -0
  137. package/dist/runtime/platform/safe-values.d.ts +37 -0
  138. package/dist/runtime/platform/safe-values.js +140 -0
  139. package/dist/runtime/render/SectionRenderer.d.ts +4 -0
  140. package/dist/runtime/render/SectionRenderer.js +166 -0
  141. package/dist/runtime/render/listing-query.d.ts +52 -0
  142. package/dist/runtime/render/listing-query.js +122 -0
  143. package/dist/runtime/render/localized-copy.d.ts +86 -0
  144. package/dist/runtime/render/localized-copy.js +129 -0
  145. package/dist/runtime/render/product-listing.d.ts +11 -0
  146. package/dist/runtime/render/product-listing.js +243 -0
  147. package/dist/runtime/render/section-actions.d.ts +66 -0
  148. package/dist/runtime/render/section-actions.js +202 -0
  149. package/dist/runtime/render/starter.d.ts +11 -0
  150. package/dist/runtime/render/starter.js +81 -0
  151. package/dist/runtime/seo/head.d.ts +115 -0
  152. package/dist/runtime/seo/head.js +281 -0
  153. package/dist/runtime/seo/structured-data.d.ts +83 -0
  154. package/dist/runtime/seo/structured-data.js +252 -0
  155. package/dist/runtime/shell/PageShell.d.ts +18 -0
  156. package/dist/runtime/shell/PageShell.js +113 -0
  157. package/dist/runtime/shell/RuntimeBridge.d.ts +14 -0
  158. package/dist/runtime/shell/RuntimeBridge.js +93 -0
  159. package/dist/runtime/shell/overlay-state.d.ts +31 -0
  160. package/dist/runtime/shell/overlay-state.js +59 -0
  161. package/dist/runtime/shell/preview-mode.d.ts +2 -0
  162. package/dist/runtime/shell/preview-mode.js +11 -0
  163. package/dist/runtime/shell/spa-router.d.ts +14 -0
  164. package/dist/runtime/shell/spa-router.js +76 -0
  165. package/dist/runtime/shell/url-redirects.d.ts +6 -0
  166. package/dist/runtime/shell/url-redirects.js +37 -0
  167. package/dist/runtime/slots.d.ts +74 -0
  168. package/dist/runtime/slots.js +16 -0
  169. package/dist/runtime.d.ts +34 -0
  170. package/dist/runtime.js +40 -0
  171. package/dist/sdk/app-block.d.ts +29 -0
  172. package/dist/sdk/app-block.js +22 -0
  173. package/dist/sdk/index.d.ts +26 -0
  174. package/dist/sdk/index.js +26 -0
  175. package/dist/sdk/referrals.d.ts +62 -0
  176. package/dist/sdk/referrals.js +20 -0
  177. package/dist/sdk/rewards.d.ts +85 -0
  178. package/dist/sdk/rewards.js +29 -0
  179. package/dist/sdk/runtime-context.d.ts +202 -0
  180. package/dist/sdk/runtime-context.js +73 -0
  181. package/dist/sdk/schema.d.ts +206 -0
  182. package/dist/sdk/schema.js +121 -0
  183. package/dist/sdk/server-render.d.ts +8 -0
  184. package/dist/sdk/server-render.js +12 -0
  185. package/dist/sdk/store-lookup.d.ts +27 -0
  186. package/dist/sdk/store-lookup.js +29 -0
  187. package/dist/sdk/store.d.ts +149 -0
  188. package/dist/sdk/store.js +5 -0
  189. package/dist/sdk/sx.d.ts +40 -0
  190. package/dist/sdk/sx.js +112 -0
  191. package/dist/sdk/theme.d.ts +191 -0
  192. package/dist/sdk/theme.js +10 -0
  193. package/dist/sdk/types.d.ts +757 -0
  194. package/dist/sdk/types.js +7 -0
  195. package/dist/server.d.ts +111 -0
  196. package/dist/server.js +533 -0
  197. package/docs/headless-api.md +220 -0
  198. package/package.json +76 -22
  199. package/dist/index.js.map +0 -1
  200. package/dist/ssg-BZ5FYTw7.js +0 -1802
  201. package/dist/ssg-BZ5FYTw7.js.map +0 -1
  202. package/dist/ssg.js +0 -8
  203. package/dist/ssg.js.map +0 -1
  204. package/dist/types/cart.d.ts +0 -86
  205. package/dist/types/data.d.ts +0 -357
  206. package/dist/types/editor.d.ts +0 -5
  207. package/dist/types/index.d.ts +0 -20
  208. package/dist/types/preview-bridge.d.ts +0 -9
  209. package/dist/types/storefront.d.ts +0 -333
  210. package/dist/types/types.d.ts +0 -44
@@ -0,0 +1,202 @@
1
+ /**
2
+ * The actions Nova's sections expose as props, wired to the platform: cart
3
+ * mutations, search, locale/market change. Sections stay presentational; this
4
+ * is the one place a click becomes a request.
5
+ */
6
+ import { useCallback, useMemo, useRef, useState } from 'react';
7
+ import { useCart } from '../kit/cart.js';
8
+ import { useT } from '../kit/theme-context.js';
9
+ import { useOpenCartAfterAdd, useOverlays } from '../shell/overlay-state.js';
10
+ /** A quantity change the store refused and said why (theme-abi Cart-1). */
11
+ class RefusedWrite extends Error {
12
+ constructor(refusal) {
13
+ super(refusal.code);
14
+ this.refusal = refusal;
15
+ }
16
+ }
17
+ /**
18
+ * The shopper-facing words for a refusal, in the page's language (locales/*.json): the merchant's
19
+ * limit when it is known, else that the most has been reached.
20
+ */
21
+ export function refusalMessage(t, refusal) {
22
+ return refusal.maxQuantity
23
+ ? t('cartLimitReached', 'You can buy up to {max} of this item.').replace('{max}', String(refusal.maxQuantity))
24
+ : t('cartLimitReachedAny', "You've reached the maximum quantity for this item.");
25
+ }
26
+ /**
27
+ * The shopper-facing words for an add stock cut (theme-abi Cart-2), in the page's language (locales/*.json): sold out,
28
+ * only some added, or none added because the cart already holds all that is in stock.
29
+ */
30
+ export function addStockMessage(t, stock) {
31
+ if (stock.code === 'out_of_stock')
32
+ return t('cartSoldOut', 'Sold out. This item is no longer in stock.');
33
+ if (stock.added === 0)
34
+ return t('cartNoMoreStock', 'Your cart already has all we have in stock of this item.');
35
+ if (stock.added === 1)
36
+ return t('cartAddedOnlyOne', "Only 1 was added to your cart. That's all we have in stock.");
37
+ return t('cartAddedOnly', "Only {count} were added to your cart. That's all we have in stock.").replace('{count}', String(stock.added));
38
+ }
39
+ /** The well-formed `{ key, value }` string pairs of an add's `options.attributes` (anything else is dropped). */
40
+ export function cartLineAttributes(raw) {
41
+ if (!Array.isArray(raw))
42
+ return [];
43
+ return raw
44
+ .filter((a) => !!a && typeof a === 'object' && typeof a.key === 'string' && typeof a.value === 'string')
45
+ .map((a) => ({ key: a.key, value: a.value }));
46
+ }
47
+ export function useCartActions() {
48
+ const cart = useCart();
49
+ const overlays = useOverlays();
50
+ const openCartAfterAdd = useOpenCartAfterAdd();
51
+ const [busy, setBusy] = useState(() => new Set());
52
+ const [failed, setFailed] = useState(() => new Set());
53
+ const [refused, setRefused] = useState(() => new Map());
54
+ const t = useT();
55
+ // A second click on the same line while its request is open is dropped: the
56
+ // state update above is async, so the guard reads a ref.
57
+ const inFlight = useRef(new Set());
58
+ const mark = (set, id, on) => set((prev) => {
59
+ const next = new Set(prev);
60
+ if (on)
61
+ next.add(id);
62
+ else
63
+ next.delete(id);
64
+ return next;
65
+ });
66
+ const withLine = useCallback(async (lineId, run) => {
67
+ if (inFlight.current.has(lineId))
68
+ return;
69
+ inFlight.current.add(lineId);
70
+ mark(setBusy, lineId, true);
71
+ mark(setFailed, lineId, false);
72
+ const refuse = (refusal) => setRefused((prev) => {
73
+ if (!refusal && !prev.has(lineId))
74
+ return prev;
75
+ const next = new Map(prev);
76
+ if (refusal)
77
+ next.set(lineId, refusal);
78
+ else
79
+ next.delete(lineId);
80
+ return next;
81
+ });
82
+ refuse(null);
83
+ try {
84
+ await run();
85
+ }
86
+ catch (e) {
87
+ mark(setFailed, lineId, true);
88
+ if (e instanceof RefusedWrite)
89
+ refuse(e.refusal);
90
+ }
91
+ finally {
92
+ inFlight.current.delete(lineId);
93
+ mark(setBusy, lineId, false);
94
+ }
95
+ }, []);
96
+ const addToCart = useCallback(async (variantId, quantity, options) => {
97
+ const attributes = cartLineAttributes(options?.attributes);
98
+ const ok = await cart.add({ variantId, quantity, ...(options?.sellingPlanId ? { sellingPlanId: options.sellingPlanId } : {}), ...(attributes.length ? { attributes } : {}) });
99
+ // Cart-2: a sold-out or partly added line is said where the shopper is, not hidden behind the opened cart.
100
+ const stock = cart.lastAddStock?.() ?? null;
101
+ if (stock) {
102
+ if (options?.showsStock !== true)
103
+ overlays.toast(addStockMessage(t, stock), ok ? 'default' : 'error');
104
+ return ok;
105
+ }
106
+ if (ok)
107
+ openCartAfterAdd();
108
+ else {
109
+ // Read now, not from `cart.error`: this render's cart predates the add.
110
+ const refusal = cart.lastRefusal?.() ?? null;
111
+ overlays.toast(refusal ? refusalMessage(t, refusal) : t('addFailed', 'This item could not be added to your cart.'), 'error');
112
+ }
113
+ return ok;
114
+ }, [cart, openCartAfterAdd, overlays, t]);
115
+ return useMemo(() => ({
116
+ addToCart,
117
+ lastAddStock: () => cart.lastAddStock?.() ?? null,
118
+ setQuantity: (lineId, quantity) => withLine(lineId, async () => {
119
+ await cart.updateQuantity(lineId, quantity);
120
+ // A refusal the store explained marks the line failed, with its reason (Cart-1).
121
+ const refusal = cart.lastRefusal?.() ?? null;
122
+ if (refusal)
123
+ throw new RefusedWrite(refusal);
124
+ }),
125
+ removeLine: (lineId) => withLine(lineId, () => cart.remove(lineId)),
126
+ busyLines: busy,
127
+ failedLines: failed,
128
+ refusedLines: refused,
129
+ setNote: (note) => cart.updateNote(note),
130
+ ...(cart.applyDiscountCode && cart.removeDiscountCode ? { applyDiscountCode: cart.applyDiscountCode, removeDiscountCode: cart.removeDiscountCode } : {}),
131
+ ...(cart.lastDiscountRefusal ? { lastDiscountRefusal: cart.lastDiscountRefusal } : {}),
132
+ ...(cart.lastDiscountReplaced ? { lastDiscountReplaced: cart.lastDiscountReplaced } : {}),
133
+ }), [addToCart, withLine, cart, busy, failed, refused]);
134
+ }
135
+ /** Go to a storefront URL through the SPA router when it is on (a real click), else a full load. */
136
+ export function navigate(href) {
137
+ if (typeof window === 'undefined')
138
+ return;
139
+ const a = document.createElement('a');
140
+ a.href = href;
141
+ a.hidden = true;
142
+ document.body.appendChild(a);
143
+ a.click();
144
+ a.remove();
145
+ }
146
+ export const searchHref = (query) => `/search?q=${encodeURIComponent(query.trim())}`;
147
+ const LANGUAGE_TAG = /^[A-Za-z]{2,3}(-[A-Za-z0-9]{2,8})*$/;
148
+ /**
149
+ * The URL a Region & language choice loads: `?country=CC` when the country changes, `?locale=ll`
150
+ * when the language changes (the platform honours both and remembers them for later pages). Only
151
+ * what changed is set, so a language switch never pins a country the visitor never picked.
152
+ */
153
+ export function localeHref(href, next, current = {}) {
154
+ const url = new URL(href);
155
+ const country = (next.countryCode ?? '').toUpperCase();
156
+ const language = next.languageCode ?? '';
157
+ let changed = false;
158
+ if (/^[A-Z]{2}$/.test(country) && country !== (current.countryCode ?? '').toUpperCase()) {
159
+ url.searchParams.set('country', country);
160
+ changed = true;
161
+ }
162
+ if (LANGUAGE_TAG.test(language) && language.toLowerCase() !== (current.languageCode ?? '').toLowerCase()) {
163
+ url.searchParams.set('locale', language);
164
+ changed = true;
165
+ }
166
+ if (!changed)
167
+ return null;
168
+ // A new market or language is a new listing: the page number belongs to the old one.
169
+ url.searchParams.delete('page');
170
+ return url.toString();
171
+ }
172
+ /** Region & language change: a fresh load, since prices and copy are answered for the new market and language. */
173
+ export function changeLocale(next, current) {
174
+ if (typeof window === 'undefined')
175
+ return;
176
+ const href = localeHref(window.location.href, next, current);
177
+ if (!href)
178
+ return;
179
+ if (next.countryCode && /^[A-Za-z]{2}$/.test(next.countryCode)) {
180
+ try {
181
+ window.localStorage.setItem('tq-country', next.countryCode.toUpperCase());
182
+ }
183
+ catch {
184
+ /* private mode */
185
+ }
186
+ }
187
+ window.location.href = href;
188
+ }
189
+ /** Market change: prices are fetched for a country, so a change is a fresh load (same as the old switcher). */
190
+ export function changeCountry(countryCode) {
191
+ if (typeof window === 'undefined' || !/^[A-Za-z]{2}$/.test(countryCode))
192
+ return;
193
+ try {
194
+ window.localStorage.setItem('tq-country', countryCode.toUpperCase());
195
+ }
196
+ catch {
197
+ /* private mode */
198
+ }
199
+ const url = new URL(window.location.href);
200
+ url.searchParams.set('country', countryCode.toUpperCase());
201
+ window.location.href = url.toString();
202
+ }
@@ -0,0 +1,11 @@
1
+ import type { ContentNode } from '../kit/types.js';
2
+ /** True where starter text is the point — the editor's "Add section" preview shows a section as its preset reads. */
3
+ export declare const ShowStarterContext: import("react").Context<boolean>;
4
+ /** A node type's setting defaults — what an unset field renders. */
5
+ export type DefaultsOf = (type: string) => Record<string, unknown> | undefined;
6
+ /** A node type's copy fields (text / textarea / richtext) — what a shopper reads. */
7
+ export type CopyFieldsOf = (type: string) => ReadonlySet<string> | undefined;
8
+ /** The node a shopper sees, or null when the section is still starter content. */
9
+ export declare function withoutStarter(node: ContentNode, starter: ReadonlySet<string>, defaultsOf?: DefaultsOf, copyFieldsOf?: CopyFieldsOf): ContentNode | null;
10
+ /** The theme's starter text as a lookup set (built once per theme). */
11
+ export declare function starterSet(list: readonly string[] | undefined): ReadonlySet<string>;
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Starter content on the live storefront (theme `starterText`).
3
+ *
4
+ * A theme's starter templates guide the merchant with instructions in place of
5
+ * content. Those must never reach a shopper: a field still holding one renders
6
+ * empty, a section whose copy is nothing but starter text is not rendered, and
7
+ * a block holding one is left out (a testimonial nobody wrote, a "Milestone 1").
8
+ * A section the merchant wrote copy into stays — an untouched subtext must not
9
+ * take the merchant's own heading down with it. The editor renders nodes as
10
+ * they are, so the merchant still sees every field to fill.
11
+ */
12
+ import { createContext } from 'react';
13
+ /** True where starter text is the point — the editor's "Add section" preview shows a section as its preset reads. */
14
+ export const ShowStarterContext = createContext(false);
15
+ const plain = (v) => v.replace(/<[^>]*>/g, '').trim();
16
+ // An unset field renders its default, so a default is checked like a saved value.
17
+ const holdsStarter = (type, settings, starter, defaultsOf) => {
18
+ for (const v of Object.values({ ...defaultsOf?.(type), ...settings }))
19
+ if (typeof v === 'string' && starter.has(v.trim()))
20
+ return true;
21
+ return false;
22
+ };
23
+ function pruneBlocks(blocks, starter, defaultsOf) {
24
+ if (!blocks)
25
+ return blocks;
26
+ const kept = [];
27
+ for (const b of blocks) {
28
+ if (holdsStarter(b.type, b.settings, starter, defaultsOf))
29
+ continue;
30
+ kept.push(b.blocks ? { ...b, blocks: pruneBlocks(b.blocks, starter, defaultsOf) } : b);
31
+ }
32
+ return kept;
33
+ }
34
+ /**
35
+ * A section's settings with its starter fields emptied, or null when none of
36
+ * its copy is the merchant's (or its copy fields are unknown).
37
+ */
38
+ function sectionWithoutStarter(node, starter, defaultsOf, copyFieldsOf) {
39
+ const copy = copyFieldsOf?.(node.type);
40
+ if (!copy)
41
+ return null;
42
+ const merged = { ...defaultsOf?.(node.type), ...node.settings };
43
+ const cleared = {};
44
+ let authored = false;
45
+ for (const [k, v] of Object.entries(merged)) {
46
+ if (typeof v !== 'string')
47
+ continue;
48
+ if (starter.has(v.trim()))
49
+ cleared[k] = '';
50
+ // Only saved copy counts: a default nobody wrote does not keep a section up.
51
+ else if (copy.has(k) && k in (node.settings ?? {}) && plain(v))
52
+ authored = true;
53
+ }
54
+ return authored ? { ...node.settings, ...cleared } : null;
55
+ }
56
+ /** The node a shopper sees, or null when the section is still starter content. */
57
+ export function withoutStarter(node, starter, defaultsOf, copyFieldsOf) {
58
+ if (starter.size === 0)
59
+ return node;
60
+ if (holdsStarter(node.type, node.settings, starter, defaultsOf)) {
61
+ const settings = sectionWithoutStarter(node, starter, defaultsOf, copyFieldsOf);
62
+ if (!settings)
63
+ return null;
64
+ node = { ...node, settings };
65
+ }
66
+ if (!node.blocks?.length)
67
+ return node;
68
+ const blocks = pruneBlocks(node.blocks, starter, defaultsOf);
69
+ return blocks && blocks.length === node.blocks.length && blocks.every((b, i) => b === node.blocks[i]) ? node : { ...node, blocks };
70
+ }
71
+ const sets = new WeakMap();
72
+ /** The theme's starter text as a lookup set (built once per theme). */
73
+ export function starterSet(list) {
74
+ if (!list?.length)
75
+ return EMPTY;
76
+ let s = sets.get(list);
77
+ if (!s)
78
+ sets.set(list, (s = new Set(list.map((x) => x.trim()).filter(Boolean))));
79
+ return s;
80
+ }
81
+ const EMPTY = new Set();
@@ -0,0 +1,115 @@
1
+ /**
2
+ * Per-route document head — title, description, canonical, Open Graph, Twitter
3
+ * card and favicon — computed from the merchant's own SEO fields.
4
+ *
5
+ * Extracted from `browser-entry.tsx` so BOTH entry points can use it: the boot path
6
+ * (which runs once) and the SPA router in `layouts/layout.tsx` (which runs on
7
+ * every soft navigation). Previously only boot applied a head, so every page a
8
+ * shopper reached by clicking kept the previous page's <title>, canonical and
9
+ * og: tags — including the link they then shared.
10
+ *
11
+ * Title precedence: the resource's SEO title verbatim (the merchant owns it) →
12
+ * "<resource title> — <shop>" → the shop name. Description: SEO description →
13
+ * page bodySummary → shop description (home only).
14
+ */
15
+ import type { DataApi } from '../kit/index.js';
16
+ import type { MetaobjectPage } from '../kit/data.js';
17
+ import { type JsonLd } from './structured-data.js';
18
+ /**
19
+ * A meta description from a resource's own text when the merchant wrote no SEO
20
+ * one: tags stripped, whitespace collapsed, cut at a word near 160 characters
21
+ * (what results pages show).
22
+ */
23
+ export declare function summary(text: string | null | undefined, max?: number): string;
24
+ /** Build a HeadMeta from a resource's SEO (shared by blog/article, which resolve
25
+ * their SEO asynchronously rather than from the sync bootstrap). */
26
+ export declare function headFrom(seo: {
27
+ title?: string | null;
28
+ description?: string | null;
29
+ keywords?: string[] | null;
30
+ } | null | undefined, resourceTitle: string, shopName: string, fallbackDesc?: string | null, image?: string | null): HeadMeta;
31
+ /**
32
+ * Per-route document head (title + meta description) from the merchant's SEO
33
+ * fields. Precedence — title: the resource's SEO title verbatim (merchant owns
34
+ * it) → "<resource title> — <shop>" → shop name; description: SEO description →
35
+ * page bodySummary → shop description (home only). Runs client-side on every
36
+ * route (the SSG only prerenders home), so product/collection/page pages that
37
+ * are client-rendered still get a correct, unique title + description.
38
+ */
39
+ export interface HeadMeta {
40
+ title: string;
41
+ description: string;
42
+ keywords: string[];
43
+ /** Absolute URL of the page's lead image, for og:image / twitter:image. */
44
+ image: string;
45
+ /** 'website' for the home page, 'product'/'article' for detail pages. */
46
+ type: string;
47
+ /** Shop name — og:site_name. */
48
+ siteName: string;
49
+ /** Absolute URL for the favicon / tab icon (square brand mark). */
50
+ favicon: string;
51
+ /** schema.org JSON-LD for the page (a Product on product routes), or null. */
52
+ jsonLd?: JsonLd | null;
53
+ }
54
+ /** Resolve a possibly-relative asset URL to an absolute one, against the
55
+ * current origin — social scrapers require absolute og:image URLs. */
56
+ export declare function absUrl(url: string | undefined | null): string;
57
+ /**
58
+ * The metaobject page entry whose head this is: the route's own (`<definitionHandle>/<handle>` equal to the route
59
+ * handle), else null. Exported for tests.
60
+ */
61
+ export declare function metaobjectHeadSource(entry: MetaobjectPage | null | undefined, routeHandle: string): MetaobjectPage | null;
62
+ export declare function computeHead(pathname: string, data: DataApi, settings: {
63
+ shopName?: unknown;
64
+ },
65
+ /** The request origin on the server render; the browser origin when omitted. */
66
+ origin?: string): HeadMeta;
67
+ /**
68
+ * Write the computed head into the live document (client-side).
69
+ *
70
+ * Beyond title/description/keywords this now emits the canonical link + Open
71
+ * Graph + Twitter card tags that were missing entirely — which is why a shared
72
+ * product link previewed as the bare shop name on every page. The canonical and
73
+ * og:url use the page's own URL (the host the storefront is served on is the
74
+ * correct canonical for that page); forcing canonical to a configured primary
75
+ * domain when a shopper is on a different host is the store-api#558 follow-up.
76
+ */
77
+ export declare function applyHead({ title, description, keywords, image, type, siteName, favicon, jsonLd }: HeadMeta): void;
78
+ /**
79
+ * The shop's name as the storefront shows it: the store's own name
80
+ * (Settings › General), else the theme's `shopName` setting (an AI-built theme
81
+ * may still carry the name it was built under), else 'Store'. The store name
82
+ * wins so a rename in Settings reaches the header, footer and titles (D-13).
83
+ */
84
+ export declare function shopNameOf(themeShopName: unknown, storeName: unknown): string;
85
+ /**
86
+ * A route's title: the resource's SEO title verbatim (the merchant owns it),
87
+ * else "<resource title> — <shop>", else the shop name alone.
88
+ */
89
+ export declare function documentTitle(input: {
90
+ seoTitle?: string | null;
91
+ resourceTitle?: string | null;
92
+ shopName: string;
93
+ }): string;
94
+ /**
95
+ * The page name in the `<title>` of a route that has no resource of its own (board 35 #76): the cart, search, the
96
+ * collections list, a policy and the Not found page each name the page, as "<page> — <shop>" does on a product,
97
+ * instead of the bare shop name on every one of them. The reference storefront does the same: its `page_title` names
98
+ * the cart, the search with its terms, the collection list, the policy and the 404 page, and only the home page takes
99
+ * the shop's own title (Online Store > Preferences; that stays `homeHead`).
100
+ *
101
+ * The words are the theme's strings (`seo.title.*`, translated per language and by the merchant like any theme
102
+ * word), with these English fallbacks for a theme that ships none. `{query}` in `seo.title.searchResults` is the
103
+ * shopper's search; the caller escapes the whole title. Null for every other route (the head keeps its title).
104
+ */
105
+ export declare const ROUTE_TITLE_FALLBACK: Readonly<Record<string, string>>;
106
+ export declare function routeTitleOf(input: {
107
+ kind: string;
108
+ /** The route's resource is missing (an unknown product, collection, page, blog or policy): a Not found page. */
109
+ missing?: boolean;
110
+ /** The theme's words for the page's language, the merchant's translation first. */
111
+ t: (key: string) => string | undefined;
112
+ searchQuery?: string | null;
113
+ /** The policy's own title (Settings > Policies), on a policy route. */
114
+ policyTitle?: string | null;
115
+ }): string | null;