Skip to content

Commit

Permalink
Merge pull request #26 from buttondown/issue-25
Browse files Browse the repository at this point in the history
Gracefully handle broken JSON+LD
  • Loading branch information
catdevnull committed Sep 13, 2024
2 parents e82e9a3 + c99b81f commit 1b426e6
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions app/domain/[domain]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ export async function generateMetadata(
};
}

function formatJson(json: string) {
try {
return {
valid: true,
value: JSON.stringify(JSON.parse(json || "{}"), null, 2),
};
} catch {
return { valid: false, value: json };
}
}

export default async function Page({
params,
}: {
Expand All @@ -57,6 +68,11 @@ export default async function Page({
await reify(params.domain, data);
}

const jsonld = data.detected_technologies.find(
(datum) => datum.identifier === "jsonld"
)?.metadata.value;
const formattedJsonLd = formatJson(jsonld ?? "{}");

return (
<div className="">
<h1>
Expand Down Expand Up @@ -141,27 +157,15 @@ export default async function Page({
</Grid.Item>
))}
</Grid.Container>
<SectionHeader>JSON+LD</SectionHeader>
<ul>
{data.detected_technologies.find(
(datum) => datum.identifier === "jsonld"
)?.metadata && (
{jsonld && (
<>
<SectionHeader>JSON+LD</SectionHeader>
<pre className="whitespace-pre max-w-full overflow-x-scroll">
{JSON.stringify(
JSON.parse(
data.detected_technologies.find(
(datum) => datum.identifier === "jsonld"
)?.metadata.value || "{}"
),
null,
2
)}
{formattedJsonLd.value}
</pre>
)}
<ul className="only:block hidden opacity-50">
No JSON+LD record found
</ul>
</ul>
{!formattedJsonLd.valid && <p>(this JSON isn&apos;t valid)</p>}
</>
)}
</div>
);
}

0 comments on commit 1b426e6

Please sign in to comment.