Iframe preview "Invalid time value" error for documents without publishedAt date
I can see the issue in your code! The problem is that both assemblePostUrl and assembleCompanyUrl are being called regardless of the document type, and assemblePostUrl is trying to format a date that doesn't exist on your company documents.
Looking at your code:
const blogUrl = assemblePostUrl({ displayed, options });
const companyUrl = assembleCompanyUrl({ displayed, options });When you're viewing a company document, assemblePostUrl still runs and tries to execute this line:
const dateSegment = format(new Date(publishedAt), "yyyy/MM");Since company documents don't have a publishedAt field, this throws the "Invalid time value" error.
The fix: Only call the appropriate URL assembly function based on the document type. Here's the corrected code:
const IframePreview = (props) => {
const { options } = props;
const { displayed } = props.document;
if (!displayed) {
return (
<div className={styles.componentWrapper}>
<p>There is no document to preview</p>
</div>
);
}
// Only assemble the URL for the appropriate document type
let previewUrl;
if (displayed._type === "post") {
previewUrl = assemblePostUrl({ displayed, options });
} else if (displayed._type === "company") {
previewUrl = assembleCompanyUrl({ displayed, options });
}
if (!previewUrl) {
return (
<div className={styles.componentWrapper}>
<p>Hmm. Having problems constructing the web front-end URL.</p>
</div>
);
}
return (
<div className={styles.componentWrapper}>
<div className={styles.iframeContainer}>
<iframe src={previewUrl} frameBorder={"0"} />
</div>
</div>
);
};This way, you're only calling the URL assembly function that matches your document type, avoiding the date formatting error for company documents. The iframe rendering logic is also simplified since both document types can use the same iframe element.
Show original thread1 reply
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.