@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.
- package/LICENSE +21 -0
- package/README.md +125 -10
- package/dist/abi/canvas-live.d.ts +67 -0
- package/dist/abi/canvas-live.js +154 -0
- package/dist/abi/cart.d.ts +59 -0
- package/dist/abi/cart.js +387 -0
- package/dist/abi/convert.d.ts +344 -0
- package/dist/abi/convert.js +1278 -0
- package/dist/abi/data-api.d.ts +15 -0
- package/dist/abi/data-api.js +209 -0
- package/dist/abi/jcs.d.ts +6 -0
- package/dist/abi/jcs.js +28 -0
- package/dist/abi/labels.d.ts +3 -0
- package/dist/abi/labels.js +34 -0
- package/dist/abi/page-data.d.ts +256 -0
- package/dist/abi/page-data.js +678 -0
- package/dist/abi/page.d.ts +28 -0
- package/dist/abi/page.js +57 -0
- package/dist/abi/referrals.d.ts +23 -0
- package/dist/abi/referrals.js +89 -0
- package/dist/abi/rewards.d.ts +27 -0
- package/dist/abi/rewards.js +82 -0
- package/dist/abi/seo.d.ts +47 -0
- package/dist/abi/seo.js +68 -0
- package/dist/abi/shell.d.ts +23 -0
- package/dist/abi/shell.js +10 -0
- package/dist/abi/state-pack.d.ts +17 -0
- package/dist/abi/state-pack.js +43 -0
- package/dist/abi/wire.d.ts +38 -0
- package/dist/abi/wire.js +84 -0
- package/dist/build.d.ts +116 -0
- package/dist/build.js +310 -0
- package/dist/client.d.ts +19 -0
- package/dist/client.js +87 -0
- package/dist/contract.d.ts +6 -0
- package/dist/contract.js +6 -0
- package/dist/headless/ai.d.ts +132 -0
- package/dist/headless/ai.js +99 -0
- package/dist/headless/hooks.d.ts +276 -0
- package/dist/headless/hooks.js +540 -0
- package/dist/headless/i18n.d.ts +13 -0
- package/dist/headless/i18n.js +45 -0
- package/dist/headless/index.d.ts +37 -0
- package/dist/headless/index.js +38 -0
- package/dist/headless/money.d.ts +25 -0
- package/dist/headless/money.js +61 -0
- package/dist/headless/page.d.ts +43 -0
- package/dist/headless/page.js +53 -0
- package/dist/headless/platform.d.ts +55 -0
- package/dist/headless/platform.js +77 -0
- package/dist/headless/primitives/cart.d.ts +90 -0
- package/dist/headless/primitives/cart.js +153 -0
- package/dist/headless/primitives/collection.d.ts +68 -0
- package/dist/headless/primitives/collection.js +80 -0
- package/dist/headless/primitives/product.d.ts +101 -0
- package/dist/headless/primitives/product.js +117 -0
- package/dist/headless/primitives/storefront.d.ts +106 -0
- package/dist/headless/primitives/storefront.js +186 -0
- package/dist/headless/primitives/util.d.ts +19 -0
- package/dist/headless/primitives/util.js +42 -0
- package/dist/headless/strings.d.ts +9 -0
- package/dist/headless/strings.js +190 -0
- package/dist/headless/structured-data.d.ts +31 -0
- package/dist/headless/structured-data.js +57 -0
- package/dist/headless/theme.d.ts +99 -0
- package/dist/headless/theme.js +54 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +18 -553
- package/dist/runtime/data/StoreProvider.d.ts +84 -0
- package/dist/runtime/data/StoreProvider.js +373 -0
- package/dist/runtime/data/handle.d.ts +14 -0
- package/dist/runtime/data/handle.js +16 -0
- package/dist/runtime/data/routes.d.ts +87 -0
- package/dist/runtime/data/routes.js +158 -0
- package/dist/runtime/data/to-ui.d.ts +281 -0
- package/dist/runtime/data/to-ui.js +977 -0
- package/dist/runtime/entries/register.d.ts +9 -0
- package/dist/runtime/entries/register.js +9 -0
- package/dist/runtime/entries/sections.d.ts +16 -0
- package/dist/runtime/entries/sections.js +19 -0
- package/dist/{types → runtime/kit}/SectionTree.d.ts +2 -1
- package/dist/runtime/kit/SectionTree.js +100 -0
- package/dist/runtime/kit/analytics.d.ts +63 -0
- package/dist/runtime/kit/analytics.js +250 -0
- package/dist/runtime/kit/cart.d.ts +215 -0
- package/dist/runtime/kit/cart.js +545 -0
- package/dist/{types → runtime/kit}/composition.d.ts +1 -1
- package/dist/runtime/kit/composition.js +3 -0
- package/dist/{types → runtime/kit}/config.d.ts +2 -2
- package/dist/runtime/kit/config.js +6 -0
- package/dist/runtime/kit/consent.d.ts +29 -0
- package/dist/runtime/kit/consent.js +159 -0
- package/dist/runtime/kit/contract/content.d.ts +199 -0
- package/dist/runtime/kit/contract/content.js +106 -0
- package/dist/runtime/kit/contract/field-types.d.ts +337 -0
- package/dist/runtime/kit/contract/field-types.js +206 -0
- package/dist/runtime/kit/contract/groups.d.ts +230 -0
- package/dist/runtime/kit/contract/groups.js +402 -0
- package/dist/runtime/kit/contract/index.d.ts +33 -0
- package/dist/runtime/kit/contract/index.js +33 -0
- package/dist/runtime/kit/contract/transform.d.ts +82 -0
- package/dist/runtime/kit/contract/transform.js +133 -0
- package/dist/runtime/kit/contract/validate.d.ts +75 -0
- package/dist/runtime/kit/contract/validate.js +480 -0
- package/dist/runtime/kit/data.d.ts +720 -0
- package/dist/runtime/kit/data.js +1117 -0
- package/dist/{types → runtime/kit}/defineSection.d.ts +1 -1
- package/dist/runtime/kit/defineSection.js +10 -0
- package/dist/{types → runtime/kit}/dynamic-source.d.ts +40 -18
- package/dist/runtime/kit/dynamic-source.js +294 -0
- package/dist/runtime/kit/index.d.ts +24 -0
- package/dist/runtime/kit/index.js +24 -0
- package/dist/{types → runtime/kit}/jsx-to-json.d.ts +1 -1
- package/dist/runtime/kit/jsx-to-json.js +28 -0
- package/dist/{types → runtime/kit}/mount.d.ts +25 -4
- package/dist/runtime/kit/mount.js +124 -0
- package/dist/{types → runtime/kit}/registry.d.ts +1 -1
- package/dist/runtime/kit/registry.js +12 -0
- package/dist/{types → runtime/kit}/ssg.d.ts +9 -3
- package/dist/runtime/kit/ssg.js +56 -0
- package/dist/runtime/kit/storefront.d.ts +639 -0
- package/dist/runtime/kit/storefront.js +723 -0
- package/dist/{types → runtime/kit}/theme-context.d.ts +8 -2
- package/dist/runtime/kit/theme-context.js +25 -0
- package/dist/runtime/kit/types.d.ts +147 -0
- package/dist/runtime/kit/types.js +1 -0
- package/dist/runtime/platform/TrackingPixels.d.ts +14 -0
- package/dist/runtime/platform/TrackingPixels.js +63 -0
- package/dist/runtime/platform/environment.d.ts +68 -0
- package/dist/runtime/platform/environment.js +85 -0
- package/dist/runtime/platform/locale.d.ts +27 -0
- package/dist/runtime/platform/locale.js +42 -0
- package/dist/runtime/platform/route-analytics.d.ts +15 -0
- package/dist/runtime/platform/route-analytics.js +72 -0
- package/dist/runtime/platform/safe-href.d.ts +25 -0
- package/dist/runtime/platform/safe-href.js +69 -0
- package/dist/runtime/platform/safe-values.d.ts +37 -0
- package/dist/runtime/platform/safe-values.js +140 -0
- package/dist/runtime/render/SectionRenderer.d.ts +4 -0
- package/dist/runtime/render/SectionRenderer.js +166 -0
- package/dist/runtime/render/listing-query.d.ts +52 -0
- package/dist/runtime/render/listing-query.js +122 -0
- package/dist/runtime/render/localized-copy.d.ts +86 -0
- package/dist/runtime/render/localized-copy.js +129 -0
- package/dist/runtime/render/product-listing.d.ts +11 -0
- package/dist/runtime/render/product-listing.js +243 -0
- package/dist/runtime/render/section-actions.d.ts +66 -0
- package/dist/runtime/render/section-actions.js +202 -0
- package/dist/runtime/render/starter.d.ts +11 -0
- package/dist/runtime/render/starter.js +81 -0
- package/dist/runtime/seo/head.d.ts +115 -0
- package/dist/runtime/seo/head.js +281 -0
- package/dist/runtime/seo/structured-data.d.ts +83 -0
- package/dist/runtime/seo/structured-data.js +252 -0
- package/dist/runtime/shell/PageShell.d.ts +18 -0
- package/dist/runtime/shell/PageShell.js +113 -0
- package/dist/runtime/shell/RuntimeBridge.d.ts +14 -0
- package/dist/runtime/shell/RuntimeBridge.js +93 -0
- package/dist/runtime/shell/overlay-state.d.ts +31 -0
- package/dist/runtime/shell/overlay-state.js +59 -0
- package/dist/runtime/shell/preview-mode.d.ts +2 -0
- package/dist/runtime/shell/preview-mode.js +11 -0
- package/dist/runtime/shell/spa-router.d.ts +14 -0
- package/dist/runtime/shell/spa-router.js +76 -0
- package/dist/runtime/shell/url-redirects.d.ts +6 -0
- package/dist/runtime/shell/url-redirects.js +37 -0
- package/dist/runtime/slots.d.ts +74 -0
- package/dist/runtime/slots.js +16 -0
- package/dist/runtime.d.ts +34 -0
- package/dist/runtime.js +40 -0
- package/dist/sdk/app-block.d.ts +29 -0
- package/dist/sdk/app-block.js +22 -0
- package/dist/sdk/index.d.ts +26 -0
- package/dist/sdk/index.js +26 -0
- package/dist/sdk/referrals.d.ts +62 -0
- package/dist/sdk/referrals.js +20 -0
- package/dist/sdk/rewards.d.ts +85 -0
- package/dist/sdk/rewards.js +29 -0
- package/dist/sdk/runtime-context.d.ts +202 -0
- package/dist/sdk/runtime-context.js +73 -0
- package/dist/sdk/schema.d.ts +206 -0
- package/dist/sdk/schema.js +121 -0
- package/dist/sdk/server-render.d.ts +8 -0
- package/dist/sdk/server-render.js +12 -0
- package/dist/sdk/store-lookup.d.ts +27 -0
- package/dist/sdk/store-lookup.js +29 -0
- package/dist/sdk/store.d.ts +149 -0
- package/dist/sdk/store.js +5 -0
- package/dist/sdk/sx.d.ts +40 -0
- package/dist/sdk/sx.js +112 -0
- package/dist/sdk/theme.d.ts +191 -0
- package/dist/sdk/theme.js +10 -0
- package/dist/sdk/types.d.ts +757 -0
- package/dist/sdk/types.js +7 -0
- package/dist/server.d.ts +111 -0
- package/dist/server.js +533 -0
- package/docs/headless-api.md +220 -0
- package/package.json +76 -22
- package/dist/index.js.map +0 -1
- package/dist/ssg-BZ5FYTw7.js +0 -1802
- package/dist/ssg-BZ5FYTw7.js.map +0 -1
- package/dist/ssg.js +0 -8
- package/dist/ssg.js.map +0 -1
- package/dist/types/cart.d.ts +0 -86
- package/dist/types/data.d.ts +0 -357
- package/dist/types/editor.d.ts +0 -5
- package/dist/types/index.d.ts +0 -20
- package/dist/types/preview-bridge.d.ts +0 -9
- package/dist/types/storefront.d.ts +0 -333
- package/dist/types/types.d.ts +0 -44
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* The store context Nova's adapters read (`ui/adapters` StoreContext), built
|
|
4
|
+
* from the theme's data layer.
|
|
5
|
+
*
|
|
6
|
+
* Sync first: everything the bootstrap payload already holds (shop, menus,
|
|
7
|
+
* collections, products, the route's page) is mapped during render, so the
|
|
8
|
+
* server markup and the client's first render agree. What the bootstrap lacks
|
|
9
|
+
* is requested and loaded after render, into a cache keyed by WHAT was asked —
|
|
10
|
+
* `collection-products:<handle>`, `search:<query>` — never by a boolean. A soft
|
|
11
|
+
* navigation to another handle therefore reads another key: the previous
|
|
12
|
+
* page's data cannot render under the new URL.
|
|
13
|
+
*/
|
|
14
|
+
import { createContext, useContext, useEffect, useMemo, useRef, useState } from 'react';
|
|
15
|
+
import { getThemeBundle } from '../slots.js';
|
|
16
|
+
import { useData } from '../kit/data.js';
|
|
17
|
+
import { useCart } from '../kit/cart.js';
|
|
18
|
+
import { matchRoute } from './routes.js';
|
|
19
|
+
import { hasDecided, getConsentMode, isConsentArmed, onConsentChange, onConsentModeChange } from '../kit/consent.js';
|
|
20
|
+
import * as map from './to-ui.js';
|
|
21
|
+
import { usePreviewMode } from '../shell/preview-mode.js';
|
|
22
|
+
import { DynamicSourceProvider } from '../kit/dynamic-source.js';
|
|
23
|
+
/** The resource cache: read during render, filled after it. */
|
|
24
|
+
class Resources {
|
|
25
|
+
constructor(seed) {
|
|
26
|
+
this.cache = new Map();
|
|
27
|
+
this.wanted = new Map();
|
|
28
|
+
for (const [key, value] of Object.entries(seed ?? {}))
|
|
29
|
+
if (value != null)
|
|
30
|
+
this.cache.set(key, { status: 'done', value });
|
|
31
|
+
}
|
|
32
|
+
/** The loaded value, or undefined — and ask for it when it has not been asked for. */
|
|
33
|
+
get(key, load) {
|
|
34
|
+
const hit = this.cache.get(key);
|
|
35
|
+
if (hit)
|
|
36
|
+
return hit.status === 'done' ? hit.value : undefined;
|
|
37
|
+
if (load)
|
|
38
|
+
this.wanted.set(key, load);
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
status(key) {
|
|
42
|
+
return this.cache.get(key)?.status ?? 'idle';
|
|
43
|
+
}
|
|
44
|
+
/** Start every requested load; `changed` runs once per settled load. */
|
|
45
|
+
flush(changed) {
|
|
46
|
+
for (const [key, load] of this.wanted) {
|
|
47
|
+
if (this.cache.has(key))
|
|
48
|
+
continue;
|
|
49
|
+
this.cache.set(key, { status: 'loading' });
|
|
50
|
+
load()
|
|
51
|
+
.then((value) => this.cache.set(key, { status: 'done', value }))
|
|
52
|
+
.catch(() => this.cache.set(key, { status: 'error' }))
|
|
53
|
+
.finally(changed);
|
|
54
|
+
}
|
|
55
|
+
this.wanted.clear();
|
|
56
|
+
}
|
|
57
|
+
/** Replace a key's value (a listing's next page). */
|
|
58
|
+
put(key, value) {
|
|
59
|
+
this.cache.set(key, { status: 'done', value });
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const StoreCtx = createContext(null);
|
|
63
|
+
export function useStore() {
|
|
64
|
+
return useContext(StoreCtx);
|
|
65
|
+
}
|
|
66
|
+
/** An image setting holds a URL. Its size comes from the URL when it carries one, else 4:3 (MediaFrame's own fallback). */
|
|
67
|
+
export function imageFromUrl(url) {
|
|
68
|
+
const src = url.trim();
|
|
69
|
+
if (!src)
|
|
70
|
+
return null;
|
|
71
|
+
let width = 1200;
|
|
72
|
+
let height = 900;
|
|
73
|
+
const svg = /^data:image\/svg\+xml/.test(src) ? decodeURIComponent(src) : '';
|
|
74
|
+
const w = /\bwidth="(\d+)"/.exec(svg)?.[1] ?? new URLSearchParams(src.split('?')[1] ?? '').get('width');
|
|
75
|
+
const h = /\bheight="(\d+)"/.exec(svg)?.[1] ?? new URLSearchParams(src.split('?')[1] ?? '').get('height');
|
|
76
|
+
if (w && h && Number(w) > 0 && Number(h) > 0) {
|
|
77
|
+
width = Number(w);
|
|
78
|
+
height = Number(h);
|
|
79
|
+
}
|
|
80
|
+
return { src, width, height, alt: '' };
|
|
81
|
+
}
|
|
82
|
+
const PICTURE_PATH = /\.(?:avif|gif|jpe?g|png|svg|webp)$/i;
|
|
83
|
+
/**
|
|
84
|
+
* A video setting's URL → the video, or nothing when the URL is not a video. A video setting saved with a picture
|
|
85
|
+
* (the dashboard's media picker used to answer a video with its poster frame) is no playable source: `<video>` on a
|
|
86
|
+
* .webp fails, and a hero then drew an empty grey box where its media should be. Without a video the section falls
|
|
87
|
+
* back to its picture or to text, as for an empty setting.
|
|
88
|
+
*/
|
|
89
|
+
export function videoFromUrl(url) {
|
|
90
|
+
const src = url.trim();
|
|
91
|
+
if (!src)
|
|
92
|
+
return null;
|
|
93
|
+
let path = src;
|
|
94
|
+
try {
|
|
95
|
+
path = new URL(src, 'https://media.invalid').pathname;
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
path = src.split(/[?#]/)[0] ?? src;
|
|
99
|
+
}
|
|
100
|
+
if (/^data:image\//i.test(src) || PICTURE_PATH.test(path))
|
|
101
|
+
return null;
|
|
102
|
+
return { src };
|
|
103
|
+
}
|
|
104
|
+
/** A read-through map: `ctx.menus['main-menu']` resolves on access. */
|
|
105
|
+
function lookup(resolve) {
|
|
106
|
+
return new Proxy({}, {
|
|
107
|
+
get: (_t, key) => (typeof key === 'string' && key ? resolve(key) : undefined),
|
|
108
|
+
has: (_t, key) => typeof key === 'string' && resolve(key) !== undefined,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
const PRODUCTS_PER_REQUEST = 24;
|
|
112
|
+
/** The handle the platform gives the store's privacy policy (lib/abi/convert.ts POLICY_SLOT). */
|
|
113
|
+
const PRIVACY_POLICY = 'privacy-policy';
|
|
114
|
+
/**
|
|
115
|
+
* The search page's sort control (`?sort=`). Relevance is the platform's default order; the others
|
|
116
|
+
* are the orders the data plane answers for search (Theme ABI `catalog.search` `sortKey`). The ids
|
|
117
|
+
* match lib/abi/data-api.ts `SEARCH_ORDER`, which keys the server-loaded result the same way.
|
|
118
|
+
*/
|
|
119
|
+
const SEARCH_SORT_IDS = ['relevance', 'price-asc', 'price-desc', 'title-asc', 'created-desc'];
|
|
120
|
+
const SEARCH_SORT_LABELS = {
|
|
121
|
+
en: { relevance: 'Relevance', 'price-asc': 'Price, low to high', 'price-desc': 'Price, high to low', 'title-asc': 'Name, A–Z', 'created-desc': 'Newest' },
|
|
122
|
+
th: { relevance: 'ความเกี่ยวข้อง', 'price-asc': 'ราคา ต่ำไปสูง', 'price-desc': 'ราคา สูงไปต่ำ', 'title-asc': 'ชื่อ ก–ฮ', 'created-desc': 'ใหม่ล่าสุด' },
|
|
123
|
+
ar: { relevance: 'الأكثر صلة', 'price-asc': 'السعر، من الأقل إلى الأعلى', 'price-desc': 'السعر، من الأعلى إلى الأقل', 'title-asc': 'الاسم، أ–ي', 'created-desc': 'الأحدث' },
|
|
124
|
+
};
|
|
125
|
+
/** A known non-default sort id, else '' (relevance). */
|
|
126
|
+
export const searchSortOf = (sort) => (sort && sort !== 'relevance' && SEARCH_SORT_IDS.includes(sort) ? sort : '');
|
|
127
|
+
/** Which page of the account area (`data.accountUrl(path)`) an `/account*` path opens. */
|
|
128
|
+
export function accountPageOf(pathname) {
|
|
129
|
+
const sub = pathname.replace(/^\/account\/?/, '').split('/')[0];
|
|
130
|
+
return sub === 'orders' || sub === 'addresses' ? sub : 'profile';
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* The breadcrumb words: the theme's for the locale, each replaced by the merchant's translation of the same key when
|
|
134
|
+
* there is one (Settings > Languages > Translate, Default theme content; board 35 #41: "Home / Collections" stayed
|
|
135
|
+
* English on a Burmese page).
|
|
136
|
+
*/
|
|
137
|
+
export function breadcrumbLabels(base, merchant) {
|
|
138
|
+
if (!merchant)
|
|
139
|
+
return base;
|
|
140
|
+
const out = { ...base };
|
|
141
|
+
for (const k of Object.keys(base)) {
|
|
142
|
+
const v = Object.prototype.hasOwnProperty.call(merchant, k) ? merchant[k] : undefined;
|
|
143
|
+
if (typeof v === 'string' && v.trim())
|
|
144
|
+
out[k] = v;
|
|
145
|
+
}
|
|
146
|
+
return out;
|
|
147
|
+
}
|
|
148
|
+
export function StoreProvider({ location, locale, settings, labels: merchant, children }) {
|
|
149
|
+
const data = useData();
|
|
150
|
+
const cart = useCart();
|
|
151
|
+
const labels = breadcrumbLabels(getThemeBundle().theme.labels(locale), merchant);
|
|
152
|
+
const [, setVersion] = useState(0);
|
|
153
|
+
// One cache for the page's lifetime. The post-hydration revalidation swaps in
|
|
154
|
+
// a new data source seconds after the first loads were issued; starting clean
|
|
155
|
+
// then re-sent every query a second time for the same answers.
|
|
156
|
+
const resources = useRef(null);
|
|
157
|
+
resources.current ??= new Resources(data.preloaded);
|
|
158
|
+
const cache = resources.current;
|
|
159
|
+
const bump = useRef(() => setVersion((v) => v + 1)).current;
|
|
160
|
+
// Consent is the shopper's own stored answer and a per-request verdict: the
|
|
161
|
+
// server cannot know either, so both are read after mount (first render = closed).
|
|
162
|
+
const [consent, setConsentState] = useState({ enabled: false, answered: true });
|
|
163
|
+
const preview = usePreviewMode();
|
|
164
|
+
useEffect(() => {
|
|
165
|
+
// Theme ABI v2: the platform owns consent. Ask it after hydration (never in the server render,
|
|
166
|
+
// so every page stays cacheable), and never on a preview render — the merchant is editing,
|
|
167
|
+
// not shopping, and the banner covered the canvas.
|
|
168
|
+
if (data.consent) {
|
|
169
|
+
if (preview)
|
|
170
|
+
return;
|
|
171
|
+
let alive = true;
|
|
172
|
+
data.consent
|
|
173
|
+
.get()
|
|
174
|
+
.then((r) => {
|
|
175
|
+
// The merchant's banner settings decide where it shows (map.consentBannerShows); absent = as before.
|
|
176
|
+
const required = r.bannerRequired && map.consentBannerShows(r, data.shop?.consentBanner);
|
|
177
|
+
if (alive)
|
|
178
|
+
setConsentState({ enabled: required, answered: r.known || !required });
|
|
179
|
+
})
|
|
180
|
+
.catch(() => {
|
|
181
|
+
/* no readout ⇒ no banner; the platform's default applies */
|
|
182
|
+
});
|
|
183
|
+
// The shopper's choice in the banner (RuntimeBridge `consent.decide`) answers it for this page.
|
|
184
|
+
const offChoice = onConsentChange(() => setConsentState((c) => ({ ...c, answered: true })));
|
|
185
|
+
return () => {
|
|
186
|
+
alive = false;
|
|
187
|
+
offChoice();
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
const read = () => setConsentState({ enabled: isConsentArmed() && getConsentMode() !== 'NONE', answered: hasDecided() });
|
|
191
|
+
read();
|
|
192
|
+
const offMode = onConsentModeChange(read);
|
|
193
|
+
const offChoice = onConsentChange(read);
|
|
194
|
+
return () => {
|
|
195
|
+
offMode();
|
|
196
|
+
offChoice();
|
|
197
|
+
};
|
|
198
|
+
}, [data.consent, preview]);
|
|
199
|
+
// The banner links the privacy policy only when the store has published it. A page other than
|
|
200
|
+
// that policy's own does not carry it, so it is asked for once, when the banner is armed.
|
|
201
|
+
const [privacyFetched, setPrivacyFetched] = useState(undefined);
|
|
202
|
+
// The page's list of published policies (Theme ABI `Shop.policyLinks`) or the merchant's own link
|
|
203
|
+
// already answers it: no request.
|
|
204
|
+
const shopLinks = data.shop;
|
|
205
|
+
const privacyKnown = Boolean(map.toPolicies(data.shop)[PRIVACY_POLICY]) || Array.isArray(shopLinks?.policyLinks) || Boolean(shopLinks?.consentBanner?.privacyLinkUrl);
|
|
206
|
+
useEffect(() => {
|
|
207
|
+
if (!consent.enabled || privacyKnown || privacyFetched !== undefined || !data.fetchPolicy)
|
|
208
|
+
return;
|
|
209
|
+
let alive = true;
|
|
210
|
+
data.fetchPolicy(PRIVACY_POLICY).then((p) => { if (alive)
|
|
211
|
+
setPrivacyFetched(map.toPolicy(p) ?? null); }, () => { if (alive)
|
|
212
|
+
setPrivacyFetched(null); });
|
|
213
|
+
return () => {
|
|
214
|
+
alive = false;
|
|
215
|
+
};
|
|
216
|
+
}, [consent.enabled, privacyKnown, privacyFetched, data]);
|
|
217
|
+
// Loads requested during this render start once it has committed.
|
|
218
|
+
useEffect(() => cache.flush(bump));
|
|
219
|
+
const pathname = location.split('?')[0] || '/';
|
|
220
|
+
const search = new URLSearchParams(location.split('?')[1] ?? '');
|
|
221
|
+
const query = search.get('q') ?? '';
|
|
222
|
+
const searchSort = searchSortOf(search.get('sort'));
|
|
223
|
+
const route = matchRoute(pathname);
|
|
224
|
+
const shop = useMemo(() => map.toShop(data.shop, { shopName: str(settings.shopName), logo: str(settings.logo) }), [data.shop, settings.shopName, settings.logo]);
|
|
225
|
+
let sources = {};
|
|
226
|
+
const ctx = (() => {
|
|
227
|
+
const product = (handle) => {
|
|
228
|
+
const full = cache.get(`product:${handle}`, data.fetchProduct ? () => data.fetchProduct(handle) : undefined);
|
|
229
|
+
const raw = full ?? data.productByHandle?.(handle) ?? null;
|
|
230
|
+
return raw ? map.toProduct(raw) : undefined;
|
|
231
|
+
};
|
|
232
|
+
const collection = (handle) => {
|
|
233
|
+
const base = data.collectionByHandle?.(handle) ?? null;
|
|
234
|
+
const page = cache.get(`collection-products:${handle}`, data.collectionProducts ? () => data.collectionProducts(handle, { first: PRODUCTS_PER_REQUEST }) : undefined);
|
|
235
|
+
if (!base)
|
|
236
|
+
return undefined;
|
|
237
|
+
// The bootstrap is baked at build time: a collection whose products were
|
|
238
|
+
// assigned later is an empty shell there. Live products win once loaded.
|
|
239
|
+
const products = page?.products?.length ? page.products : base.products;
|
|
240
|
+
return map.toCollection(base, { products });
|
|
241
|
+
};
|
|
242
|
+
const menu = (handle) => {
|
|
243
|
+
const sync = data.menu?.(handle) ?? null;
|
|
244
|
+
const raw = sync ?? cache.get(`menu:${handle}`, data.fetchMenu ? () => data.fetchMenu(handle) : undefined);
|
|
245
|
+
return map.toMenu(raw ?? null) ?? undefined;
|
|
246
|
+
};
|
|
247
|
+
const blog = (handle) => {
|
|
248
|
+
const raw = cache.get(`blog:${handle}`, data.blogByHandle ? () => data.blogByHandle(handle, { articles: 12 }) : undefined);
|
|
249
|
+
return map.toBlog(raw ?? null) ?? undefined;
|
|
250
|
+
};
|
|
251
|
+
// The landing page is in the bootstrap; any other is fetched (soft navigation).
|
|
252
|
+
const page = (handle) => map.toPage(data.pageByHandle?.(handle) ?? cache.get(`page:${handle}`, data.fetchPage ? () => data.fetchPage(handle) : undefined) ?? null) ?? undefined;
|
|
253
|
+
const currentProduct = route.template === 'product' && route.handle ? product(route.handle) : undefined;
|
|
254
|
+
// The platform's reviews of the route's product (storefront-query rev 2.38, 12F): what the product route's
|
|
255
|
+
// snapshot or `fetchProduct` answered; null on every other route — the section renders nothing there.
|
|
256
|
+
const currentReviews = route.template === 'product' && route.handle && currentProduct
|
|
257
|
+
? map.toReviews(cache.get(`product:${route.handle}`, undefined) ?? data.productByHandle?.(route.handle) ?? null)
|
|
258
|
+
: null;
|
|
259
|
+
const currentCollection = route.template === 'collection' && route.handle ? collection(route.handle) : undefined;
|
|
260
|
+
const productCollections = (route.template === 'product' && route.handle
|
|
261
|
+
? (cache.get(`product:${route.handle}`, undefined) ?? data.productByHandle?.(route.handle) ?? null)?.collections
|
|
262
|
+
: undefined) ?? [];
|
|
263
|
+
const currentProductCollection = productCollections.find((c) => c && c.handle && c.title) ?? null;
|
|
264
|
+
const currentPage = route.template === 'page' && route.handle ? page(route.handle) : undefined;
|
|
265
|
+
const currentBlog = route.blogHandle ? blog(route.blogHandle) : route.template === 'blog' && route.handle ? blog(route.handle) : undefined;
|
|
266
|
+
const currentArticle = route.template === 'article' && route.blogHandle && route.handle
|
|
267
|
+
? map.toArticle(cache.get(`article:${route.blogHandle}/${route.handle}`, data.articleByHandle ? () => data.articleByHandle(route.blogHandle, route.handle) : undefined) ?? null, route.blogHandle) ?? undefined
|
|
268
|
+
: undefined;
|
|
269
|
+
// O4: the same records the route's resources were built from, metafields included, for dynamic sources.
|
|
270
|
+
sources = {
|
|
271
|
+
shop: data.shop ?? null,
|
|
272
|
+
...(currentProduct && route.handle ? { product: (cache.get(`product:${route.handle}`, undefined) ?? data.productByHandle?.(route.handle) ?? null) } : {}),
|
|
273
|
+
...(currentCollection && route.handle ? { collection: (data.collectionByHandle?.(route.handle) ?? null) } : {}),
|
|
274
|
+
...(currentPage && route.handle
|
|
275
|
+
? { page: (data.pageByHandle?.(route.handle) ?? cache.get(`page:${route.handle}`, undefined) ?? null) }
|
|
276
|
+
: {}),
|
|
277
|
+
...(currentArticle && route.blogHandle && route.handle
|
|
278
|
+
? { article: (cache.get(`article:${route.blogHandle}/${route.handle}`, undefined) ?? null) }
|
|
279
|
+
: {}),
|
|
280
|
+
// A metaobject page: the route's entry, as the server loaded it (Theme ABI route kind `metaobject`)
|
|
281
|
+
// (only the entry of THIS path: a soft navigation to another entry never shows the previous one's fields)
|
|
282
|
+
...(route.resource === 'metaobject' && data.routeMetaobject && `${data.routeMetaobject.definitionHandle}/${data.routeMetaobject.handle}` === route.handle
|
|
283
|
+
? { metaobjectPage: data.routeMetaobject }
|
|
284
|
+
: {}),
|
|
285
|
+
};
|
|
286
|
+
const policies = map.toPolicies(data.shop);
|
|
287
|
+
if (privacyFetched && !policies[privacyFetched.handle])
|
|
288
|
+
policies[privacyFetched.handle] = privacyFetched;
|
|
289
|
+
const searchRaw = route.template === 'search' && query
|
|
290
|
+
? cache.get(searchSort ? `search:${query}|${searchSort}` : `search:${query}`, data.search ? () => data.search(query, { first: PRODUCTS_PER_REQUEST, ...(searchSort ? { sort: searchSort } : {}) }) : undefined)
|
|
291
|
+
: undefined;
|
|
292
|
+
const ctx = {
|
|
293
|
+
shop,
|
|
294
|
+
template: route.template,
|
|
295
|
+
menus: lookup(menu),
|
|
296
|
+
images: lookup((url) => imageFromUrl(url) ?? undefined),
|
|
297
|
+
videos: lookup((url) => videoFromUrl(url) ?? undefined),
|
|
298
|
+
cart: map.toCartSummary(cart),
|
|
299
|
+
localization: map.toLocalization(data.localization),
|
|
300
|
+
breadcrumbs: map.toBreadcrumbs({
|
|
301
|
+
pathname,
|
|
302
|
+
labels,
|
|
303
|
+
// On a product page: the first collection the platform names for the product (Theme ABI `Product.collections`).
|
|
304
|
+
collection: currentCollection ? { handle: route.handle ?? '', title: currentCollection.title } : (currentProductCollection ?? null),
|
|
305
|
+
product: currentProduct,
|
|
306
|
+
page: currentPage,
|
|
307
|
+
blog: currentBlog,
|
|
308
|
+
article: currentArticle,
|
|
309
|
+
policy: route.template === 'policy' && route.handle ? policies[route.handle] : null,
|
|
310
|
+
}),
|
|
311
|
+
consentEnabled: consent.enabled,
|
|
312
|
+
consentAnswered: consent.answered,
|
|
313
|
+
brandColor: str(settings.colorPrimary),
|
|
314
|
+
collections: lookup(collection),
|
|
315
|
+
currentCollection,
|
|
316
|
+
products: lookup(product),
|
|
317
|
+
currentProduct,
|
|
318
|
+
...(route.template === 'product' && search.get('variant') ? { currentVariantId: search.get('variant') } : {}),
|
|
319
|
+
pages: lookup(page),
|
|
320
|
+
// No shopper history or consent-backed tracking exists yet: the section waits for the platform.
|
|
321
|
+
recentlyViewed: null,
|
|
322
|
+
newestCollections: (data.allCollections?.() ?? []).slice(0, 8).map((c) => map.toCollection(c)),
|
|
323
|
+
currentPage,
|
|
324
|
+
currentArticle,
|
|
325
|
+
currentBlog: currentBlog ? { title: currentBlog.title, url: currentBlog.url } : undefined,
|
|
326
|
+
currentTag: route.tag,
|
|
327
|
+
blogs: lookup(blog),
|
|
328
|
+
reviews: currentReviews,
|
|
329
|
+
ugcFeed: null,
|
|
330
|
+
currentCart: map.toCart(cart),
|
|
331
|
+
checkoutUrl: cart.checkoutUrl ?? '/checkout',
|
|
332
|
+
cartLink: route.template === 'cart' ? search.get('cart_link') : null,
|
|
333
|
+
searchQuery: query,
|
|
334
|
+
searchResults: searchRaw ? map.toSearchResults(query, searchRaw) : null,
|
|
335
|
+
searchSortOptions: query ? SEARCH_SORT_IDS.map((id) => ({ id, label: (SEARCH_SORT_LABELS[(locale || 'en').split('-')[0]] ?? SEARCH_SORT_LABELS.en)[id] })) : [],
|
|
336
|
+
searchSort: searchSort || 'relevance',
|
|
337
|
+
account: null,
|
|
338
|
+
// The account slot's Account BFF URL (Theme ABI v2 §4), on the customer route only: `/account/orders` and
|
|
339
|
+
// `/account/addresses` open those pages of the account area, every other `/account*` path its profile.
|
|
340
|
+
...(data.accountUrl ? { accountUrl: data.accountUrl(accountPageOf(pathname)) } : {}),
|
|
341
|
+
// The platform's native form endpoint (ABI §1): a newsletter / contact form posts to its own page.
|
|
342
|
+
...(data.formEndpoint ? { newsletterEndpoint: data.formEndpoint, contactEndpoint: data.formEndpoint, formEndpoint: data.formEndpoint } : {}),
|
|
343
|
+
...(data.routeParams ? { routeParams: data.routeParams } : {}),
|
|
344
|
+
...(data.formResult ? { formResult: data.formResult } : {}),
|
|
345
|
+
policies,
|
|
346
|
+
currentPolicy: route.template === 'policy' && route.handle ? (policies[route.handle] ?? null) : null,
|
|
347
|
+
};
|
|
348
|
+
// Loaded only when a section reads them: every page would otherwise fetch
|
|
349
|
+
// the store's locations and the product's recommendations for nothing.
|
|
350
|
+
Object.defineProperties(ctx, {
|
|
351
|
+
recommendations: {
|
|
352
|
+
enumerable: true,
|
|
353
|
+
get: () => {
|
|
354
|
+
if (!currentProduct?.id)
|
|
355
|
+
return undefined;
|
|
356
|
+
const list = cache.get(`recommendations:${currentProduct.id}`, data.productRecommendations ? () => data.productRecommendations(currentProduct.id) : undefined);
|
|
357
|
+
return list ? { related: map.toProducts(list), complementary: [] } : undefined;
|
|
358
|
+
},
|
|
359
|
+
},
|
|
360
|
+
locations: {
|
|
361
|
+
enumerable: true,
|
|
362
|
+
get: () => map.toLocations(cache.get('locations', data.locations ? () => data.locations() : undefined) ?? null),
|
|
363
|
+
},
|
|
364
|
+
});
|
|
365
|
+
return ctx;
|
|
366
|
+
})();
|
|
367
|
+
const sourceFor = useMemo(() => (kind, handle) => ((kind === 'product' ? data.productByHandle?.(handle) : data.collectionByHandle?.(handle)) ?? null), [data]);
|
|
368
|
+
const value = useMemo(() => ({ ctx, resources: cache, bump, location, settings, sources, sourceFor }), [ctx, cache, bump, location, settings, sources, sourceFor]);
|
|
369
|
+
return (_jsx(StoreCtx.Provider, { value: value, children: _jsx(DynamicSourceProvider, { value: sources, children: children }) }));
|
|
370
|
+
}
|
|
371
|
+
function str(v) {
|
|
372
|
+
return typeof v === 'string' && v.trim() ? v.trim() : undefined;
|
|
373
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A handle read out of `location.pathname` is percent-encoded — the stored
|
|
3
|
+
* handle is not. A Thai or CJK handle ("เสื้อยืด") arrives as "%E0%B9%80…",
|
|
4
|
+
* matches nothing, and the route silently falls back to whatever record is
|
|
5
|
+
* first — the wrong product, or a blank page. Decode every handle taken from
|
|
6
|
+
* a URL.
|
|
7
|
+
*
|
|
8
|
+
* Malformed input (a stray `%` in a handle) keeps the raw segment rather than
|
|
9
|
+
* throwing URIError, which would take the whole render down.
|
|
10
|
+
*/
|
|
11
|
+
export declare function decodeHandle(raw: string): string;
|
|
12
|
+
export declare function decodeHandle(raw: string | undefined): string | undefined;
|
|
13
|
+
/** First capture of `re` against the current pathname, decoded. SSR-safe. */
|
|
14
|
+
export declare function handleFromPath(re: RegExp): string | undefined;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export function decodeHandle(raw) {
|
|
2
|
+
if (raw === undefined)
|
|
3
|
+
return undefined;
|
|
4
|
+
try {
|
|
5
|
+
return decodeURIComponent(raw);
|
|
6
|
+
}
|
|
7
|
+
catch {
|
|
8
|
+
return raw;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
/** First capture of `re` against the current pathname, decoded. SSR-safe. */
|
|
12
|
+
export function handleFromPath(re) {
|
|
13
|
+
if (typeof window === 'undefined')
|
|
14
|
+
return undefined;
|
|
15
|
+
return decodeHandle(window.location.pathname.match(re)?.[1]);
|
|
16
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/** The resource a URL addresses, if it addresses one. */
|
|
2
|
+
export interface RouteMatch {
|
|
3
|
+
/** Base template name — a file under `templates/<name>.json`. */
|
|
4
|
+
template: string;
|
|
5
|
+
/** Resource kind, for the record types that carry a `templateSuffix`. */
|
|
6
|
+
resource?: 'product' | 'collection' | 'page' | 'blog' | 'article' | 'policy' | 'metaobject';
|
|
7
|
+
/** Decoded handle of the addressed resource. */
|
|
8
|
+
handle?: string;
|
|
9
|
+
/** Blog handle, for article routes (`/blogs/<blog>/<article>`). */
|
|
10
|
+
blogHandle?: string;
|
|
11
|
+
/** Decoded tag, for a blog filtered by tag (`/blogs/<blog>/tagged/<tag>`). */
|
|
12
|
+
tag?: string;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Match a URL pathname to a template + the resource it addresses.
|
|
16
|
+
*
|
|
17
|
+
* Order matters: `/blogs/<blog>/<article>` must be tested BEFORE
|
|
18
|
+
* `/blogs/<handle>` so the blog list doesn't shadow article detail.
|
|
19
|
+
*/
|
|
20
|
+
export declare function matchRoute(pathname: string): RouteMatch;
|
|
21
|
+
/** Base template name for a pathname. */
|
|
22
|
+
export declare function resolveTemplate(pathname: string): string;
|
|
23
|
+
/** The handle a pathname addresses for a given resource kind, else undefined. */
|
|
24
|
+
export declare function routeHandle(pathname: string, resource: NonNullable<RouteMatch['resource']>): string | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* The detail-route handle variables the bootstrap query should prefetch, so a
|
|
27
|
+
* record's `templateSuffix` is known before the template is chosen.
|
|
28
|
+
*/
|
|
29
|
+
export declare function detailHandles(pathname: string): {
|
|
30
|
+
pageHandle?: string;
|
|
31
|
+
productHandle?: string;
|
|
32
|
+
collectionHandle?: string;
|
|
33
|
+
policyBodies?: boolean;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* `<base>.<suffix>` when the theme ships that template file, else `<base>`.
|
|
37
|
+
*
|
|
38
|
+
* `templateExists` is injected rather than globbed here so the entry and the
|
|
39
|
+
* layout can each pass the glob they already hold, and so this module stays
|
|
40
|
+
* testable without Vite.
|
|
41
|
+
*/
|
|
42
|
+
export declare function variantOf(base: string, suffix: string | null | undefined, templateExists: (name: string) => boolean): string;
|
|
43
|
+
/** Minimal shape of the data layer this module needs to read a templateSuffix. */
|
|
44
|
+
export interface TemplateSuffixSource {
|
|
45
|
+
productByHandle: (handle: string) => {
|
|
46
|
+
templateSuffix?: string | null;
|
|
47
|
+
} | null;
|
|
48
|
+
collectionByHandle: (handle: string) => {
|
|
49
|
+
templateSuffix?: string | null;
|
|
50
|
+
} | null;
|
|
51
|
+
pageByHandle: (handle: string) => {
|
|
52
|
+
templateSuffix?: string | null;
|
|
53
|
+
} | null;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The template to actually render for a pathname, including the merchant's
|
|
57
|
+
* per-resource template assignment (`templateSuffix`).
|
|
58
|
+
*
|
|
59
|
+
* This is the function the SPA router was missing: soft navigation looked up
|
|
60
|
+
* the BASE template only, so a product assigned `product.bundle` rendered
|
|
61
|
+
* `product` once the shopper arrived by clicking rather than by URL.
|
|
62
|
+
*
|
|
63
|
+
* Blog and article suffixes are resolved asynchronously by the caller (they are
|
|
64
|
+
* not in the sync bootstrap), so they are left at their base here.
|
|
65
|
+
*/
|
|
66
|
+
export declare function resolvePageTemplate(pathname: string, data: TemplateSuffixSource, templateExists: (name: string) => boolean): string;
|
|
67
|
+
/** The async lookups a blog or article template variant needs. */
|
|
68
|
+
export interface AsyncSuffixSource {
|
|
69
|
+
blogByHandle?: (handle: string, opts?: {
|
|
70
|
+
articles?: number;
|
|
71
|
+
}) => Promise<{
|
|
72
|
+
templateSuffix?: string | null;
|
|
73
|
+
} | null | undefined>;
|
|
74
|
+
articleByHandle?: (blogHandle: string, articleHandle: string) => Promise<{
|
|
75
|
+
templateSuffix?: string | null;
|
|
76
|
+
} | null | undefined>;
|
|
77
|
+
fetchPage?: (handle: string) => Promise<{
|
|
78
|
+
templateSuffix?: string | null;
|
|
79
|
+
} | null | undefined>;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Blog and article records — and any page but the landing one — are not in the sync bootstrap, so their
|
|
83
|
+
* `templateSuffix` (`blog.featured`, `article.longform`) is looked up here —
|
|
84
|
+
* by the server before it renders, and by the SPA router before it swaps the
|
|
85
|
+
* tree. Any other route keeps the template it already resolved.
|
|
86
|
+
*/
|
|
87
|
+
export declare function resolveAsyncVariant(route: RouteMatch, resolved: string, data: AsyncSuffixSource, templateExists: (name: string) => boolean): Promise<string>;
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The theme's ONE route table.
|
|
3
|
+
*
|
|
4
|
+
* `resolveTemplate` used to exist twice, byte-for-byte, in `browser-entry.tsx` and
|
|
5
|
+
* `layouts/layout.tsx`, each with a comment asking the next person to keep them
|
|
6
|
+
* in sync. They drive different things — the entry picks the template to mount,
|
|
7
|
+
* the layout picks the template to swap in on a soft navigation — so a route
|
|
8
|
+
* added to one and not the other renders a 404 on exactly one of the two paths.
|
|
9
|
+
*
|
|
10
|
+
* Route matching lives here. Anything that needs "what does this URL mean" reads
|
|
11
|
+
* it from this module.
|
|
12
|
+
*/
|
|
13
|
+
import { decodeHandle } from './handle.js';
|
|
14
|
+
/** Strip the trailing slash(es) from a pathname, keeping "/" itself. */
|
|
15
|
+
function normalize(pathname) {
|
|
16
|
+
return pathname !== '/' ? pathname.replace(/\/+$/, '') || '/' : '/';
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Match a URL pathname to a template + the resource it addresses.
|
|
20
|
+
*
|
|
21
|
+
* Order matters: `/blogs/<blog>/<article>` must be tested BEFORE
|
|
22
|
+
* `/blogs/<handle>` so the blog list doesn't shadow article detail.
|
|
23
|
+
*/
|
|
24
|
+
export function matchRoute(pathname) {
|
|
25
|
+
const p = normalize(pathname);
|
|
26
|
+
if (p === '/')
|
|
27
|
+
return { template: 'index' };
|
|
28
|
+
if (p === '/cart')
|
|
29
|
+
return { template: 'cart' };
|
|
30
|
+
if (p === '/search')
|
|
31
|
+
return { template: 'search' };
|
|
32
|
+
if (p === '/contact')
|
|
33
|
+
return { template: 'contact' };
|
|
34
|
+
if (p === '/404')
|
|
35
|
+
return { template: '404' };
|
|
36
|
+
if (p === '/collections')
|
|
37
|
+
return { template: 'list-collections' };
|
|
38
|
+
let m;
|
|
39
|
+
if ((m = p.match(/^\/collections\/([^/]+)$/)))
|
|
40
|
+
return { template: 'collection', resource: 'collection', handle: decodeHandle(m[1]) };
|
|
41
|
+
if ((m = p.match(/^\/products\/([^/]+)$/)))
|
|
42
|
+
return { template: 'product', resource: 'product', handle: decodeHandle(m[1]) };
|
|
43
|
+
if ((m = p.match(/^\/pages\/([^/]+)$/)))
|
|
44
|
+
return { template: 'page', resource: 'page', handle: decodeHandle(m[1]) };
|
|
45
|
+
// A metaobject entry's web page (Theme ABI route kind `metaobject`): the handle is `<definition>/<entry>`, as the
|
|
46
|
+
// platform's `route.handle`. Its template is the platform's choice (the entry's type), never resolved here.
|
|
47
|
+
if ((m = p.match(/^\/pages\/([^/]+)\/([^/]+)$/)))
|
|
48
|
+
return { template: 'metaobject', resource: 'metaobject', handle: `${decodeHandle(m[1])}/${decodeHandle(m[2])}` };
|
|
49
|
+
// Shop policies (Settings) — a menu item of type "Policy" links to
|
|
50
|
+
// /policies/<handle> (privacy-policy, refund-policy, …).
|
|
51
|
+
if ((m = p.match(/^\/policies\/([^/]+)$/)))
|
|
52
|
+
return { template: 'policy', resource: 'policy', handle: decodeHandle(m[1]) };
|
|
53
|
+
// Customer account — /account and its sub-routes (login, orders, addresses).
|
|
54
|
+
if (p === '/account' || /^\/account\/[^/]+/.test(p))
|
|
55
|
+
return { template: 'account' };
|
|
56
|
+
// A blog filtered to one tag — the blog's own template, narrowed.
|
|
57
|
+
if ((m = p.match(/^\/blogs\/([^/]+)\/tagged\/([^/]+)$/)))
|
|
58
|
+
return { template: 'blog', resource: 'blog', handle: decodeHandle(m[1]), tag: decodeHandle(m[2]) };
|
|
59
|
+
if ((m = p.match(/^\/blogs\/([^/]+)\/([^/]+)$/)))
|
|
60
|
+
return {
|
|
61
|
+
template: 'article',
|
|
62
|
+
resource: 'article',
|
|
63
|
+
blogHandle: decodeHandle(m[1]),
|
|
64
|
+
handle: decodeHandle(m[2]),
|
|
65
|
+
};
|
|
66
|
+
if ((m = p.match(/^\/blogs\/([^/]+)$/)))
|
|
67
|
+
return { template: 'blog', resource: 'blog', handle: decodeHandle(m[1]) };
|
|
68
|
+
return { template: '404' };
|
|
69
|
+
}
|
|
70
|
+
/** Base template name for a pathname. */
|
|
71
|
+
export function resolveTemplate(pathname) {
|
|
72
|
+
return matchRoute(pathname).template;
|
|
73
|
+
}
|
|
74
|
+
/** The handle a pathname addresses for a given resource kind, else undefined. */
|
|
75
|
+
export function routeHandle(pathname, resource) {
|
|
76
|
+
const match = matchRoute(pathname);
|
|
77
|
+
return match.resource === resource ? match.handle : undefined;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* The detail-route handle variables the bootstrap query should prefetch, so a
|
|
81
|
+
* record's `templateSuffix` is known before the template is chosen.
|
|
82
|
+
*/
|
|
83
|
+
export function detailHandles(pathname) {
|
|
84
|
+
const m = matchRoute(pathname);
|
|
85
|
+
if (m.resource === 'policy')
|
|
86
|
+
return { policyBodies: true };
|
|
87
|
+
if (m.resource === 'page' && m.handle)
|
|
88
|
+
return { pageHandle: m.handle };
|
|
89
|
+
if (m.resource === 'product' && m.handle)
|
|
90
|
+
return { productHandle: m.handle };
|
|
91
|
+
if (m.resource === 'collection' && m.handle)
|
|
92
|
+
return { collectionHandle: m.handle };
|
|
93
|
+
return {};
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* `<base>.<suffix>` when the theme ships that template file, else `<base>`.
|
|
97
|
+
*
|
|
98
|
+
* `templateExists` is injected rather than globbed here so the entry and the
|
|
99
|
+
* layout can each pass the glob they already hold, and so this module stays
|
|
100
|
+
* testable without Vite.
|
|
101
|
+
*/
|
|
102
|
+
export function variantOf(base, suffix, templateExists) {
|
|
103
|
+
const candidate = suffix ? `${base}.${suffix}` : base;
|
|
104
|
+
return templateExists(candidate) ? candidate : base;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* The template to actually render for a pathname, including the merchant's
|
|
108
|
+
* per-resource template assignment (`templateSuffix`).
|
|
109
|
+
*
|
|
110
|
+
* This is the function the SPA router was missing: soft navigation looked up
|
|
111
|
+
* the BASE template only, so a product assigned `product.bundle` rendered
|
|
112
|
+
* `product` once the shopper arrived by clicking rather than by URL.
|
|
113
|
+
*
|
|
114
|
+
* Blog and article suffixes are resolved asynchronously by the caller (they are
|
|
115
|
+
* not in the sync bootstrap), so they are left at their base here.
|
|
116
|
+
*/
|
|
117
|
+
export function resolvePageTemplate(pathname, data, templateExists) {
|
|
118
|
+
const m = matchRoute(pathname);
|
|
119
|
+
if (!m.resource || !m.handle)
|
|
120
|
+
return m.template;
|
|
121
|
+
let suffix;
|
|
122
|
+
if (m.resource === 'product')
|
|
123
|
+
suffix = data.productByHandle(m.handle)?.templateSuffix;
|
|
124
|
+
else if (m.resource === 'collection')
|
|
125
|
+
suffix = data.collectionByHandle(m.handle)?.templateSuffix;
|
|
126
|
+
else if (m.resource === 'page')
|
|
127
|
+
suffix = data.pageByHandle(m.handle)?.templateSuffix;
|
|
128
|
+
else
|
|
129
|
+
return m.template;
|
|
130
|
+
return variantOf(m.template, suffix, templateExists);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Blog and article records — and any page but the landing one — are not in the sync bootstrap, so their
|
|
134
|
+
* `templateSuffix` (`blog.featured`, `article.longform`) is looked up here —
|
|
135
|
+
* by the server before it renders, and by the SPA router before it swaps the
|
|
136
|
+
* tree. Any other route keeps the template it already resolved.
|
|
137
|
+
*/
|
|
138
|
+
export async function resolveAsyncVariant(route, resolved, data, templateExists) {
|
|
139
|
+
try {
|
|
140
|
+
if (route.resource === 'article' && route.blogHandle && route.handle && data.articleByHandle) {
|
|
141
|
+
const a = await data.articleByHandle(route.blogHandle, route.handle);
|
|
142
|
+
return variantOf('article', a?.templateSuffix, templateExists);
|
|
143
|
+
}
|
|
144
|
+
if (route.resource === 'page' && route.handle && data.fetchPage) {
|
|
145
|
+
const p = await data.fetchPage(route.handle);
|
|
146
|
+
return variantOf('page', p?.templateSuffix, templateExists);
|
|
147
|
+
}
|
|
148
|
+
if (route.resource === 'blog' && route.handle && data.blogByHandle) {
|
|
149
|
+
// The same request the blog's sections make (12 posts), so it is answered once.
|
|
150
|
+
const b = await data.blogByHandle(route.handle, { articles: 12 });
|
|
151
|
+
return variantOf('blog', b?.templateSuffix, templateExists);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
/* an unreachable record keeps its base template */
|
|
156
|
+
}
|
|
157
|
+
return resolved;
|
|
158
|
+
}
|