Files
coder/site/src/utils/metadata.ts
T
Michael Smith 0f2d4fdb6d fix: prevent metadata queries from short-circuiting (#10312)
* fix: prevent metadata queries from short-circuiting

* fix: use correct type definitions
2023-10-17 10:20:56 -06:00

25 lines
693 B
TypeScript

export const getMetadataAsJSON = <T extends NonNullable<unknown>>(
property: string,
): T | undefined => {
const appearance = document.querySelector(`meta[property=${property}]`);
if (appearance) {
const rawContent = appearance.getAttribute("content");
if (rawContent) {
try {
return JSON.parse(rawContent);
} catch (err) {
// In development, the metadata is always going to be empty; error is
// only a concern for production
if (process.env.NODE_ENV === "production") {
console.warn(`Failed to parse ${property} metadata. Error message:`);
console.warn(err);
}
}
}
}
return undefined;
};