Skip to content

Commit

Permalink
Merge pull request #296 from opensourcepledge/vladh/unpublished-drafts
Browse files Browse the repository at this point in the history
Blog: prevent indexing of unpublished drafts
  • Loading branch information
vladh authored Dec 20, 2024
2 parents 7dd087f + e494616 commit fff2c28
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
9 changes: 6 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ After new content is published, the website must be rebuilt and republished. Thi
update members and deploy” workflow on the `main` branch. See here:
https://github.com/opensourcepledge/opensourcepledge.com/actions/workflows/deploy.yml

In Sanity, you can set a “Publish Date/Time” field. You can use this field to publish a blog post, but ensure it does
not actually show up on the website before a certain date/time. However, do keep in mind that the website will still
have to be rebuilt and redeployed using the above action _after_ the set date/time.
In Sanity, you can set a “Publish Date/Time” field. You can use this field to save a blog post to the Sanity database,
but ensure it does not actually show up on the website before a certain date/time. However, do keep in mind that the
website will still have to be rebuilt and redeployed using the above action _after_ the set date/time.

You can view a draft of your unpublished posts by visiting the appropriate URL after building, eg
`https://opensourcepledge.com/blog/my-post-slug/`.

## New Member Workflows

Expand Down
4 changes: 3 additions & 1 deletion src/layouts/Layout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ interface Props {
title?: string;
canonicalUrl?: string;
navless?: boolean;
preventIndexing?: boolean;
}
const { title, canonicalUrl, navless } = Astro.props;
const { title, canonicalUrl, navless, preventIndexing } = Astro.props;
---

<!doctype html>
Expand All @@ -30,6 +31,7 @@ const { title, canonicalUrl, navless } = Astro.props;
<meta property="og:determiner" content="the">
<meta property="og:url" content={Astro.url}>
<meta property="article:published_time" content="2024-10-08">
{preventIndexing && <meta name="robots" content="noindex">}
<link rel="me" href="https://fosstodon.org/@opensourcepledge">
<link rel="canonical" href={canonicalUrl || Astro.url}>

Expand Down
9 changes: 5 additions & 4 deletions src/pages/blog/[slug].astro
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const article = await sanityClient.fetch<SanityDocument>(ARTICLE_QUERY, Astro.pa
if (!article) {
return Astro.rewrite('/404');
}
const isPublished = dayjs(article.publishDateTime).isBefore(dayjs().utc());
export async function getStaticPaths(): Promise<{ params: { slug: string } }> {
const SLUGS_QUERY = `*[_type == "article"]{
Expand All @@ -36,17 +37,14 @@ export async function getStaticPaths(): Promise<{ params: { slug: string } }> {
return await sanityClient.fetch(SLUGS_QUERY, Astro.params);
}
// NOTE: Custom components need to go inside `type`.
// This is undocumented?
// https://github.com/theisel/astro-portabletext/pull/173
const components = {
type: {
captionedImage: CaptionedImage,
}
};
---

<Layout title={article.title} canonicalUrl={article.canonicalUrl}>
<Layout title={article.title} canonicalUrl={article.canonicalUrl} preventIndexing={!isPublished}>
<div class="edge-blobs">
<Blob kind="grad-dots-04" top="12%" right="-2rem"></Blob>
<Blob kind="grad-dots-05" top="45%" left="-2rem"></Blob>
Expand All @@ -59,6 +57,9 @@ const components = {

<main id="main-content">
<section>
{isPublished ||
<div class="highlight-box"><strong>Note:</strong> This is an unpublished draft.</div>
}
<h1>{article.title}</h1>
<div class="meta">
<div
Expand Down

0 comments on commit fff2c28

Please sign in to comment.