@decocms/blocks 8.1.0-next.0 → 8.1.0-next.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -92
- package/src/index.ts +2 -12
- package/src/protocol/storage/memory.ts +1 -1
- package/src/v8/background.ts +3 -3
- package/src/v8/builtins/legacy.ts +1 -1
- package/src/v8/cli/index.ts +1 -1
- package/src/v8/fetch.ts +8 -27
- package/src/v8/remoteLoader.ts +1 -1
- package/src/v8/telemetry.ts +1 -1
- package/src/cms/applySectionConventions.ts +0 -148
- package/src/cms/blockSource.ts +0 -228
- package/src/cms/client.ts +0 -85
- package/src/cms/deferredTrigger.ts +0 -58
- package/src/cms/draftSource.ts +0 -476
- package/src/cms/index.ts +0 -180
- package/src/cms/loadDecofileDirectory.ts +0 -89
- package/src/cms/loader.ts +0 -563
- package/src/cms/registry.ts +0 -264
- package/src/cms/renderJson.ts +0 -179
- package/src/cms/resolve.ts +0 -2583
- package/src/cms/schema.ts +0 -1219
- package/src/cms/sectionLoaderContext.ts +0 -113
- package/src/cms/sectionLoaders.ts +0 -686
- package/src/cms/sectionMixins.ts +0 -153
- package/src/flags/audience.ts +0 -56
- package/src/flags/everyone.ts +0 -29
- package/src/flags/flag.ts +0 -15
- package/src/flags/multivariate/image.ts +0 -11
- package/src/flags/multivariate/message.ts +0 -11
- package/src/flags/multivariate/page.ts +0 -16
- package/src/flags/multivariate/section.ts +0 -16
- package/src/flags/multivariate.ts +0 -1
- package/src/flags/types.ts +0 -59
- package/src/flags/utils/multivariate.ts +0 -20
- package/src/hooks/Image.tsx +0 -314
- package/src/hooks/JsonLd.tsx +0 -353
- package/src/hooks/LazySection.tsx +0 -121
- package/src/hooks/LiveControls.tsx +0 -144
- package/src/hooks/Picture.tsx +0 -127
- package/src/hooks/RenderSection.tsx +0 -91
- package/src/hooks/SectionErrorFallback.tsx +0 -97
- package/src/hooks/Stats.tsx +0 -134
- package/src/hooks/index.ts +0 -35
- package/src/hooks/useLoadMore.ts +0 -100
- package/src/matchers/builtins.ts +0 -459
- package/src/matchers/countryNames.ts +0 -104
- package/src/matchers/override.ts +0 -136
- package/src/matchers/posthog.ts +0 -154
- package/src/middleware/decoState.ts +0 -55
- package/src/middleware/healthMetrics.ts +0 -133
- package/src/middleware/hydrationContext.ts +0 -79
- package/src/middleware/index.ts +0 -88
- package/src/middleware/liveness.ts +0 -21
- package/src/middleware/observability.ts +0 -800
- package/src/middleware/validateSection.ts +0 -100
- package/src/preview/DraftPreviewBadge.tsx +0 -306
- package/src/preview/decoMark.ts +0 -11
- package/src/preview/index.ts +0 -8
- package/src/sdk/abTesting.ts +0 -556
- package/src/sdk/analytics.ts +0 -147
- package/src/sdk/cacheHeaders.ts +0 -633
- package/src/sdk/cacheStorage.ts +0 -287
- package/src/sdk/cachedLoader.ts +0 -556
- package/src/sdk/clx.ts +0 -5
- package/src/sdk/cn.ts +0 -28
- package/src/sdk/composite.ts +0 -114
- package/src/sdk/cookie.ts +0 -129
- package/src/sdk/crypto.ts +0 -199
- package/src/sdk/csp.ts +0 -59
- package/src/sdk/deepOmit.ts +0 -52
- package/src/sdk/detectDevice.ts +0 -45
- package/src/sdk/djb2.ts +0 -20
- package/src/sdk/encoding.ts +0 -44
- package/src/sdk/env.ts +0 -54
- package/src/sdk/experiments.ts +0 -314
- package/src/sdk/fetchCache.ts +0 -269
- package/src/sdk/fetchTimeout.ts +0 -58
- package/src/sdk/flags.ts +0 -138
- package/src/sdk/http.ts +0 -124
- package/src/sdk/index.ts +0 -97
- package/src/sdk/inflightTimeout.ts +0 -53
- package/src/sdk/instrumentedFetch.ts +0 -362
- package/src/sdk/invoke.ts +0 -298
- package/src/sdk/logger.ts +0 -304
- package/src/sdk/mergeCacheControl.ts +0 -150
- package/src/sdk/nonce.ts +0 -46
- package/src/sdk/normalizeUrls.ts +0 -91
- package/src/sdk/observability.ts +0 -116
- package/src/sdk/otel.ts +0 -1118
- package/src/sdk/otelAdapters/clickhouseCollector.ts +0 -65
- package/src/sdk/otelAdapters.ts +0 -144
- package/src/sdk/otelHttpLog.ts +0 -419
- package/src/sdk/otelHttpMeter.ts +0 -532
- package/src/sdk/otelHttpTracer.ts +0 -543
- package/src/sdk/redirects.ts +0 -336
- package/src/sdk/requestContext.ts +0 -281
- package/src/sdk/requestContextStorage.browser.ts +0 -43
- package/src/sdk/requestContextStorage.ts +0 -35
- package/src/sdk/responseCache.ts +0 -82
- package/src/sdk/retry.ts +0 -45
- package/src/sdk/serverTimings.ts +0 -68
- package/src/sdk/signal.ts +0 -42
- package/src/sdk/sitemap.ts +0 -160
- package/src/sdk/urlRedaction.ts +0 -82
- package/src/sdk/urlUtils.ts +0 -183
- package/src/sdk/useDevice.ts +0 -89
- package/src/sdk/useDeviceContext.tsx +0 -108
- package/src/sdk/useId.ts +0 -7
- package/src/sdk/useScript.ts +0 -210
- package/src/sdk/useSuggestions.ts +0 -188
- package/src/sdk/wrapCaughtErrors.ts +0 -107
- package/src/setup.ts +0 -120
- package/src/types/index.ts +0 -53
- package/src/types/widgets.ts +0 -14
package/src/hooks/Image.tsx
DELETED
|
@@ -1,314 +0,0 @@
|
|
|
1
|
-
import type { ImgHTMLAttributes } from "react";
|
|
2
|
-
import { forwardRef } from "react";
|
|
3
|
-
|
|
4
|
-
// -------------------------------------------------------------------------
|
|
5
|
-
// Known asset prefixes that get stripped to produce a relative src path
|
|
6
|
-
// -------------------------------------------------------------------------
|
|
7
|
-
|
|
8
|
-
// Anchored: only a leading host is stripped.
|
|
9
|
-
const DECO_ASSET_PREFIX_RE =
|
|
10
|
-
/^https:\/\/(?:assets\.decocache\.com|deco-sites-assets\.s3\.sa-east-1\.amazonaws\.com)\//;
|
|
11
|
-
// decoims.com — the legacy default image CDN (pre-#495). Decofiles are full of
|
|
12
|
-
// these URLs; passing one as `src=` to a Cloudflare Deco CDN makes it fetch
|
|
13
|
-
// itself → 508 Loop Detected. Azion CDNs (edgedeco/decoazn) fetch it fine but
|
|
14
|
-
// may 404 on the relative path, so only strip it for the Cloudflare ones.
|
|
15
|
-
const DECOIMS_PREFIX_RE = /^https:\/\/decoims\.com\//;
|
|
16
|
-
const CLOUDFLARE_CDNS = new Set(["assets.decocms.com", "decoims.com"]);
|
|
17
|
-
|
|
18
|
-
// -------------------------------------------------------------------------
|
|
19
|
-
// Configurable CDN domain
|
|
20
|
-
// -------------------------------------------------------------------------
|
|
21
|
-
|
|
22
|
-
let imageCdnDomain = "assets.decocms.com";
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Register the image CDN domain used by `getOptimizedMediaUrl`.
|
|
26
|
-
* Call once in your site's setup.ts before any page loads.
|
|
27
|
-
*
|
|
28
|
-
* Available domains:
|
|
29
|
-
* - `assets.decocms.com` (Cloudflare, default — best compression, same edge as Workers)
|
|
30
|
-
* - `deco-assets.edgedeco.com` (Azion IMS)
|
|
31
|
-
* - `deco-assets.decoazn.com` (Azion IMS, legacy)
|
|
32
|
-
*/
|
|
33
|
-
export function registerImageCdnDomain(domain: string) {
|
|
34
|
-
imageCdnDomain = domain.replace(/^https?:\/\//, "").replace(/\/+$/, "");
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export function getImageCdnDomain(): string {
|
|
38
|
-
return imageCdnDomain;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// -------------------------------------------------------------------------
|
|
42
|
-
// Configurable image quality
|
|
43
|
-
// -------------------------------------------------------------------------
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Quality levels the Deco image CDN accepts as a site-wide default.
|
|
47
|
-
*
|
|
48
|
-
* The CDN maps these to 60% / 70% / 80%. It also accepts `original` (100%),
|
|
49
|
-
* deliberately excluded here: a global 100% default hurts performance on
|
|
50
|
-
* every page, which is the same call `DefaultQualityOptions` makes in the
|
|
51
|
-
* Fresh implementation this file was ported from (deco-cx/apps,
|
|
52
|
-
* website/components/Image.tsx).
|
|
53
|
-
*
|
|
54
|
-
* A union rather than `string` because the CDN **fails silently** on
|
|
55
|
-
* anything else: it answers 200 and serves its 60% default. So the obvious
|
|
56
|
-
* `registerImageQuality("80")` — quality means 1-100 in Cloudflare Images,
|
|
57
|
-
* next/image and imgix — would quietly serve the LOWEST quality while
|
|
58
|
-
* changing the cache key on every image. Measured against production
|
|
59
|
-
* decoims.com on one asset: no param 2911 B, `low` 2911, `medium` 3235,
|
|
60
|
-
* `high` 5189, and `"80"` / `"HIGH"` / garbage all 2911.
|
|
61
|
-
*/
|
|
62
|
-
export type ImageQuality = "low" | "medium" | "high";
|
|
63
|
-
|
|
64
|
-
let imageQuality: ImageQuality | undefined;
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Register the quality level `getOptimizedMediaUrl` asks the image CDN for.
|
|
68
|
-
*
|
|
69
|
-
* Call once at module scope in your site's setup, NOT from a loader, action or
|
|
70
|
-
* anything else on the request path. This is module-level state shared by every
|
|
71
|
-
* request in a Worker isolate: setting it per-request would leak across
|
|
72
|
-
* concurrent requests, and setting it on only one of the SSR/hydration paths
|
|
73
|
-
* would produce mismatched `src`/`srcSet` and re-download every image.
|
|
74
|
-
*
|
|
75
|
-
* Unset by default, which emits no `quality` param and leaves the CDN on its
|
|
76
|
-
* 60% default — so existing sites are byte-for-byte unaffected and no CDN
|
|
77
|
-
* cache is invalidated. Set it when a site's art direction needs fidelity over
|
|
78
|
-
* bytes: on fashion/editorial catalogues the default compression visibly
|
|
79
|
-
* softens fabric texture and print detail.
|
|
80
|
-
*
|
|
81
|
-
* Applies to every URL built by `getOptimizedMediaUrl` — `Image`, `getSrcSet`
|
|
82
|
-
* and also `Video` when it is given `forceOptimizedSrc`. It does NOT apply to
|
|
83
|
-
* VTEX- or Shopify-hosted sources: those are resized through their own native
|
|
84
|
-
* URL syntax (`optimizeVTEX` / `optimizeShopify`), which returns before the
|
|
85
|
-
* query-param block. On a VTEX storefront that means product imagery is
|
|
86
|
-
* untouched and only CMS/banner assets are affected.
|
|
87
|
-
*/
|
|
88
|
-
export function registerImageQuality(quality: ImageQuality | undefined) {
|
|
89
|
-
imageQuality = quality || undefined;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
export function getImageQuality(): ImageQuality | undefined {
|
|
93
|
-
return imageQuality;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// -------------------------------------------------------------------------
|
|
97
|
-
// Fit options & optimization types
|
|
98
|
-
// -------------------------------------------------------------------------
|
|
99
|
-
|
|
100
|
-
export type FitOptions = "contain" | "cover" | "fill";
|
|
101
|
-
|
|
102
|
-
export const FACTORS = [1, 2];
|
|
103
|
-
|
|
104
|
-
interface OptimizationOptions {
|
|
105
|
-
originalSrc: string;
|
|
106
|
-
width: number;
|
|
107
|
-
height?: number;
|
|
108
|
-
fit: FitOptions;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// -------------------------------------------------------------------------
|
|
112
|
-
// Platform-specific URL optimizers (fallbacks when the CDN can handle
|
|
113
|
-
// the native platform's resize syntax directly)
|
|
114
|
-
// -------------------------------------------------------------------------
|
|
115
|
-
|
|
116
|
-
function optimizeVTEX(originalSrc: string, width: number, height?: number): string {
|
|
117
|
-
const src = new URL(originalSrc);
|
|
118
|
-
const [slash, arquivos, ids, rawId, ...rest] = src.pathname.split("/");
|
|
119
|
-
const [trueId] = rawId.split("-");
|
|
120
|
-
|
|
121
|
-
src.pathname = [slash, arquivos, ids, `${trueId}-${width}-${height ?? width}`, ...rest].join("/");
|
|
122
|
-
|
|
123
|
-
return src.href;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
function optimizeShopify(originalSrc: string, width: number, height?: number): string {
|
|
127
|
-
const url = new URL(originalSrc);
|
|
128
|
-
url.searchParams.set("width", `${width}`);
|
|
129
|
-
if (height) url.searchParams.set("height", `${height}`);
|
|
130
|
-
url.searchParams.set("crop", "center");
|
|
131
|
-
return url.href;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
// -------------------------------------------------------------------------
|
|
135
|
-
// Core optimization function
|
|
136
|
-
// Ported from deco-cx/apps website/components/Image.tsx
|
|
137
|
-
// -------------------------------------------------------------------------
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Builds an optimized image URL.
|
|
141
|
-
*
|
|
142
|
-
* For Deco-hosted images (decocache / S3 / decoims), strips the known prefix and
|
|
143
|
-
* routes through the Deco image CDN for edge resize + format conversion.
|
|
144
|
-
*
|
|
145
|
-
* For platform-specific images (VTEX, Shopify), rewrites the URL using
|
|
146
|
-
* the platform's native resize params — no CDN proxy needed.
|
|
147
|
-
*
|
|
148
|
-
* Data URIs are returned as-is.
|
|
149
|
-
*/
|
|
150
|
-
export function getOptimizedMediaUrl(opts: OptimizationOptions): string {
|
|
151
|
-
const { originalSrc, width, height, fit } = opts;
|
|
152
|
-
|
|
153
|
-
// Defensive: an upstream CMS payload occasionally has missing/null image
|
|
154
|
-
// fields. Crashing the entire React tree on `undefined.startsWith` would
|
|
155
|
-
// take down the whole page. Return an empty string so the resulting
|
|
156
|
-
// `<img>` is rendered with no src — the browser shows the broken-image
|
|
157
|
-
// placeholder and SSR completes cleanly.
|
|
158
|
-
if (typeof originalSrc !== "string" || originalSrc.length === 0) {
|
|
159
|
-
if (typeof process !== "undefined" && process.env?.NODE_ENV !== "production") {
|
|
160
|
-
console.warn(
|
|
161
|
-
`[Image] getOptimizedMediaUrl called with empty/undefined src — rendering empty src instead of crashing.`,
|
|
162
|
-
);
|
|
163
|
-
}
|
|
164
|
-
return "";
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
if (originalSrc.startsWith("data:")) {
|
|
168
|
-
return originalSrc;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
if (/(vteximg\.com\.br|vtexassets\.com|myvtex\.com)\/arquivos\/ids\/\d+/.test(originalSrc)) {
|
|
172
|
-
return optimizeVTEX(originalSrc, width, height);
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
if (originalSrc.startsWith("https://cdn.shopify.com")) {
|
|
176
|
-
return optimizeShopify(originalSrc, width, height);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
let imageSource = originalSrc.replace(DECO_ASSET_PREFIX_RE, "").split("?")[0];
|
|
180
|
-
if (CLOUDFLARE_CDNS.has(imageCdnDomain)) imageSource = imageSource.replace(DECOIMS_PREFIX_RE, "");
|
|
181
|
-
|
|
182
|
-
// Already on the image CDN — strip the host so we don't proxy through ourselves.
|
|
183
|
-
const cdnPrefix = `https://${imageCdnDomain}/`;
|
|
184
|
-
if (imageSource.startsWith(cdnPrefix)) {
|
|
185
|
-
imageSource = imageSource.slice(cdnPrefix.length);
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
const params = new URLSearchParams();
|
|
189
|
-
params.set("fit", fit);
|
|
190
|
-
params.set("width", `${width}`);
|
|
191
|
-
if (height) params.set("height", `${height}`);
|
|
192
|
-
if (imageQuality) params.set("quality", imageQuality);
|
|
193
|
-
|
|
194
|
-
return `https://${imageCdnDomain}/image?${params}&src=${imageSource}`;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* Generates a srcset string with responsive multipliers.
|
|
199
|
-
*/
|
|
200
|
-
export function getSrcSet(
|
|
201
|
-
originalSrc: string,
|
|
202
|
-
width: number,
|
|
203
|
-
height?: number,
|
|
204
|
-
fit?: FitOptions,
|
|
205
|
-
factors: number[] = FACTORS,
|
|
206
|
-
): string | undefined {
|
|
207
|
-
if (typeof originalSrc !== "string" || originalSrc.length === 0) {
|
|
208
|
-
return undefined;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
const entries: string[] = [];
|
|
212
|
-
|
|
213
|
-
for (const factor of factors) {
|
|
214
|
-
const w = Math.trunc(factor * width);
|
|
215
|
-
const h = height ? Math.trunc(factor * height) : undefined;
|
|
216
|
-
|
|
217
|
-
const src = getOptimizedMediaUrl({
|
|
218
|
-
originalSrc,
|
|
219
|
-
width: w,
|
|
220
|
-
height: h,
|
|
221
|
-
fit: fit ?? "cover",
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
if (src) {
|
|
225
|
-
entries.push(`${src} ${w}w`);
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
return entries.length > 0 ? entries.join(", ") : undefined;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
// -------------------------------------------------------------------------
|
|
233
|
-
// Image component
|
|
234
|
-
// -------------------------------------------------------------------------
|
|
235
|
-
|
|
236
|
-
export interface ImageProps
|
|
237
|
-
extends Omit<ImgHTMLAttributes<HTMLImageElement>, "src" | "width" | "height"> {
|
|
238
|
-
src: string;
|
|
239
|
-
/** @description Improves Web Vitals (CLS/LCP) */
|
|
240
|
-
width: number;
|
|
241
|
-
/** @description Improves Web Vitals (CLS/LCP) */
|
|
242
|
-
height?: number;
|
|
243
|
-
/** @description Object-fit */
|
|
244
|
-
fit?: FitOptions;
|
|
245
|
-
/**
|
|
246
|
-
* @description Web Vitals (LCP). Injects a `<link rel="preload">` tag
|
|
247
|
-
* alongside the `<img>`, sets `fetchPriority="high"` and `loading="eager"`.
|
|
248
|
-
* Use once per page for the LCP image.
|
|
249
|
-
*/
|
|
250
|
-
preload?: boolean;
|
|
251
|
-
/** @description Media query for responsive preloading (e.g. "(min-width: 768px)") */
|
|
252
|
-
media?: string;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
export const Image = forwardRef<HTMLImageElement, ImageProps>(function Image(
|
|
256
|
-
{
|
|
257
|
-
src,
|
|
258
|
-
width,
|
|
259
|
-
height,
|
|
260
|
-
fit = "cover",
|
|
261
|
-
preload,
|
|
262
|
-
media,
|
|
263
|
-
loading,
|
|
264
|
-
decoding,
|
|
265
|
-
srcSet: srcSetProp,
|
|
266
|
-
sizes,
|
|
267
|
-
fetchPriority,
|
|
268
|
-
...rest
|
|
269
|
-
},
|
|
270
|
-
ref,
|
|
271
|
-
) {
|
|
272
|
-
if (!height && typeof process !== "undefined") {
|
|
273
|
-
console.warn(`Missing height. This image will NOT be optimized: ${src}`);
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
const optimizedSrc = getOptimizedMediaUrl({
|
|
277
|
-
originalSrc: src,
|
|
278
|
-
width,
|
|
279
|
-
height,
|
|
280
|
-
fit,
|
|
281
|
-
});
|
|
282
|
-
const srcSet = srcSetProp ?? getSrcSet(src, width, height, fit);
|
|
283
|
-
const resolvedSizes = srcSet ? (sizes ?? "(max-width: 768px) 100vw, 50vw") : undefined;
|
|
284
|
-
|
|
285
|
-
return (
|
|
286
|
-
<>
|
|
287
|
-
{preload && (
|
|
288
|
-
<link
|
|
289
|
-
as="image"
|
|
290
|
-
rel="preload"
|
|
291
|
-
href={optimizedSrc}
|
|
292
|
-
imageSrcSet={srcSet}
|
|
293
|
-
imageSizes={resolvedSizes}
|
|
294
|
-
fetchPriority={fetchPriority ?? "high"}
|
|
295
|
-
media={media}
|
|
296
|
-
/>
|
|
297
|
-
)}
|
|
298
|
-
<img
|
|
299
|
-
{...rest}
|
|
300
|
-
src={optimizedSrc}
|
|
301
|
-
srcSet={srcSet}
|
|
302
|
-
sizes={resolvedSizes}
|
|
303
|
-
width={width}
|
|
304
|
-
height={height}
|
|
305
|
-
loading={loading ?? (preload ? "eager" : "lazy")}
|
|
306
|
-
decoding={decoding ?? "async"}
|
|
307
|
-
fetchPriority={preload ? "high" : fetchPriority}
|
|
308
|
-
ref={ref}
|
|
309
|
-
/>
|
|
310
|
-
</>
|
|
311
|
-
);
|
|
312
|
-
});
|
|
313
|
-
|
|
314
|
-
export default Image;
|
package/src/hooks/JsonLd.tsx
DELETED
|
@@ -1,353 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* SEO JSON-LD structured data components.
|
|
3
|
-
*
|
|
4
|
-
* Generates JSON-LD script tags for Product (PDP), ProductList (PLP),
|
|
5
|
-
* and BreadcrumbList schemas. Compatible with Google's Rich Results
|
|
6
|
-
* requirements.
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* ```tsx
|
|
10
|
-
* import { ProductJsonLd, PLPJsonLd, BreadcrumbJsonLd } from "@decocms/blocks/hooks";
|
|
11
|
-
*
|
|
12
|
-
* // In a PDP route
|
|
13
|
-
* <ProductJsonLd product={product} />
|
|
14
|
-
*
|
|
15
|
-
* // In a PLP route
|
|
16
|
-
* <PLPJsonLd page={productListingPage} />
|
|
17
|
-
*
|
|
18
|
-
* // Anywhere with breadcrumbs
|
|
19
|
-
* <BreadcrumbJsonLd breadcrumb={breadcrumbList} />
|
|
20
|
-
* ```
|
|
21
|
-
*
|
|
22
|
-
* Type note: originally these components imported `Product`,
|
|
23
|
-
* `ProductListingPage`, `BreadcrumbList`, etc. from apps-start's
|
|
24
|
-
* commerce types module (now `@decocms/apps-commerce/types`).
|
|
25
|
-
* `@decocms/blocks` must not depend on any `apps-*` package (one-way
|
|
26
|
-
* dependency rule: `apps-*` depends on `blocks`, never the reverse), so
|
|
27
|
-
* that import can't carry over as-is. Each function here only reads a
|
|
28
|
-
* small, flat subset of the full schema.org Product/Offer/ListItem
|
|
29
|
-
* graph (the original code already didn't fully trust the nominal
|
|
30
|
-
* `Product.offers` type either — see the `as Offer[] | AggregateOffer`
|
|
31
|
-
* casts it used). The types below are local, minimal, structural
|
|
32
|
-
* equivalents of just that subset: any commerce `Product` /
|
|
33
|
-
* `ProductListingPage` / `BreadcrumbList` value is a structural
|
|
34
|
-
* superset and can be passed in directly without a cast.
|
|
35
|
-
*/
|
|
36
|
-
|
|
37
|
-
// -------------------------------------------------------------------------
|
|
38
|
-
// Minimal structural types (see file header for why these aren't imported
|
|
39
|
-
// from @decocms/apps-commerce/types)
|
|
40
|
-
// -------------------------------------------------------------------------
|
|
41
|
-
|
|
42
|
-
interface JsonLdOffer {
|
|
43
|
-
price?: number;
|
|
44
|
-
priceCurrency?: string;
|
|
45
|
-
availability?: string;
|
|
46
|
-
seller?: string;
|
|
47
|
-
priceValidUntil?: string;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
interface JsonLdAggregateOffer {
|
|
51
|
-
"@type"?: string;
|
|
52
|
-
lowPrice?: number;
|
|
53
|
-
priceCurrency?: string;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
interface JsonLdPriceSpecification {
|
|
57
|
-
priceType?: string;
|
|
58
|
-
price?: number;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
interface JsonLdAggregateRating {
|
|
62
|
-
ratingValue?: number;
|
|
63
|
-
reviewCount?: number;
|
|
64
|
-
ratingCount?: number;
|
|
65
|
-
bestRating?: number;
|
|
66
|
-
worstRating?: number;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
interface JsonLdImage {
|
|
70
|
-
url?: string;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
interface JsonLdBrand {
|
|
74
|
-
name?: string;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export interface JsonLdProduct {
|
|
78
|
-
name?: string;
|
|
79
|
-
description?: string;
|
|
80
|
-
url?: string;
|
|
81
|
-
sku?: string;
|
|
82
|
-
productID?: string;
|
|
83
|
-
gtin?: string;
|
|
84
|
-
brand?: JsonLdBrand | null;
|
|
85
|
-
image?: JsonLdImage[] | null;
|
|
86
|
-
offers?: JsonLdOffer[] | JsonLdAggregateOffer;
|
|
87
|
-
aggregateRating?: JsonLdAggregateRating;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
interface JsonLdSeo {
|
|
91
|
-
canonical?: string;
|
|
92
|
-
title?: string;
|
|
93
|
-
description?: string;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export interface JsonLdProductListingPage {
|
|
97
|
-
products?: JsonLdProduct[];
|
|
98
|
-
seo?: JsonLdSeo | null;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
interface JsonLdListItem {
|
|
102
|
-
position?: number;
|
|
103
|
-
name?: string;
|
|
104
|
-
item?: string;
|
|
105
|
-
url?: string;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
export interface JsonLdBreadcrumbList {
|
|
109
|
-
itemListElement?: JsonLdListItem[];
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// -------------------------------------------------------------------------
|
|
113
|
-
// JSON-LD script renderer
|
|
114
|
-
// -------------------------------------------------------------------------
|
|
115
|
-
|
|
116
|
-
function JsonLdScript({ data }: { data: unknown }) {
|
|
117
|
-
return (
|
|
118
|
-
<script type="application/ld+json" dangerouslySetInnerHTML={{ __html: JSON.stringify(data) }} />
|
|
119
|
-
);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
// -------------------------------------------------------------------------
|
|
123
|
-
// Product (PDP)
|
|
124
|
-
// -------------------------------------------------------------------------
|
|
125
|
-
|
|
126
|
-
export interface ProductJsonLdProps {
|
|
127
|
-
product: JsonLdProduct;
|
|
128
|
-
/** Override the canonical URL. Defaults to product.url. */
|
|
129
|
-
url?: string;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
function getBestOffer(offers: JsonLdOffer[] | JsonLdAggregateOffer | undefined): {
|
|
133
|
-
price?: number;
|
|
134
|
-
priceCurrency?: string;
|
|
135
|
-
availability?: string;
|
|
136
|
-
seller?: string;
|
|
137
|
-
priceValidUntil?: string;
|
|
138
|
-
} {
|
|
139
|
-
if (!offers) return {};
|
|
140
|
-
|
|
141
|
-
if ("@type" in offers && offers["@type"] === "AggregateOffer") {
|
|
142
|
-
const agg = offers as JsonLdAggregateOffer;
|
|
143
|
-
return {
|
|
144
|
-
price: agg.lowPrice,
|
|
145
|
-
priceCurrency: agg.priceCurrency,
|
|
146
|
-
};
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
if (Array.isArray(offers) && offers.length > 0) {
|
|
150
|
-
const best = offers.reduce((a, b) => {
|
|
151
|
-
const ap = a.price ?? Infinity;
|
|
152
|
-
const bp = b.price ?? Infinity;
|
|
153
|
-
return ap <= bp ? a : b;
|
|
154
|
-
});
|
|
155
|
-
return {
|
|
156
|
-
price: best.price,
|
|
157
|
-
priceCurrency: best.priceCurrency,
|
|
158
|
-
availability: best.availability,
|
|
159
|
-
seller: best.seller,
|
|
160
|
-
priceValidUntil: best.priceValidUntil,
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
return {};
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
function _getListPrice(priceSpec: JsonLdPriceSpecification[] | undefined): number | undefined {
|
|
168
|
-
if (!priceSpec) return undefined;
|
|
169
|
-
const list = priceSpec.find(
|
|
170
|
-
(p) =>
|
|
171
|
-
p.priceType === "https://schema.org/ListPrice" || p.priceType === "https://schema.org/SRP",
|
|
172
|
-
);
|
|
173
|
-
return list?.price;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
export function ProductJsonLd({ product, url }: ProductJsonLdProps) {
|
|
177
|
-
const offer = getBestOffer(product.offers);
|
|
178
|
-
const images = product.image?.map((img) => img.url).filter(Boolean) ?? [];
|
|
179
|
-
const rating = product.aggregateRating;
|
|
180
|
-
|
|
181
|
-
const data: Record<string, unknown> = {
|
|
182
|
-
"@context": "https://schema.org",
|
|
183
|
-
"@type": "Product",
|
|
184
|
-
name: product.name,
|
|
185
|
-
description: product.description,
|
|
186
|
-
image: images.length === 1 ? images[0] : images,
|
|
187
|
-
url: url ?? product.url,
|
|
188
|
-
sku: product.sku,
|
|
189
|
-
productID: product.productID,
|
|
190
|
-
brand: product.brand ? { "@type": "Brand", name: product.brand.name } : undefined,
|
|
191
|
-
gtin: product.gtin,
|
|
192
|
-
};
|
|
193
|
-
|
|
194
|
-
if (offer.price != null) {
|
|
195
|
-
data.offers = {
|
|
196
|
-
"@type": "Offer",
|
|
197
|
-
price: offer.price,
|
|
198
|
-
priceCurrency: offer.priceCurrency ?? "BRL",
|
|
199
|
-
availability: offer.availability ?? "https://schema.org/InStock",
|
|
200
|
-
seller: offer.seller ? { "@type": "Organization", name: offer.seller } : undefined,
|
|
201
|
-
priceValidUntil: offer.priceValidUntil,
|
|
202
|
-
url: url ?? product.url,
|
|
203
|
-
};
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
if (rating?.ratingValue) {
|
|
207
|
-
data.aggregateRating = {
|
|
208
|
-
"@type": "AggregateRating",
|
|
209
|
-
ratingValue: rating.ratingValue,
|
|
210
|
-
reviewCount: rating.reviewCount ?? rating.ratingCount ?? 0,
|
|
211
|
-
bestRating: rating.bestRating ?? 5,
|
|
212
|
-
worstRating: rating.worstRating ?? 1,
|
|
213
|
-
};
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
return <JsonLdScript data={data} />;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
// -------------------------------------------------------------------------
|
|
220
|
-
// Product Listing Page (PLP)
|
|
221
|
-
// -------------------------------------------------------------------------
|
|
222
|
-
|
|
223
|
-
export interface PLPJsonLdProps {
|
|
224
|
-
page: JsonLdProductListingPage;
|
|
225
|
-
/** Override the canonical URL. */
|
|
226
|
-
url?: string;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
export function PLPJsonLd({ page, url }: PLPJsonLdProps) {
|
|
230
|
-
const items = (page.products ?? []).map((product, index) => {
|
|
231
|
-
const offer = getBestOffer(product.offers);
|
|
232
|
-
return {
|
|
233
|
-
"@type": "ListItem" as const,
|
|
234
|
-
position: index + 1,
|
|
235
|
-
item: {
|
|
236
|
-
"@type": "Product" as const,
|
|
237
|
-
name: product.name,
|
|
238
|
-
url: product.url,
|
|
239
|
-
image: product.image?.[0]?.url,
|
|
240
|
-
offers:
|
|
241
|
-
offer.price != null
|
|
242
|
-
? {
|
|
243
|
-
"@type": "Offer" as const,
|
|
244
|
-
price: offer.price,
|
|
245
|
-
priceCurrency: offer.priceCurrency ?? "BRL",
|
|
246
|
-
availability: offer.availability ?? "https://schema.org/InStock",
|
|
247
|
-
}
|
|
248
|
-
: undefined,
|
|
249
|
-
},
|
|
250
|
-
};
|
|
251
|
-
});
|
|
252
|
-
|
|
253
|
-
const data = {
|
|
254
|
-
"@context": "https://schema.org",
|
|
255
|
-
"@type": "ItemList",
|
|
256
|
-
url: url ?? page.seo?.canonical,
|
|
257
|
-
name: page.seo?.title,
|
|
258
|
-
description: page.seo?.description,
|
|
259
|
-
numberOfItems: page.products?.length ?? 0,
|
|
260
|
-
itemListElement: items,
|
|
261
|
-
};
|
|
262
|
-
|
|
263
|
-
return <JsonLdScript data={data} />;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
// -------------------------------------------------------------------------
|
|
267
|
-
// Breadcrumb
|
|
268
|
-
// -------------------------------------------------------------------------
|
|
269
|
-
|
|
270
|
-
export interface BreadcrumbJsonLdProps {
|
|
271
|
-
breadcrumb: JsonLdBreadcrumbList;
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
export function BreadcrumbJsonLd({ breadcrumb }: BreadcrumbJsonLdProps) {
|
|
275
|
-
const items = (breadcrumb.itemListElement ?? []).map((item, index) => {
|
|
276
|
-
return {
|
|
277
|
-
"@type": "ListItem" as const,
|
|
278
|
-
position: item.position ?? index + 1,
|
|
279
|
-
name: item.name,
|
|
280
|
-
item: item.item ?? item.url,
|
|
281
|
-
};
|
|
282
|
-
});
|
|
283
|
-
|
|
284
|
-
const data = {
|
|
285
|
-
"@context": "https://schema.org",
|
|
286
|
-
"@type": "BreadcrumbList",
|
|
287
|
-
itemListElement: items,
|
|
288
|
-
numberOfItems: items.length,
|
|
289
|
-
};
|
|
290
|
-
|
|
291
|
-
return <JsonLdScript data={data} />;
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
// -------------------------------------------------------------------------
|
|
295
|
-
// Generic SEO Meta
|
|
296
|
-
// -------------------------------------------------------------------------
|
|
297
|
-
|
|
298
|
-
export interface SeoMetaProps {
|
|
299
|
-
title?: string;
|
|
300
|
-
description?: string;
|
|
301
|
-
canonical?: string;
|
|
302
|
-
image?: string;
|
|
303
|
-
noIndex?: boolean;
|
|
304
|
-
type?: "website" | "article" | "product";
|
|
305
|
-
siteName?: string;
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
/**
|
|
309
|
-
* Generates Open Graph and Twitter Card meta tags.
|
|
310
|
-
*
|
|
311
|
-
* Use this in combination with TanStack Router's `meta()` route option,
|
|
312
|
-
* or render directly in the component tree (tags will be hoisted to <head>
|
|
313
|
-
* by React's built-in behavior with TanStack Start).
|
|
314
|
-
*/
|
|
315
|
-
export function seoMetaTags(props: SeoMetaProps): Array<Record<string, string>> {
|
|
316
|
-
const tags: Array<Record<string, string>> = [];
|
|
317
|
-
|
|
318
|
-
if (props.title) {
|
|
319
|
-
tags.push({ title: props.title });
|
|
320
|
-
tags.push({ property: "og:title", content: props.title });
|
|
321
|
-
tags.push({ name: "twitter:title", content: props.title });
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
if (props.description) {
|
|
325
|
-
tags.push({ name: "description", content: props.description });
|
|
326
|
-
tags.push({ property: "og:description", content: props.description });
|
|
327
|
-
tags.push({ name: "twitter:description", content: props.description });
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
if (props.canonical) {
|
|
331
|
-
tags.push({ property: "og:url", content: props.canonical });
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
if (props.image) {
|
|
335
|
-
tags.push({ property: "og:image", content: props.image });
|
|
336
|
-
tags.push({ name: "twitter:image", content: props.image });
|
|
337
|
-
tags.push({ name: "twitter:card", content: "summary_large_image" });
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
if (props.type) {
|
|
341
|
-
tags.push({ property: "og:type", content: props.type });
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
if (props.siteName) {
|
|
345
|
-
tags.push({ property: "og:site_name", content: props.siteName });
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
if (props.noIndex) {
|
|
349
|
-
tags.push({ name: "robots", content: "noindex, nofollow" });
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
return tags;
|
|
353
|
-
}
|