@decocms/blocks 7.77.8 → 7.77.10

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@decocms/blocks",
3
- "version": "7.77.8",
3
+ "version": "7.77.10",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=24"
@@ -861,6 +861,196 @@ describe("resolvePageSeoBlock — per-section ignoreStructuredData drives the fe
861
861
  });
862
862
  });
863
863
 
864
+ // ---------------------------------------------------------------------------
865
+ // A framework SEO section inside `page.sections`
866
+ // ---------------------------------------------------------------------------
867
+ //
868
+ // On Deco for Fresh `commerce/sections/Seo/SeoPDPV2.tsx` was a section like
869
+ // any other, so a migrated decofile may carry it in `sections[]` with no
870
+ // `page.seo` at all (the Oficina Reserva PDP: `sections[0]`). It was in
871
+ // SKIP_RESOLVE_TYPES, so the list dropped it and the PDP shipped with no
872
+ // canonical, no `og:url`, the site's default image and `og:type=website`.
873
+ describe("resolveDecoPage — SEO section in page.sections", () => {
874
+ const KEY = "site/loaders/__test/pdpLoader";
875
+ const HUMAN_UA =
876
+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120 Safari/537.36";
877
+ const seoSec = {
878
+ __resolveType: "commerce/sections/Seo/SeoPDPV2.tsx",
879
+ ignoreStructuredData: true,
880
+ optimizeImageForVTEX: true,
881
+ jsonLD: { __resolveType: KEY },
882
+ };
883
+ const bodySec = { __resolveType: "site/sections/ProductDetails.tsx" };
884
+ const ctx = (): MatcherContext => ({
885
+ userAgent: HUMAN_UA,
886
+ url: "https://store.com/camisa/p",
887
+ path: "/camisa/p",
888
+ });
889
+
890
+ beforeEach(() => {
891
+ clearCommerceLoaders();
892
+ setAsyncRenderingConfig({ botAwareSeo: false });
893
+ (getSection as ReturnType<typeof vi.fn>).mockImplementation((key: string) =>
894
+ key.startsWith("site/") ? { default: () => null } : undefined,
895
+ );
896
+ });
897
+ afterEach(() => {
898
+ clearCommerceLoaders();
899
+ (getSection as ReturnType<typeof vi.fn>).mockReset();
900
+ (findPageByPath as ReturnType<typeof vi.fn>).mockReset();
901
+ });
902
+
903
+ it("becomes the page's SEO block when the page has no `seo`, keeping its slot in the list", async () => {
904
+ let calls = 0;
905
+ registerCommerceLoader(KEY, async () => {
906
+ calls++;
907
+ return {
908
+ "@type": "ProductDetailsPage",
909
+ product: { name: "Camisa" },
910
+ seo: { canonical: "https://store.com/camisa/p" },
911
+ };
912
+ });
913
+ (findPageByPath as ReturnType<typeof vi.fn>).mockReturnValue({
914
+ page: { name: "PDP", sections: [seoSec, bodySec] },
915
+ params: {},
916
+ blockKey: "pdp",
917
+ });
918
+
919
+ const result = await resolveDecoPage("/camisa/p", ctx());
920
+
921
+ expect(result?.seoSection?.component).toBe("commerce/sections/Seo/SeoPDPV2.tsx");
922
+ // The toggle does not skip the fetch for a section in the list — on Fresh
923
+ // it only governs the JSON-LD; canonical/image still come from the data.
924
+ expect(calls).toBe(1);
925
+ expect(result?.seoSection?.props.jsonLD).toMatchObject({ "@type": "ProductDetailsPage" });
926
+ expect(result?.seoSection?.props.optimizeImageForVTEX).toBe(true);
927
+ // It renders nothing, and the body keeps its original index.
928
+ expect(result?.resolvedSections.map((s) => s.component)).toEqual([
929
+ "site/sections/ProductDetails.tsx",
930
+ ]);
931
+ expect(result?.resolvedSections[0].index).toBe(1);
932
+ });
933
+
934
+ it("is found under a Lazy wrapper too", async () => {
935
+ registerCommerceLoader(KEY, async () => ({ "@type": "ProductDetailsPage", product: {} }));
936
+ (findPageByPath as ReturnType<typeof vi.fn>).mockReturnValue({
937
+ page: { name: "PDP", sections: [{ __resolveType: WELL_KNOWN_TYPES.LAZY, section: seoSec }] },
938
+ params: {},
939
+ blockKey: "pdp",
940
+ });
941
+
942
+ const result = await resolveDecoPage("/camisa/p", ctx());
943
+ expect(result?.seoSection?.component).toBe("commerce/sections/Seo/SeoPDPV2.tsx");
944
+ });
945
+
946
+ it("`page.seo` stays the primary source when both are present", async () => {
947
+ registerCommerceLoader(KEY, async () => ({ "@type": "ProductDetailsPage", product: {} }));
948
+ (findPageByPath as ReturnType<typeof vi.fn>).mockReturnValue({
949
+ page: {
950
+ name: "PDP",
951
+ seo: { __resolveType: "website/sections/Seo/SeoV2.tsx", title: "From page.seo" },
952
+ sections: [seoSec, bodySec],
953
+ },
954
+ params: {},
955
+ blockKey: "pdp",
956
+ });
957
+
958
+ const result = await resolveDecoPage("/camisa/p", ctx());
959
+ expect(result?.seoSection?.component).toBe("website/sections/Seo/SeoV2.tsx");
960
+ expect(result?.seoSection?.props.title).toBe("From page.seo");
961
+ expect(result?.resolvedSections.map((s) => s.component)).toEqual([
962
+ "site/sections/ProductDetails.tsx",
963
+ ]);
964
+ });
965
+
966
+ it("the site-wide botAwareSeo flag still skips the fetch for humans", async () => {
967
+ setAsyncRenderingConfig({ botAwareSeo: true });
968
+ let calls = 0;
969
+ registerCommerceLoader(KEY, async () => {
970
+ calls++;
971
+ return { "@type": "ProductDetailsPage", product: {} };
972
+ });
973
+ (findPageByPath as ReturnType<typeof vi.fn>).mockReturnValue({
974
+ page: { name: "PDP", sections: [seoSec] },
975
+ params: {},
976
+ blockKey: "pdp",
977
+ });
978
+
979
+ const result = await resolveDecoPage("/camisa/p", ctx());
980
+ expect(calls).toBe(0);
981
+ expect(result?.seoSection?.props).not.toHaveProperty("jsonLD");
982
+ });
983
+ });
984
+
985
+ describe("extractSeoFromProps — SeoPDPV2 head fields", () => {
986
+ const pdpJsonLD = (image = "https://acme.vteximg.com.br/arquivos/ids/123-55-55/foto.jpg") => ({
987
+ "@type": "ProductDetailsPage",
988
+ breadcrumbList: { "@type": "BreadcrumbList", itemListElement: [] },
989
+ product: { "@type": "Product", name: "Camisa", image: [{ url: image }] },
990
+ seo: { title: "Camisa", description: "Linho", canonical: "https://x.com/camisa/p" },
991
+ });
992
+
993
+ it('emits og:type product (deco-cx renders <Seo type="product">)', () => {
994
+ const seo = extractSeoFromProps({ jsonLD: pdpJsonLD() });
995
+ expect(seo.type).toBe("product");
996
+ expect(seo.canonical).toBe("https://x.com/camisa/p");
997
+ });
998
+
999
+ it("optimizeImageForVTEX asks VTEX for the 1200×1200 crop", () => {
1000
+ const seo = extractSeoFromProps({ jsonLD: pdpJsonLD(), optimizeImageForVTEX: true });
1001
+ expect(seo.image).toBe("https://acme.vteximg.com.br/arquivos/ids/123-1200-1200/foto.jpg");
1002
+ });
1003
+
1004
+ it("without optimizeImageForVTEX the image is the product's first, as is", () => {
1005
+ const seo = extractSeoFromProps({ jsonLD: pdpJsonLD() });
1006
+ expect(seo.image).toBe("https://acme.vteximg.com.br/arquivos/ids/123-55-55/foto.jpg");
1007
+ });
1008
+
1009
+ it("optimizeImageForVTEX leaves a non-VTEX image alone", () => {
1010
+ const seo = extractSeoFromProps({
1011
+ jsonLD: pdpJsonLD("https://cdn.example.com/foto.jpg"),
1012
+ optimizeImageForVTEX: true,
1013
+ });
1014
+ expect(seo.image).toBe("https://cdn.example.com/foto.jpg");
1015
+ });
1016
+
1017
+ it("structuredDataControl 'disable for all' emits no JSON-LD even for bots, but keeps the metadata", () => {
1018
+ const seo = extractSeoFromProps(
1019
+ { jsonLD: pdpJsonLD(), ignoreStructuredData: true, structuredDataControl: "disable for all" },
1020
+ { isEager: true },
1021
+ );
1022
+ expect(seo.jsonLDs).toBeUndefined();
1023
+ expect(seo.canonical).toBe("https://x.com/camisa/p");
1024
+ expect(seo.type).toBe("product");
1025
+ });
1026
+
1027
+ it("structuredDataControl 'always include' wins over the deprecated toggle", () => {
1028
+ const seo = extractSeoFromProps({
1029
+ jsonLD: pdpJsonLD(),
1030
+ ignoreStructuredData: true,
1031
+ structuredDataControl: "always include",
1032
+ });
1033
+ expect(seo.jsonLDs).toHaveLength(1);
1034
+ });
1035
+
1036
+ it("PLP: configJsonLD.structuredDataControl 'disable for all' suppresses the ItemList for bots", () => {
1037
+ const seo = extractSeoFromProps(
1038
+ {
1039
+ jsonLD: {
1040
+ "@type": "ProductListingPage",
1041
+ breadcrumb: { "@type": "BreadcrumbList", itemListElement: [] },
1042
+ products: [{ "@type": "Product", name: "P1", url: "https://x.com/p1/p" }],
1043
+ seo: { title: "PLP" },
1044
+ },
1045
+ configJsonLD: { structuredDataControl: "disable for all" },
1046
+ },
1047
+ { isEager: true },
1048
+ );
1049
+ expect(seo.jsonLDs).toBeUndefined();
1050
+ expect(seo.title).toBe("PLP");
1051
+ });
1052
+ });
1053
+
864
1054
  describe("extractSeoFromProps — commerce jsonLD structured data", () => {
865
1055
  const plp = (overrides: Record<string, unknown> = {}) => ({
866
1056
  "@type": "ProductListingPage",
@@ -575,7 +575,26 @@ interface ResolveContext {
575
575
  // Configuration
576
576
  // ---------------------------------------------------------------------------
577
577
 
578
+ /**
579
+ * SEO sections of the deco-cx `website`/`commerce` apps. They have no
580
+ * component here: their resolved props become the page's `<head>` through
581
+ * `resolvePageSeoBlock` + `extractSeoFromProps`. On Deco for Fresh they were
582
+ * ordinary sections, so a decofile may carry one inside `page.sections` as
583
+ * well as under `page.seo` — `resolveDecoPage` picks it out of the list;
584
+ * anywhere else in the tree they are skipped like the other framework types.
585
+ */
586
+ const FRAMEWORK_SEO_TYPES = new Set([
587
+ "commerce/sections/Seo/SeoPDP.tsx",
588
+ "commerce/sections/Seo/SeoPDPV2.tsx",
589
+ "commerce/sections/Seo/SeoPLP.tsx",
590
+ "commerce/sections/Seo/SeoPLPV2.tsx",
591
+ "website/sections/Seo/Seo.tsx",
592
+ "website/sections/Seo/SeoV2.tsx",
593
+ "deco-sites/std/sections/SEO.tsx",
594
+ ]);
595
+
578
596
  const SKIP_RESOLVE_TYPES = new Set([
597
+ ...FRAMEWORK_SEO_TYPES,
579
598
  "Deco",
580
599
  "htmx/sections/htmx.tsx",
581
600
  "website/sections/Analytics/Analytics.tsx",
@@ -585,13 +604,6 @@ const SKIP_RESOLVE_TYPES = new Set([
585
604
  "website/loaders/pages.ts",
586
605
  "website/loaders/redirects.ts",
587
606
  "website/loaders/fonts/googleFonts.ts",
588
- "commerce/sections/Seo/SeoPDP.tsx",
589
- "commerce/sections/Seo/SeoPDPV2.tsx",
590
- "commerce/sections/Seo/SeoPLP.tsx",
591
- "commerce/sections/Seo/SeoPLPV2.tsx",
592
- "website/sections/Seo/Seo.tsx",
593
- "website/sections/Seo/SeoV2.tsx",
594
- "deco-sites/std/sections/SEO.tsx",
595
607
  ]);
596
608
 
597
609
  /** Add a __resolveType that should be skipped during resolution. */
@@ -1356,8 +1368,21 @@ async function resolveRawSection(
1356
1368
  export async function resolvePageSeoBlock(
1357
1369
  seoBlock: Record<string, unknown> | undefined,
1358
1370
  rctx: ResolveContext,
1371
+ opts?: {
1372
+ /**
1373
+ * Treat the section's `ignoreStructuredData` toggle as a fetch-skip for
1374
+ * humans (the `page.seo` behaviour below). Off for a SEO section found in
1375
+ * `page.sections`: on Deco for Fresh the toggle only decides whether the
1376
+ * JSON-LD is emitted — `deriveCommerceSeoFromJsonLD` honours it there —
1377
+ * while canonical, image and robots still come from the resolved data,
1378
+ * and the loader behind `jsonLD` is the body's own, memoized per request.
1379
+ * The site-wide `botAwareSeo` flag applies either way.
1380
+ */
1381
+ toggleSkipsFetch?: boolean;
1382
+ },
1359
1383
  ): Promise<ResolvedSection | null> {
1360
1384
  if (!seoBlock || typeof seoBlock !== "object") return null;
1385
+ const toggleSkipsFetch = opts?.toggleSkipsFetch ?? true;
1361
1386
 
1362
1387
  // Bot-aware SEO: crawlers get the SEO block fully resolved (e.g. a
1363
1388
  // ProductListingPage for JSON-LD ItemList) — that content exists for indexing;
@@ -1436,7 +1461,8 @@ export async function resolvePageSeoBlock(
1436
1461
  // Skip only when the editor turned on `ignoreStructuredData` for this
1437
1462
  // section, or the site-wide `botAwareSeo` shortcut is enabled.
1438
1463
  const skipCommerceForHuman =
1439
- !isEagerReq && (globalBotAware || sectionIgnoresStructuredData(rawProps));
1464
+ !isEagerReq &&
1465
+ (globalBotAware || (toggleSkipsFetch && sectionIgnoresStructuredData(rawProps)));
1440
1466
  const propsToResolve = skipCommerceForHuman ? stripCommerceLoaderProps(rawProps) : rawProps;
1441
1467
  try {
1442
1468
  const resolvedProps = await resolveProps(propsToResolve, rctx);
@@ -1517,6 +1543,46 @@ function sectionIgnoresStructuredData(props: Record<string, unknown>): boolean {
1517
1543
  return configJsonLD?.ignoreStructuredData === true;
1518
1544
  }
1519
1545
 
1546
+ type StructuredDataControl = "always include" | "disable for users" | "disable for all";
1547
+
1548
+ /**
1549
+ * When the section's structured data is emitted, as deco-cx
1550
+ * `commerce/utils/structuredData.ts` reads it: `structuredDataControl` (top
1551
+ * level on the PDP section, under `configJsonLD` on the PLP one) wins, and
1552
+ * the deprecated `ignoreStructuredData` toggle means "disable for users".
1553
+ */
1554
+ function structuredDataMode(props: Record<string, unknown>): StructuredDataControl {
1555
+ const configJsonLD = props.configJsonLD as { structuredDataControl?: unknown } | undefined;
1556
+ const control = props.structuredDataControl ?? configJsonLD?.structuredDataControl;
1557
+ if (
1558
+ control === "always include" ||
1559
+ control === "disable for users" ||
1560
+ control === "disable for all"
1561
+ ) {
1562
+ return control;
1563
+ }
1564
+ return sectionIgnoresStructuredData(props) ? "disable for users" : "always include";
1565
+ }
1566
+
1567
+ /**
1568
+ * VTEX image-server resize, by rewriting the `/arquivos/ids/<id>-<w>-<h>`
1569
+ * segment — the same rewrite as deco-cx `website/components/Image.tsx`
1570
+ * (`optimizeVTEX`) and `hooks/Image.tsx` here. Returns the source untouched
1571
+ * when it is not such a URL.
1572
+ */
1573
+ function optimizeVtexImage(originalSrc: string, width: number, height: number): string {
1574
+ try {
1575
+ const src = new URL(originalSrc);
1576
+ const [slash, arquivos, ids, rawId, ...rest] = src.pathname.split("/");
1577
+ if (arquivos !== "arquivos" || ids !== "ids" || !rawId) return originalSrc;
1578
+ const [trueId] = rawId.split("-");
1579
+ src.pathname = [slash, arquivos, ids, `${trueId}-${width}-${height}`, ...rest].join("/");
1580
+ return src.href;
1581
+ } catch {
1582
+ return originalSrc;
1583
+ }
1584
+ }
1585
+
1520
1586
  /**
1521
1587
  * Return a copy of `rawProps` with every top-level field that resolves to a
1522
1588
  * commerce loader removed. Applied to page SEO props for human (non-bot)
@@ -2172,8 +2238,17 @@ function deriveCommerceSeoFromJsonLD(
2172
2238
  type === "ProductListingPage" ? jsonLD.breadcrumb : jsonLD.breadcrumbList,
2173
2239
  );
2174
2240
  }
2175
- if (type === "ProductDetailsPage" && !seo.image) {
2176
- seo.image = jsonLD.product?.image?.[0]?.url;
2241
+ if (type === "ProductDetailsPage") {
2242
+ if (!seo.image) {
2243
+ const image = jsonLD.product?.image?.[0]?.url as string | undefined;
2244
+ // `optimizeImageForVTEX` on SeoPDPV2: the 1200×1200 crop deco-cx asks
2245
+ // VTEX's image server for (`website/components/Image.tsx` optimizeVTEX).
2246
+ seo.image =
2247
+ image && props.optimizeImageForVTEX === true ? optimizeVtexImage(image, 1200, 1200) : image;
2248
+ }
2249
+ // SeoPDPV2 renders `<Seo type="product">`: `og:type` product, and the
2250
+ // Twitter card becomes `summary_large_image`.
2251
+ if (!seo.type) seo.type = "product";
2177
2252
  }
2178
2253
 
2179
2254
  const isEmpty =
@@ -2189,8 +2264,8 @@ function deriveCommerceSeoFromJsonLD(
2189
2264
  // humans only; crawlers (and the `?__deco_ssr=1` audit override) still get it
2190
2265
  // so indexing/rich results are unaffected. An empty page contributes no
2191
2266
  // ItemList regardless.
2192
- const ignore = sectionIgnoresStructuredData(props);
2193
- if ((ignore && !isEager) || isEmpty) return;
2267
+ const mode = structuredDataMode(props);
2268
+ if (mode === "disable for all" || (mode === "disable for users" && !isEager) || isEmpty) return;
2194
2269
 
2195
2270
  seo.jsonLDs =
2196
2271
  type === "ProductListingPage"
@@ -2349,9 +2424,24 @@ async function resolveDecoPageImpl(
2349
2424
  const deferredSections: DeferredSection[] = [];
2350
2425
  let flatIndex = 0;
2351
2426
 
2427
+ // A framework SEO section placed in the list (Deco for Fresh rendered it as
2428
+ // a section emitting `<Head>`). It renders nothing here; it is resolved as
2429
+ // the page's SEO block below, after the body, so the commerce loader it
2430
+ // shares with the body (a named `PDP Loader`) is served from the memo.
2431
+ // It keeps its slot in `flatIndex`: deferred sections are addressed by
2432
+ // their position in the raw list (`reExtractRawProps`).
2433
+ let seoInSections: Record<string, unknown> | null = null;
2434
+
2352
2435
  for (const section of rawSections) {
2353
2436
  const currentFlatIndex = flatIndex;
2354
2437
 
2438
+ const finalKey = resolveFinalSectionKey(section, ctx);
2439
+ if (finalKey && FRAMEWORK_SEO_TYPES.has(finalKey)) {
2440
+ seoInSections ??= section as Record<string, unknown>;
2441
+ flatIndex++;
2442
+ continue;
2443
+ }
2444
+
2355
2445
  const shouldDefer =
2356
2446
  useAsync && shouldDeferSection(section, currentFlatIndex, currentAsyncConfig!, isBotReq, ctx);
2357
2447
 
@@ -2469,6 +2559,8 @@ async function resolveDecoPageImpl(
2469
2559
 
2470
2560
  // Resolve page-level SEO block (page.seo field) — always eager.
2471
2561
  // Runs after sections to benefit from memoized commerce loader results.
2562
+ // `page.seo` is the primary source; a SEO section in the list stands in
2563
+ // only when the page has none.
2472
2564
  let seoSection: ResolvedSection | null = null;
2473
2565
  if (page.seo) {
2474
2566
  try {
@@ -2476,6 +2568,12 @@ async function resolveDecoPageImpl(
2476
2568
  } catch (e) {
2477
2569
  onResolveError(e, "page.seo", "Page SEO block resolution");
2478
2570
  }
2571
+ } else if (seoInSections) {
2572
+ try {
2573
+ seoSection = await resolvePageSeoBlock(seoInSections, rctx, { toggleSkipsFetch: false });
2574
+ } catch (e) {
2575
+ onResolveError(e, "page.sections", "Page SEO section resolution");
2576
+ }
2479
2577
  }
2480
2578
 
2481
2579
  return {