mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
* fix: prevent metadata queries from short-circuiting * fix: use correct type definitions
25 lines
693 B
TypeScript
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;
|
|
};
|