@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,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The data Nova components receive. Components are presentational: they never
|
|
3
|
+
* fetch. A storefront adapter maps Tanqory API payloads onto these shapes; the
|
|
4
|
+
* lab maps fixtures onto them. Optional means "a real store can lack it" —
|
|
5
|
+
* every component must render (or deliberately render nothing) without it.
|
|
6
|
+
*/
|
|
7
|
+
export const SOCIAL_NETWORKS = ['instagram', 'facebook', 'x', 'tiktok', 'youtube'];
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import type { GiftCardBalance } from './sdk/types.js';
|
|
2
|
+
import type { RenderContext, ThemeManifest } from '@tanqory/theme-abi';
|
|
3
|
+
import { type ThemeBundle } from './runtime/slots.js';
|
|
4
|
+
import type { ContentNode } from './runtime/kit/types.js';
|
|
5
|
+
import type { Shop } from './runtime/kit/storefront.js';
|
|
6
|
+
import type { PagePlanOptions } from './abi/page-data.js';
|
|
7
|
+
type Settings = Record<string, unknown>;
|
|
8
|
+
export interface RenderOptions {
|
|
9
|
+
/** The theme's ABI manifest — the same object the entry exports as `manifest`. */
|
|
10
|
+
manifest: ThemeManifest;
|
|
11
|
+
/** The theme: its definition, starter content and locales (store-independent, module scope). */
|
|
12
|
+
bundle: ThemeBundle;
|
|
13
|
+
/**
|
|
14
|
+
* The global settings the theme renders with, from the manifest defaults merged with the store's
|
|
15
|
+
* saved values. Runs before anything else; the result is also what the browser hydrates with.
|
|
16
|
+
* Default: unchanged.
|
|
17
|
+
*/
|
|
18
|
+
effectiveSettings?(settings: Settings): Settings;
|
|
19
|
+
/**
|
|
20
|
+
* The page's section nodes as they render under these settings (e.g. a global setting filling a
|
|
21
|
+
* section's own empty one). Pure; runs on the server and again in the browser (`hydrate`), so both
|
|
22
|
+
* build the same tree. Default: unchanged.
|
|
23
|
+
*/
|
|
24
|
+
transformNodes?(nodes: ContentNode[], settings: Settings): ContentNode[];
|
|
25
|
+
/**
|
|
26
|
+
* The settings once the store's shop record is loaded (e.g. the shop's brand colour as the theme
|
|
27
|
+
* colour's fallback). Default: unchanged.
|
|
28
|
+
*/
|
|
29
|
+
shopSettings?(settings: Settings, shop: Shop): Settings;
|
|
30
|
+
/**
|
|
31
|
+
* `<link rel="alternate" hreflang>` for every language the store publishes (`<canonical>?locale=<language>`),
|
|
32
|
+
* plus `x-default` (the canonical URL), on a 200 page of a store with two or more languages (abi/seo.ts
|
|
33
|
+
* `hreflangAlternates`). Default true (2.0.0-alpha.2); `false` leaves them out.
|
|
34
|
+
*/
|
|
35
|
+
hreflang?: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* NEW-PROD-2 (storefront-query rev 2.52): the CSP style hashes of the 3D viewer the theme ships (e.g. the
|
|
38
|
+
* `<style>` its custom element puts in its shadow root, which no theme `<style>` hash can cover). Announced
|
|
39
|
+
* (`x-tq-csp-style-hash`) only on a page whose product has a 3D model. Default: none.
|
|
40
|
+
*/
|
|
41
|
+
modelViewerStyleHashes?: readonly string[];
|
|
42
|
+
/**
|
|
43
|
+
* The storefront extras a theme written against `@tanqory/theme-kit/headless` relies on. Absent (Nova, and every
|
|
44
|
+
* theme built on 2.0.0-alpha.0): the render is exactly the earlier one. Present (`storefront: {}` is enough),
|
|
45
|
+
* every member defaults to on unless it says otherwise.
|
|
46
|
+
*/
|
|
47
|
+
storefront?: StorefrontOptions;
|
|
48
|
+
}
|
|
49
|
+
/** `RenderOptions.storefront`. */
|
|
50
|
+
export interface StorefrontOptions {
|
|
51
|
+
/** BreadcrumbList JSON-LD on product, collection, page and article pages, and CollectionPage JSON-LD on a collection. Default true. */
|
|
52
|
+
structuredData?: boolean;
|
|
53
|
+
/**
|
|
54
|
+
* The merchant's dashboard wording for the theme's strings (`ctx.data.localization.strings`), used by `t()`
|
|
55
|
+
* from the headless API. One data call, made beside the page's first round. Default true.
|
|
56
|
+
*/
|
|
57
|
+
merchantStrings?: boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Data the page loads whatever its sections are called. Default: none of them, so a page that shows no market
|
|
60
|
+
* selector does not vary on the shopper's country.
|
|
61
|
+
*/
|
|
62
|
+
plan?: Omit<PagePlanOptions, 'reservedCalls'>;
|
|
63
|
+
}
|
|
64
|
+
export type RenderFunction = (request: Request, ctx: RenderContext) => Promise<Response>;
|
|
65
|
+
/**
|
|
66
|
+
* NEW-OS-11 "Instant page transitions": under ABI v2 every navigation is a full page load (abi/shell.tsx),
|
|
67
|
+
* so the theme setting `enableSpaNavigation` turns on CROSS-DOCUMENT view transitions instead: the browser
|
|
68
|
+
* cross-fades the next server-rendered page in place of a white flash (Chrome/Edge 126+, Safari 18.2+;
|
|
69
|
+
* others load normally). Live storefront only, never the editor canvas, and none for reduced motion.
|
|
70
|
+
* Inline <style>, so it is announced by hash like every other block (no CSP change).
|
|
71
|
+
*/
|
|
72
|
+
export declare const VIEW_TRANSITIONS_ID = "tq-view-transitions";
|
|
73
|
+
export declare const VIEW_TRANSITIONS_CSS: string;
|
|
74
|
+
/**
|
|
75
|
+
* The gift card balance view the `gift-card-balance` form answers (NEW-PROD-12), member by member, or null when the
|
|
76
|
+
* answer is not that shape: the theme never sees a code, a buyer or anything the lookup does not promise.
|
|
77
|
+
*/
|
|
78
|
+
export declare function giftCardBalanceOf(v: unknown): GiftCardBalance | null;
|
|
79
|
+
/**
|
|
80
|
+
* The ABI `render(request, ctx)` for one theme. Call it once at module scope: it hands the theme
|
|
81
|
+
* bundle to the runtime and registers the theme's sections (store-independent state only).
|
|
82
|
+
*/
|
|
83
|
+
export declare function createRender(options: RenderOptions): RenderFunction;
|
|
84
|
+
/**
|
|
85
|
+
* The live-only hints the storefront gateway acts on (storefront-edge core/platform-hints.ts); it drops every `x-tq-*`
|
|
86
|
+
* before the page leaves, so a browser never sees them.
|
|
87
|
+
* x-tq-speculation: on NEW-OS-11 part 2 — `enableSpaNavigation` is not off: the gateway adds its
|
|
88
|
+
* `Speculation-Rules` header, so hovering a link prerenders the next page.
|
|
89
|
+
* x-tq-turnstile: on A4 — the store's spam protection asks for the Turnstile widget on a form this theme can show
|
|
90
|
+
* (`shop.spamProtection`, either switch): the gateway allows Cloudflare Turnstile in the CSP.
|
|
91
|
+
* x-tq-media: video, model NEW-PROD-2 — a product on the page has a YouTube / Vimeo video (the gateway frames
|
|
92
|
+
* `www.youtube-nocookie.com` and `player.vimeo.com`) and/or a 3D model (`connect-src blob:`).
|
|
93
|
+
* Never in the editor canvas (a preview host ignores them too).
|
|
94
|
+
*/
|
|
95
|
+
export declare function platformHints(preview: boolean, settings: Record<string, unknown>, shop: Pick<Shop, 'spamProtection'> | null | undefined, products?: readonly {
|
|
96
|
+
media?: readonly {
|
|
97
|
+
type?: string;
|
|
98
|
+
}[] | null;
|
|
99
|
+
}[]): Record<string, string>;
|
|
100
|
+
/** Which kinds of third-party / 3D media the page's products carry (kit `ProductMedia.type`). */
|
|
101
|
+
export declare function pageMedia(products: readonly {
|
|
102
|
+
media?: readonly {
|
|
103
|
+
type?: string;
|
|
104
|
+
}[] | null;
|
|
105
|
+
}[]): {
|
|
106
|
+
video: boolean;
|
|
107
|
+
model: boolean;
|
|
108
|
+
};
|
|
109
|
+
export type { ThemeBundle } from './runtime/slots.js';
|
|
110
|
+
export type { ContentNode } from './runtime/kit/types.js';
|
|
111
|
+
export type { PageData, NativeFormResult } from './abi/page-data.js';
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,533 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* `@tanqory/theme-kit/server` — the Theme ABI v2 serve entry (theme-abi-v2.md §1) as a factory.
|
|
4
|
+
*
|
|
5
|
+
* // entry.abi.tsx
|
|
6
|
+
* export const manifest = manifestJson as unknown as ThemeManifest
|
|
7
|
+
* export const render = createRender({ manifest, bundle })
|
|
8
|
+
*
|
|
9
|
+
* The theme's sections render through the kit runtime: StoreProvider → SectionRenderer → each
|
|
10
|
+
* section's `data()` binding. What the ABI decides is only where data and the page come from:
|
|
11
|
+
*
|
|
12
|
+
* - routing: the template and resource come from `ctx.route`, never from the URL;
|
|
13
|
+
* - data: every read is `ctx.data`, planned in abi/page-data.ts (≤ 4 calls, ≤ 2 rounds);
|
|
14
|
+
* - headers: only `content-type`, the style hashes, `ctx.cache.hint()` and two live-only platform hints the
|
|
15
|
+
* gateway acts on (`x-tq-speculation: on` ⇒ prerender rules, `x-tq-turnstile: on` ⇒ Turnstile in the CSP);
|
|
16
|
+
* - the clock: `ctx.now()`;
|
|
17
|
+
* - tenancy: nothing derived from `ctx` outlives the render (module scope holds only the theme).
|
|
18
|
+
*
|
|
19
|
+
* The built entry is ONE file with no imports: the theme's bundler inlines this module and its
|
|
20
|
+
* dependencies (react-dom/server included).
|
|
21
|
+
*/
|
|
22
|
+
import { labelsOf } from './abi/labels.js';
|
|
23
|
+
import { packPageData } from './abi/state-pack.js';
|
|
24
|
+
import { renderToString } from 'react-dom/server';
|
|
25
|
+
import { setThemeBundle } from './runtime/slots.js';
|
|
26
|
+
import { sectionModules } from './runtime/entries/sections.js';
|
|
27
|
+
import { registerSections } from './runtime/kit/registry.js';
|
|
28
|
+
import { SectionTree } from './runtime/kit/SectionTree.js';
|
|
29
|
+
import { DataProvider } from './runtime/kit/data.js';
|
|
30
|
+
import { ThemeProvider } from './runtime/kit/theme-context.js';
|
|
31
|
+
import { PreviewModeProvider } from './runtime/shell/preview-mode.js';
|
|
32
|
+
import { computeHead, documentTitle, headFrom, routeTitleOf, shopNameOf } from './runtime/seo/head.js';
|
|
33
|
+
import { articleJsonLd, homeJsonLd, jsonLdTag } from './runtime/seo/structured-data.js';
|
|
34
|
+
import { localeStrings, themeLocaleOf } from './runtime/platform/locale.js';
|
|
35
|
+
import { AbiShell } from './abi/shell.js';
|
|
36
|
+
import { homeHead, hreflangAlternates } from './abi/seo.js';
|
|
37
|
+
import { AbiCartProvider } from './abi/cart.js';
|
|
38
|
+
import { listingQueryOfRequest, loadPageData, pageParamsOf, searchQueryOf, searchSortOf } from './abi/page-data.js';
|
|
39
|
+
import { createDataApi } from './abi/data-api.js';
|
|
40
|
+
import { collectStyles, STYLE_RULES_ID } from './sdk/sx.js';
|
|
41
|
+
import { ServerRenderProvider } from './sdk/server-render.js';
|
|
42
|
+
import { locationOf, nodesOf, notFoundTree } from './abi/page.js';
|
|
43
|
+
import { HeadSinkContext, PageInfoContext } from './headless/page.js';
|
|
44
|
+
import { absoluteOn, breadcrumbJsonLd, collectionJsonLd } from './headless/structured-data.js';
|
|
45
|
+
const RTL = new Set(['ar', 'he', 'fa', 'ur', 'yi', 'ps', 'dv', 'ckb']);
|
|
46
|
+
const esc = (v) => String(v ?? '').replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
47
|
+
/** JSON for a non-executable `<script type="application/json">` data block. */
|
|
48
|
+
const LINE_SEP = String.fromCharCode(0x2028);
|
|
49
|
+
const PARA_SEP = String.fromCharCode(0x2029);
|
|
50
|
+
const jsonBlock = (v) => JSON.stringify(v).replace(/</g, '\\u003c').split(LINE_SEP).join('\\u2028').split(PARA_SEP).join('\\u2029');
|
|
51
|
+
/** Cache tags: ≤ 8, ^[a-z0-9:_-]{1,64}$ (CacheHint). */
|
|
52
|
+
function tagsFor(ctx) {
|
|
53
|
+
const ok = (t) => /^[a-z0-9:_-]{1,64}$/.test(t);
|
|
54
|
+
const tags = [`kind:${ctx.route.kind.replace(/[^a-z0-9_-]/g, '-')}`];
|
|
55
|
+
if (ctx.route.handle && ok(`${ctx.route.kind}:${ctx.route.handle}`))
|
|
56
|
+
tags.push(`${ctx.route.kind}:${ctx.route.handle}`);
|
|
57
|
+
return tags.filter(ok).slice(0, 8);
|
|
58
|
+
}
|
|
59
|
+
/** Script/style tags for this artifact's client bundle — every URL under assets.base with SRI (§2.2). */
|
|
60
|
+
function assetTags(ctx) {
|
|
61
|
+
const base = ctx.assets.base;
|
|
62
|
+
const entries = Object.values(ctx.assets.manifest);
|
|
63
|
+
const byFile = new Map(entries.map((e) => [e.file, e]));
|
|
64
|
+
const entry = entries.find((e) => e.isEntry && e.kind === 'script');
|
|
65
|
+
if (!entry)
|
|
66
|
+
return '';
|
|
67
|
+
const link = (file, rel) => {
|
|
68
|
+
const e = byFile.get(file);
|
|
69
|
+
return e ? `<link rel="${rel}" href="${esc(`${base}/${e.file}`)}" integrity="${esc(e.integrity)}" crossorigin="anonymous">` : '';
|
|
70
|
+
};
|
|
71
|
+
return [
|
|
72
|
+
...(entry.css ?? []).map((f) => link(f, 'stylesheet')),
|
|
73
|
+
...(entry.imports ?? []).map((f) => link(f, 'modulepreload')),
|
|
74
|
+
`<script type="module" src="${esc(`${base}/${entry.file}`)}" integrity="${esc(entry.integrity)}" crossorigin="anonymous"></script>`,
|
|
75
|
+
].join('');
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* NEW-OS-11 "Instant page transitions": under ABI v2 every navigation is a full page load (abi/shell.tsx),
|
|
79
|
+
* so the theme setting `enableSpaNavigation` turns on CROSS-DOCUMENT view transitions instead: the browser
|
|
80
|
+
* cross-fades the next server-rendered page in place of a white flash (Chrome/Edge 126+, Safari 18.2+;
|
|
81
|
+
* others load normally). Live storefront only, never the editor canvas, and none for reduced motion.
|
|
82
|
+
* Inline <style>, so it is announced by hash like every other block (no CSP change).
|
|
83
|
+
*/
|
|
84
|
+
export const VIEW_TRANSITIONS_ID = 'tq-view-transitions';
|
|
85
|
+
export const VIEW_TRANSITIONS_CSS = '@view-transition{navigation:auto}' +
|
|
86
|
+
'@media (prefers-reduced-motion:reduce){::view-transition-group(*),::view-transition-old(*),::view-transition-new(*){animation:none!important}}';
|
|
87
|
+
/** `sha256-<base64>` of the text of every inline <style> element, in document order, deduplicated. */
|
|
88
|
+
async function styleHashes(html) {
|
|
89
|
+
const out = [];
|
|
90
|
+
for (const m of html.matchAll(/<style\b[^>]*>([\s\S]*?)<\/style>/g)) {
|
|
91
|
+
const digest = new Uint8Array(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(m[1])));
|
|
92
|
+
let bin = '';
|
|
93
|
+
for (const b of digest)
|
|
94
|
+
bin += String.fromCharCode(b);
|
|
95
|
+
const h = `sha256-${btoa(bin)}`;
|
|
96
|
+
if (!out.includes(h))
|
|
97
|
+
out.push(h);
|
|
98
|
+
}
|
|
99
|
+
return out;
|
|
100
|
+
}
|
|
101
|
+
/** A discount code as the edge passes it on: printable, no space, no markup character, at most 64. */
|
|
102
|
+
const DISCOUNT_CODE = /^[^\s<>"'&\u0000-\u001f\u007f]{1,64}$/;
|
|
103
|
+
const MONEY_AMOUNT = /^(0|[1-9][0-9]{0,14})(\.[0-9]{1,4})?$/;
|
|
104
|
+
const CURRENCY = /^[A-Z]{3}$/;
|
|
105
|
+
const GIFT_CARD_STATUS = new Set(['ACTIVE', 'PARTIAL', 'EMPTY', 'EXPIRED', 'DEACTIVATED']);
|
|
106
|
+
const RFC3339 = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,3})?Z$/;
|
|
107
|
+
const isRecord = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
108
|
+
const moneyOf = (v) => isRecord(v) && typeof v['amount'] === 'string' && MONEY_AMOUNT.test(v['amount']) && typeof v['currencyCode'] === 'string' && CURRENCY.test(v['currencyCode']) ? { amount: v['amount'], currencyCode: v['currencyCode'] } : null;
|
|
109
|
+
/**
|
|
110
|
+
* The gift card balance view the `gift-card-balance` form answers (NEW-PROD-12), member by member, or null when the
|
|
111
|
+
* answer is not that shape: the theme never sees a code, a buyer or anything the lookup does not promise.
|
|
112
|
+
*/
|
|
113
|
+
export function giftCardBalanceOf(v) {
|
|
114
|
+
if (!isRecord(v))
|
|
115
|
+
return null;
|
|
116
|
+
const last = v['lastCharacters'];
|
|
117
|
+
const balance = moneyOf(v['balance']);
|
|
118
|
+
const initialValue = moneyOf(v['initialValue']);
|
|
119
|
+
const status = v['status'];
|
|
120
|
+
const expiresOn = v['expiresOn'];
|
|
121
|
+
if (typeof last !== 'string' || !/^[A-Za-z0-9]{1,8}$/.test(last) || !balance || !initialValue)
|
|
122
|
+
return null;
|
|
123
|
+
if (typeof v['enabled'] !== 'boolean' || typeof status !== 'string' || !GIFT_CARD_STATUS.has(status))
|
|
124
|
+
return null;
|
|
125
|
+
if (expiresOn !== null && (typeof expiresOn !== 'string' || !RFC3339.test(expiresOn)))
|
|
126
|
+
return null;
|
|
127
|
+
return { lastCharacters: last, balance, initialValue, enabled: v['enabled'], status: status, expiresOn };
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* The render of a declared form's POST (theme-abi §1 rows 2, 6, 8): a verified post is submitted through
|
|
131
|
+
* `ctx.actions.form.submit` — the platform routes it (newsletter → the store's subscriber list, contact → the
|
|
132
|
+
* merchant) — and the page renders its outcome. A post that failed the origin or CSRF check never ran (`expired`:
|
|
133
|
+
* the shopper reloads and submits again). Null on every GET.
|
|
134
|
+
*/
|
|
135
|
+
async function nativeFormResult(ctx) {
|
|
136
|
+
const req = ctx.request;
|
|
137
|
+
// Post/Redirect/Get (storefront-edge core/form-flash.ts): the GET after an accepted POST carries what the POST came
|
|
138
|
+
// to, so the page says the same thanks it said before, and a reload sends nothing again.
|
|
139
|
+
const flash = req.formFlash;
|
|
140
|
+
if (flash && typeof flash.formId === 'string' && /^[a-z][a-z0-9-]{0,39}$/.test(flash.formId) && flash.status === 'ok')
|
|
141
|
+
return { formId: flash.formId, status: 'ok' };
|
|
142
|
+
if (req.formError)
|
|
143
|
+
return { formId: '*', status: 'expired' };
|
|
144
|
+
if (!req.formId || !req.form)
|
|
145
|
+
return null;
|
|
146
|
+
const formId = req.formId;
|
|
147
|
+
const email = typeof req.form.email === 'string' && req.form.email ? req.form.email.slice(0, 320) : undefined;
|
|
148
|
+
const failed = (status) => ({ formId, status, ...(email ? { email } : {}) });
|
|
149
|
+
const actions = ctx.actions;
|
|
150
|
+
if (!actions?.form)
|
|
151
|
+
return failed('unavailable');
|
|
152
|
+
try {
|
|
153
|
+
const r = await actions.form.submit(formId, req.form);
|
|
154
|
+
// NEW-MKT-4: a pop-up sign-up's code, for this shopper only (not in the ABI result type yet: read as unknown)
|
|
155
|
+
if (r.ok) {
|
|
156
|
+
// NEW-PROD-12: the gift card balance view the platform answered to this shopper (not in the ABI result type yet)
|
|
157
|
+
const giftCard = giftCardBalanceOf(r.giftCard);
|
|
158
|
+
return {
|
|
159
|
+
formId,
|
|
160
|
+
status: 'ok',
|
|
161
|
+
...(typeof r.discountCode === 'string' && DISCOUNT_CODE.test(r.discountCode) ? { discountCode: r.discountCode } : {}),
|
|
162
|
+
// double opt-in: the sign-up waits for the emailed confirmation (only the literal `true` counts)
|
|
163
|
+
...(r.pendingConfirmation === true ? { pendingConfirmation: true } : {}),
|
|
164
|
+
...(giftCard ? { giftCard } : {}),
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
return failed(r.reason === 'invalid' ? 'invalid' : r.reason === 'rate_limited' ? 'rate_limited' : 'unavailable');
|
|
168
|
+
}
|
|
169
|
+
catch (e) {
|
|
170
|
+
ctx.log.warn('form submit failed', { formId, error: String(e?.name ?? e) });
|
|
171
|
+
return failed('unavailable');
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* The ABI `render(request, ctx)` for one theme. Call it once at module scope: it hands the theme
|
|
176
|
+
* bundle to the runtime and registers the theme's sections (store-independent state only).
|
|
177
|
+
*/
|
|
178
|
+
export function createRender(options) {
|
|
179
|
+
const { manifest, bundle } = options;
|
|
180
|
+
const effectiveSettings = options.effectiveSettings ?? ((s) => s);
|
|
181
|
+
const transformNodes = options.transformNodes ?? ((n) => n);
|
|
182
|
+
const shopSettings = options.shopSettings ?? ((s) => s);
|
|
183
|
+
// ── store-independent module state ─────────────────────────────────────────
|
|
184
|
+
setThemeBundle(bundle);
|
|
185
|
+
registerSections(Object.values(sectionModules()).map((m) => m.default));
|
|
186
|
+
const LOCALE_MAPS = Object.fromEntries(Object.entries(bundle.locales).map(([path, mod]) => [path.match(/\/([^/]+)\.json$/)?.[1] ?? 'en', mod.default]));
|
|
187
|
+
const COMPONENTS = manifest.components;
|
|
188
|
+
/** The planner's view of a section: its attributes (resource settings name what to load). */
|
|
189
|
+
const defOf = (type) => (COMPONENTS[type] ? { name: type, attributes: COMPONENTS[type].attributes ?? {} } : undefined);
|
|
190
|
+
/** Global setting defaults from the manifest (a store's settings may omit a key it never set). */
|
|
191
|
+
const SETTING_DEFAULTS = Object.fromEntries((manifest.settingsSchema ?? []).flatMap((g) => g.items.filter((it) => it.default !== undefined).map((it) => [it.key, it.default])));
|
|
192
|
+
/** The theme's interface language: the request's, else the theme setting's, else English. */
|
|
193
|
+
function uiLanguage(language, settings) {
|
|
194
|
+
const primary = language.split('-')[0].toLowerCase();
|
|
195
|
+
if (LOCALE_MAPS[primary])
|
|
196
|
+
return primary;
|
|
197
|
+
return themeLocaleOf(settings.locale, Object.keys(LOCALE_MAPS));
|
|
198
|
+
}
|
|
199
|
+
return async function render(_request, ctx) {
|
|
200
|
+
let settings = effectiveSettings({ ...SETTING_DEFAULTS, ...ctx.content.settings });
|
|
201
|
+
const nodes = transformNodes(nodesOf(ctx.content.page.regions), settings);
|
|
202
|
+
const searchQuery = searchQueryOf(ctx);
|
|
203
|
+
const searchSort = searchSortOf(ctx);
|
|
204
|
+
const params = pageParamsOf(ctx, searchQuery, searchSort);
|
|
205
|
+
const listing = listingQueryOfRequest(ctx);
|
|
206
|
+
const location = locationOf(ctx.route.canonical, params, listing);
|
|
207
|
+
const now = ctx.now();
|
|
208
|
+
const sf = options.storefront;
|
|
209
|
+
// The merchant's wording for the theme's strings: one call beside the plan's first round (headless `t()`, and every
|
|
210
|
+
// theme's `labels`: the store's translations of its own words, abi/labels.ts; board 35 #41).
|
|
211
|
+
// A non-headless theme asks only on a live page and only when the host offers the read (the editor shows the theme's words).
|
|
212
|
+
const wantStrings = sf ? sf.merchantStrings !== false : ctx.mode !== 'preview' && typeof ctx.data.localization?.strings === 'function';
|
|
213
|
+
const stringsCall = wantStrings && ctx.capabilities.has('data:localization')
|
|
214
|
+
? ctx.data.localization.strings(ctx.locale.language).catch((e) => {
|
|
215
|
+
const name = e?.name;
|
|
216
|
+
if (name === 'DataLimitError' || name === 'CapabilityError')
|
|
217
|
+
throw e;
|
|
218
|
+
ctx.log.warn('merchant strings unavailable', { error: String(e?.message ?? e) });
|
|
219
|
+
return {};
|
|
220
|
+
})
|
|
221
|
+
: null;
|
|
222
|
+
let pd;
|
|
223
|
+
let merchantStrings = {};
|
|
224
|
+
try {
|
|
225
|
+
if (sf) {
|
|
226
|
+
const [loaded, strings] = await Promise.all([
|
|
227
|
+
loadPageData(ctx, nodes, defOf, [], searchQuery, searchSort, listing, { ...(sf.plan ?? {}), reservedCalls: stringsCall ? 1 : 0 }),
|
|
228
|
+
stringsCall ?? Promise.resolve({}),
|
|
229
|
+
]);
|
|
230
|
+
pd = loaded;
|
|
231
|
+
merchantStrings = strings;
|
|
232
|
+
}
|
|
233
|
+
else {
|
|
234
|
+
const [loaded, strings] = await Promise.all([
|
|
235
|
+
// the strings read is one call beside the plan's first round: the plan stays inside the limit with it
|
|
236
|
+
loadPageData(ctx, nodes, defOf, [], searchQuery, searchSort, listing, stringsCall ? { reservedCalls: 1 } : undefined),
|
|
237
|
+
stringsCall ?? Promise.resolve({}),
|
|
238
|
+
]);
|
|
239
|
+
pd = loaded;
|
|
240
|
+
merchantStrings = strings;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
catch (e) {
|
|
244
|
+
const name = e?.name;
|
|
245
|
+
// A breach or a missing capability is the host's to report (ABI error value, §8) — never swallowed.
|
|
246
|
+
if (name === 'DataLimitError' || name === 'CapabilityError')
|
|
247
|
+
throw e;
|
|
248
|
+
ctx.log.error('page data unavailable', { error: String(e?.message ?? e) });
|
|
249
|
+
return new Response('<!doctype html><html lang="' + esc(ctx.locale.language) + '"><title>Temporarily unavailable</title><h1>This store is temporarily unavailable</h1></html>', {
|
|
250
|
+
status: 503,
|
|
251
|
+
headers: { 'content-type': 'text/html; charset=utf-8' },
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
// The store's shop record may still adjust the settings (the theme's hook). The browser
|
|
255
|
+
// hydrates from `state.settings`, so both renders use the same values.
|
|
256
|
+
settings = shopSettings(settings, pd.shop);
|
|
257
|
+
// Native forms (§1): a newsletter / contact form posts to this same page; a POST's render submits it.
|
|
258
|
+
pd = { ...pd, formEndpoint: new URL(ctx.route.canonical).pathname, formResult: await nativeFormResult(ctx), routeParams: { ...ctx.route.params } };
|
|
259
|
+
// Cache (§5): the platform default applies; hints may only tighten or vary. Only the cart route is
|
|
260
|
+
// personal (the account page is the platform's iframe).
|
|
261
|
+
ctx.cache.hint({
|
|
262
|
+
tags: tagsFor(ctx),
|
|
263
|
+
...(ctx.route.kind === 'cart' ? { private: true } : {}),
|
|
264
|
+
vary: [...(pd.localization.country.isoCode ? ['x-tq-country'] : []), ...(pd.signedIn !== null ? ['isSignedIn'] : [])],
|
|
265
|
+
});
|
|
266
|
+
const data = createDataApi(pd, null);
|
|
267
|
+
const lang = ctx.locale.language;
|
|
268
|
+
const declared = ctx.locale.direction;
|
|
269
|
+
const dir = declared ?? (RTL.has(lang.split('-')[0].toLowerCase()) ? 'rtl' : 'ltr');
|
|
270
|
+
const ui = uiLanguage(lang, settings);
|
|
271
|
+
const strings = localeStrings(ui, LOCALE_MAPS);
|
|
272
|
+
const preview = ctx.mode === 'preview';
|
|
273
|
+
// Headless themes: the page facts (also copied into the hydration state) and this render's head sink (<Seo>).
|
|
274
|
+
const page = sf ? pageInfoOf(ctx, params, ui, preview, strings, merchantStrings) : null;
|
|
275
|
+
// The theme's own words as the merchant translated them for this language (abi/labels.ts); the editor shows the theme's.
|
|
276
|
+
const labels = preview ? {} : labelsOf(merchantStrings, ctx.locale.market);
|
|
277
|
+
const sink = sf ? { jsonLd: [] } : null;
|
|
278
|
+
// Dynamic style values become class rules collected for THIS render (sdk/sx.ts): the
|
|
279
|
+
// platform CSP has `style-src-attr 'none'`, so no `style=""` attribute may reach the page.
|
|
280
|
+
const { result: body, css: styleRules } = collectStyles(() => renderToString(_jsx(PageExtras, { page: page, sink: sink, children: _jsx(ServerRenderProvider, { value: true, children: _jsx(PreviewModeProvider, { value: preview, children: _jsx(DataProvider, { value: data, children: _jsx(ThemeProvider, { settings: settings, locale: strings, children: _jsx(AbiCartProvider, { initial: pd.cart, remote: null, children: _jsx(AbiShell, { location: location, locale: ui, contentLanguage: lang, settings: settings, preview: preview, labels: labels, children: _jsx(SectionTree, { tree: (!preview && pd.routeMissing ? notFoundTree(nodes) : nodes) }) }) }) }) }) }) }) })));
|
|
281
|
+
const canonicalPath = new URL(ctx.route.canonical).pathname;
|
|
282
|
+
let head = computeHead(canonicalPath, data, settings, new URL(ctx.route.canonical).origin);
|
|
283
|
+
const shopName = shopNameOf(settings.shopName, pd.shop.name);
|
|
284
|
+
if (ctx.route.kind === 'blog' && ctx.route.handle && pd.blogs[ctx.route.handle]) {
|
|
285
|
+
const b = pd.blogs[ctx.route.handle];
|
|
286
|
+
head = { ...head, ...headFrom(b.seo, b.title, shopName) };
|
|
287
|
+
}
|
|
288
|
+
else if (ctx.route.kind === 'article' && ctx.route.handle) {
|
|
289
|
+
const a = Object.values(pd.articles).find((x) => x.handle === ctx.route.handle);
|
|
290
|
+
if (a)
|
|
291
|
+
head = { ...head, ...headFrom(a.seo, a.title, shopName, a.excerpt, a.image?.url) };
|
|
292
|
+
}
|
|
293
|
+
// The home page: the store's own search & social settings, then the fallbacks (abi/seo.ts).
|
|
294
|
+
let imageMeta = {};
|
|
295
|
+
if (ctx.route.kind === 'home') {
|
|
296
|
+
const home = homeHead(pd.shop, shopName, nodes, strings['seo.homeDescription'] ?? '');
|
|
297
|
+
head = { ...head, ...(home.title ? { title: home.title } : {}), description: home.description, image: home.image };
|
|
298
|
+
imageMeta = { alt: home.imageAlt, width: home.imageWidth, height: home.imageHeight };
|
|
299
|
+
}
|
|
300
|
+
else if ((ctx.route.kind === 'collection' || ctx.route.kind === 'collections') && !head.description) {
|
|
301
|
+
// SEO-13: a listing without its own text (`/collections/all`, the collection list, an empty collection
|
|
302
|
+
// description) falls back to the store's description like the home page — never no meta description at all.
|
|
303
|
+
const home = homeHead(pd.shop, shopName, nodes, strings['seo.homeDescription'] ?? '');
|
|
304
|
+
head = { ...head, description: home.description, ...(!head.image && home.image ? { image: home.image } : {}) };
|
|
305
|
+
}
|
|
306
|
+
// Routes with no resource of their own name the page in the title (board 35 #76): "Cart — <shop>", "Search: mug —
|
|
307
|
+
// <shop>", "Collections — <shop>", "<policy title> — <shop>", "Page not found — <shop>".
|
|
308
|
+
const routeTitle = routeTitleOf({
|
|
309
|
+
kind: ctx.route.kind,
|
|
310
|
+
missing: pd.routeMissing,
|
|
311
|
+
t: (key) => labels[key] ?? strings[key],
|
|
312
|
+
searchQuery,
|
|
313
|
+
policyTitle: ctx.route.kind === 'policy' && ctx.route.handle
|
|
314
|
+
? Object.values(pd.shop.policies ?? {}).find((p) => p?.handle === ctx.route.handle)?.title
|
|
315
|
+
: null,
|
|
316
|
+
});
|
|
317
|
+
if (routeTitle)
|
|
318
|
+
head = { ...head, title: documentTitle({ resourceTitle: routeTitle, shopName }) };
|
|
319
|
+
// An unknown product, collection, page or blog, or an unpublished policy, is a real 404, not a 200 with an empty template.
|
|
320
|
+
const status = ctx.route.kind === 'not_found' || pd.routeMissing ? 404 : 200;
|
|
321
|
+
// SEO-01 / SEO-05 / SEO-09: the page's own structured data, for every theme (a headless theme's storefrontHead adds
|
|
322
|
+
// its breadcrumbs and, for a collection, the same CollectionPage — so that one is only added here without it).
|
|
323
|
+
const pageJsonLd = [];
|
|
324
|
+
if (status === 200 && !preview) {
|
|
325
|
+
const origin = new URL(ctx.route.canonical).origin;
|
|
326
|
+
if (ctx.route.kind === 'home')
|
|
327
|
+
pageJsonLd.push(...homeJsonLd(pd.shop, { origin, shopName, language: lang }));
|
|
328
|
+
else if (ctx.route.kind === 'collection' && !sf && ctx.route.handle && pd.collections[ctx.route.handle]) {
|
|
329
|
+
const c = collectionJsonLd(pd.collections[ctx.route.handle], { url: ctx.route.canonical, origin });
|
|
330
|
+
if (c)
|
|
331
|
+
pageJsonLd.push(c);
|
|
332
|
+
}
|
|
333
|
+
else if (ctx.route.kind === 'article' && ctx.route.handle) {
|
|
334
|
+
const a = Object.values(pd.articles).find((x) => x.handle === ctx.route.handle);
|
|
335
|
+
const b = ctx.route.blogHandle ? pd.blogs[ctx.route.blogHandle] : undefined;
|
|
336
|
+
const ld = articleJsonLd(a, { url: ctx.route.canonical, origin, shopName, ...(b?.title ? { blogTitle: b.title } : {}) });
|
|
337
|
+
if (ld)
|
|
338
|
+
pageJsonLd.push(ld);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
// <Seo> in a headless theme: what its components said about this page wins over the defaults above.
|
|
342
|
+
let extraHead = '';
|
|
343
|
+
if (sf && sink) {
|
|
344
|
+
head = { ...head, ...(sink.title ? { title: sink.title } : {}), ...(sink.description ? { description: sink.description } : {}), ...(sink.image ? { image: sink.image } : {}) };
|
|
345
|
+
extraHead = storefrontHead(ctx, sf, sink, pd, bundle, ui, status);
|
|
346
|
+
}
|
|
347
|
+
const meta = [
|
|
348
|
+
head.description ? `<meta name="description" content="${esc(head.description)}">` : '',
|
|
349
|
+
`<link rel="canonical" href="${esc(ctx.route.canonical)}">`,
|
|
350
|
+
head.title ? `<meta property="og:title" content="${esc(head.title)}">` : '',
|
|
351
|
+
head.description ? `<meta property="og:description" content="${esc(head.description)}">` : '',
|
|
352
|
+
head.image ? `<meta property="og:image" content="${esc(head.image)}">` : '',
|
|
353
|
+
head.image && imageMeta.width ? `<meta property="og:image:width" content="${imageMeta.width}">` : '',
|
|
354
|
+
head.image && imageMeta.height ? `<meta property="og:image:height" content="${imageMeta.height}">` : '',
|
|
355
|
+
head.image && imageMeta.alt ? `<meta property="og:image:alt" content="${esc(imageMeta.alt)}">` : '',
|
|
356
|
+
head.type ? `<meta property="og:type" content="${esc(head.type)}">` : '',
|
|
357
|
+
`<meta property="og:url" content="${esc(ctx.route.canonical)}">`,
|
|
358
|
+
head.siteName ? `<meta property="og:site_name" content="${esc(head.siteName)}">` : '',
|
|
359
|
+
`<meta name="twitter:card" content="${head.image ? 'summary_large_image' : 'summary'}">`,
|
|
360
|
+
head.title ? `<meta name="twitter:title" content="${esc(head.title)}">` : '',
|
|
361
|
+
head.description ? `<meta name="twitter:description" content="${esc(head.description)}">` : '',
|
|
362
|
+
head.image ? `<meta name="twitter:image" content="${esc(head.image)}">` : '',
|
|
363
|
+
head.favicon ? `<link rel="icon" href="${esc(head.favicon)}">` : '',
|
|
364
|
+
...(options.hreflang !== false && status === 200
|
|
365
|
+
? hreflangAlternates(ctx.route.canonical, ctx.locale.availableLanguages).map((a) => `<link rel="alternate" hreflang="${esc(a.hreflang)}" href="${esc(a.href)}">`)
|
|
366
|
+
: []),
|
|
367
|
+
jsonLdTag(head.jsonLd),
|
|
368
|
+
...pageJsonLd.map((b) => jsonLdTag(b)),
|
|
369
|
+
extraHead,
|
|
370
|
+
].filter(Boolean).join('\n ');
|
|
371
|
+
// What the browser needs to rebuild the same tree: this page's content and the data it rendered.
|
|
372
|
+
const state = { route: { ...ctx.route, params }, page: ctx.content.page, settings, data: pd, now, preview, ui, lang, ...(listing ? { listing } : {}), ...(page ? { kit: page } : {}), ...(Object.keys(labels).length ? { labels } : {}) };
|
|
373
|
+
const html = `<!doctype html>
|
|
374
|
+
<html lang="${esc(lang)}" dir="${dir}">
|
|
375
|
+
<head>
|
|
376
|
+
<meta charset="utf-8">
|
|
377
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
378
|
+
<title>${esc(head.title)}</title>
|
|
379
|
+
${meta}
|
|
380
|
+
${assetTags(ctx)}
|
|
381
|
+
${styleRules ? `<style id="${STYLE_RULES_ID}">${styleRules}</style>` : ''}
|
|
382
|
+
${!preview && settings.enableSpaNavigation !== false ? `<style id="${VIEW_TRANSITIONS_ID}">${VIEW_TRANSITIONS_CSS}</style>` : ''}
|
|
383
|
+
</head>
|
|
384
|
+
<body data-tq-mode="${esc(ctx.mode)}">
|
|
385
|
+
<script type="application/json" id="tq-state">${jsonBlock({ ...state, data: packPageData(state.data) })}</script>
|
|
386
|
+
<div id="root">${body}</div>
|
|
387
|
+
</body>
|
|
388
|
+
</html>`;
|
|
389
|
+
// §5: no 'unsafe-inline'. Every inline <style> block is announced by hash (`style-src-elem`).
|
|
390
|
+
const headers = new Headers({ 'content-type': 'text/html; charset=utf-8' });
|
|
391
|
+
for (const hash of await styleHashes(html))
|
|
392
|
+
headers.append('x-tq-csp-style-hash', hash);
|
|
393
|
+
for (const [name, value] of Object.entries(platformHints(preview, settings, pd.shop, Object.values(pd.products ?? {}))))
|
|
394
|
+
headers.set(name, value);
|
|
395
|
+
// NEW-PROD-2: a 3D model on the page ⇒ the theme's viewer styles are announced too (bounded by the edge's budget)
|
|
396
|
+
if (!preview && pageMedia(Object.values(pd.products ?? {})).model)
|
|
397
|
+
for (const hash of options.modelViewerStyleHashes ?? [])
|
|
398
|
+
if (/^sha(256|384|512)-[A-Za-z0-9+/=]+$/.test(hash))
|
|
399
|
+
headers.append('x-tq-csp-style-hash', hash);
|
|
400
|
+
// the platform accepted this POST's form: the gateway may answer with a redirect to the page (Post/Redirect/Get,
|
|
401
|
+
// storefront-edge core/form-flash.ts) instead of this page, so a reload does not send it again
|
|
402
|
+
const formId = ctx.request.formId;
|
|
403
|
+
if (typeof formId === 'string' && pd.formResult?.status === 'ok' && pd.formResult.formId === formId)
|
|
404
|
+
headers.set('x-tq-form-done', formId);
|
|
405
|
+
return new Response(html, { status, headers });
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* The live-only hints the storefront gateway acts on (storefront-edge core/platform-hints.ts); it drops every `x-tq-*`
|
|
410
|
+
* before the page leaves, so a browser never sees them.
|
|
411
|
+
* x-tq-speculation: on NEW-OS-11 part 2 — `enableSpaNavigation` is not off: the gateway adds its
|
|
412
|
+
* `Speculation-Rules` header, so hovering a link prerenders the next page.
|
|
413
|
+
* x-tq-turnstile: on A4 — the store's spam protection asks for the Turnstile widget on a form this theme can show
|
|
414
|
+
* (`shop.spamProtection`, either switch): the gateway allows Cloudflare Turnstile in the CSP.
|
|
415
|
+
* x-tq-media: video, model NEW-PROD-2 — a product on the page has a YouTube / Vimeo video (the gateway frames
|
|
416
|
+
* `www.youtube-nocookie.com` and `player.vimeo.com`) and/or a 3D model (`connect-src blob:`).
|
|
417
|
+
* Never in the editor canvas (a preview host ignores them too).
|
|
418
|
+
*/
|
|
419
|
+
export function platformHints(preview, settings, shop, products = []) {
|
|
420
|
+
if (preview)
|
|
421
|
+
return {};
|
|
422
|
+
const out = {};
|
|
423
|
+
if (settings.enableSpaNavigation !== false)
|
|
424
|
+
out['x-tq-speculation'] = 'on';
|
|
425
|
+
const sp = shop?.spamProtection;
|
|
426
|
+
if (sp && (sp.contactForms || sp.authPages))
|
|
427
|
+
out['x-tq-turnstile'] = 'on';
|
|
428
|
+
// NEW-PROD-2 (rev 2.52): a YouTube / Vimeo video ⇒ the gateway frames those two players; a 3D model ⇒ it lets the
|
|
429
|
+
// viewer read its file (same-origin model route, `blob:` textures)
|
|
430
|
+
const m = pageMedia(products);
|
|
431
|
+
const media = [...(m.video ? ['video'] : []), ...(m.model ? ['model'] : [])];
|
|
432
|
+
if (media.length)
|
|
433
|
+
out['x-tq-media'] = media.join(', ');
|
|
434
|
+
return out;
|
|
435
|
+
}
|
|
436
|
+
/** Which kinds of third-party / 3D media the page's products carry (kit `ProductMedia.type`). */
|
|
437
|
+
export function pageMedia(products) {
|
|
438
|
+
let video = false;
|
|
439
|
+
let model = false;
|
|
440
|
+
for (const p of products)
|
|
441
|
+
for (const m of p?.media ?? []) {
|
|
442
|
+
if (m?.type === 'external_video')
|
|
443
|
+
video = true;
|
|
444
|
+
else if (m?.type === 'model_3d')
|
|
445
|
+
model = true;
|
|
446
|
+
}
|
|
447
|
+
return { video, model };
|
|
448
|
+
}
|
|
449
|
+
/** The headless providers around the tree; a plain pass-through when the theme does not use them. */
|
|
450
|
+
function PageExtras({ page, sink, children }) {
|
|
451
|
+
if (!page)
|
|
452
|
+
return children;
|
|
453
|
+
return (_jsx(PageInfoContext.Provider, { value: page, children: _jsx(HeadSinkContext.Provider, { value: sink, children: children }) }));
|
|
454
|
+
}
|
|
455
|
+
/** `PageInfo` for this render: route facts, languages, assets, and only the merchant strings that differ from the theme's. */
|
|
456
|
+
function pageInfoOf(ctx, params, ui, preview, theme, merchant) {
|
|
457
|
+
const lang = ctx.locale.language;
|
|
458
|
+
// The merchant's strings are for the page's language; a theme rendering another language keeps its own words.
|
|
459
|
+
const sameLanguage = lang.split('-')[0].toLowerCase() === ui;
|
|
460
|
+
const strings = {};
|
|
461
|
+
if (sameLanguage)
|
|
462
|
+
for (const [k, v] of Object.entries(merchant))
|
|
463
|
+
if (typeof v === 'string' && v !== theme[k])
|
|
464
|
+
strings[k] = v;
|
|
465
|
+
return {
|
|
466
|
+
kind: ctx.route.kind,
|
|
467
|
+
...(ctx.route.handle ? { handle: ctx.route.handle } : {}),
|
|
468
|
+
...(ctx.route.blogHandle ? { blogHandle: ctx.route.blogHandle } : {}),
|
|
469
|
+
params,
|
|
470
|
+
canonical: ctx.route.canonical,
|
|
471
|
+
language: lang,
|
|
472
|
+
ui,
|
|
473
|
+
languages: [...new Set(ctx.locale.availableLanguages)],
|
|
474
|
+
preview,
|
|
475
|
+
assetsBase: ctx.assets.base,
|
|
476
|
+
strings,
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
/** Robots, breadcrumbs, collection page and the theme's own JSON-LD (headless themes). */
|
|
480
|
+
function storefrontHead(ctx, sf, sink, pd, bundle, ui, status) {
|
|
481
|
+
const out = [];
|
|
482
|
+
const canonical = ctx.route.canonical;
|
|
483
|
+
let origin = '';
|
|
484
|
+
try {
|
|
485
|
+
origin = new URL(canonical).origin;
|
|
486
|
+
}
|
|
487
|
+
catch {
|
|
488
|
+
origin = '';
|
|
489
|
+
}
|
|
490
|
+
if (sink.noindex)
|
|
491
|
+
out.push('<meta name="robots" content="noindex">');
|
|
492
|
+
const blocks = [];
|
|
493
|
+
if (sf.structuredData !== false && status === 200 && origin) {
|
|
494
|
+
const home = bundle.theme.labels(ui).home;
|
|
495
|
+
const crumbs = [{ name: home, url: absoluteOn(origin, '/') }];
|
|
496
|
+
const h = ctx.route.handle;
|
|
497
|
+
if (ctx.route.kind === 'product' && h && pd.products[h]) {
|
|
498
|
+
const p = pd.products[h];
|
|
499
|
+
const c = p.collections?.find((x) => x && x.handle && x.title);
|
|
500
|
+
if (c)
|
|
501
|
+
crumbs.push({ name: c.title, url: absoluteOn(origin, `/collections/${encodeURIComponent(c.handle)}`) });
|
|
502
|
+
crumbs.push({ name: p.title });
|
|
503
|
+
}
|
|
504
|
+
else if (ctx.route.kind === 'collection' && h && pd.collections[h]) {
|
|
505
|
+
const c = pd.collections[h];
|
|
506
|
+
crumbs.push({ name: c.title });
|
|
507
|
+
const page = collectionJsonLd(c, { url: canonical, origin });
|
|
508
|
+
if (page)
|
|
509
|
+
blocks.push(page);
|
|
510
|
+
}
|
|
511
|
+
else if (ctx.route.kind === 'page' && h && pd.pages[h]) {
|
|
512
|
+
crumbs.push({ name: pd.pages[h].title });
|
|
513
|
+
}
|
|
514
|
+
else if (ctx.route.kind === 'article' && h) {
|
|
515
|
+
const a = Object.values(pd.articles).find((x) => x.handle === h);
|
|
516
|
+
const b = ctx.route.blogHandle ? pd.blogs[ctx.route.blogHandle] : undefined;
|
|
517
|
+
if (b)
|
|
518
|
+
crumbs.push({ name: b.title, url: absoluteOn(origin, `/blogs/${encodeURIComponent(b.handle)}`) });
|
|
519
|
+
if (a)
|
|
520
|
+
crumbs.push({ name: a.title });
|
|
521
|
+
}
|
|
522
|
+
const trail = crumbs.length > 1 ? breadcrumbJsonLd(crumbs) : null;
|
|
523
|
+
if (trail)
|
|
524
|
+
blocks.push(trail);
|
|
525
|
+
}
|
|
526
|
+
// The theme's own blocks never repeat the Product the render already prints (exactly one per product page).
|
|
527
|
+
for (const b of sink.jsonLd)
|
|
528
|
+
if (![b['@type']].flat().includes('Product'))
|
|
529
|
+
blocks.push(b);
|
|
530
|
+
for (const b of blocks)
|
|
531
|
+
out.push(jsonLdTag(b));
|
|
532
|
+
return out.filter(Boolean).join('\n ');
|
|
533
|
+
}
|