wpheadless-lib 1.1.17 → 1.1.18

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.
@@ -34,6 +34,7 @@ type RewriteOpts = {
34
34
  wpApiUrl?: string;
35
35
  basePath?: string;
36
36
  inLanguage?: string;
37
+ canonicalUrl?: string;
37
38
  };
38
39
  /**
39
40
  * Replace Yoast URLs so they match the frontend domain instead of the WP API domain.
package/dist/utils/seo.js CHANGED
@@ -110,6 +110,47 @@ const rewriteYoastSchemaUrls = (schema, opts, canonical) => {
110
110
  return schema;
111
111
  return rewriteSchemaValue(schema, origins);
112
112
  };
113
+ const applyCanonicalUrlOverride = (schema, canonicalUrl) => {
114
+ if (!canonicalUrl || !schema || typeof schema !== "object")
115
+ return schema;
116
+ const graph = schema["@graph"];
117
+ if (!Array.isArray(graph))
118
+ return schema;
119
+ const pageUrls = new Set();
120
+ for (const node of graph) {
121
+ if (!nodeHasType(node, "WebPage"))
122
+ continue;
123
+ if (typeof node["@id"] === "string")
124
+ pageUrls.add(node["@id"]);
125
+ if (typeof node.url === "string")
126
+ pageUrls.add(node.url);
127
+ }
128
+ if (!pageUrls.size)
129
+ return schema;
130
+ const replacePageReferences = (value) => {
131
+ if (typeof value === "string") {
132
+ return pageUrls.has(value) ? canonicalUrl : value;
133
+ }
134
+ if (Array.isArray(value))
135
+ return value.map(replacePageReferences);
136
+ if (value && typeof value === "object") {
137
+ return Object.fromEntries(Object.entries(value).map(([key, val]) => [
138
+ key,
139
+ replacePageReferences(val),
140
+ ]));
141
+ }
142
+ return value;
143
+ };
144
+ const normalizedGraph = graph.map((node) => {
145
+ const next = replacePageReferences(node);
146
+ if (nodeHasType(next, "WebPage")) {
147
+ next["@id"] = canonicalUrl;
148
+ next.url = canonicalUrl;
149
+ }
150
+ return next;
151
+ });
152
+ return { ...schema, "@graph": normalizedGraph };
153
+ };
113
154
  const getYoastDescription = (yoast) => {
114
155
  const description = yoast?.description || yoast?.og_description;
115
156
  return typeof description === "string" && description.trim()
@@ -256,7 +297,8 @@ export function getYoastSchema(entity, opts) {
256
297
  return null;
257
298
  const options = opts ?? {};
258
299
  const enriched = enrichYoastSchema(yoast.schema, entity, yoast, options);
259
- return rewriteYoastSchemaUrls(enriched, options, yoast.canonical) || null;
300
+ const rewritten = rewriteYoastSchemaUrls(enriched, options, yoast.canonical);
301
+ return applyCanonicalUrlOverride(rewritten, options.canonicalUrl) || null;
260
302
  }
261
303
  export function stripBreadcrumbsFromSchema(schema) {
262
304
  if (!schema || typeof schema !== "object")
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wpheadless-lib",
3
- "version": "1.1.17",
3
+ "version": "1.1.18",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "dist/index.js",