Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions commerce/sections/Seo/SeoPDPV2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import {
import { ProductDetailsPage } from "../../types.ts";
import { canonicalFromBreadcrumblist } from "../../utils/canonical.ts";
import { AppContext } from "../../mod.ts";
import {
shouldIncludeStructuredData,
StructuredDataControl,
} from "../../utils/structuredData.ts";

export interface Props {
/** @title Data Source */
Expand All @@ -23,8 +27,15 @@ export interface Props {
/**
* @title Ignore Structured Data
* @description By default, Structured Data is sent to everyone. Use this to prevent Structured Data from being sent to your customers, it will still be sent to crawlers and bots. Be aware that some integrations may not work if Structured Data is not sent.
* @deprecated Use `structuredDataControl` instead.
*/
ignoreStructuredData?: boolean;
/**
* @title Structured Data Control
* @description Choose when to include JSON-LD structured data. Default sends to everyone. "disable for users" shows only to bots/crawlers. "disable for all" removes completely. Note: some third-party integrations may require structured data to function properly.
* @default "always include"
*/
structuredDataControl?: StructuredDataControl;
}

/** @title Product details */
Expand All @@ -41,6 +52,7 @@ export function loader(_props: Props, _req: Request, ctx: AppContext) {
jsonLD,
omitVariants,
ignoreStructuredData,
structuredDataControl,
} = props;

const title = renderTemplateString(
Expand All @@ -63,9 +75,13 @@ export function loader(_props: Props, _req: Request, ctx: AppContext) {
jsonLD.product.isVariantOf.hasVariant = [];
}

const jsonLDs = (ignoreStructuredData && !ctx.isBot) || !jsonLD
? []
: [jsonLD];
// Handle deprecated prop
const mode = structuredDataControl ??
(ignoreStructuredData ? "disable for users" : "always include");

const jsonLDs = shouldIncludeStructuredData(jsonLD, mode, ctx.isBot)
? [jsonLD]
: [];

return {
...seoSiteProps,
Expand Down
22 changes: 19 additions & 3 deletions commerce/sections/Seo/SeoPLPV2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import {
import { ProductListingPage } from "../../types.ts";
import { canonicalFromBreadcrumblist } from "../../utils/canonical.ts";
import { AppContext } from "../../mod.ts";
import {
shouldIncludeStructuredData,
StructuredDataControl,
} from "../../utils/structuredData.ts";

export interface ConfigJsonLD {
/**
Expand All @@ -16,8 +20,15 @@ export interface ConfigJsonLD {
/**
* @title Ignore Structured Data
* @description By default, Structured Data is sent to everyone. Use this to prevent Structured Data from being sent to your customers, it will still be sent to crawlers and bots. Be aware that some integrations may not work if Structured Data is not sent.
* @deprecated Use `structuredDataControl` instead.
*/
ignoreStructuredData?: boolean;
/**
* @title Structured Data Control
* @description Choose when to include JSON-LD structured data. Default sends to everyone. "disable for users" shows only to bots/crawlers. "disable for all" removes completely. Note: some third-party integrations may require structured data to function properly.
* @default "always include"
*/
structuredDataControl?: StructuredDataControl;
}

export interface Props {
Expand Down Expand Up @@ -82,9 +93,14 @@ export function loader(_props: Props, _req: Request, ctx: AppContext) {
});
}

const jsonLDs = (configJsonLD?.ignoreStructuredData && !ctx.isBot) || !jsonLD
? []
: [jsonLD];
const mode = configJsonLD?.structuredDataControl ??
(configJsonLD?.ignoreStructuredData
? "disable for users"
: "always include");

const jsonLDs = shouldIncludeStructuredData(jsonLD, mode, ctx.isBot)
? [jsonLD]
: [];

return {
...seoSiteProps,
Expand Down
16 changes: 16 additions & 0 deletions commerce/utils/structuredData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// commerce/utils/structuredData.ts
export type StructuredDataControl =
| "disable for users"
| "disable for all"
| "always include";

export function shouldIncludeStructuredData(
jsonLD: unknown,
control: StructuredDataControl = "always include",
isBot: boolean,
): boolean {
if (!jsonLD) return false;

return control !== "disable for all" &&
(control !== "disable for users" || isBot);
}
Loading