Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.
TypeScript aeoai-agentai-searchanswer-engine-optimizationbrand-monitoringchatgptclaudecopilotdockergeminigeogoogle-ai-overviewsgrokmcpnextjsopen-sourceperplexityseosupabasetypescript
fix(audit): read a JSON-LD @graph's nodes in the wrapper's @context (#1681)
Cihan2bdd96c6af04
diff --git a/server/src/lib/audit/signals/helpers.js b/server/src/lib/audit/signals/helpers.jsindex f0945d575..20048ed62 100644--- a/server/src/lib/audit/signals/helpers.js+++ b/server/src/lib/audit/signals/helpers.js@@ -7,6 +7,9 @@/*** Parse every <script type="application/ld+json"> block and flatten @graph /* arrays into a single list of typed nodes. Returns `{ nodes, total, valid }`.+ * A @graph's node takes the wrapper's @context when it has none of its own,+ * as JSON-LD reads it: WordPress's SEO plugins write one @graph with the+ * context on the wrapper alone.*/export function jsonLd(ctx) {const nodes = [];@@ -27,8 +30,14 @@ export function jsonLd(ctx) {for (const item of items) {if (!item || typeof item !== 'object') continue;if (Array.isArray(item['@graph'])) {+ const context = item['@context'];for (const n of item['@graph']) {- if (n && typeof n === 'object') nodes.push(n);+ if (!n || typeof n !== 'object') continue;+ nodes.push(+ context !== undefined && n['@context'] === undefined+ ? { '@context': context, ...n }+ : n,+ );}} else {nodes.push(item);diff --git a/server/src/lib/audit/signals/structure.js b/server/src/lib/audit/signals/structure.jsindex 557a25758..fa148e207 100644--- a/server/src/lib/audit/signals/structure.js+++ b/server/src/lib/audit/signals/structure.js@@ -113,10 +113,22 @@ export const jsonLdValidity = {return { status: 'fail', score: 0, evidence: { reason: 'no JSON-LD blocks' } };}// Among parsed nodes, how many declare a schema.org @context + a @type?- const wellFormed = nodes.filter((n) => {- const ctxStr = JSON.stringify(n['@context'] ?? '').toLowerCase();- return ctxStr.includes('schema.org') && typesOf(n).length > 0;- }).length;+ // A @graph's nodes share their wrapper's context: read each context once.+ const schemaOrg = new Map();+ const declaresSchemaOrg = (context) => {+ if (!schemaOrg.has(context)) {+ schemaOrg.set(+ context,+ JSON.stringify(context ?? '')+ .toLowerCase()+ .includes('schema.org'),+ );+ }+ return schemaOrg.get(context);+ };+ const wellFormed = nodes.filter(+ (n) => declaresSchemaOrg(n['@context']) && typesOf(n).length > 0,+ ).length;let status = 'fail';let score = 0;diff --git a/server/src/lib/audit/signals/structure.test.js b/server/src/lib/audit/signals/structure.test.jsnew file mode 100644index 000000000..de5a73815--- /dev/null+++ b/server/src/lib/audit/signals/structure.test.js@@ -0,0 +1,57 @@+import { describe, expect, it } from 'vitest';+import * as cheerio from 'cheerio';+import { jsonLdRelevance, jsonLdValidity } from './structure.js';++/** Structured data as the audit reads it: a @graph's nodes in its wrapper's context. */++const page = (json) => ({+ $: cheerio.load(+ `<html><head><script type="application/ld+json">${JSON.stringify(json)}</script></head><body></body></html>`,+ ),+});++/** One @graph, the context on the wrapper alone, as WordPress's SEO plugins write it. */+const GRAPH = {+ '@context': 'https://schema.org',+ '@graph': [+ { '@type': 'WebPage', '@id': 'https://acme.example/#webpage', name: 'Pricing' },+ { '@type': 'Organization', '@id': 'https://acme.example/#org', name: 'Acme' },+ ],+};++describe('json-ld-validity', () => {+ it('passes one @graph whose context is on the wrapper alone', () => {+ expect(jsonLdValidity.evaluate(page(GRAPH))).toMatchObject({+ status: 'pass',+ evidence: { wellFormedNodes: 2 },+ });+ });++ it('keeps a node’s own context, and fails a @graph with none anywhere', () => {+ const own = {+ '@context': 'https://example.org/vocab',+ '@graph': [{ '@context': 'https://schema.org', '@type': 'Product', name: 'Boot' }],+ };+ expect(jsonLdValidity.evaluate(page(own)).status).toBe('pass');+ const none = { '@graph': GRAPH['@graph'] };+ expect(jsonLdValidity.evaluate(page(none)).status).toBe('fail');+ });++ it('reads a large shared context once, not once per node', () => {+ // About 500 KB: a wrapper context of ~250 KB and thousands of tiny nodes.+ const vocab = {};+ for (let i = 0; i < 7_300; i++) vocab[`t${i}`] = `https://schema.org/T${i}`;+ const nodes = [];+ for (let i = 0; i < 9_500; i++) nodes.push({ '@type': 'Thing', n: i });+ const big = { '@context': ['https://schema.org', vocab], '@graph': nodes };+ const started = Date.now();+ expect(jsonLdValidity.evaluate(page(big)).status).toBe('pass');+ expect(Date.now() - started).toBeLessThan(1000);+ });+});++describe('json-ld-relevance', () => {+ it('reads the types inside a @graph', () => {+ expect(jsonLdRelevance.evaluate(page(GRAPH)).status).not.toBe('fail');+ });+});